
/*
 * 本时生活
 */

const app = getApp()


Page({

    /**
     * 页面的初始数据
     */
    data: {
        addressInfo: '',
        type       : '', //类型
        addressId  : '',
        defaultVal :'',
        name       : '',
        mobile     : '',
        address    : '',

        //省份选择
        areas      : [],
        areaSn     : '',
        areaIndex  : 0,

        //市级选择
        cityList   : [],
        cityId     : 0,
        cityIndex  : 0,

        //区域选择
        regiList   : [],
        regiId     : 0,
        regiIndex  : 0,

        cityLayer  : false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad (options) {
        this.setData({
            type    : options.type
        })
        if (options.type == 'Add') {
            wx.setNavigationBarTitle({
                title: '添加收货地址'
            })
            // 获取位置
            wx.getLocation({
                type: 'gcj02',
                success: res=> {
                    this.setData({
                        longitude : res.longitude,
                        latitude  : res.latitude
                    })
                    // 解析坐标
                    getApp().qqmapsdk.reverseGeocoder({
                        location: {
                            latitude    : res.latitude,
                            longitude   : res.longitude
                        },
                        success: res=>{
                            if(res.message == "query ok"){
                                let   addressInfo = res.result.address_component
                                let   areaIndex = this.data.areas.findIndex(val => val.name == addressInfo.province)
                                this.setData({
                                    areaIndex : areaIndex
                                })
                                this.getProvince()
                            }else{
                                wx.showToast({
                                    title: res.message,
                                    icon : 'none'
                                })
                            }
                        }
                    })
                }
            })
            this.getProvince()
        } else if (options.type == 'Compile') {
            wx.setNavigationBarTitle({
                title: '编辑收货地址'
            })
            this.setData({
                addressId: options.id
            })
            this.getUserAddress()
        }
    },

    /**
     * 获取收货人信息
     */
    getUserAddress(){
        wx.$api.address.edit(this.data.addressId).then(res=>{
            let areasValue = res.data.provinces.findIndex(val=> val.name == res.data.address.province_name),
                cityValue  = res.data.cities.findIndex(val=> val.name == res.data.address.city_name),
                regiValue  = res.data.districts.findIndex(val=> val.name == res.data.address.district_name)
            this.setData({
                name        : res.data.address.name,
                mobile      : res.data.address.mobile,
                areas       : res.data.provinces,
                cityList    : res.data.cities,
                regiList    : res.data.districts,
                areaIndex   : areasValue,
                cityIndex   : cityValue,
                regiIndex   : regiValue,
                areaSn      : res.data.address.province_id,
                cityId      : res.data.address.city_id,
                regiId      : res.data.address.district_id,
                address     : res.data.address.address,
                defaultList : res.data.address,
                isDefault   : res.data.address.is_default
            })
        }).catch(err => {});
    },

    /**
     * 获取省信息
     */
    getProvince() {
        wx.$api.address.create().then(res=>{
            let areaArr    =  res.data.provinces,
                areaIndex  =  this.data.areaIndex
            this.citylist(areaArr[areaIndex].code)
            this.setData({
                areaSn      : areaArr[areaIndex].code,
                areas       : areaArr
            })
        }).catch(err => {});
    },

    /**
     * 所在省份
     */
    areasChange(e) {
        let area     = this.data.areas,
            index    = e.detail.value,
            atcode   = area[index].code
        if (index != this.data.areaIndex) {
            this.setData({
                areaIndex   : index,
                areaSn      : atcode
            })

            // 获取市级列表
            this.citylist(atcode)
        }
    },

    /**
     * 市级列表
     */
    citylist(code) {
        wx.$api.address.children(code).then(res=>{
            let cityArr = res.data
            this.regilist(cityArr[0].code)
            this.setData({
                cityId      : cityArr[0].code,
                cityList    : cityArr,
                cityIndex   : 0
            })
        }).catch(err => {});
    },

    /**
    * 市级下拉筛选
    */
    city(e) {
        let city     = this.data.cityList,
            index    = e.detail.value,
            citycode = city[index].code
        if (index != this.data.areaIndex) {
            this.setData({
                cityIndex: index,
                cityId   : citycode
            })

            // 获取市级列表
            this.regilist(citycode)
        }
    },
    

    /**
     * 区列表
     */
    regilist(areaCode) {
        wx.$api.address.children(areaCode).then(res=>{
            this.setData({
                regiList    : res.data,
                regiId      : res.data[0].code,
                regiIndex   : 0
            })
        }).catch(err => {});
    },

    /**
    * 区下拉筛选
    */
    regi(e) {
        let newIndex = e.detail.value
        this.setData({
            regiIndex   : newIndex,
            regiId      : this.data.regiList[newIndex].code
        })
    },

    /**
     * 保存信息
     */
    siteform(e) {
        if(this.data.type == 'Compile') {
            // 编辑地址
            wx.$api.address.keep(this.data.addressId, e.detail.value.name, e.detail.value.mobile, this.data.areaSn, this.data.cityId, this.data.regiId, e.detail.value.address).then(res=>{
                wx.navigateBack()
            }).catch(err => {});
        } else {
            // 创建地址
            wx.$api.address.add(e.detail.value.name, e.detail.value.mobile, this.data.areaSn, this.data.cityId, this.data.regiId, e.detail.value.address, this.data.defaultVal).then(res=>{
                wx.navigateBack()
            }).catch(err => {});
        }
    },

    /**
     * 设为默认地址
     */
    addressDefault(e) {
        let val = e.detail.value
        this.setData({
            defaultVal: val
        })
    }
})