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

{% block custom_head_css_js %}
    <link href="{% static "css/plugins/select2/select2.min.css" %}" rel="stylesheet">
    <link href="{% static "css/plugins/sweetalert/sweetalert.css" %}" rel="stylesheet">
    <link href="{% static "css/plugins/dataTables/dataTables.min.css" %}" rel="stylesheet">
    <link href="{% static "css/plugins/awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.css" %}" rel="stylesheet">
    <script src="{% static "js/plugins/select2/select2.full.min.js" %}"></script>
    <script src="{% static "js/plugins/sweetalert/sweetalert.min.js" %}"></script>
    <script src="{% static "js/plugins/dataTables/dataTables.min.js" %}"></script>
    <style>
.user_div {
    color: #5e5e5e;
    font-family: "Open Sans";
    padding: 3px 8px;
    text-shadow: none;
}
.user_div .ui_container {
    background-color: #d1dade;
    width: 100%;
}
.user_div a {
    color: #5e5e5e;
}
.user_div .remove {
    color: #fff;
}
dl {
  width: 100%;
  overflow: hidden;
  padding: 0;
  margin: 10px;
  border-bottom: 1px solid #e7eaec;
}
dt {
  float: left;
  width: 30%;
  padding: 0;
  margin: 0
}
dd {
  float: left;
  width: 70%;
  padding: 0;
  margin: 0
}

#group_user_row dt {
  border-bottom: 1px solid #e7eaec;
  width: 100%;
  margin-bottom: 20px;
}

#group_user_row dd {
  width: 100%;
  overflow: auto;
}
    </style>
{% 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 'users:user-group-detail' pk=object.id %}" class="text-center"><i class="fa fa-laptop"></i> {% trans 'User Group Detail' %} </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>{{ object.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>
                                    </div>
                                </div>
                                <div class="ibox-content">
                                        <dl>
                                            <dt width="20%">{% trans 'Name' %}:</dt>
                                            <dd><b>{{ object.name }}</b></dd>
                                        </dl>
                                        <dl>
                                            <dt>{% trans 'Comment' %}:</dt>
                                            <dd><b>{{ object.comment }}</b></dd>
                                        </dl>
                                        <dl>
                                            <dt>{% trans 'Created at' %}:</dt>
                                            <dd><b>{{ object.date_created }}</b></dd>
                                        </dl>
                                        <dl id="group_user_row">
                                            <dt>{% trans 'Users' %}:</dt>
                                            <dd style="line-height: 2" id="group_user_container">
                                            {% for user in object.users.all %}
                                            <div class="col-sm-4 user_div"><div class="ui_container row"><div class="col-xs-9"><a href="{{ user.get_absolute_url }}" title="{{ user.name }}" data-toggle="tooltip">{{ user.name|truncatechars:15 }}</a></div><div class="col-xs-3"><a data-uid="{{ user.id }}" class="btn_remove m-l-5"><i class="remove fa fa-times-circle"></i></a></div></div></div>
                                            {% endfor %}
                                            </dd>
                                        </dl>
                                </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 modify' %}
                                </div>
                                <div class="panel-body">
                                    <table class="table">
                                        <tbody>
                                            <tr>
                                                <td>{% trans 'Add User' %}:</td>
                                                <td>
                                                    <span class="pull-right">
                                                        <button type="button" class="btn btn-primary btn-xs" id="btn_group_add_user" style="width: 54px" data-toggle="modal" data-target="#select_user_modal">{% trans 'Add' %}</button>
                                                    </span>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>{% trans 'Delete' %}:</td>
                                                <td>
                                                    <span class="pull-right">
                                                        <button type="button" class="btn btn-danger btn-xs" id="btn_group_delete" style="width: 54px">{% trans 'Delete' %}</button>
                                                    </span>
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    {% include "users/_select_user_modal.html" %}
{% endblock %}
{% block custom_foot_js %}
<script>
$(document).on('click', '.btn_remove', function(){
    var $this = $(this);
    var uid = $this.data('uid');
    var the_url = '{% url "users:delete-user-from-group-api" pk=object.id uid=99991937 %}'.replace('99991937', uid);
    var success = function(){
        $this.closest('.user_div').remove();
    };
    var error = function(){};
    APIUpdateAttr({url: the_url, body: "{}", method: "DELETE", success: success, error: error});
    return false;
}).on('click', '#btn_group_delete', function() {
    function doDelete() {
        var the_url = '{% url "users:user-group-detail-api" pk=object.id %}';
        var success = function() {
            window.location.href = '{% url "users:user-group-list" %}';
        };
        APIUpdateAttr({url: the_url, body: "{}", method: "DELETE", success: success});
    }
    swal({
        title: "{% trans 'Are you sure?' %}",
        text: "{% trans 'This will delete the current group, but will not delete any user of it.' %}",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "{% trans 'Confirm' %}",
        closeOnConfirm: false
    }, function() {
        doDelete();
    });
}).on('shown.bs.modal', '#select_user_modal', function() {
    if ($.fn.dataTable.isDataTable('#select_user_table')) {
        return true;
    }
    var options = {
        ele: $('#select_user_table'),
        pageLength: 10,
        buttons: [],
        columnDefs: [
            {targets: 6, 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: 4, createdCell: function (td, cellData) {
                var innerHtml = cellData.length > 8 ? cellData.substring(0, 8) + '...': cellData;
                $(td).html('<a href="javascript:void(0);" data-toggle="tooltip" title="' + cellData + '">' + innerHtml + '</a>');
             }}
        ],
        ajax_url: '{% url "users:user-bulk-update-api" %}',
        columns: [{data: function(){return ""}}, {data: "username" }, {data: "name" }, {data: "get_role_display" }, {data: "group_display" },
                  {data: function(){return 999}}, {data: "active_display" }],
    };
    jumpserver.initDataTable(options);
}).on('click', '#btn_select_user', function() {
    var $data_table = $('#select_user_table').DataTable();
    var plain_id_list = [];
    var selected_users = [];
    $data_table.rows({selected: true}).every(function(){
        plain_id_list.push(this.data().id);
        selected_users.push({id: this.data().id, name: this.data().name});
    });
    if (plain_id_list === []) {
        return false;
    };
    var body = {
        id: {{ object.id }},
        users: plain_id_list.map(Number)
    };
    $('#select_user_modal').modal('hide');
    var the_url = "{% url 'users:user-group-detail-api' pk=object.id %}";
    var success = function() {
        toastr.success('{% trans "The selected users has been added to current group." %}');
        var html = "";
        $.each(selected_users, function(index, user) {
            html += [
                '<div class="col-sm-4 user_div"><div class="ui_container row"><div class="col-xs-9"><a title="',
                user.name,
                '" data-toggle="tooltip" href="',
                '{% url "users:user-detail" pk=99991937 %}'.replace(99991937, user.id),
                '">',
                user.name.length >=13 ? user.name.substring(0, 12) + '...' : user.name,
                '</a></div><div class="col-xs-3"><a data-uid="',
                user.id,
                '" class="btn_remove m-l-5"><i class="remove fa fa-times-circle"></i></a></div></div></div>\n',
            ].join("");
        });
        $(html).appendTo($('#group_user_container'));
    }
    APIUpdateAttr({url: the_url, body: JSON.stringify(body), success: success});
})
</script>
{% endblock %}
