@using MineTec.ProManager.Authorization

@section styles{
    <style type="text/css">
        #dataDictionary-tree {
            width: 100% !important;
            height: -webkit-calc(100vh - 130px);
            height: -moz-calc(100vh - 130px);
            height: calc(100vh - 130px);
        }
    </style>
}

<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">字典项</label>
                <div class="layui-input-inline">
                    <input type="text" name="filterText" placeholder="" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="dataDictionaryItem-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
            </div>
        </form>
    </div>
</div>

<div class="layui-row layui-col-space15">
    <div class="layui-col-md3">
        <div class="layui-card">
            <div class="layui-card-body">
                <div id="dataDictionaryContent" style="overflow: auto">
                    <ul id="dataDictionary-tree" class="dtree" data-id="0"></ul>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-col-md9">
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="dataDictionaryItem-table" lay-filter="dataDictionaryItem-table"></table>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="dataDictionaryItem-toolbar">
    @if (await PermissionChecker.IsGrantedAsync(AppPermissionNames.Pages_ResourceManagement_DataDictionary_DataDictionaryItem_Delete))
    {
        <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
            <i class="layui-icon layui-icon-delete"></i>
            删除
        </button>
    }
    @if (await PermissionChecker.IsGrantedAsync(AppPermissionNames.Pages_ResourceManagement_DataDictionary_DataDictionaryItem_Create))
    {
        <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="dataDictionaryItem-bar">
    @if (await PermissionChecker.IsGrantedAsync(AppPermissionNames.Pages_ResourceManagement_DataDictionary_DataDictionaryItem_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_ResourceManagement_DataDictionary_DataDictionaryItem_Delete))
    {
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
            <i class="layui-icon layui-icon-delete"></i>
        </button>
    }
</script>

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

            form.on('submit(dataDictionaryItem-query)', function (data) {
                table.reload('dataDictionaryItem-table', {
                    where: data.field
                });
				return false;
			});

            dtree.render({
                elem: "#dataDictionary-tree",
                initLevel: "1",
                line: true,
                ficon: ["1", "-1"],
                icon: ["0", "0"],
                method: 'get',
                url: "@Url.Action("GetDataDictionaryList", "DataDictionary", new { area= "Resource" })",
                response: {
                    message: "msg",
                    statusCode: 200,
                    title: "typeName"
                },
                dataStyle: "layuiStyle"
            });

            dtree.on("node(dataDictionary-tree)", function (obj) {
                table.reload('dataDictionaryItem-table', {
                    where: { "dataDictionaryId": obj.param.nodeId }
                });
			});

            table.render({
				elem: '#dataDictionaryItem-table',
				url: '@Url.Action("GetDataDictionaryItemList", "DataDictionary", new { area= "Resource" })',
				height: 'full-150',
                page: false,
                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: 'code',
						    align: 'center'
					    },
					    {
						    title: '名称',
						    field: 'name',
						    align: 'center'
					    },
					    {
						    title: '操作',
						    toolbar: '#dataDictionaryItem-bar',
						    align: 'center',
						    width: 130
					    }
				    ]
			    ],
				skin: 'line',
				toolbar: '#dataDictionaryItem-toolbar',
				defaultToolbar: [{
					layEvent: 'refresh',
					icon: 'layui-icon-refresh',
				}, 'filter', 'print', 'exports']
            });

            table.on('tool(dataDictionaryItem-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(dataDictionaryItem-table)', function(obj){
		        if(obj.event === 'add'){
		            window.add();
		        } else if(obj.event === 'refresh'){
		            window.refresh();
		        } else if(obj.event === 'batchRemove'){
                    let checkData = table.checkStatus(obj.config.id).data;
				    if (checkData.length === 0) {
					    parent.layer.msg("未选中数据", {
						    icon: 3,
						    time: 1000
					    });
					    return false;
                    }
                    var ids = checkData.map(function (d) { return { "id": d.id }; });
                    window.remove(ids);
		        }
            });

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

            window.add = function () {
                let currentNode = dtree.getNowParam("dataDictionary-tree");
                if (currentNode.nodeId == undefined) {
					parent.layer.msg("未选中数据", {
						icon: 3,
						time: 1000
					});
					return false;
                }

				parent.layer.open({
                    type: 2,
                    title: "添加字典项",
                    content: "@Url.Action("CreateOrUpdateDataDictionaryItem", "DataDictionary", new { area="Resource" })" + abp.utils.formatString("?dataDictionaryId={0}", currentNode.nodeId),
                    area: ["500px","265px"],
                    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("CreateOrUpdateDataDictionaryItem", "DataDictionary", new { area="Resource" })" + abp.utils.formatString("?id={0}", data.id),
                    area: ["500px","265px"],
                    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("DeleteDataDictionaryItem", "DataDictionary", new { area="Resource" })",
                        data: JSON.stringify(data),
                        abpHandleError: false
                    }).done(function (data) {
                        if (data.code == 200) {
                            abp.notify.success(data.msg);
                            table.reload('dataDictionaryItem-table');
                        }
                    }).fail(function (jqXHR) {
                        parent.layer.msg(jqXHR.message, { icon: 5 });
                    }).always(function () {
                        parent.layer.close(waitIndex);
                    });
                });
            }

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

