/**
 * [OElove] (C)2010-2099 oelove.com Inc. biz
 * Email：service@phpcoo.com，phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2019/07/01 by OEdev $ for layui
*/
$(function(){
    //地区4联动...
    layui.use('form', function(){
        $form = layui.form;
        $form.on('select(area)', function(data){
            //深度
            $depth = data.elem.getAttribute("data-depth");
            $depth = parseInt($depth);
            //名称
            $jname = data.elem.getAttribute("data-name");
            if (typeof($jname) == "undefined" || $jname == null) {
                $jname = "dist";
            }
            //默认文本
            $jtext = data.elem.getAttribute("data-text");
            if (typeof($jtext) == "undefined" || $jtext == null) {
                $jtext = "请选择";
            }
            $rootid = data.value;//当前select的值
            
            if ($depth < 4) {
                //获取子地区
                $.ajax({
                    type: "POST",
                    url: _ROOT_PATH+"index.php",
                    cache: false,
                    data: {
                        m:"index", c:"area", a:"child", parentid:$rootid, r:get_rndnum(8)
                    },
                    dataType: "json",
                    success: function($data) {
                        $option_html = "";
                        $.each($data, function($entryIndex, $obj){
                            $option_html += "<option value='"+$obj.areaid+"'>"+$obj.areaname+"</option>";
                        });
                        
                        //html
                        $child_name = $jname+""+($depth+1);
                        $html_result = "";
                        if ($option_html.length > 0) { //存在option
                            $html_result += "<select name='"+$child_name+"' id='"+$child_name+"' lay-filter='area' data-depth='"+($depth+1)+"' data-name='"+$jname+"'>";
                            $html_result += "<option value=''>"+$jtext+"</option>";
                            $html_result += $option_html;
                            $html_result += "</select>";
                        }
                        $("#json_"+$child_name).html($html_result);
                        
                        
                        //逻辑处理 9.02.25
                        if ($depth == 1) {
                            //清空二级
                            if ($rootid == "") {
                                if ($("#"+$jname+"2").length > 0) {
                                    $("#"+$jname+"2").find("option").remove();
                                    $("#"+$jname+"2").append("<option value=''>"+$jtext+"</option>");
                                }
                            }
                            //清空三级
                            if ($("#json_"+$jname+"3").length > 0) {
                                $("#json_"+$jname+"3").html(""); 
                            }
                            //清空四级
                            if ($("#json_"+$jname+"4").length > 0) {
                                $("#json_"+$jname+"4").html("");
                            }
                        }
                        else if ($depth == 2) {
                            //清空三级
                            if ($rootid == "") {
                                if ($("#"+$jname+"3").length > 0) {
                                    $("#"+$jname+"3").find("option").remove();
                                    $("#"+$jname+"3").append("<option value=''>"+$jtext+"</option>");
                                }
                            }
                            //清空四级
                            if ($("#json_"+$jname+"4").length > 0) {
                                $("#json_"+$jname+"4").html("");
                            }
                        }
                        else if ($depth == 3) {
                            
                            //清空四级
                            if ($rootid == "") {
                                if ($("#json_"+$jname+"4").length > 0) {
                                    $("#json_"+$jname+"4").html("");
                                }
                            }
                        }
                        
                        
                        /* 注释 9.02.25
                        //清空depth以上的SELECT
                        if ($depth == 1) {
                            
                            if ($rootid == "") {
                                //未选中
                                $("#json_"+$jname+($depth+1)).html(""); //第二级 9.02.25
                            }
                            
                            $("#json_"+$jname+($depth+2)).html(""); //第三级
                            $("#json_"+$jname+($depth+3)).html(""); //第四级
                        }
                        else if ($depth == 2) {
                            $("#json_"+$jname+($depth+2)).html(""); //第四级
                        }
                        */
                        $form.render('select'); //渲染select样式...
                    },
                    error: function() {
                        
                    }
                });
                
            }
        });
    });
    
    //户籍3联动...9.07.01
    layui.use('form', function(){
        $form = layui.form;
        $form.on('select(hometown)', function(data){
            //深度
            $depth = data.elem.getAttribute("data-depth");
            $depth = parseInt($depth);
            //名称
            $jname = data.elem.getAttribute("data-name");
            if (typeof($jname) == "undefined" || $jname == null) {
                $jname = "home";
            }
            //默认文本
            $jtext = data.elem.getAttribute("data-text");
            if (typeof($jtext) == "undefined" || $jtext == null) {
                $jtext = "请选择";
            }
            $rootid = data.value;//当前select的值
            
            if ($depth < 3) {
                //获取子地区
                $.ajax({
                    type: "POST",
                    url: _ROOT_PATH+"index.php",
                    cache: false,
                    data: {
                        m:"index", c:"area", a:"hometown", parentid:$rootid, r:get_rndnum(8)
                    },
                    dataType: "json",
                    success: function($data) {
                        $option_html = "";
                        $.each($data, function($entryIndex, $obj){
                            $option_html += "<option value='"+$obj.areaid+"'>"+$obj.areaname+"</option>";
                        });
                        
                        //html
                        $child_name = $jname+""+($depth+1);
                        $html_result = "";
                        if ($option_html.length > 0) { //存在option
                            $html_result += "<select name='"+$child_name+"' id='"+$child_name+"' lay-filter='hometown' data-depth='"+($depth+1)+"' data-name='"+$jname+"'>";
                            $html_result += "<option value=''>"+$jtext+"</option>";
                            $html_result += $option_html;
                            $html_result += "</select>";
                        }
                        $("#json_"+$child_name).html($html_result);
                        
                        //逻辑处理 9.02.25
                        if ($depth == 1) {
                            //清空二级
                            if ($rootid == "") {
                                if ($("#"+$jname+"2").length > 0) {
                                    $("#"+$jname+"2").find("option").remove();
                                    $("#"+$jname+"2").append("<option value=''>"+$jtext+"</option>");
                                }
                            }
                            //清空三级
                            if ($("#json_"+$jname+"3").length > 0) {
                                $("#json_"+$jname+"3").html(""); 
                            }
                        }
                        else if ($depth == 2) {
                            //清空三级
                            if ($rootid == "") {
                                if ($("#"+$jname+"3").length > 0) {
                                    $("#"+$jname+"3").find("option").remove();
                                    $("#"+$jname+"3").append("<option value=''>"+$jtext+"</option>");
                                }
                            }
                        }
                        
                        $form.render('select'); //渲染select样式...
                    },
                    error: function() {
                        
                    }
                });
                
            }
        });
    });
    
    //院校2联动...
    layui.use('form', function(){
        $form = layui.form;
        $form.on('select(school)', function(data){
            //名称
            $jname = data.elem.getAttribute("data-name"); //院校select id
            if (typeof($jname) == "undefined" || $jname == null) {
                $jname = "schid";
            }
            //默认文本
            $jtext = data.elem.getAttribute("data-text");
            if (typeof($jtext) == "undefined" || $jtext == null) {
                $jtext = "请选择";
            }
            $areaid = data.value;//当选一级地区...
            
            //获取院校
            $.ajax({
                type: "POST",
                url: _ROOT_PATH+"index.php",
                cache: false,
                data: {
                    m:"index", c:"ajax", a:"getschool", areaid:$areaid, r:get_rndnum(8)
                },
                dataType: "json",
                success: function($data) {
                    $option_html = "";
                    $.each($data, function($entryIndex, $obj){
                        $option_html += "<option value='"+$obj.schid+"'>"+$obj.name+"</option>";
                    });
                    
                    //html
                    $html_result = "";
                    if ($option_html.length > 0) { //存在option
                        $html_result += "<select name='"+$jname+"' id='"+$jname+"'>";
                        $html_result += "<option value=''>"+$jtext+"</option>";
                        $html_result += $option_html;
                        $html_result += "</select>";
                    }
                    $("#json_"+$jname).html($html_result);
                    
                    $form.render('select'); //渲染select样式...
                },
                error: function() {
                    
                }
            });
        });
    });
    
});
