<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <base th:href="${#request.getContextPath() + '/'}">
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="assets/fonts/font-awesome/css/font-awesome.css"/>
    <link rel="stylesheet" href="assets/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="assets/layuiadmin/style/admin.css" media="all">
</head>
<body>
<div class="layui-fluid layui-anim layui-anim-fadein" style="padding: 0;">
    <div class="layui-row">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="test-table-reload-btn" style="margin-bottom: 10px;">
                        <div class="layui-inline">
                            <input type="text" id="keywords" class="layui-input search-item" placeholder="输入关键字">
                        </div>
                        <div class="layui-btn-group">
                            <button type="button" id="search" class="layui-btn layui-btn-sm">
                                <i class="fa fa-lg fa-search"></i> 搜索
                            </button>
                            <button type="button" class="layui-btn layui-btn-sm" id="uploadExcel">
                                <i class="fa fa-lg fa-cloud-upload"></i> 导入数据
                            </button>
                            <button type="button" class="layui-btn layui-btn-sm" id="restCode">
                                <i class="fa fa-lg fa-recycle"></i> 重置编码
                            </button>
                        </div>
                    </div>
                    <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
                    <!-- 表头按钮组 -->
                    <script type="text/html" id="headerToolBar">
                        <div class="layui-btn-group">
                            <button type="button" class="layui-btn layui-btn-sm" lay-event="save">
                                <i class="fa fa-lg fa-plus"></i> 新增
                            </button>
                            <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" lay-event="update">
                                <i class="fa fa-lg fa-edit"></i> 编辑
                            </button>
                            <button type="button" class="layui-btn layui-btn-danger layui-btn-sm" lay-event="remove">
                                <i class="fa fa-lg fa-trash"></i> 删除
                            </button>
                            <button type="button" class="layui-btn layui-btn-warm layui-btn-sm" lay-event="merge">
                                <i class="fa fa-lg fa-compress"></i> 合并
                            </button>
                        </div>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="assets/layuiadmin/layui/layui.js"></script>
