{% extends 'base.html' %}
{% 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 class="active">
                                <a href="{% url 'perms:asset-permission-detail' pk=asset_permission.id %}" class="text-center"><i class="fa fa-laptop"></i> {% trans 'Detail' %} </a>
                            </li>
                            <li>
                                <a href="{% url 'perms:asset-permission-user-list' pk=asset_permission.id %}" class="text-center">
                                <i class="fa fa-bar-chart-o"></i> {% trans 'Users and user groups' %}
                                </a>
                            </li>
                            <li>
                                <a href="{% url 'perms:asset-permission-asset-list' pk=asset_permission.id  %}" class="text-center">
                                <i class="fa fa-bar-chart-o"></i> {% trans 'Assets and asset groups' %}</a>
                            </li>
                            <li class="pull-right">
                                <a class="btn btn-outline btn-default" href="{% url 'perms:asset-permission-update' pk=asset_permission.id %}"><i class="fa fa-edit"></i>Update</a>
                            </li>
                            <li class="pull-right">
                                <a class="btn btn-outline btn-danger btn-delete-perm">
                                    <i class="fa fa-edit"></i>Delete
                                </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 class="label"><b>{{ asset_permission.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">
                                        <tbody>
                                        <tr class="no-borders-tr">
                                            <td>{% trans 'Name' %}:</td>
                                            <td><b>{{ asset_permission.name }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'User count' %}:</td>
                                            <td><b>{{ asset_permission.users.count }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'User group count' %}:</td>
                                            <td><b>{{ asset_permission.users.count }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Asset count' %}:</td>
                                            <td><b>{{ asset_permission.assets.count }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Asset group count' %}:</td>
                                            <td><b>{{ asset_permission.asset_groups.count }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'System user count' %}:</td>
                                            <td><b>{{ asset_permission.system_users.count }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Date expired' %}:</td>
                                            <td><b>{{ asset_permission.date_expired }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Date created' %}:</td>
                                            <td><b>{{ asset_permission.date_created }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Created by' %}:</td>
                                            <td><b>{{ asset_permission.created_by  }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Comment' %}:</td>
                                            <td><b>{{ asset_permission.comment }}</b></td>
                                        </tr>
                                        </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 update' %}
                                </div>
                                <div class="panel-body">
                                    <table class="table">
                                        <tbody>
                                        <tr class="no-borders-tr">
                                            <td width="50%">Active:</td>
                                            <td><span style="float: right">
                                                <div class="switch">
                                                    <div class="onoffswitch">
                                                        <input type="checkbox" {% if asset_permission.is_active %} checked {% endif %} class="onoffswitch-checkbox" id="is_active">
                                                        <label class="onoffswitch-label" for="is_active">
                                                            <span class="onoffswitch-inner"></span>
                                                            <span class="onoffswitch-switch"></span>
                                                        </label>
                                                    </div>
                                                </div>
                                            </span></td>
                                        </tr>
                                        <tr>
                                            <td width="50%">{% trans 'Retest asset connectivity' %}:</td>
                                            <td>
                                                <span style="float: right">
                                                    <button type="button" class="btn btn-primary btn-xs" style="width: 54px">{% trans 'Start' %}</button>
                                                </span>
                                            </td>
                                        </tr>

                                        <tr>
                                            <td width="50%">{% trans 'Repush system user' %}:</td>
                                            <td>
                                                <span style="float: right">
                                                    <button type="button" class="btn btn-primary btn-xs" style="width: 54px">{% trans 'Push' %}</button>
                                                </span>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>

                            <div class="panel panel-info">
                                <div class="panel-heading">
                                    <i class="fa fa-info-circle"></i> {% trans 'System user' %}
                                </div>
                                <div class="panel-body">
                                    <table class="table group_edit">
                                        <tbody>
                                        <form>
                                            <tr>
                                                <td colspan="2" class="no-borders">
                                                    <select data-placeholder="{% trans 'Select system users' %}" class="select2" style="width: 100%" multiple="" tabindex="4">
                                                        {% for system_user in system_users_remain %}
                                                        <option value="{{ system_user.id }}" id="opt_{{ system_user.id }}">{{ system_user.name }}: {{ system_user.username }}</option>
                                                        {% endfor %}
                                                    </select>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="2" class="no-borders">
                                                    <button type="button" class="btn btn-info btn-small" id="btn_add_system_user">{% trans 'Join' %}</button>
                                                </td>
                                            </tr>
                                        </form>

                                        {% for system_user in system_users %}
                                        <tr>
                                          <td ><b class="bdg-system-user" data-uid={{ system_user.id }}>{{ system_user.name }}</b></td>
                                          <td>
                                              <button class="btn btn-danger btn-xs btn-del" data-uid="{{ system_user.id }}" type="button" style="float: right;"><i class="fa fa-minus"></i></button>
                                          </td>
                                        </tr>
                                        {% endfor %}
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

{% endblock %}
{% block custom_foot_js %}
    <script>
        jumpserver.system_users_selected = {};
        function addSystemUser(system_users) {
            var the_url = "{% url 'api-perms:asset-permission-detail' pk=asset_permission.id %}";
            var body = {
                system_users: Object.assign([], system_users)
            };
            var success = function(data) {
                window.location.reload();
            };
            APIUpdateAttr({
                url: the_url,
                body: JSON.stringify(body),
                success: success
            });
        }
        function removeSystemUser(system_users, tr) {
            var the_url = "{% url 'api-perms:remove-system-user-asset-permission' pk=asset_permission.id %}";
            var body = {
                system_users: system_users
            };
            var success = function (data) {
                tr.remove()
            };
            APIUpdateAttr({
                url: the_url,
                body: JSON.stringify(body),
                method: 'PUT',
                success: success
            })
        }
        $(document).ready(function () {
            $('.select2').select2()
                .on('select2:select', function(evt) {
                     var data = evt.params.data;
                     jumpserver.system_users_selected[data.id] = data.text;
                })
                .on('select2:unselect', function(evt) {
                    var data = evt.params.data;
                    delete jumpserver.system_users_selected[data.id]
                })

        }).on('click', '.btn-delete-perm', function () {
            var $this = $(this);
            var name = "{{ asset_permission.name }}";
            var uid = "{{ asset_permission.id }}";
            var the_url = '{% url "api-perms:asset-permission-detail" pk=DEFAULT_PK %}'.replace('{{ DEFAULT_PK }}', uid);
            var redirect_url = "{% url 'perms:asset-permission-list' %}";
            objectDelete($this, name, the_url, redirect_url);
        }).on('click', '#btn_add_system_user', function () {
            if (Object.keys(jumpserver.system_users_selected).length === 0) {
                return false;
            }
            var system_users = $('.bdg-system-user').map(function() {
                return $(this).data('uid');
            }).get();
            $.map(jumpserver.system_users_selected, function(value, index) {
                system_users.push(parseInt(index));
                $('#opt_' + index).remove();
            });
            addSystemUser(system_users)
        }).on('click', '.btn-del', function () {
            var $this = $(this);
            var $uid = $this.data('uid');
            var $tr = $this.closest('tr');
            var $badge = $tr.find('.bdg-system-user');
            var $system_user = $badge.html() || $badge.text();
            $('#groups_selected').append(
                '<option value="' + $uid + '" id="opt_' + $uid + '">' + $system_user + '</option>'
            );
            removeSystemUser($uid, $tr)
        })
    </script>
{% endblock %}
