//动态获取省市区
function showRegion(curr, next) {
    if (next) {
        $("#" + next + " option").remove();
        $("#" + curr).nextAll().empty();
        var currVal = $('#' + curr + ' option:selected').val();
        $.ajax({
            url: "/Api/Common/GetArea",
            type: 'post',
            data: { upid: currVal },
            dataType: 'json',
            async: false,
            success: function (data) {
                if (data) {
                    if (next == 'province') {
                        $("#" + next).append('<option value ="">-省份-</option>');
                    } else if (next == 'city') {
                        $("#" + next).append('<option value ="">-城市-</option>')
                    } else if (next == 'region') {
                        $("#" + next).append('<option value ="">-州县-</option>')
                    }
                    var obj = eval(data);
                    $(obj).each(function (index) {
                        var val = obj[index];
                        $("<option value=" + val.Code + ">" + val.Name + "</option>").appendTo($("#" + next));
                    });
                }

            }
        });
    }
}

function InitSheng(provinceId) {
    $.ajax({
        url: "/Api/Common/GetArea",
        type: 'post',
        data: { upid: 100000 },
        dataType: 'json',
        async: false,
        success: function (data) {
            if (data) {
                console.log(222)
                $("#" + provinceId).append('<option value ="" >-省份-</option>');
                var obj = eval(data);
                $(obj).each(function (index) {
                    var val = obj[index];
                    $("<option value=" + val.Code + ">" + val.Name + "</option>").appendTo($("#" + provinceId));
                });

            }
        }
    });
}

function InitArea(provinceOpt, cityOpt, regionOpt) {
    var provinceId = provinceOpt[0];
    var provinceValue = provinceOpt[1];

    InitSheng(provinceId);
    if (provinceOpt) {
        $("#" + provinceId + " option").each(function () {
            if ($(this).text() == provinceValue)
                $(this).attr("selected", "selected");
        });

        showRegion('province', 'city');

    }
    if (cityOpt) {
        var cityId = cityOpt[0];
        var cityValue = cityOpt[1];
        $("#" + cityId + " option").each(function () {
            if ($(this).text() == cityValue)
                $(this).attr("selected", "selected");
        });

        showRegion('city', 'region');
    }
    if (regionOpt) {
        var regionId = regionOpt[0];
        var regionValue = regionOpt[1];
        $("#" + regionId + " option").each(function () {
            if ($(this).text() == regionValue)
                $(this).attr("selected", "selected");
        });
    }

}