 /*
 * 手太欠
 * 愿这世界都如故事里一样 美好而动人~
 */

Page({
    data: {
        addressId  : '',
        nameValue  : '',
        mobile     : '',
        address    : '',
        isDefault  : '',
        disabled   : false,
        //省份选择
        areas      : [],
        areaId     : '',
        areaIndex  : 0,
        //市级选择
        cityList   : [],
        cityId     : 0,
        cityIndex  : 0,
        //区域选择
        regiList   : [],
        regiId     : 0,
        regiIndex  : 0,
    },

    onLoad(options) {
        this.setData({
            addressId: options.addressid
        })
    },

    onShow() {
        // 获取收货人信息
        this.getUserAddress()
    },

     /**
     * 收货人信息
     */
    getUserAddress(){
        wx.$api.site.siteSee(this.data.addressId).then(res => {
            let areasValue = res.data.provinces.findIndex(val=> val.name == res.data.province.name),
                cityValue  = res.data.cities.findIndex(val=> val.name == res.data.city.name),
                regiValue  = res.data.districts.findIndex(val=> val.name == res.data.district.name)
            this.setData({
                nameValue   : res.data.name,
                mobile      : res.data.mobile,
                areas       : res.data.provinces,
                cityList    : res.data.cities,
                regiList    : res.data.districts,
                areaIndex   : areasValue,
                cityIndex   : cityValue,
                regiIndex   : regiValue,
                areaId      : res.data.province.region_id,
                cityId      : res.data.city.region_id,
                regiId      : res.data.district.region_id,
                address     : res.data.address,
                isDefault   : res.data.default
            })
        }).catch(err => {})
    },

    /**
     * 省市区列表
     */
    createInfo() {
        wx.$api.site.create().then(res => {
            let areas         =  res.data,
                areaIndex     =  this.data.areaIndex
            this.setData({
                areas    : areas,
                areaId   : areas[areaIndex].id,
            })

            this.citylist(areas[areaIndex].id)
        }).catch(err => {})
    },

    /**
     * 所在省份-下拉
     */
    areasChange(e) {
        let area     = this.data.areas,
            index    = e.detail.value,
            atcode   = area[index].id
        if (index != this.data.areaIndex) {
            this.setData({
                areaIndex  : index,
                areaId     : atcode,
                cityList   : [],
                cityId     : 0,
                cityIndex  : 0,
                regiList   : [],
                regiId     : 0,
                regiIndex  : 0,
            })
            // 获取市级列表
            this.citylist(atcode)
        }
    },

    /**
     * 市级列表
     */
    citylist(cityId) {
        wx.$api.site.create({
            parent_id: cityId
        }).then(res=>{
            let cityArr = res.data
            this.setData({
                cityId     : cityArr[0].id,
                cityIndex  : 0,
                cityList   : cityArr,
                regiList   : [],
                regiId     : 0,
                regiIndex  : 0,
            })
            
            // 获取区级列表
            this.regilist(cityArr[0].id)
        })
    },

    /**
     * 市级下拉筛选
     */
    cityDrop(e) {
        let city     = this.data.cityList,
            index    = e.detail.value,
            citycode = city[index].id
        if (index != this.data.cityIndex) {
            this.setData({
                cityIndex : index,
                cityId    : citycode
            })
            
            // 获取区列表
            this.regilist(citycode)
        }
    },
    
    /**
     * 区列表
     */
    regilist(areaId) {
        wx.$api.site.create({
            parent_id: areaId
        }).then(res=>{
            this.setData({
                regiList  : res.data,
                regiId    : res.data[0].id,
                regiIndex : 0
            })
        })
    },
    
    /**
     * 区下拉筛选
     */
    regiDrop(e) {
        let newIndex = e.detail.value
        this.setData({
            regiIndex  : newIndex,
            regiId     : this.data.regiList[newIndex].id
        })
    },

    /**
     * 默认地址
     */
    addressDefault() {
        wx.$api.site.siteDefault(this.data.addressId).then(res => {
            this.setData({
                isDefault: !this.data.isDefault
            })
        }).catch(err => {})
    },

    /*
        姓名截取
    */
    bindinput(e) {
        this.setData({
            nameValue: e.detail.value.substr(0,5)
        })
    },

    /**
     * 提交表单
     */
    siteform(e) {
        let value = e.detail.value
        let data = {
            name        : this.data.nameValue,
            mobile      : value.mobile,
            address     : value.address,
            province_id : this.data.areaId,
            city_id     : this.data.cityId,	
            district_id : this.data.regiId,
            is_default  : this.data.isDefault
        }
        wx.$api.site.siteEdit(this.data.addressId, data).then(res => {
            this.setData({
                disabled: true
            })
            wx.navigateBack()
        }).catch(err => {})
    },

    /**
     * 删除地址
     */
    addressRemove(){
        wx.showModal({
            title       : '提示',
            content     : '是否删除地址',
            confirmText : '删除',
            confirmColor: '#e92152',
            success     : res => {
                if (res.confirm) {
                    wx.showLoading({
                        title: '删除中',
                    })
                    wx.$api.site.siteDel(this.data.addressId).then(() => {
                        wx.hideLoading()
                        wx.navigateBack()
                    })
                }
            }
        })
    },
})
