{% extends '_base_list.html' %}
{% load i18n %}

{% block table_search %}
{% endblock %}

{% block help_message %}
    <div class="alert alert-info help-message">
    提前规划好集群中的系统用户，授权时选择的资产(组内资产)必须存在该系统用户，否则可能无法成功登录
    </div>
{% endblock %}

{% block table_container %}
<div class="uc pull-left m-r-5">
    <a href="{% url 'perms:asset-permission-create' %}" class="btn btn-sm btn-primary ">
        {% trans "Create permission" %}
    </a>
</div>
<table class="table table-striped table-bordered table-hover" id="permission_list_table" >
    <thead>
    <tr>
      <th class="text-center">
          <input type="checkbox" id="check_all" class="ipt_check_all" >
      </th>
      <th class="text-center">{% trans 'Name' %}</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">{% trans 'Is valid' %}</th>
      <th class="text-center">{% trans 'Action' %}</th>
    </tr>
    </thead>
    <tbody>
    </tbody>
</table>
{% endblock %}

{% block custom_foot_js %}
<script>
function initTable() {
    var options = {
        ele: $('#permission_list_table'),
        columnDefs: [
            {targets: 1, createdCell: function (td, cellData, rowData) {
                var detail_btn = '<a href="{% url "perms:asset-permission-detail" pk=DEFAULT_PK %}">' + cellData + '</a>';
                $(td).html(detail_btn.replace('{{ DEFAULT_PK }}', rowData.id));
            }},
            {targets: 2, createdCell: function (td, cellData) {
                $(td).html(cellData.length);
            }},
            {targets: 3, createdCell: function (td, cellData) {
                $(td).html(cellData.length);
            }},
            {targets: 4, createdCell: function (td, cellData) {
                $(td).html(cellData.length);
            }},
            {targets: 5, createdCell: function (td, cellData) {
                $(td).html(cellData.length);
            }},
            {targets: 6, createdCell: function (td, cellData) {
                $(td).html(cellData.length);
            }},
            {targets: 7, 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: 8, createdCell: function (td, cellData) {
                var update_btn = '<a href="{% url "perms:asset-permission-update" pk=DEFAULT_PK %}" class="btn btn-xs m-l-xs btn-info">{% trans "Update" %}</a>'.replace('{{ DEFAULT_PK }}', cellData);
                var del_btn = '<a class="btn btn-xs btn-danger m-l-xs btn-del-permission" data-uid="{{ DEFAULT_PK }}">{% trans "Delete" %}</a>'.replace('{{ DEFAULT_PK }}', cellData);
				$(td).html(update_btn + del_btn);
            }}

        ],
        ajax_url: '{% url "api-perms:asset-permission-list" %}',
        columns: [
            {data: "id" }, {data: "name"}, {data: "users_" }, {data: "user_groups_" }, {data: "assets_" },
            {data: "asset_groups_"}, {data: "system_users_"}, {data: "is_active"},  {data: "id" }
        ],
        op_html: $('#actions').html()
    };
    jumpserver.initDataTable(options);
}

$(document).ready(function(){
    initTable();
})

.on('click', '.btn-del', function () {
    var $this = $(this);
    var name = $this.data('name');
    var uid = $this.data('uid');
    var the_url = '{% url "api-perms:asset-permission-detail" pk=DEFAULT_PK %}'
            .replace('{{ DEFAULT_PK }}', uid);
    objectDelete($this, name, the_url);
})
</script>
{% endblock %}
