// subpages/addshopaddress/addshopaddress.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        token: '',
        area1: '',
        area2: '',
        area3: '',
        area4: '',
        detailContent: '',
        name: '',
        phone: '',
        zipCode: '',

        tempProvIndex: -1,
        tempProvArray: [],

        cityIndex: -1,
        cityArray: [],

        countyIndex: -1,
        countyArray: [],

        villageIndex: -1,
        villageArray: []

    },

    /**
     * 生命周期函数--监听页面加载
     */
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var _self = this;
        _self.getToken().then(result => {

        })
        _self.getAreaList(0, 1);
    },
    getToken() {
        var self = this;
        return new Promise(resolve => {
            wx.getStorage({
                key: 'token',
                success(res) {
                    self.setData({
                        token: res.data
                    })
                    return resolve();
                }
            })
        })
    },
    inputZip(e) {
        this.setData({
            zipCode: e.detail.value
        })
    },
    inputName(e) {
        this.setData({
            name: e.detail.value
        })
    },
    inputPhone(e) {
        this.setData({
            phone: e.detail.value
        })
    },
    inputDetail(e) {
        this.setData({
            detailContent: e.detail.value
        })
    },
    //获取
    getAreaList(areaId, type) {
        var _self = this;
        app.dialog.loading("加载中...");
        app.restAjax.get(app.restAjax.path('{shopUrl}/app/area/listbyparentidrelease/{areaParentId}',
                [app.loginUrl, areaId]),
            null, null,
            function (code, data) {
                switch (type) {
                    case 1: //省份
                        _self.setData({
                            tempProvIndex: -1,
                            tempProvArray: data
                        })
                        break;
                    case 2:
                        _self.setData({
                            cityArray: data
                        })
                        break;
                    case 3:
                        _self.setData({
                            countyArray: data
                        })
                        break;
                    case 4:
                        _self.setData({
                            villageArray: data
                        })
                        break;
                    default:
                        break;
                }
            },
            function (code, data) {
                app.dialog.msg(data.msg);
            },
            function () {
                wx.hideLoading();
            })
    },
    //省份选择
    provChange(e) {
        var index = e.detail.value
        this.setData({
            tempProvIndex: index,
            cityIndex: -1,
            cityArray: [],
            countyIndex: -1,
            countyArray: [],
            villageIndex: -1,
            villageArray: []
        })
        this.getAreaList(this.data.tempProvArray[this.data.tempProvIndex].areaId, 2);
    },
    //市选择
    cityChange(e) {
        var index = e.detail.value
        this.setData({
            cityIndex: index,
            countyIndex: -1,
            countyArray: [],
            villageIndex: -1,
            villageArray: []
        })
        this.getAreaList(this.data.cityArray[this.data.cityIndex].areaId, 3);
    },
    //区县选择
    countyChange(e) {
        var index = e.detail.value
        this.setData({
            countyIndex: index,
            villageIndex: -1,
            villageArray: []
        })
        this.getAreaList(this.data.countyArray[this.data.countyIndex].areaId, 4);
    },
    //乡镇选择
    villageChange(e) {
        var index = e.detail.value
        this.setData({
            villageIndex: index
        })
    },
    doSave() {
        if (this.checkParams()) {
            app.dialog.loading("保存中...")
            var _self = this;
            var areaName = _self.data.tempProvArray[_self.data.tempProvIndex].areaName + ' ' + _self.data.cityArray[_self.data.cityIndex].areaName + ' ' + _self.data.countyArray[_self.data.countyIndex].areaName + ' ' + _self.data.villageArray[_self.data.villageIndex].areaName
            var info = {
                areaCode: _self.data.villageArray[_self.data.villageIndex].areaCode,
                areaId: _self.data.villageArray[_self.data.villageIndex].areaId,
                areaName: areaName,
                shopAddressContent: _self.data.detailContent,
                shopAddressName: _self.data.name,
                shopAddressPhone: _self.data.phone,
                shopAddressZipcode: _self.data.zipCode
            };
            app.restAjax.post(app.restAjax.path('{url}/app/shopaddress/save', [app.shopUrl]), info, {
                headers: {
                    token: _self.data.token
                }
            }, function (code, data) {
                wx.hideLoading()
                console.log(code)
                if (code == 200) {
                    app.dialog.msg("添加成功")
                    var pages = getCurrentPages();
                    let prevPage = pages[pages.length - 2];
                    prevPage.doRefresh()
                    wx.navigateBack()
                }
            }, function (code, data) {
                wx.hideLoading()
                app.dialog.msg(data.msg);
            });
        }
    },
    checkParams() {
        if (this.data.tempProvIndex == -1) {
            wx.showToast({
                title: '请选择省份',
            })
            return false;
        }
        if (this.data.cityIndex == -1) {
            wx.showToast({
                title: '请选择市',
            })
            return false;
        }
        if (this.data.countyIndex == -1) {
            wx.showToast({
                title: '请选择区县',
            })
            return false;
        }
        if (this.data.villageIndex == -1) {
            wx.showToast({
                title: '请选择乡镇街道',
            })
            return false;
        }
        if (this.data.detailContent == '') {
            wx.showToast({
                title: '请输入详细地址',
            })
            return false;
        }
        if (this.data.name == '') {
            wx.showToast({
                title: '请输入姓名',
            })
            return false;
        }
        if (this.data.phone == '') {
            wx.showToast({
                title: '请输入联系方式',
            })
            return false;
        }
        return true;

    }
})