<!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">
    <style>
        table.layui-table th {text-align: center;}
        table.layui-table td {text-align: center;}
    </style>
</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>
                        <button type="button" id="confirmSplitDepartmentBtn" class="layui-btn layui-btn-sm">提交拆分结果</button>
                        <button type="button" id="addSplitDepartmentBtn" class="layui-btn layui-btn-sm layui-btn-normal" style="float: right">新增拆分结果</button>
                    </div>
                    <table class="layui-table">
                        <colgroup>
                            <col width="150">
                            <col width="100">
                            <col width="100">
                            <col width="100">
                        </colgroup>
                        <thead>
                            <tr>
                                <th>组织名称</th>
                                <th>组织类型</th>
                                <th>组织状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody id="table"></tbody>
                    </table>
                    <script id="tableTemplate" type="text/html">
                        {{# if(d.length == 0) { }}
                        <tr><td colspan="4">暂无拆分组织</td></tr>
                        {{# } else { }}
                        {{#     for(var i = 0, item; item = d[i++];) { }}
                        <tr>
                            <td>{{item.departmentName}}</td>
                            <td>
                                {{# if(item.departmentType == '1'){ }}
                                机构
                                {{# } else if(item.departmentType == '2'){ }}
                                部门
                                {{# } else { }}
                                错误
                                {{# } }}
                            </td>
                            <td>
                                {{# if(item.departmentState == '1'){ }}
                                正常
                                {{# } else if(item.departmentState == '2'){ }}
                                异常
                                {{# } else if(item.departmentState == '3'){ }}
                                停用
                                {{# } else { }}
                                错误
                                {{# } }}
                            </td>
                            <td>
                                <div class="layui-btn-group">
                                    <button type="button" id="updateSplitDepartmentBtn" class="layui-btn layui-btn-xs layui-btn-normal" data-id="{{item.departmentId}}">编辑</button>
                                    <button type="button" id="removeSplitDepartmentBtn" class="layui-btn layui-btn-xs layui-btn-danger" data-id="{{item.departmentId}}">删除</button>
                                    <button type="button" id="bindSplitDepartmentUserBtn" class="layui-btn layui-btn-xs" data-id="{{item.departmentId}}">绑定人员</button>
                                </div>
                            </td>
                        </tr>
                        {{#     } }}
                        {{# } }}
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="assets/layuiadmin/layui/layui.js"></script>
<script type="text/javascript">
    var splitDepartmentArray = [];
    layui.config({
        base: 'assets/layuiadmin/'
    }).extend({
        index: 'lib/index'
    }).use(['index'], function() {
        var $ = layui.$;
        var $win = $(window);
        var laytpl = layui.laytpl;
        var hrefParams = top.restAjax.params(window.location.href);
        var departmentId = hrefParams.departmentId;
        var departmentParentId = hrefParams.departmentParentId;

        function closeBox() {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        }
        // 初始化表格
        function initTable() {
            var getTpl = document.getElementById('tableTemplate').innerHTML;
            var view = document.getElementById('table');
            laytpl(getTpl).render(splitDepartmentArray, function(html) {
                view.innerHTML = html;
            });
        }
        initTable();

        $(document).on('click', '#addSplitDepartmentBtn', function() {
            layer.open({
                type: 2,
                title: false,
                closeBtn: 0,
                area: ['100%', '100%'],
                shadeClose: true,
                anim: 2,
                content: top.restAjax.path('route/department/save-split?departmentParentId={departmentParentId}', [departmentParentId]),
                end: function() {
                    initTable();
                }
            });
        });

        $(document).on('click', '#updateSplitDepartmentBtn', function() {
            var departmentId = this.dataset.id;
            layer.open({
                type: 2,
                title: false,
                closeBtn: 0,
                area: ['100%', '100%'],
                shadeClose: true,
                anim: 2,
                content: top.restAjax.path('route/department/update-split?departmentId={departmentId}&departmentParentId={departmentParentId}', [departmentId, departmentParentId]),
                end: function() {
                    initTable();
                }
            });
        });

        $(document).on('click', '#removeSplitDepartmentBtn', function() {
            var departmentId = this.dataset.id;
            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);
                    for(var i = 0, item; item = splitDepartmentArray[i++];) {
                        if(item.departmentId == departmentId) {
                            splitDepartmentArray.splice(i - 1, 1);
                            initTable();
                            break;
                        }
                    }
                }
            })
        });

        $(document).on('click', '#bindSplitDepartmentUserBtn', function() {
            var departmentId = this.dataset.id;
            var splitDepartment;
            for(var i = 0, item; item = splitDepartmentArray[i++];) {
                if(item.departmentId == departmentId) {
                    splitDepartment = item;
                    break;
                }
            }
            top.dialog.dialogData.selectedUserIds = splitDepartment.userIds;
            top.dialog.open({
                url: top.restAjax.path('route/department/user/select-user', []),
                title: '选择用户',
                width: '500px',
                height: '500px',
                onClose: function() {
                    var selectedUsers = top.dialog.dialogData.selectedDepartmentUsers;
                    if(selectedUsers != null && selectedUsers.length > 0) {
                        var selectedUserIds = '';
                        for (var i = 0, item; item = selectedUsers[i++];) {
                            if ('' != selectedUserIds) {
                                selectedUserIds += '_';
                            }
                            selectedUserIds += item.userId;
                        }
                        splitDepartment.userIds = selectedUserIds;
                    }
                    top.dialog.dialogData.selectedUserIds = null;
                    top.dialog.dialogData.selectedDepartmentUsers = [];
                }
            })
        });

        $(document).on('click', '#confirmSplitDepartmentBtn', function() {
            if(splitDepartmentArray.length > 1) {
                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 layerIndex;
                        top.restAjax.put(top.restAjax.path('api/department/updatesplit/{departmentId}', [departmentId]), {
                            splitDepartments: splitDepartmentArray
                        }, null, function(code, data) {
                            top.dialog.msg('拆分成功');
                            closeBox();
                        }, function(code, data) {
                            top.dialog.msg(data.msg);
                        }, function () {
                            layerIndex = top.dialog.msg('正在拆分...', {icon: 16, time: 0, shade: 0.3});
                        }, function () {
                            top.dialog.close(layerIndex);
                        });
                    }
                })
            } else {
                top.dialog.msg('拆分结果至少需要2个');
            }
        });
    });
</script>
</body>
</html>