<!doctype html>
<html lang="en" 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">
    <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-md4 layui-col-sm4 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">客户名称</label>
                            <div class="layui-input-block">
                                <input type="text" id="customerName" name="customerName" class="layui-input" value="" placeholder="请输入客户名称" lay-verify="required">
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md8 layui-col-sm8 layui-col-xs12">
                        <div class="layui-form-item" pane>
                            <label class="layui-form-label">客户类型</label>
                            <div class="layui-input-block">
                                <input type="radio" name="customerType" value="1" title="政府机构" checked>
                                <input type="radio" name="customerType" value="2" title="企业">
                                <input type="radio" name="customerType" value="3" title="个人">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-md4 layui-col-sm4 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">联系人1</label>
                            <div class="layui-input-block">
                                <input type="text" id="contactPerson1" name="contactPerson1" class="layui-input" value="" placeholder="请输入联系人1" >
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md4 layui-col-sm4 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">联系座机1</label>
                            <div class="layui-input-block">
                                <input type="text" id="contactTel1" name="contactTel1" class="layui-input" value="" placeholder="请输入联系座机1" >
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md4 layui-col-sm4 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">联系电话1</label>
                            <div class="layui-input-block">
                                <input type="text" id="contactPhone1" name="contactPhone1" class="layui-input" value="" placeholder="请输入联系电话1" >
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-md4 layui-col-sm4 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">联系邮箱1</label>
                            <div class="layui-input-block">
                                <input type="text" id="contactEmail1" name="contactEmail1" class="layui-input" value="" placeholder="请输入联系邮箱1" >
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-md4 layui-col-sm4 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">联系人2</label>
                            <div class="layui-input-block">
                                <input type="text" id="contactPerson2" name="contactPerson2" class="layui-input" value="" placeholder="请输入联系人2" >
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md4 layui-col-sm4 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">联系座机2</label>
                            <div class="layui-input-block">
                                <input type="text" id="contactTel2" name="contactTel2" class="layui-input" value="" placeholder="请输入联系座机2" >
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md4 layui-col-sm4 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">联系电话2</label>
                            <div class="layui-input-block">
                                <input type="text" id="contactPhone2" name="contactPhone2" class="layui-input" value="" placeholder="请输入联系电话2" >
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-md4 layui-col-sm4 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">联系邮箱2</label>
                            <div class="layui-input-block">
                                <input type="text" id="contactEmail2" name="contactEmail2" class="layui-input" value="" placeholder="请输入联系邮箱2" >
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-md4 layui-col-sm4 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">1级区域</label>
                            <div class="layui-input-block layui-form" id="area1IdSelectTemplateBox" lay-filter="area1IdSelectTemplateBox"></div>
                            <script id="area1IdSelectTemplate" type="text/html">
                                <select id="area1Id" name="area1Id" lay-filter="area1Id" lay-search>
                                    <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-md4 layui-col-sm4 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">2级区域</label>
                            <div class="layui-input-block layui-form" id="area2IdSelectTemplateBox" lay-filter="area2IdSelectTemplateBox"></div>
                            <script id="area2IdSelectTemplate" type="text/html">
                                <select id="area2Id" name="area2Id" lay-filter="area2Id" lay-search>
                                    <option value="">请选择2级区域</option>
                                    {{# for(var i = 0, item; item = d[i++];) { }}
                                    <option value="{{item.areaId}}">{{item.areaName}}</option>
                                    {{# } }}
                                </select>
                            </script>
                        </div>
                    </div>
                    <div class="layui-col-md4 layui-col-sm4 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">3级区域</label>
                            <div class="layui-input-block layui-form" id="area3IdSelectTemplateBox" lay-filter="area3IdSelectTemplateBox"></div>
                            <script id="area3IdSelectTemplate" type="text/html">
                                <select id="area3Id" name="area3Id" lay-filter="area3Id" lay-search>
                                    <option value="">请选择3级区域</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-md4 layui-col-sm4 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">4级区域</label>
                            <div class="layui-input-block layui-form" id="area4IdSelectTemplateBox" lay-filter="area4IdSelectTemplateBox"></div>
                            <script id="area4IdSelectTemplate" type="text/html">
                                <select id="area4Id" name="area4Id" lay-filter="area4Id" lay-search>
                                    <option value="">请选择4级区域</option>
                                    {{# for(var i = 0, item; item = d[i++];) { }}
                                    <option value="{{item.areaId}}">{{item.areaName}}</option>
                                    {{# } }}
                                </select>
                            </script>
                        </div>
                    </div>
                    <div class="layui-col-md4 layui-col-sm4 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">5级区域</label>
                            <div class="layui-input-block layui-form" id="area5IdSelectTemplateBox" lay-filter="area5IdSelectTemplateBox"></div>
                            <script id="area5IdSelectTemplate" type="text/html">
                                <select id="area5Id" name="area5Id" lay-search>
                                    <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-md4 layui-col-sm4 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">详细地址</label>
                            <div class="layui-input-block">
                                <input type="text" id="areaAddress" name="areaAddress" class="layui-input" value="" placeholder="请输入详细地址" >
                            </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', 'laytpl'], function(){
        var $ = layui.$;
        var form = layui.form;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;

        // 初始化选择框、单选、复选模板
        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 initArea1IdSelect() {
            top.restAjax.get(top.restAjax.path('api/dataarea/listareabyparentid/0', []), {}, null, function(code, data, args) {
                initSelectRadioCheckboxTemplate('area1IdSelectTemplate', 'area1IdSelectTemplateBox', data);
            }, function(code, data) {
                top.dialog.msg(data.msg);
            });
        }

        // 初始化2级区域下拉选择
        function initArea2IdSelect(area1) {
            if(!area1) {
                initSelectRadioCheckboxTemplate('area2IdSelectTemplate', 'area2IdSelectTemplateBox', []);
                return;
            }
            top.restAjax.get(top.restAjax.path('api/dataarea/listareabyparentid/{area1}', [area1]), {}, null, function(code, data, args) {
                initSelectRadioCheckboxTemplate('area2IdSelectTemplate', 'area2IdSelectTemplateBox', data);
            }, function(code, data) {
                top.dialog.msg(data.msg);
            });
        }

        // 初始化3级区域下拉选择
        function initArea3IdSelect(area2) {
            if(!area2) {
                initSelectRadioCheckboxTemplate('area3IdSelectTemplate', 'area3IdSelectTemplateBox', []);
                return;
            }
            top.restAjax.get(top.restAjax.path('api/dataarea/listareabyparentid/{area2}', [area2]), {}, null, function(code, data, args) {
                initSelectRadioCheckboxTemplate('area3IdSelectTemplate', 'area3IdSelectTemplateBox', data);
            }, function(code, data) {
                top.dialog.msg(data.msg);
            });
        }

        // 初始化4级区域下拉选择
        function initArea4IdSelect(area3) {
            if(!area3) {
                initSelectRadioCheckboxTemplate('area4IdSelectTemplate', 'area4IdSelectTemplateBox', []);
                return;
            }
            top.restAjax.get(top.restAjax.path('api/dataarea/listareabyparentid/{area3}', [area3]), {}, null, function(code, data, args) {
                initSelectRadioCheckboxTemplate('area4IdSelectTemplate', 'area4IdSelectTemplateBox', data);
            }, function(code, data) {
                top.dialog.msg(data.msg);
            });
        }

        // 初始化5级区域下拉选择
        function initArea5IdSelect(area4) {
            if(!area4) {
                initSelectRadioCheckboxTemplate('area5IdSelectTemplate', 'area5IdSelectTemplateBox', []);
                return;
            }
            top.restAjax.get(top.restAjax.path('api/dataarea/listareabyparentid/{area4}', [area4]), {}, null, function(code, data, args) {
                initSelectRadioCheckboxTemplate('area5IdSelectTemplate', 'area5IdSelectTemplateBox', data);
            }, function(code, data) {
                top.dialog.msg(data.msg);
            });
        }

        function closeBox() {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        }

        // 初始化内容
        function initData() {
            initArea1IdSelect();
            initArea2IdSelect();
            initArea3IdSelect();
            initArea4IdSelect();
            initArea5IdSelect();
        }
        initData();

        // 提交表单
        form.on('submit(submitForm)', function(formData) {
            top.dialog.confirm(top.dataMessage.commit, function(index) {
                top.dialog.close(index);
                var loadLayerIndex;
                top.restAjax.post(top.restAjax.path('api/customer/savecustomer', []), formData.field, null, function(code, data) {
                    var layerIndex = top.dialog.msg(top.dataMessage.commitSuccess, {
                        time: 0,
                        btn: [top.dataMessage.button.yes, top.dataMessage.button.no],
                        shade: 0.3,
                        yes: function(index) {
                            top.dialog.close(index);
                            window.location.reload();
                        },
                        btn2: function() {
                            closeBox();
                        }
                    });
                }, function(code, data) {
                    top.dialog.msg(data.msg);
                }, function() {
                    loadLayerIndex = top.dialog.msg(top.dataMessage.committing, {icon: 16, time: 0, shade: 0.3});
                }, function() {
                    top.dialog.close(loadLayerIndex);
                });
            });
            return false;
        });

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

        // area1Id 选择事件
        form.on('select(area1Id)', function(data) {
            initArea2IdSelect(data.value);
            initArea3IdSelect(data.value);
            initArea4IdSelect(data.value);
            initArea5IdSelect(data.value);
        });
        // area2Id 选择事件
        form.on('select(area2Id)', function(data) {
            initArea3IdSelect(data.value);
            initArea4IdSelect(data.value);
            initArea5IdSelect(data.value);
        });
        // area3Id 选择事件
        form.on('select(area3Id)', function(data) {
            initArea4IdSelect(data.value);
            initArea5IdSelect(data.value);
        });
        // area4Id 选择事件
        form.on('select(area4Id)', function(data) {
            initArea5IdSelect(data.value);
        });

        // 校验
        form.verify({
        });
    });
</script>
</body>
</html>