{% extends 'assets/base.html' %}
{% load bob %}
{% load i18n %}
{% load icons %}

{% block extra_headers %}
{{ block.super }}
<style type="text/css">
input[type=submit] {
    padding: 0px;
}
div#sidebar_search input, div#sidebar_search select{
    width: 90%;
}
form.form-inline {
    display: inline;
}
</style>
{% endblock %}

{% block scripts %}
    {{ block.super }}
    <script type="text/javascript">
      $(document).ready(function() {
            $('#post_edit_all').click(function() {
                var ids = [];
                $("#assets_table input[type=checkbox]").each(
                    function(index, val){
                        if(val.checked) {
                            ids.push($(val).val());
                        };
                    }
                );
                if (ids.length) {
                    window.location.href = 'bulkedit?select=' + ids.join('&select=');
                }
                return false;
            });
            $('.del-asset-btn').click(function() {
                if (!confirm("Are you sure to delete Asset?")) {
                    return false;
                }
            });
            require(['reports'], function(reports) {
                reports.setup({
                    trigger: $('.pagination').find('a').last(),
                    progressBar: '#async-progress',
                    url: '{{ csv_url }}',
                    etaEl: '#eta'
                });
            });
        });
    </script>
{% endblock %}


{% block sidebar_search%}
    <div class="form search-form well well-small" id="sidebar_search">
        {% trans "Hint: you can use quotation marks for exact matches." %}
        <hr>
        {% form form=form method="GET" action='' submit_label='Search' css_class=''%}
    </div>
{% endblock %}

{% block content %}
    <h3>{{header}}</h3>
        <table id='assets_table' class="table table-bordered table-striped">
        {% table_header columns url_query sort fugue_icons=1 sort_variable_name=sort_variable_name %}
                <tbody>
                {% for row in bob_page %}
                <tr {% if row.deleted %} class="asset-deleted" {% endif %}>
                    <td class="column-checkbox">
                        <input type="checkbox" name="select" value="{{row.id}}">
                    </td>
                    <td>
                        <a href="./edit/{% if row.get_data_type == 'part'%}part{% elif row.get_data_type == 'device' %}device{% endif %}/{{row.id}}">
                            {% icon row.get_data_icon %}&nbsp;{{row.get_data_type|capfirst}}
                        </a>
                    </td>
                    <td>{{row.sn|default:'-' }}</td>
                    <td>{{row.barcode|default:'-' }}</td>
                    <td>{{row.model.name|default:'-' }}</td>
                    <td>{{row.invoice_no|default:'-' }}</td>
                    <td>{{row.order_no|default:'-' }}</td>
                    <td>{{row.get_status_display}}</td>
                    <td>{{row.warehouse}}</td>
                    <td>{{row.venture}}</td>
                    <td>{{row.price}}</td>
                    <td>
                        {% if row.device_info.ralph_device_id %}
                            <i class="fugue-icon fugue-tick-circle"></i>
                        {% else %}
                            <i class="fugue-icon fugue-cross-octagon"></i>
                        {% endif %}
                    </td>
                    <td>

                    <div class="btn-group">
                      <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
                        Action
                        <span class="caret"></span>
                      </a>
                      <ul class="dropdown-menu pull-right">

                    {% if row.get_data_type == 'device' %}
                        <li><a href="./edit/device/{{row.id}}">Edit</a></li>
                        <li><a href="./history/device/{{row.id}}">History</a></li>
                        {% if row.device_info.ralph_device_id%}
                            <li><a href="{% url search 'info' row.device_info.ralph_device_id %}">View in Ralph</a></li>
                        {% endif %}
                    {% else %}
                        <li><a href="./edit/part/{{row.id}}">Edit</a></li>
                        <li><a href="./history/part/{{row.id}}">History</a></li>
                    {% endif %}
                        {% if not row.deleted %}
                            <li class="divider"></li>
                            <li>
                                <form class='form-inline' style="padding: 3px 20px;" method="POST" action="./delete/asset/">
                                    {% csrf_token %}
                                    <input type='hidden' name='record_id' value='{{ row.id }}'/>
                                        <input class='btn btn-link del-asset-btn' style="color: #ff0011;" value='Delete' type='submit'></input>
                                </form>
                            </li>
                        {% endif %}
                        </ul>
                    </div>
                    </td>
                </tr>
                {% endfor %}
                </tbody>
            </table>

            {% pagination bob_page url_query=url_query show_all=0 show_csv=1 fugue_icons=1 export_variable_name=export_variable_name %}
            <div id="eta"></div>
            <div class="progress" id="async-progress">
                <div class="bar"></div>
            </div>
            <div class="btn-group">
              <a class="btn dropdown-toggle btn-primary" data-toggle="dropdown" href="#">
                Bulk actions...
                <span class="caret"></span>
              </a>
            <ul class="dropdown-menu">
                <li>
                    <a href="#" id='post_edit_all'>Edit selected</a>
                </li>
            </ul>
            </div>
{% endblock %}
