{% extends 'base.html' %}
{% load common_tags %}
{% load users_tags %}
{% load bootstrap %}
{% 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-detail' pk=user_object.id %}" class="text-center"><i class="fa fa-laptop"></i> {% trans 'User detail' %} </a>
                            </li>
                            <li class="active">
                                <a href="{% url 'users:user-asset-permission' pk=user_object.id %}" class="text-center"><i class="fa fa-bar-chart-o"></i> {% trans 'Asset permission' %}</a>
                            </li>
                            <li>
                                <a href="{% url 'users:user-granted-asset' pk=user_object.id %}" class="text-center"><i class="fa fa-cubes"></i> {% trans 'Asset granted' %}</a>
                            </li>
                            <li>
                                <a href="{% url 'users:user-login-history' pk=user_object.id %}" class="text-center"><i class="fa fa-calculator-o"></i> {% trans 'Login history' %}</a>
                            </li>
                            <form id="search_form" method="get" action="" class="pull-right mail-search">
                            <div class="input-group">
                                <input type="text" class="form-control input-sm" name="keyword" placeholder="Search" value="{{ keyword }}">
                                <div class="input-group-btn">
                                    <button id="search_btn" type="submit" class="btn btn-sm btn-primary">
                                        搜索
                                    </button>
                                </div>
                            </div>
                            </form>
                        </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_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>
                                        <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">
                                        <thead>
                                            <tr>
                                                <th class="text-center"><a href="{% url 'perms:asset-permission-list' %}?sort=name">{% trans 'Name' %}</a></th>
                                                    <th class="text-center">{% trans 'User ' %}</th>
                                                    <th class="text-center">{% trans 'User group ' %}</th>
                                                    <th class="text-center">{% trans 'Asset ' %}</th>
                                                    <th class="text-center">{% trans 'Asset group ' %}</th>
                                                    <th class="text-center">{% trans 'System user ' %}</th>
                                                    <th class="text-center">
                                                        <a href="#">{% trans 'Is valid' %}</a>
                                                    </th>
                                                    <th></th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {% for asset_permission in object_list %}
                                            <tr class="gradeX">
                                                <td class="text-center">
                                                    <a href="{% url 'perms:asset-permission-detail' pk=asset_permission.id %}">
                                                        {{ asset_permission.name }}
                                                    </a>
                                                </td>
                                                <td class="text-center">{{ asset_permission.users.count}}</td>
                                                <td class="text-center">{{ asset_permission.user_groups.count}}</td>
                                                <td class="text-center">{{ asset_permission.assets.count }}</td>
                                                <td class="text-center">{{ asset_permission.asset_groups.count }}</td>
                                                <td class="text-center">{{ asset_permission.system_users.count }}</td>
                                                <td class="text-center">
                                                {% if asset_permission.is_valid %}
                                                    <i class="fa fa-check text-navy"></i>
                                                {% else %}
                                                    <i class="fa fa-times text-danger"></i>
                                                {% endif %}
                                                </td>
                                                <td>
                                                    <button class="btn btn-danger btn-xs btn_delete_user_group {% if asset_permission.is_inherit_from_user_groups %} disabled {% endif %}" type="button" style="float: right;"><i class="fa fa-minus"></i></button>
                                                </td>
                                                </td>
                                            </tr>
                                            {% endfor %}
                                        </tbody>
                                    </table>
                                    <div class="row">
                                        {% include '_pagination.html' %}
                                    </div>
                                </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' %}
                                </div>
                                <div class="panel-body">
                                    <form method="post" action="{% url 'users:user-asset-permission-create' pk=user_object.id %}">
                                        <table class="table">
                                            <tbody>
                                                {% csrf_token %}
                                                <tr class="no-borders-tr">
                                                    <td colspan="1" style="padding-top: 0">
                                                        {{ form.name|bootstrap }}
                                                    </td>
                                                </tr>
                                                <tr class="no-borders-tr">
                                                    <td colspan="1" style="padding-top: 0">
                                                        {{ form.assets|bootstrap }}
                                                    </td>
                                                </tr>
                                                <tr class="no-borders-tr">
                                                    <td colspan="1" style="padding-top: 0">
                                                        {{ form.asset_groups|bootstrap }}
                                                    </td>
                                                </tr>
                                                <tr class="no-borders-tr">
                                                    <td colspan="1" style="padding-top: 0">
                                                        {{ form.system_users|bootstrap }}
                                                    </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>
{#        function switch_user_status(obj) {#}
{#            var status = $(obj).prop('checked');#}
{##}
{#            $.ajax({#}
{#                url: "{% url 'users:user-active-api' pk=user_object.id %}",#}
{#                type: "PUT",#}
{#                data: {#}
{#                    'is_active': status#}
{#                },#}
{#                success: function (data, status) {#}
{#                    console.log(data)#}
{#                },#}
{#                error: function () {#}
{#                    console.log('error')#}
{#                }#}
{#            })#}
{#        }#}
        $(document).ready(function () {
            $('.select2').select2();
        });
    </script>
{% endblock %}