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

{% block custom_head_css_js %}
    <link href="{% static "css/plugins/sweetalert/sweetalert.css" %}" rel="stylesheet">
    <script src="{% static "js/plugins/sweetalert/sweetalert.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 'users:user-group-detail' pk=user_group.id %}" class="text-center"><i class="fa fa-laptop"></i> {% trans 'User group detail' %} </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>
                            <li class="pull-right">
                                <a class="btn btn-outline btn-default" href="{% url 'users:user-group-update' pk=user_group.id %}"><i class="fa fa-edit"></i>{% trans 'Update' %}</a>
                            </li>
                            <li class="pull-right">
                                <a class="btn btn-outline btn-danger btn-delete-user-group">
                                    <i class="fa fa-trash-o"></i>{% trans '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>{{ 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>
                                    </div>
                                </div>
                                <div class="ibox-content">
                                    <table class="table">
                                        <tbody>
                                        <tr class="no-borders-tr">
                                            <td width="20%">{% trans 'Name' %}:</td>
                                            <td><b>{{ user_group.name }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Create by' %}:</td>
                                            <td><b>{{ user_group.created_by }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Date created' %}:</td>
                                            <td><b>{{ user_group.date_created }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Comment' %}:</td>
                                            <td><b>{{ user_group.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 'User' %}
                                </div>
                                <div class="panel-body">
                                    <table class="table user_edit">
                                        <tbody>
                                        <form>
                                            <tr>
                                                <td colspan="2" class="no-borders">
                                                    <select data-placeholder="{% trans 'Add user' %}" id="slct_users" class="select2" style="width: 100%" multiple="" tabindex="4">
                                                    </select>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="2" class="no-borders">
                                                    <button type="button" class="btn btn-primary btn-small" id="btn_add_user">{% trans 'Add' %}</button>
                                                </td>
                                            </tr>
                                        </form>

                                        {% for user in user_group.users.all %}
                                        <tr>
                                          <td ><b class="bdg_user" >{{ user.name }}</b></td>
                                          <td>
                                              <button class="btn btn-danger pull-right btn-xs btn-remove-user" data-uid={{ user.id }} type="button"><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>

var usersGroupsRelationUrl = "{% url 'api-users:users-groups-relation-list' %}";

function addObjects(objectsId) {
    if (!objectsId || objectsId.length === 0) {
        return
    }
    var theUrl = usersGroupsRelationUrl;
    var body = [];
    objectsId.forEach(function (v) {
        var data = {usergroup: "{{ object.id }}"};
        data["user"] = v;
        body.push(data)
    });
    requestApi({
        url: theUrl,
        body: JSON.stringify(body),
        method: "POST",
        success: reloadPage
    });
}

function removeObject(objectId, type) {
    if (!objectId)  {
        return
    }
    var theUrl = usersGroupsRelationUrl;
    theUrl = setUrlParam(theUrl, 'usergroup', "{{ object.id }}");
    theUrl = setUrlParam(theUrl, 'user', objectId);
    requestApi({
        url: theUrl,
        method: "DELETE",
        success: reloadPage
    });
}

$(document).ready(function () {
   $('.select2').select2();
    usersSelect2Init('#slct_users')
}).on('click', '.btn-remove-user', function() {
    var objectId = $(this).data("uid");
    removeObject(objectId);
}).on('click', '#btn_add_user', function() {
    var objectsId = $("#slct_users").val();
    addObjects(objectsId);
}).on('click', '.btn-delete-user-group', function () {
    var $this = $(this);
    var name = "{{ user_group.name }}";
    var uid = "{{ user_group.id }}";
    var the_url = '{% url "api-users:user-group-detail" pk=DEFAULT_PK %}'.replace('{{ DEFAULT_PK }}', uid);
    var redirect_url = "{% url 'users:user-group-list' %}";
    objectDelete($this, name, the_url, redirect_url);
})

</script>
{% endblock %}
