@using MineTec.ProManager.Admin.ViewResources
@using MineTec.ProManager.Authorization
@{
    ViewBag.CurrentPageName = PageNames.Tenants;
}

<div class="layui-card">
    <div class="layui-card-body">
        <table id="tenant-table" lay-filter="tenant-table"></table>
    </div>
</div>

<script type="text/html" id="tenant-bar">
    @if (await PermissionChecker.IsGrantedAsync(AppPermissionNames.Pages_SystemManagement_Tenants_ChangeTenantEdition))
    {
        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
    }
</script>

@section scripts{
    <script type="text/javascript">
        layui.use(['abp', 'table'], function () {
            var $ = layui.jquery;
            var abp = layui.abp;
            var table = layui.table;

		    table.render({
		        elem: '#tenant-table',
		        url: "@Url.Action("GetTenantList", "Tenants")",
                page: false,
                height: 'full-70',
                parseData: function (res) {
                    return {
                        "code": res.result.code, //解析接口状态
                        "msg": res.result.msg, //解析提示文本
                        "count": res.result.count, //解析数据长度
                        "data": res.result.data //解析数据列表
                    };
                },
                response: {
                    statusCode: 200 //规定成功的状态码，默认：0
                },
		        cols: [
		            [
		                {title: '租户名称', field: 'tenancyName'},
		                {title: '版本名称', field: 'editionName'},
		                {title: '操作', toolbar: '#tenant-bar', align:'center', width:130}
		            ]
		        ],
		        skin: 'line',
		        toolbar: '#tenant-toolbar',
		        defaultToolbar: [{
		            layEvent: 'refresh',
		            icon: 'layui-icon-refresh',
		        }, 'filter', 'print', 'exports']
		    });

            table.on('tool(tenant-table)', function (obj) {
                var data = obj.data;
		        if(obj.event === 'edit'){
		            window.edit(data);
		        }
		    });

		    table.on('toolbar(tenant-table)', function(obj){
		        if(obj.event === 'refresh'){
		            window.refresh();
		        }
            });

            window.saveCallback = function (data) {
                parent.layer.close(data.index);
                abp.notify.success(data.msg);
                table.reload("tenant-table");
            }

		    window.edit = function(data){
		        parent.layer.open({
                    type: 2,
                    title: "切换版本",
                    content: "@Url.Action("ChangeTenantEdition", "Tenants")" + abp.utils.formatString("?id={0}", data.id),
                    area: ["600px","450px"],
                    shade: 0.1,
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        parent.window['layui-layer-iframe'+ index].submitForm();
                    }
                });
            }

	        window.refresh = function(param){
		        table.reload('tenant-table');
		    }
        });
    </script>
}