$(function() {
    $("#province_select").change(function() {
        $("#city_select option").remove();
        $("#district_select option").remove();
        $("#city_select").append("<option value='0'>市</option>");
        $("#district_select").append("<option value='0'>区/县</option>");
        var province = $("#province_select option:selected").val();
        $.ajax({
            url: "/city",
            type: "GET",
            data: {
                province_id: province,
            },
            async: false,
            dataType: "json",
            success: function(json) {
                if (json.state == 1) {
                    var cities = json.data.list
                    var optionStr = ""
                    for (let index = 0; index < cities.length; index++) {
                        const city = cities[index];
                        optionStr = optionStr + "<option value = " + city.id + ">" + city.name + "</option>"
                    }
                    $("#city_select").append(optionStr);

                } else {
                    console.log("获取城市失败", json.msg);
                }
            },
            error: function() {
                console.log("连接失败，请检查网络");
            }
        });
    });

    $("#city_select").change(function() {
        
        $("#district_select option").remove();
        $("#district_select").append("<option value='0'>区/县</option>");
        var city = $("#city_select option:selected").val();
        $.ajax({
            url: "/district",
            type: "GET",
            data: {
                city_id: city,
            },
            async: false,
            dataType: "json",
            success: function(json) {
                if (json.state == 1) {
                    var districts = json.data.list
                    var optionStr = ""
                    for (let index = 0; index < districts.length; index++) {
                        const district = districts[index];
                        optionStr = optionStr + "<option value = " + district.id + ">" + district.name + "</option>"
                    }
                    $("#district_select").append(optionStr);

                } else {
                    console.log("获取区县失败", json.msg);
                }
            },
            error: function() {
                console.log("连接失败，请检查网络");
            }
        });
    });

    $("#cat_p_select").change(function() {
        $("#cat_c_select option").remove();
        $("#cat_c_select").append("<option value='0'>详细类型</option>");
        $("#cat_c_select").parent().hide();
        $("#org_category").val("0");
        var p_cat = $("#cat_p_select option:selected").val();
        $.ajax({
            url: "/get_org_cat",
            type: "GET",
            data: {
                pid: p_cat,
            },
            async: false,
            dataType: "json",
            success: function(json) {
                if (json.state == 1) {
                    var cats = json.data.list
                    if (cats.length == 0) {
                        $("#cat_c_select").parent().hide();
                        $("#org_category").val(p_cat);
                    } else {
                        var optionStr = ""
                        for (let index = 0; index < cats.length; index++) {
                            const cat = cats[index];
                            optionStr = optionStr + "<option value = " + cat.id + ">" + cat.short_name + "</option>"
                        }
                        $("#cat_c_select").append(optionStr);
                        $("#cat_c_select").parent().show();
                    }

                } else {
                    layer.msg("获取详细类型失败: " + json.msg);
                }
            },
            error: function() {
                layer.msg("连接失败，请检查网络");
            }
        });
    });

    $("#cat_c_select").change(function() {
        $("#org_category").val($("#cat_c_select option:selected").val());
    });

    $("#submit").on("click", function() {
        var checkErr = checkInfoFull();
        if (checkErr.length > 0) {
            layer.msg(checkErr);
            return;
        }

        var postData = {
            name: $("#org_name").val(),
            short_name: $("#org_short_name").val(),
            province: $("#province_select option:selected").val(),
            city: $("#city_select option:selected").val(),
            district: $("#district_select option:selected").val(),
            address: $("#address").val(),
            category: $("#org_category").val(),
            org_phone: $("#org_phone").val(),
        }
        postRequest("/org/create/submit", postData, doSuccess, doFail);

        function doSuccess(res) {
            if (res.state == 0) {
                serverErrorMsg(res);
                return;
            }
            layer.msg("创建成功");
            window.location.href = res.data.url; //跳转网址
        }
    });
});

function checkInfoFull() {
    if ($("#org_name").val().length == 0) {
        return "请填写机构名称";
    }
    if ($("#org_short_name").val().length == 0) {
        return "请填写机构简称";
    }
    if ($("#province_select option:selected").val() == "0") {
        return "请选择省份";
    }
    if ($("#city_select option:selected").val() == "0") {
        return "请选择城市";
    }
    if ($("#district_select option:selected").val() == "0") {
        return "请选择区县";
    }
    if ($("#address").val().length == 0) {
        return "请填写地址";
    }
    if ($("#org_category").val().length == 0 || $("#org_category").val() == "0") {
        return "请选择机构类型";
    }
    if ($("#org_phone").val().length > 0) {
        if (/^(\d{3,4}-?\d{7,8}$)|(1\d{10}$)/.test($("#org_phone").val()) == false) {
            return "请填写正确的机构电话";
        }
    }
    return "";
}

function doFail(res) {
    serverErrorMsg(res);
}