{% extends "ui/report_base.html" %}
{% load url from future %}
{% load icons %}
{% load formats %}
{% load bob %}
{% load reports %}

{% block contentarea %}
    <div class="row-fluid">
        <div class="span12">
            <form class="form form-inline">
                <div class="form-actions">
                    <h4>Devices prices per Venture</h4>
                    <p>Price of devices in the venture.</p>
                    <p>The price of shared equipment is taken as a proportion.</p>
                    <ul class="unstyled">
                        <li>N/A - Unknown price</li>
                        <li>{% icon "notebook--minus" %}</i> - The component group is not defined, so you can not download prices</li>
                        <li>{% icon "fugue-prohibition-button" %} - Device does not have a model</lI>
                    </ul>
                    {% for f in form %}
                        {% if f.label %}
                        <div style="display:inline-block; vertical-align:top; margin-top: 10px;" class="control-group {% if f.errors %}error{% endif %}" >
                            <label class="control-label">Venture: </label>
                            {{ f }}
                            {% spaceless %}
                            <button class="btn">
                                {% icon 'fugue-store' %}&nbsp;Change Venture
                            </button>
                            {% endspaceless %}

                            {% if venture %}
                                {% spaceless %}
                                    <a href="?venture={{ venture }}&export=csv" class="btn">
                                        {% icon 'fugue-fugue-icon fugue-blue-document-excel-csv' %}&nbsp;Export CSV
                                    </a>{% endspaceless %}
                            {% endif %}
                            {% spaceless %}
                                <a href="?export-all=csv" class="btn">
                                    {% icon 'fugue-fugue-icon fugue-blue-document-excel-csv' %}&nbsp;Export all devices to CSV
                                </a>{% endspaceless %}
                            {% spaceless %}
                                <a href="?invalidate-cache=true{% if venture %}&venture={{ venture }}{% endif %}" class="btn">
                                    {% icon 'fugue-fugue-icon fugue-arrow-circle-double' %}&nbsp;Refresh cache
                                </a>{% endspaceless %}
                        </div>
                        {% endif %}
                    {% endfor %}
                </div>
            </form>
        </div>
    </div>

    {% if task_in_progress %}
        {% wait_for_results autoreload=1 reload_frequency=15 %}
    {% else %}
        {% if rows %}
        <div class="row-fluid">
            <div class="span12">
                <table class="table table-striped table-bordered table-condensed device-list">
                    <thead>
                        <th></th>
                        <th>Device</th>
                        <th>SN</th>
                        <th>Barcode</th>
                        <th>Deprecation date</th>
                        <th>Quoted price</th>
                        <th>Components</th>
                    </thead>
                    <tbody>
                        {% for row in rows %}
                        <tr>
                            <td>{{ forloop.counter }}.</td>
                            <td>
                                {% icon row.device.icon %}&nbsp;<a href="/ui/search/info/{{ row.device.id }}">{{ row.device.name }}</a>
                            </td>
                            <td>{{ row.device.sn }}</td>
                            <td>{{ row.device.barcode }}</td>
                            <td>
                                {% if not row.device.deprecation_date %}
                                    N/A
                                {% endif %}
                                {{ row.device.deprecation_date|date:"Y-m-d" }}
                            </td>
                            <td>{% if row.deprecated %}
                                    Deprecated ({{ row.total|currency }})
                                {% elif row.device.cached_price == None %}
                                    N/A
                                {% else %}
                                    {{ row.device.cached_price|currency }}
                                {% endif %}
                            </td>
                            <td>
                            {% if row.components %}
                                <table class="table reports-table-component">
                                    <thead>
                                        <th>Name:</th>
                                        <th>Count:</th>
                                        <th>Unit price:</th>
                                    </thead>
                                    <tbody>
                                    {% for comp in row.components %}
                                        <tr>
                                            <td>
                                                {% icon comp.icon %}&nbsp;{% if comp.model.name %}{{ comp.model.name }}{% else %}{{ comp.model }}{% endif %}
                                                {% if comp.serial %}
                                                    (Serial: {{ comp.serial }})
                                                {% endif %}
                                            </td>
                                            <td>
                                                {{ comp.count }}
                                                {% if comp.disk_count %}
                                                    {{ comp.disk_count }}
                                                {% endif %}
                                            </td>
                                            <td>{% if not comp.price %}
                                                    N/A{% if not comp.group %}&nbsp;{% icon "notebook--minus" %}
                                                {% endif %}
                                                {% else %}
                                                    {{ comp.price|currency }}
                                                {% endif %}
                                            </td>
                                        </tr>
                                    {% endfor %}
                                    </tbody>
                                </table>
                            {% else %}
                                N/A
                            {% endif %}
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
        {% else %}
            No result
        {% endif %}
    {% endif %}
{% endblock %}
