<!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;}
    </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-header">
            <span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
                <a class="close" href="javascript:void(0);">上级列表</a><span lay-separator="">/</span>
                <a href="javascript:void(0);"><cite>查看</cite></a>
            </span>
        </div>
        <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="departmentParentId" type="hidden" name="departmentParentId" lay-verify="required">
                                <input type="text" name="departmentParentName" class="layui-input" disabled>
                            </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="departmentName" type="text" name="departmentName" lay-verify="required" placeholder="请输入组织名称" class="layui-input" maxlength="50" readonly>
                            </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" readonly>
                            </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" readonly>
                            </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="departmentSummary" placeholder="请输入组织说明" class="layui-input" readonly>
                            </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">
                                <select name="departmentType" lay-verify="required" readonly>
                                    <option value="1" selected>机构</option>
                                    <option value="2">部门</option>
                                </select>
                            </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" readonly>
                                    <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" readonly>
                                <input id="departmentMasterName" type="text" class="layui-input" name="departmentMasterName" placeholder="请选择组织负责人" style="cursor: pointer;" readonly>
                            </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="请输入组织传真" readonly>
                            </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="请输入组织电话" readonly>
                            </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="请输入组织职责" readonly>
                            </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" lay-verify="required|number" placeholder="请输入组织排序" value="0" readonly>
                            </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">1级区域</label>
                            <div class="layui-input-block layui-form" id="departmentArea1IdSelectTemplateBox" lay-filter="departmentArea1IdSelectTemplateBox"></div>
                            <script id="departmentArea1IdSelectTemplate" type="text/html">
                                <select id="departmentArea1Id" name="departmentArea1Id" lay-filter="departmentArea1Id" lay-search readonly>
                                    <option value="">请选择1级区域</option>
                                    {{# for(var i = 0, item; item = d[i++];) { }}
                                    <option value="{{item.areaId}}">{{item.areaName}}</option>
                                    {{# } }}
                                </select>
                            </script>
                        </div>
                    </div>
                    <div class="layui-col-md6 layui-col-sm6 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">2级区域</label>
                            <div class="layui-input-block layui-form" id="departmentArea2IdSelectTemplateBox" lay-filter="departmentArea2IdSelectTemplateBox"></div>
                            <script id="departmentArea2IdSelectTemplate" type="text/html">
                                <select id="departmentArea2Id" name="departmentArea2Id" lay-filter="departmentArea2Id" lay-search readonly>
                                    <option value="">请选择2级区域</option>
                                    {{# for(var i = 0, item; item = d[i++];) { }}
                                    <option value="{{item.areaId}}">{{item.areaName}}</option>
                                    {{# } }}
                                </select>
                            </script>
                        </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">3级区域</label>
                            <div class="layui-input-block layui-form" id="departmentArea3IdSelectTemplateBox" lay-filter="departmentArea3IdSelectTemplateBox"></div>
                            <script id="departmentArea3IdSelectTemplate" type="text/html">
                                <select id="departmentArea3Id" name="departmentArea3Id" lay-filter="departmentArea3Id" lay-search readonly>
                                    <option value="">请选择3级区域</option>
                                    {{# for(var i = 0, item; item = d[i++];) { }}
                                    <option value="{{item.areaId}}">{{item.areaName}}</option>
                                    {{# } }}
                                </select>
                            </script>
                        </div>
                    </div>
                    <div class="layui-col-md6 layui-col-sm6 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">4级区域</label>
                            <div class="layui-input-block layui-form" id="departmentArea4IdSelectTemplateBox" lay-filter="departmentArea4IdSelectTemplateBox"></div>
                            <script id="departmentArea4IdSelectTemplate" type="text/html">
                                <select id="departmentArea4Id" name="departmentArea4Id" lay-filter="departmentArea4Id" lay-search readonly>
                                    <option value="">请选择4级区域</option>
                                    {{# for(var i = 0, item; item = d[i++];) { }}
                                    <option value="{{item.areaId}}">{{item.areaName}}</option>
                                    {{# } }}
                                </select>
                            </script>
                        </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">5级区域</label>
                            <div class="layui-input-block layui-form" id="departmentArea5IdSelectTemplateBox" lay-filter="departmentArea5IdSelectTemplateBox"></div>
                            <script id="departmentArea5IdSelectTemplate" type="text/html">
                                <select id="departmentArea5Id" name="departmentArea5Id" lay-search readonly>
                                    <option value="">请选择5级区域</option>
                                    {{# for(var i = 0, item; item = d[i++];) { }}
                                    <option value="{{item.areaId}}">{{item.areaName}}</option>
                                    {{# } }}
                                </select>
                            </script>
                        </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="departmentAddress" placeholder="请输入组织地址" readonly>
                            </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 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 departmentId = top.restAjax.params(window.location.href).departmentId;
        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();
            }
        }

        // 初始化1级区域下拉选择
        function initDepartmentArea1CodeSelect(selectValue) {
            top.restAjax.get(top.restAjax.path('api/area/listbyparentid/0', []), {}, null, function(code, data, args) {
                initSelectRadioCheckboxTemplate('departmentArea1IdSelectTemplate', 'departmentArea1IdSelectTemplateBox', data, function() {
                    var selectObj = {};
                    selectObj['departmentArea1Id'] = selectValue;
                    form.val('dataForm', selectObj);
                });
            }, function(code, data) {
                top.dialog.msg(data.msg);
            });
        }

        // 初始化2级区域下拉选择
        function initDepartmentArea2CodeSelect(area1, selectValue) {
            if(!area1) {
                initSelectRadioCheckboxTemplate('departmentArea2IdSelectTemplate', 'departmentArea2IdSelectTemplateBox', [], function() {
                    var selectObj = {};
                    selectObj['departmentArea2Id'] = selectValue;
                    form.val('dataForm', selectObj);
                });
                return;
            }
            top.restAjax.get(top.restAjax.path('api/area/listbyparentid/{area1}', [area1]), {}, null, function(code, data, args) {
                initSelectRadioCheckboxTemplate('departmentArea2IdSelectTemplate', 'departmentArea2IdSelectTemplateBox', data, function() {
                    var selectObj = {};
                    selectObj['departmentArea2Id'] = selectValue;
                    form.val('dataForm', selectObj);
                });
            }, function(code, data) {
                top.dialog.msg(data.msg);
            });
        }

        // 初始化3级区域下拉选择
        function initDepartmentArea3CodeSelect(area2, selectValue) {
            if(!area2) {
                initSelectRadioCheckboxTemplate('departmentArea3IdSelectTemplate', 'departmentArea3IdSelectTemplateBox', [], function() {
                    var selectObj = {};
                    selectObj['departmentArea3Id'] = selectValue;
                    form.val('dataForm', selectObj);
                });
                return;
            }
            top.restAjax.get(top.restAjax.path('api/area/listbyparentid/{area2}', [area2]), {}, null, function(code, data, args) {
                initSelectRadioCheckboxTemplate('departmentArea3IdSelectTemplate', 'departmentArea3IdSelectTemplateBox', data, function() {
                    var selectObj = {};
                    selectObj['departmentArea3Id'] = selectValue;
                    form.val('dataForm', selectObj);
                });
            }, function(code, data) {
                top.dialog.msg(data.msg);
            });
        }

        // 初始化4级区域下拉选择
        function initDepartmentArea4CodeSelect(area3, selectValue) {
            if(!area3) {
                initSelectRadioCheckboxTemplate('departmentArea4IdSelectTemplate', 'departmentArea4IdSelectTemplateBox', [], function() {
                    var selectObj = {};
                    selectObj['departmentArea4Id'] = selectValue;
                    form.val('dataForm', selectObj);
                });
                return;
            }
            top.restAjax.get(top.restAjax.path('api/area/listbyparentid/{area3}', [area3]), {}, null, function(code, data, args) {
                initSelectRadioCheckboxTemplate('departmentArea4IdSelectTemplate', 'departmentArea4IdSelectTemplateBox', data, function() {
                    var selectObj = {};
                    selectObj['departmentArea4Id'] = selectValue;
                    form.val('dataForm', selectObj);
                });
            }, function(code, data) {
                top.dialog.msg(data.msg);
            });
        }

        // 初始化5级区域下拉选择
        function initDepartmentArea5CodeSelect(area4, selectValue) {
            if(!area4) {
                initSelectRadioCheckboxTemplate('departmentArea5IdSelectTemplate', 'departmentArea5IdSelectTemplateBox', [], function() {
                    var selectObj = {};
                    selectObj['departmentArea5Id'] = selectValue;
                    form.val('dataForm', selectObj);
                });
                return;
            }
            top.restAjax.get(top.restAjax.path('api/area/listbyparentid/{area4}', [area4]), {}, null, function(code, data, args) {
                initSelectRadioCheckboxTemplate('departmentArea5IdSelectTemplate', 'departmentArea5IdSelectTemplateBox', data, function() {
                    var selectObj = {};
                    selectObj['departmentArea5Id'] = selectValue;
                    form.val('dataForm', selectObj);
                });
            }, function(code, data) {
                top.dialog.msg(data.msg);
            });
        }

        // 初始化
        function initData() {
            var loadLayerIndex;
            top.restAjax.get(top.restAjax.path('api/department/get/{departmentId}', [departmentId]), {}, null, function(code, data) {
                form.val('dataForm', {
                    departmentParentName: data.departmentParentName == '' ? '根节点' : data.departmentParentName,
                    departmentParentId: data.departmentParentId,
                    departmentName: data.departmentName,
                    departmentNameEn: data.departmentNameEn,
                    departmentNameOther: data.departmentNameOther,
                    departmentSummary: data.departmentSummary,
                    departmentCode: data.departmentCode,
                    departmentType: data.departmentType +'',
                    departmentState: data.departmentState +'',
                    departmentMaster: data.departmentMaster,
                    departmentMasterName: data.departmentMasterName,
                    departmentFax: data.departmentFax,
                    departmentTel: data.departmentTel,
                    departmentDuty: data.departmentDuty,
                    departmentOrder: data.departmentOrder,
                    departmentLongitude: data.departmentLongitude,
                    departmentLatitude: data.departmentLatitude,
                    departmentAddress: data.departmentAddress,
                });
                form.render(null, 'dataForm');

                initDepartmentArea1CodeSelect(data['departmentArea1Id']);
                initDepartmentArea2CodeSelect(data['departmentArea1Id'], data['departmentArea2Id']);
                initDepartmentArea3CodeSelect(data['departmentArea2Id'], data['departmentArea3Id']);
                initDepartmentArea4CodeSelect(data['departmentArea3Id'], data['departmentArea4Id']);
                initDepartmentArea5CodeSelect(data['departmentArea4Id'], data['departmentArea5Id']);
            }, function(code, data) {
                top.DialogBox.msg(data.msg);
            }, function() {
                loadLayerIndex = top.dialog.msg(top.dataMessage.loading, {icon: 16, time: 0, shade: 0.3});
            }, function() {
                top.dialog.close(loadLayerIndex);
            });
        }
        initData();

        $('.close').on('click', function() {
            closeBox();
        });
    });
</script>
</body>
</html>