const util = require('../../utils/util.js');
Component({
  properties: {
    show:{
      type: Boolean,
      observer: function (val){
        this.setData({ show: val });
      }
    },
    data:{
      type:String,
      observer:function(newVal){
        if (!util.isEmpty(newVal)) {
          let datas = newVal.split(" ");
          this.setData({ name: datas[0], mobile: datas[1], provincial: (datas[2].split('/')).length - 1 <= 1 ? datas[2] + "/" : datas[2], address: datas[3]});
        }
      }
    }
  },
  data: {
    show:false,
    name:"",
    mobile:'',
    provincial:'',
    address:'',
    datas:{},
    region: ['浙江省', '杭州市', '上城区']
  },
  methods: {
    bindRegionChange(e) {
      let value = e.detail.value, provincial = this.data.provincial;
      if (value) provincial = value[0] + '/' + value[1] + '/' + value[2];
      this.setData({ provincial });
    },
    // 微信获取地址列表
    getAddressForWX(){
      let that=this;
      wx.getSetting({
        success (res) {
          console.log(res.authSetting['scope.address'])
          if (res.authSetting['scope.address']) {
            wx.authorize({
              scope: 'scope.address',
              success: (result)=>{
                that.chooseWxAddress()
              },fail: ()=>{
                util.showTips('您已拒绝授权');
              }
            })
          } else {
            wx.openSetting({
              success (res) {
                console.log(res)
                that.chooseWxAddress()
              },fail: ()=>{
                util.showTips('您已拒绝授权');
              }
            })
          }
        }
      })
    },
    chooseWxAddress(){
      let that =this;
      wx.chooseAddress({
        success (res) {
          console.log(res)
          let pro = res.provinceName+'/'+res.cityName+'/'+res.countyName
          console.log(pro)
          that.setData({name:res.userName,mobile:res.telNumber,provincial:pro,address:res.detailInfo})
        }
      })
    },
    save(e){
      let data = e.detail.value;
      if (util.isEmpty(data.name)) {
        util.showTips('姓名不能为空');
        return false;
      }
      if (util.isEmpty(data.mobile)) {
        util.showTips('手机号不能为空');
        return false;
      }
      if (util.isPhone(data.mobile)) {
        util.showTips('请正确填写手机号');;
        return false;
      }
      if (util.isEmpty(data.provincial)) {
        util.showTips('省市区不能为空');
        return false;
      }
      if (util.isEmpty(data.address)) {
        util.showTips('详细地址不能为空');
        return false;
      }
      if (data.address.length>100) {
        util.showTips('详细地址请输入100字以内信息');
        return false;
      }
      this.setData({show:false});
      this.triggerEvent("submit", data);
    },
    close() {
      this.setData({ show: false });
      this.triggerEvent("close", this.data.show);
    }
  }
})
