<!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">
    <link rel="stylesheet" type="text/css" href="assets/js/vendor/viewer/viewer.min.css">
</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-form-item">
                    <input type="hidden" id="massPreventionId" name="massPreventionId" class="layui-input" value="" placeholder="请输入群防群治组织表ID"  maxlength="36">
                    <label class="layui-form-label">组织名称<span style="color: red;vertical-align: middle;">*</span></label>
                    <div class="layui-input-block layui-form" id="organizeNameSelectTemplateBox" lay-filter="organizeNameSelectTemplateBox"></div>
                    <script id="organizeNameSelectTemplate" type="text/html">
                        <select id="organizeName" name="organizeName" lay-filter="organizeName" lay-verify="required" lay-search>
                            <option value="">请选择组织</option>
                            {{# for(var i = 0, item; item = d[i++];) { }}
                            <option value="{{item.massPreventionId}}">{{item.organizeName}}</option>
                            {{# } }}
                        </select>
                    </script>
                </div>
                <div class="layui-form-item layui-row">
                    <div class="layui-col-lg6">
                        <label class="layui-form-label">姓名<span style="color: red;vertical-align: middle;">*</span></label>
                        <div class="layui-input-block">
                            <input type="text" id="name" name="name" class="layui-input" value="" placeholder="请输入姓名"  maxlength="50" lay-verify="required">
                        </div>
                    </div>
                    <div class="layui-col-lg6">
                        <input type="hidden" id="sexName" name="sexName" class="layui-input" value="" placeholder="请输入性别名称"  maxlength="20">
                        <input type="hidden" id="sexCode" name="sexCode" class="layui-input" value="" placeholder="请输入性别编码"  maxlength="1">
                        <label class="layui-form-label">性别<span style="color: red;vertical-align: middle;">*</span></label>
                        <div class="layui-input-block layui-form" id="sexSelectTemplateBox" lay-filter="sexSelectTemplateBox"></div>
                        <script id="sexSelectTemplate" type="text/html">
                            <select id="sex" name="sex" lay-filter="sex" lay-verify="required" lay-search>
                                <option value="">请选择性别</option>
                                {{# for(var i = 0, item; item = d[i++];) { }}
                                <option value="{{item.dataId}},{{item.dataSummary}}">{{item.dataName}}</option>
                                {{# } }}
                            </select>
                        </script>
                    </div>
                </div>
                <div class="layui-form-item layui-row">
                    <div class="layui-col-lg6">
                        <input type="hidden" id="nationName" name="nationName" class="layui-input" value="" placeholder="请输入名族名称"  maxlength="20">
                        <input type="hidden" id="nationCode" name="nationCode" class="layui-input" value="" placeholder="请输入民族编码"  maxlength="2">
                        <label class="layui-form-label">民族<span style="color: red;vertical-align: middle;">*</span></label>
                        <div class="layui-input-block layui-form" id="nationSelectTemplateBox" lay-filter="nationSelectTemplateBox"></div>
                        <script id="nationSelectTemplate" type="text/html">
                            <select id="nation" name="nation" lay-filter="nation" lay-verify="required" lay-search>
                                <option value="">请选择民族</option>
                                {{# for(var i = 0, item; item = d[i++];) { }}
                                <option value="{{item.dataId}},{{item.dataSummary}}">{{item.dataName}}</option>
                                {{# } }}
                            </select>
                        </script>
                    </div>
                    <div class="layui-col-lg6">
                        <input type="hidden" id="politicalOutlookName" name="politicalOutlookName" class="layui-input" value="" placeholder="请输入政治面貌名称"  maxlength="20">
                        <input type="hidden" id="politicalOutlookCode" name="politicalOutlookCode" class="layui-input" value="" placeholder="请输入政治面貌编码"  maxlength="2">
                        <label class="layui-form-label">政治面貌<span style="color: red;vertical-align: middle;">*</span></label>
                        <div class="layui-input-block layui-form" id="politicalOutlookSelectTemplateBox" lay-filter="politicalOutlookSelectTemplateBox"></div>
                        <script id="politicalOutlookSelectTemplate" type="text/html">
                            <select id="politicalOutlook" name="politicalOutlook" lay-search lay-filter="politicalOutlook" lay-verify="required">
                                <option value="">请选择政治面貌</option>
                                {{# for(var i = 0, item; item = d[i++];) { }}
                                <option value="{{item.dataId}},{{item.dataSummary}}">{{item.dataName}}</option>
                                {{# } }}
                            </select>
                        </script>
                    </div>
                </div>
                <div class="layui-form-item layui-row">
                    <div class="layui-col-lg6">
                        <input type="hidden" id="cardCodeName" name="cardCodeName" class="layui-input" value="" placeholder="请输入证件代码名称"  maxlength="20">
                        <input type="hidden" id="cardCodeCode" name="cardCodeCode" class="layui-input" value="" placeholder="请输入证件代码编码"  maxlength="3">
                        <label class="layui-form-label">证件代码<span style="color: red;vertical-align: middle;">*</span></label>
                        <div class="layui-input-block layui-form" id="cardCodeSelectTemplateBox" lay-filter="cardCodeSelectTemplateBox"></div>
                        <script id="cardCodeSelectTemplate" type="text/html">
                            <select id="cardCode" name="cardCode" lay-filter="cardCode" lay-verify="required" lay-search>
                                <option value="">请选择证件代码</option>
                                {{# for(var i = 0, item; item = d[i++];) { }}
                                <option value="{{item.dataId}},{{item.dataSummary}}">{{item.dataName}}</option>
                                {{# } }}
                            </select>
                        </script>
                    </div>
                    <div class="layui-col-lg6">
                        <label class="layui-form-label">证件号码<span style="color: red;vertical-align: middle;">*</span></label>
                        <div class="layui-input-block">
                            <input type="text" id="cardNum" name="cardNum" class="layui-input" value="" placeholder="请输入证件号码"  maxlength="30" lay-verify="required">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item layui-row">
                    <div class="layui-col-lg6">
                        <label class="layui-form-label">出生日期<span style="color: red;vertical-align: middle;">*</span></label>
                        <div class="layui-input-block">
                            <input type="text" id="birthday" name="birthday" class="layui-input" value="" placeholder="请选择出生日期" readonly style="cursor: pointer;" lay-verify="required">
                        </div>
                    </div>
                    <div class="layui-col-lg6">
                        <label class="layui-form-label">职务</label>
                        <div class="layui-input-block">
                            <input type="text" id="post" name="post" class="layui-input" value="" placeholder="请输入职务"  maxlength="30">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item" pane>
                    <input type="hidden" id="professionalExpertiseName" name="professionalExpertiseName" class="layui-input" value="" maxlength="20">
                    <input type="hidden" id="professionalExpertiseCode" name="professionalExpertiseCode" class="layui-input" value="" maxlength="3">
                    <label class="layui-form-label">专业特长</label>
                    <div class="layui-input-block layui-form" id="professionalExpertiseCheckboxTemplateBox" lay-filter="professionalExpertiseCheckboxTemplateBox"></div>
                    <script id="professionalExpertiseCheckboxTemplate" type="text/html">
                        {{# for(var i = 0, item; item = d[i++];) { }}
                        <input type="checkbox" name="professionalExpertise[{{item.dataId}},{{item.dataSummary}}]" value="{{item.dataId}},{{item.dataSummary}}" title="{{item.dataName}}">
                        {{# } }}
                    </script>
                </div>
                <div class="layui-form-item layui-row">
                    <div class="layui-col-lg4">
                        <input type="hidden" id="educationName" name="educationName" class="layui-input" value="" placeholder="请输入学历名称"  maxlength="20">
                        <input type="hidden" id="educationCode" name="educationCode" class="layui-input" value="" placeholder="请输入学历编码"  maxlength="2">
                        <label class="layui-form-label">学历<span style="color: red;vertical-align: middle;">*</span></label>
                        <div class="layui-input-block layui-form" id="educationSelectTemplateBox" lay-filter="educationSelectTemplateBox"></div>
                        <script id="educationSelectTemplate" type="text/html">
                            <select id="education" name="education" lay-filter="education" lay-verify="required" lay-search>
                                <option value="">请选择学历</option>
                                {{# for(var i = 0, item; item = d[i++];) { }}
                                <option value="{{item.dataId}},{{item.dataSummary}}">{{item.dataName}}</option>
                                {{# } }}
                            </select>
                        </script>
                    </div>
                    <div class="layui-col-lg4">
                        <label class="layui-form-label">手机号码<span style="color: red;vertical-align: middle;">*</span></label>
                        <div class="layui-input-block">
                            <input type="text" id="phone" name="phone" class="layui-input" value="" placeholder="请输入手机号码"  maxlength="18" lay-verify="required">
                        </div>
                    </div>
                    <div class="layui-col-lg4">
                        <label class="layui-form-label">固定电话</label>
                        <div class="layui-input-block">
                            <input type="text" id="fixedPhone" name="fixedPhone" class="layui-input" value="" placeholder="请输入固定电话"  maxlength="18">
                        </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" id="submitBtn">提交新增</button>
                            <button type="button" class="layui-btn layui-btn-primary close">返回上级</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="assets/js/vendor/wangEditor/wangEditor.min.js"></script>
<script src="assets/js/vendor/ckplayer/ckplayer/ckplayer.js"></script>
<script src="assets/js/vendor/viewer/viewer.min.js"></script>
<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;
        var wangEditor = window.wangEditor;
        var wangEditorObj = {};
        var viewerObj = {};

        // 专业特长
        var professionalExpertise = [];
        var professionalExpertiseName = [];
        var professionalExpertiseCode = [];

        form.on('checkbox', function(data) {
            var value = data.elem.value;
            var valueArr = value.split(',');
            var name = data.elem.title;
            if (data.elem.checked) {
                if (2 == valueArr.length) {
                    var professionalExpertiseObj = {};
                    professionalExpertiseObj['id'] = valueArr[0];
                    professionalExpertise.push(professionalExpertiseObj);

                    var professionalExpertiseCodeObj = {};
                    professionalExpertiseCodeObj['code'] = valueArr[1];
                    professionalExpertiseCode.push(professionalExpertiseCodeObj);
                }

                var professionalExpertiseNameObj = {};
                professionalExpertiseNameObj['name'] = name;
                professionalExpertiseName.push(professionalExpertiseNameObj);
            } else {
                for (var idIdx in professionalExpertise) {
                    if (2 == valueArr.length) {
                        if (valueArr[0] == professionalExpertise[idIdx]['id']) {
                            professionalExpertise.splice(idIdx, 1);
                        }
                    }
                }
                for (var codeIdx in professionalExpertiseCode) {
                    if (2 == valueArr.length) {
                        if (valueArr[1] == professionalExpertiseCode[codeIdx]['code']) {
                            professionalExpertiseCode.splice(codeIdx, 1);
                        }
                    }
                }
                for (var nameIdx in professionalExpertiseName) {
                    if (name == professionalExpertiseName[nameIdx]['name']) {
                        professionalExpertiseName.splice(nameIdx, 1);
                    }
                }
            }
        })

        // 组织名称change事件
        form.on('select(organizeName)', function(data){
            if(!data.value) {

            }else {
                var name = $('#organizeName option:selected').text();
                var id = $('#organizeName option:selected').val();
                $('#organizeName option:selected').val(name);
                $('#massPreventionId').val(id);
            }
            form.render("select");
        });

        // 性别change事件
        form.on('select(sex)', function(data){
            if(!data.value) {

            }else {
                var valueArr = data.value.split(',');
                if(2 == valueArr.length) {
                    var name = $('#sex option:selected').text();
                    $('#sex option:selected').val(valueArr[0]);
                    $('#sexName').val(name);
                    $('#sexCode').val(valueArr[1]);
                }
            }
            form.render("select");
        });

        // 民族change事件
        form.on('select(nation)', function(data){
            if(!data.value) {

            }else {
                var valueArr = data.value.split(',');
                if(2 == valueArr.length) {
                    var name = $('#nation option:selected').text();
                    $('#nation option:selected').val(valueArr[0]);
                    $('#nationName').val(name);
                    $('#nationCode').val(valueArr[1]);
                }
            }
            form.render("select");
        });

        // 政治面貌change事件
        form.on('select(politicalOutlook)', function(data){
            if(!data.value) {

            }else {
                var valueArr = data.value.split(',');
                if(2 == valueArr.length) {
                    var name = $('#politicalOutlook option:selected').text();
                    $('#politicalOutlook option:selected').val(valueArr[0]);
                    $('#politicalOutlookName').val(name);
                    $('#politicalOutlookCode').val(valueArr[1]);
                }
            }
            form.render("select");
        });

        // 证件代码change事件
        form.on('select(cardCode)', function(data){
            if(!data.value) {

            }else {
                var valueArr = data.value.split(',');
                if(2 == valueArr.length) {
                    var name = $('#cardCode option:selected').text();
                    $('#cardCode option:selected').val(valueArr[0]);
                    $('#cardCodeName').val(name);
                    $('#cardCodeCode').val(valueArr[1]);
                }
            }
            form.render("select");
        });

        // 学历change事件
        form.on('select(education)', function(data){
            if(!data.value) {

            }else {
                var valueArr = data.value.split(',');
                if(2 == valueArr.length) {
                    var name = $('#education option:selected').text();
                    $('#education option:selected').val(valueArr[0]);
                    $('#educationName').val(name);
                    $('#educationCode').val(valueArr[1]);
                }
            }
            form.render("select");
        });

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

        // 初始化组织名称
        function initOrganizeNameSelect() {
            top.restAjax.get(top.restAjax.path('api/massprevention/list', []), {}, null, function(code, data, args) {
                laytpl(document.getElementById('organizeNameSelectTemplate').innerHTML).render(data, function(html) {
                    document.getElementById('organizeNameSelectTemplateBox').innerHTML = html;
                });
                console.log(data)
                form.render('select', 'organizeNameSelectTemplateBox');
            }, function(code, data) {
                top.dialog.msg(data.msg);
            });
        }

        // 初始化性别字典表ID下拉选择
        function initSexSelect() {
            top.restAjax.get(top.restAjax.path('api/mongo/data/list-all/parent-id/{dataParentId}', ['4ef46940-76ab-4758-b5f0-0cbc93ffc660']), {}, null, function(code, data, args) {
                laytpl(document.getElementById('sexSelectTemplate').innerHTML).render(data, function(html) {
                    document.getElementById('sexSelectTemplateBox').innerHTML = html;
                });
                form.render('select', 'sexSelectTemplateBox');
            }, function(code, data) {
                top.dialog.msg(data.msg);
            });
        }

        // 初始化民族字典表ID下拉选择
        function initNationSelect() {
            top.restAjax.get(top.restAjax.path('api/mongo/data/list-all/parent-id/{dataParentId}', ['a981579c-572b-4fd1-8e76-5d17df97f6a4']), {}, null, function(code, data, args) {
                laytpl(document.getElementById('nationSelectTemplate').innerHTML).render(data, function(html) {
                    document.getElementById('nationSelectTemplateBox').innerHTML = html;
                });
                form.render('select', 'nationSelectTemplateBox');
            }, function(code, data) {
                top.dialog.msg(data.msg);
            });
        }

        // 初始化政治面貌字典表ID下拉选择
        function initPoliticalOutlookSelect() {
            top.restAjax.get(top.restAjax.path('api/mongo/data/list-all/parent-id/{dataParentId}', ['b2c96de0-3df8-41db-99b1-bc4e24ac6c24']), {}, null, function(code, data, args) {
                laytpl(document.getElementById('politicalOutlookSelectTemplate').innerHTML).render(data, function(html) {
                    document.getElementById('politicalOutlookSelectTemplateBox').innerHTML = html;
                });
                form.render('select', 'politicalOutlookSelectTemplateBox');
            }, function(code, data) {
                top.dialog.msg(data.msg);
            });
        }

        // 初始化证件代码字典表ID下拉选择
        function initCardCodeSelect() {
            top.restAjax.get(top.restAjax.path('api/mongo/data/list-all/parent-id/{dataParentId}', ['76c5044f-805a-4313-b1e8-79e966b97c0d']), {}, null, function(code, data, args) {
                laytpl(document.getElementById('cardCodeSelectTemplate').innerHTML).render(data, function(html) {
                    document.getElementById('cardCodeSelectTemplateBox').innerHTML = html;
                });
                form.render('select', 'cardCodeSelectTemplateBox');
            }, function(code, data) {
                top.dialog.msg(data.msg);
            });
        }

        // 初始化学历字典表ID下拉选择
        function initEducationSelect() {
            top.restAjax.get(top.restAjax.path('api/mongo/data/list-all/parent-id/{dataParentId}', ['d6b9f026-6ea9-456a-b48b-0c18d502523b']), {}, null, function(code, data, args) {
                laytpl(document.getElementById('educationSelectTemplate').innerHTML).render(data, function(html) {
                    document.getElementById('educationSelectTemplateBox').innerHTML = html;
                });
                form.render('select', 'educationSelectTemplateBox');
            }, function(code, data) {
                top.dialog.msg(data.msg);
            });
        }

        // 初始化专业特长复选
        function initProfessionalExpertiseCheckbox() {
            top.restAjax.get(top.restAjax.path('api/mongo/data/list-all/parent-id/{dataParentId}', ['65169ee8-ddba-4a83-ac5f-f3b48559d9e5']), {}, null, function(code, data, args) {
                laytpl(document.getElementById('professionalExpertiseCheckboxTemplate').innerHTML).render(data, function(html) {
                    document.getElementById('professionalExpertiseCheckboxTemplateBox').innerHTML = html;
                });
                form.render('checkbox', 'professionalExpertiseCheckboxTemplateBox');
            }, function(code, data) {
                top.dialog.msg(data.msg);
            });
        }

        // 初始化出生日期日期
        function initBirthdayDate() {
            laydate.render({
                elem: '#birthday',
                type: 'date',
                value: new Date(),
                trigger: 'click'
            });
        }

        // 初始化内容
        function initData() {
            initOrganizeNameSelect();
            initSexSelect();
            initNationSelect();
            initPoliticalOutlookSelect();
            initCardCodeSelect();
            initBirthdayDate();
            initEducationSelect();
            initProfessionalExpertiseCheckbox();
        }
        initData();

        function setData(data) {
            var value = '';
            for(var key = 0, item; item = data[key++];) {
                if(key > 1) {
                    value += ',';
                }
                for(var name in item) {
                    value += item[name];
                }
            }
            return value;
        }

        // 提交表单
        form.on('submit(submitForm)', function(formData) {
            if(professionalExpertise.length != 0) {
                var value = setData(professionalExpertise);
                formData.field['professionalExpertise'] = value;
            }
            if(professionalExpertiseCode.length != 0) {
                var value = setData(professionalExpertiseCode);
                formData.field['professionalExpertiseCode'] = value;
            }
            if(professionalExpertiseName.length != 0) {
                var value = setData(professionalExpertiseName);
                formData.field['professionalExpertiseName'] = value;
            }
            top.dialog.confirm(top.dataMessage.commit, function(index) {
                top.dialog.close(index);
                var loadLayerIndex;
                top.restAjax.post(top.restAjax.path('api/masspreventionrank/save', []), 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();
        });
    });
</script>
</body>
</html>