{% extends 'base_assets.html' %}
{% load paginator from filters %}
{% load button from filters %}
{% load widget_tweaks %}
{% load static %}

{% block css %}
    <link rel="stylesheet" href="{% static 'css/bootstrap-select.css' %}"/>
    <link rel="stylesheet" href="{% static 'css/ajax-bootstrap-select.css' %}"/>
{% endblock %}

{% block preload_js %}
    <script src="{% static 'js/bootstrap-select.js' %}"></script>
    <script src="{% static 'js/ajax-bootstrap-select.js' %}"></script>
{% endblock %}

{% block js %}
    <script>
        //Get querystring value
        function getParameterByName(name) {
            name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
            var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
            return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
        }
        //Function used to remove querystring
        function removeQString(key) {
	        var urlValue=document.location.href;

	        //Get query string value
	        var searchUrl=location.search;

	        if(key!="") {
		        oldValue = getParameterByName(key);
		        removeVal=key+"="+oldValue;
		        if(searchUrl.indexOf('?'+removeVal+'&')!= "-1") {
			        urlValue=urlValue.replace('?'+removeVal+'&','?');
		        }
		        else if(searchUrl.indexOf('&'+removeVal+'&')!= "-1") {
			        urlValue=urlValue.replace('&'+removeVal+'&','&');
		        }
		        else if(searchUrl.indexOf('?'+removeVal)!= "-1") {
			        urlValue=urlValue.replace('?'+removeVal,'');
		        }
		        else if(searchUrl.indexOf('&'+removeVal)!= "-1") {
			        urlValue=urlValue.replace('&'+removeVal,'');
		        }
	        }
	        else {
		        var searchUrl=location.search;
		        urlValue=urlValue.replace(searchUrl,'');
	        }
	        history.pushState({state:1, rand: Math.random()}, '', urlValue);
            window.location.reload(true);
        }
    </script>
{% endblock %}

{% block content %}
<div class="row">
    <div class="col px-0">
        <form id="asset-search-form" method="GET" class="form-inline justify-content-end">
              <div class="input-group px-1 mb-2 mb-sm-0 flex-nowrap">
                {% render_field form.q|add_class:'form-control' placeholder='Enter Asset ID/Desc/Serial' %}
                <label for="q" class="sr-only">Asset ID/Description/Serial Number:</label>
                <span class="input-group-append">{% button 'search' id="id_search" %}</span>
              </div>
              <div id="category-group" class="form-group px-1" style="margin-bottom: 0;">
                <label for="category" class="sr-only">Category</label>
                {% render_field form.category|attr:'multiple'|add_class:'form-control custom-select selectpicker col-sm' data-none-selected-text="Categories" data-header="Categories" data-actions-box="true" %}
              </div>
              <div id="status-group" class="form-group px-1" style="margin-bottom: 0;">
                <label for="status" class="sr-only">Status</label>
                {% render_field form.status|attr:'multiple'|add_class:'form-control custom-select selectpicker col-sm' data-none-selected-text="Statuses" data-header="Statuses" data-actions-box="true" %}
              </div>
              <button id="filter-submit" type="submit" class="btn btn-secondary" style="width: 6em">Filter</button>
        </form>
    </div>
</div>
<div class="row my-2">
    <div class="col text-right px-0">
    {% button 'new' 'asset_create' style="width: 6em" %}
    </div>
</div>
<div class="row my-2">
    <div class="col bg-dark text-white rounded pt-3">
        {# TODO Gotta be a cleaner way to do this... #}
        <p><span class="ml-2">Active Filters: </span> {% for filter in category_filters %}<span class="badge badge-info mx-1 ">{{filter}}<button type="button" class="btn btn-link p-0 ml-1 align-baseline">
  <span aria-hidden="true" class="fas fa-times" onclick="removeQString('category', '{{filter.id}}')"></span>
</button></span>{%endfor%}{% for filter in status_filters %}<span class="badge badge-info mx-1 ">{{filter}}<button type="button" class="btn btn-link p-0 ml-1 align-baseline">
  <span aria-hidden="true" class="fas fa-times" onclick="removeQString('status', '{{filter.id}}')"></span>
</button></span>{%endfor%}</p>
    </div>
</div>
<div class="row">
    <div class="col px-0">
    {% include 'partials/asset_list_table.html' %}
    </div>
</div>
{% paginator %}
{% endblock %}
