jQuery(function($) {

    var dateType = projectParams.dateType;
    var num = $("#only-district").data('setNum');

    if (dateType == 1) {
        var curTr = $('#only-0');
        var title = '请选择';
        var id = '0';            //虚值
        var district_id =  '0';  //虚值
    } else {
        var curTr = $('.dialog-table').children('tbody').children('tr').eq(-1);
        var id = curTr.attr('id') * 1 + 1;  //当前tr的id值
        var title = '全部';
        var district_id =  $("#area-parent-id").children("option:selected").val();   //获取select框的默认值
    }

    if (num > 0) {
        startJudge(num);
    } else {
        $.getJSON(projectParams.selectDistrictUrl, {id:id, district_id:district_id},function(json){
            curTr.after(json.data);
        });
    }



    /**
     * 负责区域新增一行
     */
    $(document).on('click', '.add-district-link', function() {
        var curTr = $('.dialog-table').children('tbody').children('tr').eq(-1);
        var id = curTr.attr('id') * 1 + 1;
        $.getJSON(projectParams.selectDistrictUrl, {id:id, district_id:district_id},function(json){
            curTr.after(json.data);
        });
    });


    /**
     * 当选择省的时候
     */
    $(document).on('change', '#province', function() {
        var district_id =  $("#area-parent-id").children("option:selected").val();   //获取select框的默认值
        var obj = $(this).closest('tr');  //当前操作行的数量
        selectCity(obj, title, district_id);
    });

    /**
     * 当选择市的时候
     */
    $(document).on('change', '#city', function() {
        var district_id =  $("#area-parent-id").children("option:selected").val();   //获取select框的默认值
        var obj = $(this).closest('tr');  //当前操作行的数量
        selectArea(obj, title, district_id);
    });


    /**
     * 删除当前负责的区域的一行
     */
    $(document).on('click', '.delete-district', function() {
        $(this).closest('tr').html("");  //当前操作行的数量
    });

    /**
     * 点击父级改变下面的省市区范围
     */
    $(document).on('change', '#area-parent-id', function() {
        var curTr = $('.dialog-table').children('tbody').children('tr').eq(-1);
        var id = curTr.attr('id') * 1 + 1;  //当前tr的id值
        var oldId = curTr.attr('id') * 1;
        $("#"+oldId).hide();
        var title = '全部';
        var district_id =  $("#area-parent-id").children("option:selected").val();   //获取select框的默认值
        $.getJSON(projectParams.selectDistrictUrl, {id:id, district_id:district_id},function(json){
            curTr.empty();
            curTr.after(json.data);
        });
    });

    //清空输入框
    $(document).on('click', '.clear-input', function(){
        var input = $(this).data('inputId');
        if (input != undefined) {
            $("#" + input).val('');
        }
        var hidden = $(this).data('hiddenId');
        var hiddenValue = $(this).data('hiddenValue');
        if (hidden != undefined) {
            hiddenValue = (hiddenValue == undefined) ? '' : hiddenValue;
            $("#" + hidden).val(hiddenValue);
        }
    });

});


/*
*  搜索所有的省份/城市/区域
*/
function getAllDistrict(obj, provinceId, cityId, areaId, id, district_id) {
    $.getJSON(projectParams.selectDistrictUrl, {province_id:provinceId, city_id:cityId, area_id:areaId, id:id, district_id:district_id, type:1}, function(json){
        obj.after(json.data);
    });
};

/*
*  搜索该省份下所有的城市
*/
function selectCity(obj, title, district_id) {
    obj.find("#city").html("");
    var parentId = obj.find('#province').attr("value");   //父级ID
    var id = obj.find("#city").data('setId');             //本身ID
    $.getJSON(projectParams.cityUrl, {id:parentId, district_id:district_id, title:title},function(json){
        obj.find("#city").append(json.data);
        selectArea(obj, title, district_id);
    });
};

/*
*  搜索该城市下所有的地区
*/
function selectArea(obj, title, district_id) {
    obj.find("#area").html("");
    var parentId = obj.find('#city').attr("value");    //父级ID
    var id = obj.find("#area").data('setId');          //本身ID
    $.getJSON(projectParams.areaUrl, {id:parentId, district_id:district_id, title:title},function(json){
        obj.find("#area").append(json.data);
    });
};

/*
 *  搜索该城市下所有的地区
 */
function startJudge(num){
    var district_id =  $("#area-parent-id").children("option:selected").val();   //获取select框的默认值
    for (var i = 0; i < num; i++) {
        var obj = $('#only-'+i);
        var provinceId = obj.data('setProvince');
        var cityId     = obj.data('setCity');
        var areaId     = obj.data('setArea');
        getAllDistrict(obj, provinceId, cityId, areaId, i, district_id);
    }
}

