const app = getApp();
const request = require('../../utils/request.js');
const tools = require('../../utils/tools.js');
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
Page({
  data: {
    showareaPicker: false,
    showadressPicker: false,
    areacolumnsArr: [],
    areacolumns: [
      // {
      //   values: [],
      //   className: 'column1',
      // },
      // {
      //   values: [],
      //   className: 'column2'
      // },
      // {
      //   values: [],
      //   className: 'column3'
      // }
    ],
    adresscolumnsArr: [],
    adresscolumns: [],
    errorinfo: {
      nameerr: '',
      idcarderr: '',
      areaerr: '',
      adresserr: '',
      adressDetailserr: '',
      telerr: ''
    },
    info: {
      id: '',
      name: '',
      idcard: '',
      area: [],
      adress: '',
      adressDetails: '',
      tel: ''
    },
    readonly: false
  },
  async getUnit(regioncode, communityid) {
    let options = {
      url: 'resource/miniapp/user/getXqdwList',
      data: {
        regioncode,
        accountId: wx.getStorageSync('userId')
      },
      method: 'post'
    }
    let res = await request.query(options);
    if (res.data.code === 200) {
      this.setData({
        adresscolumnsArr: res.data.data,
        adresscolumns: res.data.data.map(item => {
          return item.name
        })
      })
      let adress = ['info.adress']
      if (communityid) {
        console.log(res.data.data)
        this.setData({
          [adress]: res.data.data.length == 0 ? '' : res.data.data.filter(item => {
            return item.id == communityid
          })[0].name
        })
      } else {
        this.setData({
          [adress]: ''
        })
      }
    } else {
      Toast.fail(res.data.msg);
    }
  },
  async getArea(parentId, level) {
    // if(level===3)return false;
    console.log('我调用了几次')
    let options = {
      url: 'resource/miniapp/user/getDeptList',
      data: {
        accountId: wx.getStorageSync('userId'),
        parentId: parentId ? parentId : '',
        level: level ? level : ''
      },
      method: 'post'
    }
    let res = await request.query(options);
    if (res.data.code === 200) {
      let idx = level ? level : 0;
      console.log(res.data.data, '111111111')
      if (res.data.data.length !== 0) {
        console.log('22222222222222')
        this.setData({
          ['areacolumnsArr[' + idx + ']']: res.data.data.map(item => {
            return item
          }),
          ['areacolumns[' + idx + '].values']: res.data.data.map(item => {
            return item.name
          }),
          // ['areacolumns[' + idx + '].className']: 'column' + idx
        })
        this.getArea(res.data.data[0].id, idx + 1)
      }
      // if (res.data.data.length !== 0)  this.getArea(res.data.data[0].id, idx + 1)
    } else {
      Toast.fail(res.data.msg);
    }
  },
  async getInfo() {
    let options = {
      url: 'resource/miniapp/user/getUserInfo',
      data: {
        openId: wx.getStorageSync('openid')
      },
      method: 'post'
    }
    let res = await request.query(options);
    if (res.data.code === 200) {
      let id = 'info.id';
      let name = 'info.name';
      let idcard = 'info.idcard';
      let area = 'info.area';
      let adressDetails = 'info.adressDetails';
      let tel = 'info.tel';
      this.setData({
        [id]: res.data.data.id,
        [name]: (res.data.data.name !== 'null' && res.data.data.name) ? res.data.data.name : '',
        [idcard]: (res.data.data.idcard !== 'null' && res.data.data.idcard) ? res.data.data.idcard : '',
        [area]: res.data.data.regionName,
        [adressDetails]: (res.data.data.address !== 'null' && res.data.data.address) ? res.data.data.address : '',
        [tel]: (res.data.data.phone !== 'null' && res.data.data.phone) ? res.data.data.phone : ''
      })
      this.getUnit(res.data.data.regioncode, res.data.data.communityid);
      this.getArea();
    } else {
      Toast.fail(res.data.msg);
    }
  },
  areaClick(e) {
    this.setData({
      showareaPicker: true
    })
  },
  onareaCancel() {
    this.setData({
      showareaPicker: false
    })
  },
  onareaConfirm(e) {
    console.log(e)
    let area = 'info.area';
    this.setData({
      showareaPicker: false,
      [area]: e.detail.value[0] ? e.detail.value : []
    })
    let a = e.detail.value[e.detail.value.length - 1];
    let regioncode = this.data.areacolumnsArr[this.data.areacolumnsArr.length - 1].filter(item => {
      return item.name == a
    })[0].code
    this.getUnit(regioncode);
  },
  adressClick() {
    if (this.data.info.area.length !== 0) {
      this.setData({
        showadressPicker: true
      })
    } else {
      Toast.fail('请先选择区划');
    }
  },
  onadressCancel() {
    this.setData({
      showadressPicker: false
    })
  },
  onadressConfirm(e) {
    console.log(e.detail.value)
    let adress = 'info.adress';
    this.setData({
      [adress]: e.detail.value,
      showadressPicker: false
    })
  },
  nameChange(e) {
    console.log(e)
    let val = 'info.name'
    this.setData({
      [val]: e.detail
    })
    console.log(this.data.info.name)
  },
  idCardChange(e) {
    let val = 'info.idcard'
    this.setData({
      [val]: e.detail
    })
  },
  adressDetailsChange(e) {
    let val = 'info.adressDetails'
    this.setData({
      [val]: e.detail
    })
  },
  telChange(e) {
    let val = 'info.tel'
    this.setData({
      [val]: e.detail
    })
  },
  onareaChange(e) {
    console.log(e.detail.index)
    // if (e.detail.index < 2) {
    let item = this.data.areacolumnsArr[e.detail.index].filter(item => {
      return item.name == e.detail.value[e.detail.index]
    })[0].id
    // if (item.level === 3) return false;
    this.getArea(item.id, item.level)
    // }
  },
  async submit(e) {
    console.log(this.data.info.area, '我是选择的行政区划')
    console.log(this.data.areacolumnsArr, '我是获取的行政区划所有信息')
    let regioncode = this.data.info.area.length > 0 ? this.data.areacolumnsArr[this.data.info.area.length - 1].filter(item => {
      return item.name == this.data.info.area[this.data.info.area.length - 1]
    })[0].code : '';
    console.log(this.data.info.adress)
    let options = {
      url: 'resource/miniapp/user/editUserInfo',
      data: {
        id: this.data.info.id,
        name: this.data.info.name,
        idcard: this.data.info.idcard,
        regioncode,
        communityid: this.data.info.adress.length > 0 ? this.data.adresscolumnsArr.filter(item => {
          return item.name == this.data.info.adress
        })[0].id : '',
        address: this.data.info.adressDetails,
        phone: this.data.info.tel
      },
      method: 'post'
    }

    let res = await request.query(options);
    if (res.data.code === 200) {
      wx.showModal({
        title: '提示',
        content: '保存成功',
        showCancel: false,
        success: (res) => {
          if (res.confirm) {
            wx.switchTab({
              url: '/pages/personInfo/personInfo',
            })
          }
        }
      })
    } else {
      Toast.fail(res.data.msg);
    }
    // }
    // console.log(this.data.errorinfo)
  },
  onLoad: function () {

  },
  onShow() {
    if (!wx.getStorageSync('userId')) {
      wx.showModal({
        title: '提示',
        content: '请扫描二维码进入小程序',
        showCancel: false,
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
        }
      })
      return false;
    }
    tools.isLogin().then((res) => {
      // this.getArea();
      this.getInfo();
      this.setData({
        readonly: app.globalData.type === 'jhdx' ? false : true
      })
    }, (error) => {
      console.log(error);
    })
  }
})