{% extends 'base.html' %}
{% load i18n %}
{% load static %}
{% load bootstrap3 %}
{% block custom_head_css_js %}
    <link href="{% static 'css/plugins/select2/select2.min.css' %}" rel="stylesheet">
    <script src="{% static 'js/plugins/select2/select2.full.min.js' %}"></script>
    <link href="{% static 'css/plugins/datepicker/datepicker3.css' %}" rel="stylesheet">
{% endblock %}

{% block content %}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>{{ action }}</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                                <i class="fa fa-wrench"></i>
                            </a>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        <form method="post" class="form-horizontal" action="" >
                            {% csrf_token %}
                            <h3>{% trans 'Basic' %}</h3>
                            {% bootstrap_field form.name layout="horizontal" %}
                            <div class="hr-line-dashed"></div>
                            <h3>{% trans 'User' %}</h3>
                            {% bootstrap_field form.users layout="horizontal" %}
                            {% bootstrap_field form.user_groups layout="horizontal" %}
                            <div class="hr-line-dashed"></div>
                            <h3>{% trans 'Asset' %}</h3>
                            {% bootstrap_field form.assets layout="horizontal" %}
                            {% bootstrap_field form.nodes layout="horizontal" %}
                            {% bootstrap_field form.system_users layout="horizontal" %}
                            <div class="hr-line-dashed"></div>
                            <h3>{% trans 'Other' %}</h3>
                            <div class="form-group">
                                <label for="{{ form.is_active.id_for_label }}" class="col-sm-2 control-label">{% trans 'Active' %}</label>
                                <div class="col-sm-8">
                                    {{ form.is_active }}
                                </div>
                            </div>
                            <div class="form-group {% if form.date_expired.errors or form.date_start.errors %} has-error {% endif %}" id="date_5">
                                <label for="{{ form.date_expired.id_for_label }}" class="col-sm-2 control-label">{{ form.date_expired.label }}</label>
                                <div class="col-sm-9">
                                    <div class="input-daterange input-group" id="datepicker">
                                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                        <input type="text" class="input-sm form-control" name="date_start" value="{{ form.date_start.value|date:'Y-m-d' }}">
                                        <span class="input-group-addon">to</span>
                                        <input type="text" class="input-sm form-control" name="date_expired" value="{{ form.date_expired.value|date:'Y-m-d' }}">
                                    </div>
                                    <span class="help-block ">{{ form.date_expired.errors }}</span>
                                    <span class="help-block ">{{ form.date_start.errors }}</span>
                                </div>
                             </div>
                            {% bootstrap_field form.comment layout="horizontal" %}

                            <div class="form-group">
                                <div class="col-sm-4 col-sm-offset-2">
                                    <button class="btn btn-white" type="reset">{% trans 'Reset' %}</button>
                                    <button id="submit_button" class="btn btn-primary" type="submit">{% trans 'Submit' %}</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    {% include 'assets/_asset_list_modal.html' %}
{% endblock %}
{% block custom_foot_js %}
<script src="{% static 'js/plugins/datepicker/bootstrap-datepicker.js' %}"></script>
<script>
$(document).ready(function () {
    $('.select2').select2({
        closeOnSelect: false
    });
    $('#datepicker').datepicker({
        format: "yyyy-mm-dd",
        todayBtn: "linked",
        keyboardNavigation: false,
        forceParse: false,
        calendarWeeks: true,
        autoclose: true
    });
    $("#id_assets").parent().find(".select2-selection").on('click', function (e) {
        e.preventDefault();
        $("#asset_list_modal").modal();
    })
})
.on('click', '#btn_asset_modal_confirm', function () {
    var assets = asset_table2.selected;
    $('.select2 option:selected').each(function (i, data) {
        assets.push($(data).attr('value'))
    });
    $.each(assets, function (id, data) {
        $('.select2').val(assets).trigger('change');
    });
    $("#asset_list_modal").modal('hide');
})
</script>
{% endblock %}