/**
 * 发票管理
 */
var el = {
    id: '#id',
    dataDiv: '#dataDiv',
    receivingUser: '#receivingUser',
    receivingTel: '#receivingTel',
    province: '#province',
    provinceCode: '#provinceCode',
    provinceName: '#provinceName',
    city: '#city',
    cityCode: '#cityCode',
    cityName: '#cityName',
    county: '#county',
    countyCode: '#countyCode',
    countyName: '#countyName',
    address: '#address'
};


var id = $(el.id).val();

/**
 * 文档就绪
 */
$(function () {
    // 初始化地区下拉框
    /*$(el.county).combobox({
        required: false,
        disabled: true,
        prompt: '--- 请先选择省份 ---',
        panelHeight: 180,
        valueField: 'code',
        textField: 'name'
    });*/

    // 初始化城市下拉框
    $(el.city).combobox({
        required: false,
        disabled: true,
        prompt: '--- 请先选择省份和城市 ---',
        panelHeight: 200,
        valueField: 'code',
        textField: 'name'
    });

    //初始化省分下拉框
    $(el.province).combobox({
        url: '/pc/sys/area/rest/query?pCode=CN',
        valueField: 'code',
        textField: 'name',
        method: 'get',
        required: true,
        panelHeight: 200,
        value: $(el.provinceCode).val(),
        onSelect: function (data) {
            $(el.provinceCode).val(data.code);
            $(el.provinceName).val(data.name);
            initCity(data.code);
        }
    });
});


/**
 * 初始化城市下拉框值
 *
 * @param provinceId
 */
function initCity(code){
    $(el.city).combobox({
        url: '/pc/sys/area/rest/query',
        queryParams: {pCode: code},
        valueField: 'code',
        textField: 'name',
        method: 'get',
        required: true,
        disabled: false,
        panelHeight: 200,
        prompt: '',
        onSelect: function (data) {
            $(el.cityCode).val(data.code);
            $(el.cityName).val(data.name);
            //initCounty(data.code);
        },
        onLoadSuccess: function (data) {
            var cityCode = $(el.cityCode).val(), clearFlag = true;
            for (var i = 0; i < data.length; i++) {
                if (data[i].code === cityCode) {
                    $(el.city).combobox('setValue', cityCode);
                    clearFlag = false;
                }
            }

            /*if (clearFlag) {
                initCounty('');
            }*/
        }
    });
}


/**
 * 初始化地区下拉框
 *
 * @param cityId
 */
/*function initCounty(cityCode){
    $(el.county).combobox({
        url: '/pc/sys/area/rest/query',
        queryParams: {pCode: cityCode},
        valueField: 'code',
        textField: 'name',
        method: 'get',
        required: true,
        disabled: false,
        panelHeight: 200,
        prompt: '',
        onSelect: function (data) {
            $(el.countyCode).val(data.code);
            $(el.countyName).val(data.name);
        },
        onLoadSuccess: function (data) {
            var countyCode = $(el.countyCode).val();
            for (var i = 0; i < data.length; i++) {
                if (data[i].code === countyCode) {
                    $(el.county).combobox('setValue', countyCode);
                }
            }
        }
    });
}*/


/**
 * 数据保存
 *
 * @param callback 回调函数
 */
function dataSave(callback) {
    var checkMsg = verification();
    if (checkMsg) {
        topWin.layer.alert(checkMsg, {icon: 0});
        return false;
    }

    var params = {};
    params.id = $(el.id).val();
    params.receivingUser = $(el.receivingUser).textbox('getValue');
    params.receivingTel = $(el.receivingTel).textbox('getValue');
    params.provinceCode = $(el.provinceCode).val();
    params.provinceName = $(el.provinceName).val();
    params.cityCode = $(el.cityCode).val();
    params.cityName = $(el.cityName).val();
    //params.countyCode = $(el.countyCode).val();
    params.countyName = $(el.countyName).val();
    params.address = $(el.address).textbox('getValue');

    topWin.layer.confirm('您确定要保存吗?', {icon: 3}, function (index) {
        topWin.layer.close(index);

        var loading = topWin.layer.msg('数据保存中, 请耐心等待......', {icon: 16, time: 0, shade: 0.1});
        $.ajax({
            url: '/pc/mall/address/rest/data/save',
            type: 'POST',
            data: JSON.stringify(params),
            contentType: 'application/json',
            success: function () {
                topWin.layer.close(loading);
                callback();
            },
            error: function (data) {
                topWin.layer.close(loading);
                topWin.layer.alert(data.responseJSON, {icon: 2});
            }
        });
    });
}


/**
 * 表单数据校验
 *
 * @return {string} 校验结果
 */
function verification() {
    if ($(el.receivingUser).textbox('getValue') === '') {
        return '收货人不能为空';
    }
    if ($(el.receivingTel).textbox('getValue') === '') {
        return '收货人联系电话不能为空';
    } else {
        var tel = $(el.receivingTel).textbox('getValue');
        var rex1 = /^1[3-8]+\d{9}$/;  //手机号
        var rex2=/^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;
        if(!rex1.test(tel) && !rex2.test(tel)){
            return '请输入正确联系电话';
        }
    }
    if ($(el.provinceCode).val() === '' || $(el.cityCode).val() === '' || $(el.countyName).val() === '') {
        return '省/市/区不能为空';
    }
    if ($(el.address).textbox('getValue') === '') {
        return '详细地址不能为空';
    }
    return '';
}

