{% extends 'base.html' %}
{% load bootstrap3 %}
{% load static %}
{% load i18n %}

{% 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>
{% 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="panel-options">
                        <ul class="nav nav-tabs">
                            <li>
                                <a href="{% url 'users:user-group-detail' pk=user_group.id %}" class="text-center"><i class="fa fa-laptop"></i> {% trans 'User detail' %} </a>
                            </li>
                            <li class="active">
                                <a href="{% url 'users:user-group-asset-permission' pk=user_group.id %}" class="text-center"><i class="fa fa-bar-chart-o"></i> {% trans 'Asset permission' %}</a>
                            </li>
                            <li>
                                <a href="{% url 'users:user-group-granted-asset' pk=user_group.id %}" class="text-center"><i class="fa fa-cubes"></i> {% trans 'Asset granted' %}</a>
                            </li>
                        </ul>
                    </div>
                    <div class="tab-content">
                        <div class="col-sm-7" style="padding-left: 0;">
                            <div class="ibox float-e-margins">
                                <div class="ibox-title">
                                    <span style="float: left">{% trans 'Asset permission of ' %} <b>{{ user_group.name }}</b></span>
                                    <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>
                                        <ul class="dropdown-menu dropdown-user">
                                        </ul>
                                        <a class="close-link">
                                            <i class="fa fa-times"></i>
                                        </a>
                                    </div>
                                </div>
                                <div class="ibox-content">
                                    <table class="table table-hover " id="user_group_permissions_table" >
                                        <thead>
                                            <tr>
                                                <th class="text-center">
                                                    <input type="checkbox" id="check_all" class="ipt_check_all" >
                                                </th>
                                                <th>{% trans 'Name' %}</th>
                                                <th>{% trans 'Asset' %}</th>
                                                <th>{% trans 'Asset group' %}</th>
                                                <th>{% trans 'System user' %}</th>
                                                <th>{% trans 'Valid' %}</th>
                                                <th></th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-5" style="padding-left: 0;padding-right: 0">
                            <div class="panel panel-primary">
                                <div class="panel-heading">
                                    <i class="fa fa-info-circle"></i> {% trans 'Quick create permission for user group' %}
                                </div>
                                <div class="panel-body">
                                    <form method="post" action="{% url 'users:user-group-asset-permission-create' pk=user_group.id %}">
                                        <table class="table">
                                            <tbody>
                                                {% csrf_token %}
                                                <tr class="no-borders-tr">
                                                    <td colspan="1" style="padding-top: 0">
                                                        {% bootstrap_field form.name layout="horizontal" %}
                                                    </td>
                                                </tr>
                                                <tr class="no-borders-tr">
                                                    <td colspan="1" style="padding-top: 0">
                                                        {% bootstrap_field form.assets layout="horizontal" %}
                                                    </td>
                                                </tr>
                                                <tr class="no-borders-tr">
                                                    <td colspan="1" style="padding-top: 0">
                                                        {% bootstrap_field form.asset_groups layout="horizontal" %}
                                                    </td>
                                                </tr>
                                                <tr class="no-borders-tr">
                                                    <td colspan="1" style="padding-top: 0">
                                                        {% bootstrap_field form.system_users layout="horizontal" %}
                                                    </td>
                                                </tr>
                                                <tr class="no-borders-tr">
                                                    <td>
                                                        <button type="submit" class="btn btn-primary btn-sm">{% trans 'Submit' %}</button>
                                                    </td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

{% endblock %}
{% block custom_foot_js %}
    <script>
        $(document).ready(function () {
            $('.select2').select2();
            var options = {
                ele: $('#user_group_permissions_table'),
                buttons: [],
                order: [],
                select: [],
                columnDefs: [
                    {targets: 1, createdCell: function (td, cellData, rowData) {
                        var detail_btn = '<a href="{% url "perms:asset-permission-detail" pk=99991937 %}">' + cellData + '</a>';
                        $(td).html(detail_btn.replace('99991937', rowData.id));
                     }},
                    {targets: 2, createdCell: function (td, cellData, rowData) {
                        var dataLength = cellData.length;
                        $(td).html(dataLength);
                    }},
                    {targets: 3, createdCell: function (td, cellData, rowData) {
                        var dataLength = cellData.length;
                        $(td).html(dataLength);
                    }},
                    {targets: 4, createdCell: function (td, cellData, rowData) {
                        var dataLength = cellData.length;
                        $(td).html(dataLength);
                    }},
                    {targets: 5, 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: 6, createdCell: function (td, cellData, rowData) {
                        var btn = '<button class="btn btn-danger btn-xs btn_del_permission" id=99991937 type="button" style="float: right;"><i class="fa fa-minus"></i></button>';
                        btn = btn.replace('99991937', cellData);
                        $(td).html(btn)
                     }}
                ],
                ajax_url: '{% url "api-perms:asset-permission-list" %}?user_group={{ user_group.id }}',
                columns: [{data: function(){return ""}}, {data: "name" }, {data: "assets" }, {data: "asset_groups"},
                    {data: "system_users"}, {data: "is_active"}, {data: "id"}]
            };
            jumpserver.initDataTable(options);
        }).on('click', '.btn_del_permission', function () {
            var $this = $(this);
            var body = {
                id: $this.attr('id'),
                user_group_id: {{ user_group.id }}
            };
            var the_url = "{% url 'api-perms:revoke-user-group-asset-permission' %}";
            var success = function () {
                $this.closest('tr').remove();
            };
            APIUpdateAttr({
                url: the_url,
                body: JSON.stringify(body),
                method: 'PUT',
                success_message: '{% trans "Revoke Successfully!" %}',
                success: success
            });
        }).on('click', 'buttons-excel', function () {
            console.log('click excel')
        })
    </script>
{% endblock %}
