{% extends '_base_list.html' %}
{% load i18n %}
{% load static %}
{% load common_tags %}
{% 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>

<style>
    .custom{
        margin-right:5px;
        }
    #modal .modal-body { max-height: 200px; }
</style>
{% endblock %}
{% block content_left_head %}{% endblock %}

{% block table_search %}
    <div class="html5buttons">
        <div class="dt-buttons btn-group">
            <a class="btn btn-default btn_import" data-toggle="modal" data-target="#asset_import_modal" tabindex="0">
                <span>{% trans "Import" %}</span>
            </a>
            <a class="btn btn-default btn_export" tabindex="0">
                <span>{% trans "Export" %}</span>
            </a>
        </div>
    </div>
{% endblock %}

{% block tags_list %}
<div class="ydxbd" id="ydxbd" style="display: none;">
    <div class="tagBtnList">
    {% for tag in tag_list %}
        <a href="{% url 'assets:asset-tags' tag_id=tag.0 %}"
           {% if tag.0|int_to_str == tag_id %}
           class="tagBtn2 label label-warning" name="tag_on">
           {% else %}
           class="tagBtn2 label label-default">
           {% endif %}
           {{ tag.1}}({{ tag.2 }})
        </a>
    {% endfor %}
        <a href="{% url 'assets:asset-list' %}" class="tagBtn2 label label-default" >移除选择</a>
    </div>
</div>
{% endblock %}

{% block table_container %}
<div class="uc pull-left m-l-5 m-r-5"><a href="{% url "assets:asset-create" %}" class="btn btn-sm btn-primary"> {% trans "Create asset" %} </a></div>
<div class="uc pull-left"><a href="javascript:void(0);" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#asset_import_modal"> {% trans "Import asset" %} </a></div>
<table class="table table-striped table-bordered table-hover " id="asset_list_table" >
    <thead>
        <tr>
            <th class="text-center"><input type="checkbox" class="ipt_check_all"></th>
            <th class="text-center">{% trans 'Hostname' %}</th>
            <th class="text-center">{% trans 'IP' %}</th>
            <th class="text-center">{% trans 'Port' %}</th>
            <th class="text-center">{% trans 'Type' %}</th>
            <th class="text-center">{% trans 'Env' %}</th>
            <th class="text-center">{% trans 'Hardware' %}</th>
            <th class="text-center">{% trans 'Valid' %}</th>
            <th class="text-center">{% trans 'Alive' %}</th>
            <th class="text-center">{% trans 'Action' %}</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
<div id="actions" class="hide">
    <div class="input-group">
        <select class="form-control m-b" style="width: auto" id="slct_bulk_update">
            <option value="delete">{% trans 'Delete selected' %}</option>
            <option value="update">{% trans 'Update selected' %}</option>
            <option value="deactive">{% trans 'Deactive selected' %}</option>
        </select>
        <div class="input-group-btn pull-left" style="padding-left: 5px;">
            <button id='btn_bulk_update' style="height: 32px;"  class="btn btn-sm btn-primary">
             {% trans 'Submit' %}
            </button>
        </div>
    </div>
</div>
{% endblock %}

{% block custom_foot_js %}
<script type="text/javascript">
    window.onload = function (){
        var tag_on = document.getElementsByName("tag_on");
        var oDiv = document.getElementById("ydxbd");
        if(tag_on.length > 0){
            oDiv.style.display = "block";
        }
    };

    function tagShow() {
        var oDiv = document.getElementById("ydxbd");
            if (oDiv.style.display == 'none'){
                oDiv.style.display = "block";
            }else{
                oDiv.style.display = "none";
            }
    };  //onload;

    $(document).ready(function(){
        var options = {
            ele: $('#asset_list_table'),
            columnDefs: [
                {targets: 1, createdCell: function (td, cellData, rowData) {
                    var detail_btn = '<a href="{% url "assets:asset-detail" pk=99991937 %}">' + cellData + '</a>';
                    $(td).html(detail_btn.replace('99991937', rowData.id));
                 }},
                {targets: 7, createdCell: function (td, cellData) {
                    if (!cellData) {
                        $(td).html('<i class="fa fa-times text-danger"></i>')
                    } else {
                        $(td).html('<i class="fa fa-check text-navy"></i>')
                    }
                 }},
                {targets: 8, createdCell: function (td, cellData) {
                    if (!cellData) {
                        $(td).html('<i class="fa fa-circle text-danger"></i>')
                    } else {
                        $(td).html('<i class="fa fa-circle text-navy"></i>')
                    }
                }},
                {targets: 9, createdCell: function (td, cellData, rowData) {
                    var update_btn = '<a href="{% url "assets:asset-update" pk=99991937 %}" class="btn btn-xs btn-info">{% trans "Update" %}</a>'.replace('99991937', cellData);
                    var del_btn = '<a class="btn btn-xs btn-danger m-l-xs btn_user_delete" data-uid="99991937">{% trans "Delete" %}</a>'.replace('99991937', cellData);
                    $(td).html(update_btn + del_btn)
                 }}
            ],
            ajax_url: '{% url "api-assets:asset-list" %}',
            columns: [{data: "id"}, {data: "hostname" }, {data: "ip" }, {data: "port" },
                {data: "type_display" }, {data: "env"}, {data: "hardware"}, {data: "is_active" },
                {data: "is_active"}, {data: "id" }],
            op_html: $('#actions').html()
        };
        var table = jumpserver.initDataTable(options);

        $('.btn_export').click(function () {
        var assets = [];
        var rows = table.rows('.selected').data();
        $.each(rows, function (index, obj) {
            assets.push(obj.id)
        });
        $.ajax({
            url: "{% url "assets:export-assets-xlsx" %}",
            method: 'POST',
            data: JSON.stringify({users_id: users}),
            dataType: "json",
            success: function (data, textStatus) {
                window.open(data.redirect)
            },
            error: function () {
                toastr.error('Export failed');
            }
        })
    });

    });
</script>
{% endblock %}