// index.js
var myRequest = require("../../utils/api.js");
import {
  areaList
} from '@vant/area-data';

Page({
  data: {
    value: '',
    areaList,
    areaValue: '',
    name: '',
    tel: '',
    area: '',
    areaData: [],
    bankCardNumber: '',
    bankName: '',
    identityNumber: '',
    checked:false,

  },
  onLoad() {
    console.log(areaList)
  },
  onChange(e) {
    console.log(e)
    let key = e.target.dataset.key
    this.setData({
      [key]: e.detail
    })

  },
  showCity(e) {
    console.log('111')
    this.setData({
      show: true
    })
  },
  aggrement(e){

    let url = e.target.dataset.key
    console.log(url)
    wx.navigateTo({
      url:`../${url}/index`
    })
  },

  onChangeArea(e) {
    let area = e.detail.values.reduce((pre, cur) => {
      return {
        name: pre.name + '-' + cur.name
      }
    })
    console.log(e.detail.values)


    this.setData({
      areaData: e.detail.values,
      areaValue: e.detail.values[2].code,
      show: false,
      area: area.name
    })
  },
  checkRadio(e){
    console.log(e)
    let checked = this.data.checked
    this.setData({
      checked:!checked
    })
  },
  closeArea() {
    this.setData({
      show: false
    })
  },
  apply() {
    if(!this.data.checked){
      wx.showToast({
        title: '请阅读并勾选用户协议',
        icon: 'none'
      })
      return
    }
    if (!this.data.name) {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
      return
    }
    if (this.data.tel.length != 11) {
      wx.showToast({
        title: '请输入正确手机号',
        icon: 'none'
      })
      return
    }

    if (this.data.areaData.length == 0) {
      wx.showToast({
        title: '请选择合适的售后区域',
        icon: 'none'
      })
      return
    }
    if (this.data.areaData.length == 0) {
      wx.showToast({
        title: '请选择合适的售后区域',
        icon: 'none'
      })
      return
    }
    const reg = /^[1-9]\d{5}(19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[Xx\d]$/
    if (!reg.test(this.data.identityNumber)) {
      wx.showToast({
        title: '请输入正确的身份证',
        icon: 'none'
      })
      return
    }
    // 定义银行卡号的正则表达式

    if (!this.data.bankName) {
      wx.showToast({
        title: '请输入开户行',
        icon: 'none'
      })
      return
    }
    const bankCardRegex = /^([1-9]{1})(\d{15}|\d{16}|\d{18})$/;
    if (!bankCardRegex.test(this.data.bankCardNumber)) {
      wx.showToast({
        title: '请输入正确的银行卡号',
        icon: 'none'
      })
      return
    }



    let param = {
      phonenumber: this.data.tel,
      nickName: this.data.name,
      provinceCode: this.data.areaData[0].code,
      provinceName: this.data.areaData[0].name,
      cityCode: this.data.areaData[1].code,
      cityName: this.data.areaData[1].name,
      areaCode: this.data.areaData[2].code,
      areaName: this.data.areaData[2].name,
      bankCardNumber: this.data.bankCardNumber,
      bankName: this.data.bankName,
      identityNumber: this.data.identityNumber
    }
    console.log(param)
    myRequest.myRequest('supporterApply', param, 'post').then(res => {
      wx.showToast({
        title: '提交成功，等待管理员联系',
        icon: 'none'
      })
      setTimeout(() => {
        wx.navigateBack(-1)
      }, 1000);

    })
  }

})