<!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>
        html, body {height: 100%;}
        .layui-form-select dl {max-height: 150px !important; z-index: 10010 !important;}
        .select-area {position: relative;}
        .select-area #departmentAreaName {width: 64%}
        .select-area .select-btn {position: absolute; top: 0px; right: 0px; width: 36%; border-color: #e6e6e6;}
        .select-area .select-btn button {height: 38px; width: 50%;}
    </style>
</head>
<body>
<div class="layui-fluid layui-anim layui-anim-fadein" style="padding: 0; height: 100%;">
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form layui-form-pane" lay-filter="dataForm">
                <div class="layui-row">
                    <div class="layui-col-md6 layui-col-sm6 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">组织名称 *</label>
                            <div class="layui-input-block">
                                <input id="departmentName" type="text" name="departmentName" lay-verify="required" placeholder="请输入组织名称" class="layui-input" maxlength="50">
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md6 layui-col-sm6 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">编号</label>
                            <div class="layui-input-block">
                                <input id="departmentNo" type="text" name="departmentNo" placeholder="请输入组织编号" class="layui-input" maxlength="255">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-md6 layui-col-sm6 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">名称(英文)</label>
                            <div class="layui-input-block">
                                <input type="text" name="departmentNameEn" placeholder="请输入名称(英文)" class="layui-input" maxlength="255">
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md6 layui-col-sm6 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">名称(其他)</label>
                            <div class="layui-input-block">
                                <input type="text" name="departmentNameOther" placeholder="请输入名称(其他)" class="layui-input" maxlength="255">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-md6 layui-col-sm6 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">组织类型 *</label>
                            <div class="layui-input-block">
                                <select name="departmentType" lay-verify="required">
                                    <option value="2" selected>部门</option>
                                    <option value="1">机构</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-md12 layui-col-sm12 layui-col-xs12">
                        <div class="layui-form-item layui-form-text">
                            <label class="layui-form-label">组织说明</label>
                            <div class="layui-input-block">
                                <textarea id="departmentSummary" name="departmentSummary" class="layui-textarea" placeholder="请输入组织说明"></textarea>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-md6 layui-col-sm6 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">组织状态 *</label>
                            <div class="layui-input-block">
                                <select name="departmentState" lay-verify="required">
                                    <option value="1" selected>正常</option>
                                    <option value="2">异常</option>
                                    <option value="3">停用</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md6 layui-col-sm6 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">负责人</label>
                            <div class="layui-input-block">
                                <input id="departmentMaster" type="hidden" class="layui-input" name="departmentMaster">
                                <input id="departmentMasterName" type="text" class="layui-input" name="departmentMasterName" placeholder="请选择组织负责人" readonly style="cursor: pointer;">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-md6 layui-col-sm6 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">组织传真</label>
                            <div class="layui-input-block">
                                <input type="text" class="layui-input" name="departmentFax" placeholder="请输入组织传真" maxlength="255">
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md6 layui-col-sm6 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">组织电话</label>
                            <div class="layui-input-block">
                                <input type="text" class="layui-input" name="departmentTel" placeholder="请输入组织电话" maxlength="255">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-md6 layui-col-sm6 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">组织职责</label>
                            <div class="layui-input-block">
                                <input type="text" class="layui-input" name="departmentDuty" placeholder="请输入组织职责" maxlength="255">
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md6 layui-col-sm6 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">组织排序</label>
                            <div class="layui-input-block">
                                <input type="number" class="layui-input" name="departmentOrder" placeholder="请输入组织排序" value="0">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-md6 layui-col-sm6 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label layui-form-label-up">选择地区</label>
                            <div class="layui-input-block layui-input-block-down select-area">
                                <input type="hidden" id="departmentAreaCode" name="departmentAreaCode">
                                <input type="text" id="departmentAreaName" name="departmentAreaName" class="layui-input" value="" placeholder="请选择地区" maxlength="255" readonly style="cursor:pointer;">
                                <div class="layui-btn-group select-btn">
                                    <button type="button" id="areaSelectBtn" class="layui-btn layui-btn-sm layui-btn-primary" title="选择区域">
                                        <i class="fa fa-circle-thin"></i>
                                    </button>
                                    <button type="button" id="areaCleanBtn" class="layui-btn layui-btn-sm layui-btn-primary" title="删除区域">
                                        <i class="fa fa-times-circle"></i>
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md6 layui-col-sm6 layui-col-xs6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">组织地址</label>
                            <div class="layui-input-block">
                                <input type="text" class="layui-input" name="departmentAddress" placeholder="请输入组织地址" maxlength="255">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button type="button" class="layui-btn" lay-submit lay-filter="submitForm">提交新增</button>
                            <button type="button" class="layui-btn layui-btn-primary close">返回上级</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="assets/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: 'assets/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'laydate'], function(){
        var $ = layui.$;
        var form = layui.form;
        var laytpl = layui.laytpl;
        var departmentParentId = top.restAjax.params(window.location.href).departmentParentId;
        function closeBox() {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        }

        // 初始化选择框、单选、复选模板
        function initSelectRadioCheckboxTemplate(templateId, templateBoxId, data, callback) {
            laytpl(document.getElementById(templateId).innerHTML).render(data, function(html) {
                document.getElementById(templateBoxId).innerHTML = html;
            });
            form.render('select', templateBoxId);
            if(callback) {
                callback();
            }
        }
        
        // 初始化
        function initData() {}
        initData();

        // 提交表单
        form.on('submit(submitForm)', function(formData) {
            top.dialog.confirm('确定合并到该组织吗？操作不可撤销！', function(index) {
                top.dialog.close(index);
                var loadLayerIndex;
                formData.field.departmentParentId = departmentParentId;
                formData.field.mergeDepartmentIds = top.dialog.dialogData.mergeDepartmentIds;
                top.restAjax.put(top.restAjax.path('api/department/updatemergenew', []), formData.field, null, function(code, data) {
                    top.dialog.msg('合并成功');
                    closeBox();
                }, 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);
                });
            });
            return false;
        });

        // 选择负责人
        $(document).on('click', '#departmentMasterName', function() {
            top.dialog.dialogData.selectedUserIds = $('#departmentMaster').val();
            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) {
                        if(selectedUsers.length > 1) {
                            top.dialog.msg('负责人只能选择一人');
                            return;
                        }
                        $('#departmentMaster').val(selectedUsers[0].userId);
                        $('#departmentMasterName').val(selectedUsers[0].userName);
                    } else {
                        $('#departmentMaster').val('');
                        $('#departmentMasterName').val('');
                    }
                }
            });
        });

        // 区域选择
        (function() {
            $('#areaSelectBtn').on('click', function() {
                top.dialog.open({
                    title: '选择地区',
                    url: top.restAjax.path('route/area/get-select?areaName={areaName}', [encodeURI($('#departmentAreaName').val())]),
                    width: '600px',
                    height: '225px',
                    onClose: function() {
                        var selectedAreaArray = top.dialog.dialogData.selectedAreaArray;
                        if(selectedAreaArray.length > 0) {
                            var areaCode = '';
                            var areaName = '';
                            areaCode = selectedAreaArray[selectedAreaArray.length - 1].areaCode;
                            for(var i = 0, item; item = selectedAreaArray[i++];) {
                                if(areaName) {
                                    areaName += ' / ';
                                }
                                areaName += item.areaName;
                            }
                            $('#departmentAreaCode').val(areaCode);
                            $('#departmentAreaName').val(areaName);
                        }
                    }
                })
            });
            $('#areaCleanBtn').on('click', function () {
                $('#departmentAreaCode').val('');
                $('#departmentAreaName').val('');
            })
        })();


        $('.close').on('click', function() {
            closeBox();
        });

        // 校验部门是否存在
        var checkDepartmentNameTimeout;
        $(document).on('keydown', '#departmentName', function() {
            var self = this;
            if(checkDepartmentNameTimeout) {
                clearTimeout(checkDepartmentNameTimeout);
            }
            checkDepartmentNameTimeout = setTimeout(function() {
                if(!self.value) {
                    return;
                }
                top.restAjax.get(top.restAjax.path('api/department/countbyparentidandname/{departmentParentId}/{departmentName}', [departmentParentId, encodeURI(self.value)]), {}, null, function(code, data) {
                    if(data.data > 0) {
                        top.dialog.msg('【'+ self.value +'】在本级已经存在');
                        self.value = '';
                        return;
                    }
                }, function(code, data) {
                    top.dialog.msg(data.msg);
                });
            }, 500);
        });
    });
</script>
</body>
</html>