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

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

<script type="text/html" id="edition-toolbar">
    @if (await PermissionChecker.IsGrantedAsync(AppPermissionNames.Pages_SystemManagement_Editions_Delete))
    {
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>新增
        </button>
    }
</script>

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

@await Html.PartialAsync("_LastModificationTime")

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

		    table.render({
		        elem: '#edition-table',
		        url: "@Url.Action("GetEditionList", "Editions")",
                page: false,
                height: 'full-60',
                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: [
		            [
		                {type:'checkbox'},
		                {title: '版本名称', field: 'displayName', align:'center'},
		                {title: '最后修改时间', field: 'lastModificationTime', align:'center',templet:'#lastModificationTime'},
		                {title: '操作', toolbar: '#edition-bar', align:'center', width:130}
		            ]
		        ],
		        skin: 'line',
		        toolbar: '#edition-toolbar',
		        defaultToolbar: [{
		            layEvent: 'refresh',
		            icon: 'layui-icon-refresh',
		        }, 'filter', 'print', 'exports']
		    });

            table.on('tool(edition-table)', function (obj) {
                var data = obj.data;
		        if (obj.event === 'remove') {
                    window.remove([{ "id": data.id }]);
		        } else if(obj.event === 'edit'){
		            window.edit(data);
		        }
		    });

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

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

            window.add = function () {
				parent.layer.open({
                    type: 2,
                    title: "添加版本",
                    content: "@Url.Action("CreateOrUpdateEdition", "Editions")",
                    area: ["600px","320px"],
                    shade: 0.1,
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        parent.window['layui-layer-iframe'+ index].submitForm();
                    }
                });
            }

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

            window.remove = function (data) {
                parent.layer.confirm('确定删除吗?',{
					icon: 3,
					title: '提示'
                }, function (index) {
                    parent.layer.close(index);
					let waitIndex = parent.layer.load(2);
                    abp.ajax({
                        url: "@Url.Action("DeleteEdition", "Editions")",
                        data: JSON.stringify(data),
                        abpHandleError: false
                    }).done(function (data) {
                        if (data.code == 200) {
                            abp.notify.success(data.msg);
                            table.reload('edition-table');
                        }
                    }).fail(function (jqXHR) {
                        parent.layer.msg(jqXHR.message, { icon: 5 });
                    }).always(function () {
                        parent.layer.close(waitIndex);
                    });
                });
            }

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