import ajax from 'jquery/src/ajax/xhr.js'
import $ from 'jquery/src/ajax';



module.exports = {
   
    provinceSelect(addressList) {
        let option = [];
        addressList.map(function (item, i) {
            option.push({
                value: item.id,
                label: item.name
            });
        });
        return option;
    },
    citySelect(addressList) {
        let option = [];
        addressList.map(function (item, i) {
        	if(item.cityList==null){
        		item.cityList=[];
        	}
            if (item.cityList.length) {
                let cityArr = [];
                item.cityList.map(function (city, n) {
                    cityArr.push({
                        value: city.id,
                        label: city.name
                    });
                });
                option.push({
                    value: item.id,
                    label: item.name,
                    children: cityArr
                });
            } else {
                option.push({
                    value: item.id,
                    label: item.name
                });
            };
        });
        return option;
    },
    areaSelect(addressList) {
        let option = [];
        addressList.map(function (item, i) {
        	if(item.cityList==null){
        		item.cityList=[];
        	}
            if (item.cityList.length) {
                let cityArr = [];
                item.cityList.map(function (city, n) {
                	if(city.areaList==null){
		        		city.areaList=[];
		        	}
                    if (city.areaList.length) {
                        let areaArr = [];
                        city.areaList.map(function (area, j) {
                            areaArr.push({
                                value: area.id,
                                label: area.name
                            });
                        });
                        cityArr.push({
                            value: city.id,
                            label: city.name,
                            children: areaArr
                        });
                    } else {
                        cityArr.push({
                            value: city.id,
                            label: city.name
                        });
                    };
                });
                option.push({
                    value: item.id,
                    label: item.name,
                    children: cityArr
                });
            } else {
                option.push({
                    value: item.id,
                    label: item.name
                });
            };
        });
        return option;
    },
    getProvince(province, city, area) {
        let PList = [];
        for (let i = 0; i < addressList.length; i++) {
            PList.push({
                id: addressList[i].id,
                name: addressList[i].name
            });
            if (addressList[i].cityList) {
                for (let j = 0; j < addressList[i].cityList.length; j++) {
                    PList.push({
                        id: addressList[i].cityList[j].id,
                        name: addressList[i].cityList[j].name
                    });
                    if (addressList[i].cityList[j].areaList) {
                        for (let n = 0; n < addressList[i].cityList[j].areaList.length; n++) {
                            PList.push({
                                id: addressList[i].cityList[j].areaList[n].id,
                                name: addressList[i].cityList[j].areaList[n].name
                            });
                        }
                    };
                };
            }
        };
        PList.sort(function (a, b) {
            return a.id - b.id
        });
        let provinceKey = "";
        PList.map(function (item) {
            if (province) {
                if (province == item.id) {
                    provinceKey = item.name;
                };
                if (city == item.id) {
                    provinceKey = provinceKey + "/" + item.name;
                };
                if (area == item.id) {
                    provinceKey = provinceKey + "/" + item.name;
                };
            };
        });
        return provinceKey;
    }
}