
// pages/addAddress/addAddress.js
const wxService = require('../../utils/wxService')
const { memberId } = wx.getStorageSync('_baseUserInfo')

wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    isSelect:true,
    citys: null,
    cityView: '',
    isDefault: 0,
    formData: {},
    selectCityName: ['', ''],
    multiIndex: [0, 0, 0],
    focus: false,
    params:{
      userId:'',
      name:'',
      phone:'',
      province:'',
      city: '',
      district: '',
      address:'',
      type:1
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()
    if (options && options.id){
      wx.setNavigationBarTitle({
        title: '编辑地址'
      })
      this.setData({
        addressId: options.id,
        id: options.id
      })
    }else{
      wx.setNavigationBarTitle({
        title: '新增地址'
      })
    }
    
    
  },
  /**
 * 生命周期函数--监听页面显示
 */
  onShow: function () {
    const { memberId } = wx.getStorageSync('_baseUserInfo')
    this.init()
    if(this.data.id){
      this.getAddressInfo()
    }
  },

  init() {
    wxService.getC1().then((c1) => {
      return wxService.Promise.all([wxService.Promise.resolve(c1), wxService.getC2ByC1('北京'), wxService.getC3ByC2('北京北京市')])
    }).then(res => {
      console.log(res)
      const citys = res.map(item => {
        return [...item]
      })
      this.setData({
        citys
      })
    })
  },

  // 通过地址id查询
  getAddressInfo(){
    wxService.post(`/member/addressManage/getbyid`,{
      id: this.data.id
    }).then(res => {
      const { result, data } = res.data
      if (result == 0) {
        this.setData({
          params: {
            userId: data.userId,
            name: data.name,
            phone: data.phone,
            province: data.province,
            city: data.city,
            district: data.district,
            address: data.address,
            type: data.type
          },
          isSelect: data.type,
          cityView: data.province + ' ' + data.city + ' ' + data.district
        })
        wxService.getC1().then((c1) => {
          return wxService.Promise.all([wxService.Promise.resolve(c1), wxService.getC2ByC1('北京'), wxService.getC3ByC2('北京北京市')])
        }).then(res => {
         
          const citys = res.map(item => {
            return [...item]
          })
          this.setData({
            citys
          })
        })
      }
    }).finally(() => {
    })
  },

  bindMultiPickerChange(e) {
    const citys = this.data.citys
    const multiIndex = e.detail.value
    console.log(citys)
    if (citys) {
      const cityView = [citys[0][multiIndex[0]], citys[1][multiIndex[1]], citys[2][multiIndex[2]]].join(' ')
      
      this.setData({
        multiIndex,
        cityView,
        'params.province': citys[0][multiIndex[0]],
        'params.city': citys[1][multiIndex[1]],
        'params.district': citys[2][multiIndex[2]]
      })
      
    }
  },

  bindMultiPickerColumnChange(e) {
    console.log(e)
    const detail = e.detail
    const index = detail.value
    let cityName = ''
    switch (detail.column) {
      case 0:
        cityName = this.data.citys[0][index]
        this.data.selectCityName[0] = cityName
        wxService.getC2ByC1(cityName)
          .then(item => {
            let c2 = [...item]
            this.data.selectCityName[1] = c2[0]
            return wxService.Promise.all([wxService.Promise.resolve(item), wxService.getC3ByC2(this.data.selectCityName.join(''))])
          }).then((res) => {
            this.setData({
              'citys[1]': [...res[0]],
              'citys[2]': [...res[1]]
            })
          })
        break
      case 1:
        cityName = this.data.citys[1][index]
        this.data.selectCityName[1] = cityName
        wxService.getC3ByC2(this.data.selectCityName.join(''))
          .then(item => {
            this.setData({
              'citys[2]': [...item]
            })
          })
        break
      default:
        break
    }
  },



  settingDefaultAddress(){
    this.setData({
      isSelect: !this.data.isSelect
    })
  },

  //提交
  formSubmit(e){
    if (!e.detail.value.name){
      wx.showToast({
        title: `请输入姓名`,
        icon: 'none'
      })
      return false;
    }
    if (!e.detail.value.phone) {
      wx.showToast({
        title: `请输入电话`,
        icon: 'none'
      })
      return false;
    }
    if (!this.data.cityView) {
      wx.showToast({
        title: `请选择地区`,
        icon: 'none'
      })
      return false;
    }
    if (!e.detail.value.address) {
      wx.showToast({
        title: `请输入详细地址`,
        icon: 'none'
      })
      return false;
    }
    
    this.setData({
      params:{
        userId: memberId,
        name: e.detail.value.name,
        phone: e.detail.value.phone,
        province: this.data.params.province,
        city: this.data.params.city,
        district: this.data.params.district,
        address: e.detail.value.address,
        type: this.data.isSelect ? 1 : 0
      }
    })
    wx.showLoading({
      title: '加载中'
    })
    if (this.data.addressId){
      this.setData({
        'params.id': this.data.id
      })
      wxService.post(`/member/addressManage/update`, this.data.params).then(res => {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          wx.showToast({
            title: `修改成功`,
            icon: 'none'
          })
          wxService.router(`/pages/myAddress/myAddress`)
        }
      }).finally(() => {
        wx.hideLoading()
      })
    }else{
      wxService.post(`/member/addressManage/save`, this.data.params).then(res => {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          wx.showToast({
            title: `保存成功`,
            icon: 'none'
          })
          wxService.router(`/pages/myAddress/myAddress`)
        }
      }).finally(() => {
        wx.hideLoading()
      })

    }
    
  }
})