// pages/addressAdd/addressAdd.js
import { addAddress, editAddress, getRegion } from '../../api/address'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region:[],
    detailAddress: '',
    contactName: '',
    contactPhone: '',
    rangText: '',
    isEdit: false,
    addressId: '',
    regionIndex: [0,0,0],
    regionArray: [['浙江省','上海市'], ['杭州市','嘉定区'],['富阳区','嘉定区']]
  },
  onLoad (options) {
    this.getRegionFn()
    if (options.type && options.type == 0) {
      wx.setNavigationBarTitle({
        title: '编辑地址',
      })
      this.setData({
        isEdit: true,
        addressId: options.addressId,
        region: [options.province, options.city, options.county],
        contactPhone: options.contactPhone,
        contactName: options.contactName,
        detailAddress: options.detailAddress
      })
    }
  },

  // 获取地址的有效氛围
  getRegionFn () {
    getRegion({}).then(res => {
      console.log(res);
    })
  },

  bindMultiPickerChange: function (e) {
    console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
      regionIndex: e.detail.value
    })
  },

  bindMultiPickerColumnChange (e) {
    console.log(e);
  },

  /**
   * 保存地址
   * **/
  handleAddAddress () {
    if (this.valid()) {
      var that = this
      var data = {
        province: this.data.region[0],
        city: this.data.region[1],
        county: this.data.region[2],
        detailAddress: this.data.detailAddress,
        contactName: this.data.contactName,
        contactPhone: this.data.contactPhone,
        isDefault: false
      }
      wx.showLoading({
        title: '加载中...',
        mask: true
      })
      if (that.data.isEdit) {
        data.addressId = this.data.addressId
        editAddress(data).then(res => {
          wx.hideLoading()
          that.resetForm()
          wx.navigateBack()
        }).catch(e => {
          wx.hideLoading()
          wx.showModal({
            content: e,
            confirmColor: '#333',
            showCancel: false
          })
        })
      } else {
        addAddress(data).then(res => {
          wx.hideLoading()
          that.resetForm()
          wx.navigateBack()
        }).catch(e => {
          wx.hideLoading()
          wx.showModal({
            content: e,
            confirmColor: '#333',
            showCancel: false
          })
        })
      }
    }
  },
  bindinput () {},
  resetForm () {
    this.setData({
      region: [],
      detailAddress: '',
      contactName: '',
      contactPhone: ''
    })
  },
  // 提交表单验证
  valid () {
    var valid = false
    if (this.data.region.length == 0) {
      wx.showToast({
        title: '请选择地址',
        icon: 'error'
      })
      return
    }
    if (this.data.detailAddress == '') {
      wx.showToast({
        title: '请输入地址',
        icon: 'error'
      })
      return
    }
    if (this.data.contactName == '') {
      wx.showToast({
        title: '请输入联系人',
        icon: 'error'
      })
      return
    }
    if (this.data.contactPhone == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'error'
      })
      return
    }
    valid = true
    return valid
  },
  /**
   * 用户点击区域选择
   */
  bindRegonPickerChange(e) {
    var value = e.detail.value
    console.log(value);
    this.setData({
      region: value
    })
  }
})