<title>菜单表</title>
<!--当前位置开始-->
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <script type="text/html" template lay-done="layui.data.updateMainBreadcrumb();">
        </script>
    </div>
</div>
<!--当前位置结束-->
<style>
    /* 重写样式 */
</style>
<script type="text/html" template lay-type="Post" lay-url="{{ layui.setter.apiUrl }}Api/SysMenu/GetIndex" lay-done="layui.data.done(d);">
    <div class="layui-form coreshop-search-form">
        <div class="layui-form-item">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" name="menuName" id="menuName" placeholder="请输入菜单名称" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" name="authority" id="authority" placeholder="请输入权限标识" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <button class="layui-btn layui-btn-sm" lay-submit lay-filter="LAY-app-SysMenu-search"><i class="layui-icon layui-icon-search"></i>筛选</button>
            </div>

            <div class="layui-inline" style="float: right">
                <button class="layui-btn  layui-btn-sm mybtn  layuiadmin-btn-admin" data-type="add"><i class="layui-icon layui-icon-add-1"></i>添加数据</button>
            </div>
        </div>
    </div>
</script>

<div class="table-body">
    <table id="LAY-app-SysMenu-tableBox" lay-filter="LAY-app-SysMenu-tableBox"></table>
</div>

<script type="text/html" id="LAY-app-SysMenu-tableBox-bar">

    {{#  if(d.menuType === 0){ }}
    <a class="layui-btn layui-btn-xs" lay-event="importButton">导入按钮</a>
    {{#  } }}

    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" data-dropdown="#SysMenuTbDelDrop{{d.id}}" no-shade="true">删除</a>
    <div class="dropdown-menu-nav dropdown-popconfirm dropdown-top-right layui-hide" id="SysMenuTbDelDrop{{d.id}}"
         style="max-width: 200px; white-space: normal; min-width: auto; margin-left: 10px;">
        <div class="dropdown-anchor"></div>
        <div class="dropdown-popconfirm-title">
            <i class="layui-icon layui-icon-help"></i>
            确定要删除【{{d.menuName}}】吗？
        </div>
        <div class="dropdown-popconfirm-btn">
            <a class="layui-btn layui-btn-primary cursor" btn-cancel>取消</a>
            <a class="layui-btn layui-btn-normal cursor" lay-event="del">确定</a>
        </div>
    </div>
</script>

<script>
    var indexData;
    var debug = layui.setter.debug;
    var insTb;
    layui.data.done = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d); }

        indexData = d.data;
        layui.use(['index', 'table', 'laydate', 'util', 'coredropdown', 'coreHelper','treeTable','zTree'],
            function () {
                var $ = layui.$
                    , admin = layui.admin
                    , table = layui.table
                    , form = layui.form
                    , laydate = layui.laydate
                    , setter = layui.setter
                    , coreHelper = layui.coreHelper
                    , treeTable = layui.treeTable
                    , util = layui.util
                    , zTree = layui.zTree
                    , view = layui.view;
                var tbDataList = [];  // 表格全部数据

                //数据绑定
                    insTb = treeTable.render({
                        elem: '#LAY-app-SysMenu-tableBox',
                        tree: {
                            iconIndex: 1,
                            idName: 'id',
                            pidName: 'parentId',
                            isPidData: true,
                            //getIcon: ''
                        },
                        height: 'full-250',
                        cols: [
                            {field: 'id', title: '序列', width: 80},
                            {field: 'menuName', title: '菜单名称', minWidth: 110},
                            {
                                title: '图标', templet: '<p><i class="layui-icon {{d.menuIcon}}"></i></p>',
                                align: 'center', width: 60, minWidth: 60
                            },
                            {field: 'path', title: '菜单Url'},
                            {field: 'identificationCode', title: '文件夹标识'},
                            {field: 'component', title: 'API请求地址'},
                            {field: 'authority', title: '权限标识'},
                            {
                                field: 'sortNumber', title: '排序', align: 'center',
                                width: 60, minWidth: 60
                            },
                            {
                                title: '隐藏', templet: function (d) {
                                    return d.hide ? '<span class="text-danger">隐藏</span>' : '显示';
                                }, align: 'center', width: 60, minWidth: 60
                            },
                            {
                                title: '类型', templet: function (d) {
                                    var strs = ['<span class="layui-badge layui-badge-green">菜单</span>', '<span class="layui-badge layui-badge-gray">按钮</span>'];
                                    return strs[d.menuType];
                                }, align: 'center', width: 70, minWidth: 70
                            },
                            //{field: 'createTime', title: '创建时间'},
                            {title: '操作', toolbar: '#LAY-app-SysMenu-tableBox-bar', align: 'center', width: 180}
                        ],
                        reqData: function (data, callback) {
                            coreHelper.Post('Api/SysMenu/GetPageList',null, function (res) {
                                tbDataList = res.data ? res.data : [];
                                //for (var i = 0; i < tbDataList.length; i++) {
                                //    if (tbDataList[i].parentId==0) {
                                //        tbDataList[i].open = true;
                                //    } else {
                                //        tbDataList[i].open = false;
                                //    }

                                //}
                                callback(tbDataList);
                            });
                        }
                    });
                    //重载form
                    form.render();

                     //事件
                    var active = {
                        add: function () {
                            doCreate();
                        }
                    };
                    $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
                        var type = $(this).data('type');
                        active[type] ? active[type].call(this) : '';
                    });

                //监听工具条
                treeTable.on('tool(LAY-app-SysMenu-tableBox)',
                    function (obj) {
                        if (obj.event === 'del') {
                            doDelete(obj);
                        } else if (obj.event === 'edit') {
                            doEdit(obj)
                        }else if (obj.event === 'importButton') {
                            doImportButton(obj)
                        }
                    });
                //执行创建操作
                function doCreate() {
                    coreHelper.Post("Api/SysMenu/GetCreate", null, function (e) {
                        if (e.code === 0) {
                            admin.popup({ shadeClose: false,
                                title: '创建数据',
                                area: ['600px', '400px'],
                                id: 'LAY-popup-SysMenu-create',
                                success: function (layero, index) {
                                    view(this.id).render('System/SysMenu/create', { data: e.data,treeData:insTb.options.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-SysMenu-createForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段

                                                if (!field.parentId) {
                                                    field.parentId = 0;
                                                }

                                                field.hide = field.hide == 'on';
                                                //field.deleted = field.deleted == 'on';

                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/SysMenu/DoCreate", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        insTb.refresh();
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    });
                                    // 弹窗不出现滚动条
                                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }
                //执行编辑操作
                function doEdit(obj) {
                    coreHelper.Post("Api/SysMenu/GetEdit", { id: obj.data.id }, function (e) {
                        if (e.code === 0) {
                            admin.popup({ shadeClose: false,
                                title: '编辑数据',
                                area: ['600px', '400px'],
                                id: 'LAY-popup-SysMenu-edit',
                                success: function (layero, index) {
                                    view(this.id).render('System/SysMenu/edit', { data: e.data, treeData: insTb.options.data }).done(function() {
                                        //监听提交
                                        form.on('submit(LAY-app-SysMenu-editForm-submit)',
                                            function(data) {
                                                var field = data.field; //获取提交的字段
                                                if (!field.parentId) {
                                                    field.parentId = 0;
                                                }
                                                field.hide = field.hide == 'on';
                                                //field.deleted = field.deleted == 'on';
                                                if (debug) {
                                                    console.log(field);
                                                } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/SysMenu/DoEdit",
                                                    field,
                                                    function(e) {
                                                        console.log(e)
                                                        if (e.code === 0) {
                                                            insTb.refresh();
                                                            layer.close(index); //再执行关闭
                                                            layer.msg(e.msg);
                                                        } else {
                                                            layer.msg(e.msg);
                                                        }
                                                    });
                                            });
                                    });
                                    // 禁止弹窗出现滚动条
                                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }

                //执行权限设置操作
                function doImportButton(obj) {
                    console.log(obj);
                    admin.popup({ shadeClose: false,
                        title: '按钮选择',
                        area: ['600px', '90%'],
                        btn: ['保存', '取消'],
                        content: '<ul id="roleAuthTree" class="ztree" style="padding-left: 15px;min-height: 90%;"></ul>',
                        success: function (layero, dIndex) {
                            var loadIndex = layer.load(2);
                            coreHelper.Post('Api/Tools/GetAllControllerAndActionByAssembly', null, function (res) {
                                layer.close(loadIndex);
                                if (0 === res.code) {
                                    $.fn.zTree.init($('#roleAuthTree'), {
                                        check: { enable: true },
                                        data: {
                                            key: {
                                                name: "name",
                                                children:"action"
                                            },
                                        }
                                    }, res.data);
                                } else {
                                    layer.msg(res.msg, { icon: 2, anim: 6 });
                                }
                            });
                            // 超出一定高度滚动
                            //$(layero).children('.layui-layer-content').css({ 'max-height': '300px', 'overflow': 'auto' });
                        },
                        yes: function (dIndex) {
                            var checkedRows = $.fn.zTree.getZTreeObj('roleAuthTree').getCheckedNodes(true);
                            var ids = [];
                            var postData = [];
                            for (var i = 0; i < checkedRows.length; i++) {
                                ids.push(checkedRows[i].id);
                                if (checkedRows[i].type=='action') {
                                    let row = {
                                        menuId:obj.data.id,
                                        controllerName:checkedRows[i].controllerName,
                                        actionName:checkedRows[i].actionName,
                                        description:checkedRows[i].description,
                                        type:checkedRows[i].type,
                                    }
                                    postData.push(row);
                                }
                            }
                            console.log(postData);

                            var loadIndex = layer.load(2);
                            coreHelper.Post('Api/SysMenu/ImportButtons', {data: postData }, function (res) {
                                layer.close(loadIndex);
                                if (0 === res.code) {
                                    layer.msg(res.msg, { icon: 1 });
                                    insTb.refresh();
                                    layer.close(dIndex);
                                } else {
                                    layer.msg(res.msg, { icon: 2, anim: 6 });
                                }
                            });
                        }
                    });
                }

                //执行单个删除
                function doDelete(obj) {
                    console.log(obj);
                    coreHelper.Post("Api/SysMenu/DoDelete", { id: obj.data.id }, function (e) {
                        if (debug) { console.log(e); } //开启调试返回数据
                        //table.reload('LAY-app-SysMenu-tableBox');
                        insTb.refresh();
                        layer.msg(e.msg);
                    });
                }

            });
    };
</script>