<script type="text/javascript">
    layui.config({
        base: 'assets/layuiadmin/'
    }).extend({
        index: 'lib/index'
    }).use(['index', 'table', 'laydate', 'ztree'], function() {
        var $ = layui.$;
        var $win = $(window);
        var table = layui.table;
        var admin = layui.admin;
        var laydate = layui.laydate;
        var parentId = top.restAjax.params(window.location.href).parentId;
        var tableUrl = 'api/department/listpage?parentId={parentId}';

        // 初始化表格
        function initTable() {
            table.render({
                elem: '#dataTable',
                id: 'dataTable',
                url: top.restAjax.path(tableUrl, [parentId]),
                width: admin.screen() > 1 ? '100%' : '',
                height: $win.height() - 60,
                limit: 20,
                limits: [20, 40, 60, 80, 100, 200],
                toolbar: '#headerToolBar',
                request: {
                    pageName: 'page',
                    limitName: 'rows'
                },
                cols: [
                    [
                        {type:'checkbox', fixed: 'left'},
                        {field:'rowNum', width:80, title: '序号', fixed: 'left', align:'center', templet: '<span>{{d.LAY_INDEX}}</span>'},
                        {field:'users', width:100, title: '人员列表', align:'center',
                            templet: function(item) {
                                return '<button type="button" class="layui-btn layui-btn-xs" lay-event="userEvent"><i class="fa fa-users"></i> 查看</button>';
                            }
                        },
                        {field:'departmentName', width:180, title: '组织机构名称', align:'center',},
                        {field:'departmentSummary', width:180, title: '组织说明', align:'center',},
                        {field:'departmentCode', width:100, title: '组织编码', align:'center',},
                        {field:'departmentCode', width:100, title: '组织编号', align:'center',},
                        {field:'departmentType', width:100, title: '组织类型', align:'center',
                            templet: function(item) {
                                var value;
                                switch (item.departmentType) {
                                    case 1:
                                        value = '机构';
                                        break;
                                    case 2:
                                        value = '部门';
                                        break;
                                    default:
                                        value = '无';
                                }
                                return value;
                            }
                        },
                        {field:'departmentState', width:100, title: '组织状态', align:'center',
                            templet: function(item) {
                                var value;
                                switch (item.departmentState) {
                                    case 1:
                                        value = '<span class="layui-badge layui-bg-green">正常</span>';
                                        break;
                                    case 2:
                                        value = '<span class="layui-badge layui-bg-black">异常</span>';
                                        break;
                                    case 3:
                                        value = '<span class="layui-badge">停用</span>';
                                        break;
                                    default:
                                        value = '错误'
                                }
                                return value;
                            }
                        },
                        {field:'operation', width:100, title: '操作', fixed: 'right', align:'center',
                            templet: function(item) {
                                return '<div class="layui-btn-group">' +
                                    '<button type="button" class="layui-btn layui-btn-xs layui-btn-danger" lay-event="splitEvent">拆分</button>' +
                                    '<button type="button" class="layui-btn layui-btn-xs layui-btn-normal" lay-event="getEvent">查看</button>' +
                                    '</div>';
                            }
                        }
                    ]
                ],
                page: true,
                parseData: function(data) {
                    return {
                        'code': 0,
                        'msg': '',
                        'count': data.total,
                        'data': data.rows
                    };
                }
            });
        }
        // 重载表格
        function reloadTable(currentPage) {
            table.reload('dataTable', {
                url: top.restAjax.path(tableUrl, [parentId]),
                where: {
                    keywords: $('#keywords').val(),
                },
                page: {
                    curr: currentPage
                },
                height: $win.height() - 60,
            });
        }
        function refreshTable() {
            parent.common.refreshTree('leftTree');
            reloadTable();
        }
        // 初始化日期
        function initDate() {}
        // 删除
        function removeData(ids) {
            top.dialog.msg(top.dataMessage.delete, {
                time: 0,
                btn: [top.dataMessage.button.yes, top.dataMessage.button.no],
                shade: 0.3,
                yes: function (index) {
                    top.dialog.close(index);
                    var layIndex;
                    top.restAjax.delete(top.restAjax.path('api/department/remove/{ids}', [ids]), {}, null, function (code, data) {
                        top.dialog.msg(top.dataMessage.deleteSuccess, {time: 1000});
                        refreshTable();
                    }, function (code, data) {
                        top.dialog.msg(data.msg);
                    }, function () {
                        layIndex = top.dialog.msg(top.dataMessage.deleting, {icon: 16, time: 0, shade: 0.3});
                    }, function () {
                        top.dialog.close(layIndex);
                    });
                }
            });
        }
        initTable();
        initDate();

        // 事件 - 页面变化
        $win.on('resize', function() {
            reloadTable();
        });
        // 事件 - 搜索
        $(document).on('click', '#search', function() {
            reloadTable(1);
        });
        $(document).on('click', '#uploadExcel', function() {
            top.dialog.open({
                url: top.restAjax.path('route/department/upload/upload-excel', []),
                title: '导入部门数据',
                width: '300px',
                height: '196px',
                onClose: function() {
                    parent.location.reload();
                }
            });
        });
        $(document).on('click', '#restCode', function() {
            top.dialog.msg('确认重置？', {
                time: 0,
                btn: [top.dataMessage.button.yes, top.dataMessage.button.no],
                shade: 0.3,
                yes: function (index) {
                    top.dialog.close(index);
                    var layIndex;
                    top.restAjax.put(top.restAjax.path('api/department/resetcode', []), {}, null, function (code, data) {
                        top.dialog.msg('重置成功', {time: 1000});
                        refreshTable();
                    }, function (code, data) {
                        top.dialog.msg(data.msg);
                    }, function () {
                        layIndex = top.dialog.msg('重置中...', {icon: 16, time: 0, shade: 0.3});
                    }, function () {
                        top.dialog.close(layIndex);
                    });
                }
            });
        });
        // 事件 - 增删改
        table.on('toolbar(dataTable)', function(obj) {
            var layEvent = obj.event;
            var checkStatus = table.checkStatus('dataTable');
            var checkDatas = checkStatus.data;
            if(layEvent === 'save') {
                layer.open({
                    type: 2,
                    title: false,
                    closeBtn: 0,
                    area: ['100%', '100%'],
                    shadeClose: true,
                    anim: 2,
                    content: top.restAjax.path('route/department/save?departmentParentId={parentId}', [parentId]),
                    end: function() {
                        reloadTable();
                    }
                });
            } else if(layEvent === 'update') {
                if(checkDatas.length === 0) {
                    top.dialog.msg(top.dataMessage.table.selectEdit);
                } else if(checkDatas.length > 1) {
                    top.dialog.msg(top.dataMessage.table.selectOneEdit);
                } else {
                    layer.open({
                        type: 2,
                        title: false,
                        closeBtn: 0,
                        area: ['100%', '100%'],
                        shadeClose: true,
                        anim: 2,
                        content: top.restAjax.path('route/department/update?departmentId={departmentId}', [checkDatas[0].departmentId]),
                        end: function() {
                            reloadTable();
                        }
                    });
                }
            } else if(layEvent === 'remove') {
                if(checkDatas.length === 0) {
                    top.dialog.msg(top.dataMessage.table.selectDelete);
                } else {
                    var ids = '';
                    for(var i = 0, item; item = checkDatas[i++];) {
                        if(i > 1) {
                            ids += '_';
                        }
                        ids += item.departmentId;
                    }
                    removeData(ids);
                }
            } else if(layEvent === 'merge') {
                if(checkDatas.length === 0) {
                    top.dialog.msg('请选择要合并的组织');
                } else {
                    var departmentIds = '';
                    for(var i = 0, item; item = checkDatas[i++];) {
                        if(departmentIds.length > 0) {
                            departmentIds += '_';
                        }
                        departmentIds += item.departmentId;
                    }
                    top.dialog.dialogData.mergeDepartmentIds = departmentIds;
                    top.dialog.open({
                        url: top.restAjax.path('route/department/get-merge', []),
                        title: '合并选项',
                        width: '300px',
                        height: '180px',
                        onClose: function() {
                            if(!top.dialog.dialogData.mergeType) {
                                return;
                            }
                            var mergeType = top.dialog.dialogData.mergeType;
                            if(mergeType === 'newDepartment') {
                                top.dialog.open({
                                    url: top.restAjax.path('route/department/save-merge?departmentParentId={parentId}', [parentId]),
                                    title: '新建组织',
                                    width: '800px',
                                    height: '500px',
                                    onClose: function() {
                                        refreshTable();
                                    }
                                });
                            } else if(mergeType === 'selectDepartment') {
                                top.dialog.open({
                                    url: top.restAjax.path('route/department/list-tree-select', []),
                                    title: '选择组织',
                                    width: '200px',
                                    height: '400px',
                                    onClose: function() {
                                        var selectNodes = top.dialog.dialogData.selectedNodes;
                                        console.log(selectNodes);
                                        if(selectNodes && selectNodes.length > 0) {
                                            for(var i = 0, item; item = checkDatas[i++];) {
                                                if(item.departmentId == selectNodes[0].id) {
                                                    top.dialog.msg('不能选中要合并的组织');
                                                    return;
                                                }
                                            }
                                            top.dialog.confirm('确定合并到该组织吗？操作不可撤销！', function(index) {
                                                top.dialog.close(index);
                                                var loadLayerIndex;
                                                top.restAjax.put(top.restAjax.path('api/department/updatemerge/{departmentId}', [selectNodes[0].id]), {
                                                    mergeDepartmentIds: top.dialog.dialogData.mergeDepartmentIds
                                                }, null, function(code, data) {
                                                    top.dialog.msg('合并成功');
                                                    refreshTable();
                                                }, function(code, data) {
                                                    top.dialog.msg(data.msg);
                                                }, function() {
                                                    loadLayerIndex = top.dialog.msg('正在合并...', {icon: 16, time: 0, shade: 0.3});
                                                }, function() {
                                                    top.dialog.close(loadLayerIndex);
                                                });
                                            });
                                        }
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });
        table.on('tool(dataTable)', function(obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if(layEvent === 'userEvent') {
                top.dialog.open({
                    url: top.restAjax.path('route/department/user/list?departmentId={departmentId}', [data.departmentId]),
                    title: '【'+ data.departmentName +'】用户列表',
                    width: '800px',
                    height: '500px',
                    onClose: function() {}
                });
            } else if(layEvent === 'splitEvent') {
                top.dialog.open({
                    url: top.restAjax.path('route/department/list-split?departmentId={departmentId}&departmentParentId={departmentParentId}', [data.departmentId, data.departmentParentId]),
                    title: '拆分组织',
                    width: '800px',
                    height: '500px',
                    onClose: function() {
                        refreshTable();
                    }
                });
            } else if(layEvent === 'getEvent') {
                layer.open({
                    type: 2,
                    title: false,
                    closeBtn: 0,
                    area: ['100%', '100%'],
                    shadeClose: true,
                    anim: 2,
                    content: top.restAjax.path('route/department/get?departmentId={departmentId}', [data.departmentId]),
                    end: function() {}
                });
            }
        })
    });
</script>
</body>
</html>