const api = require('../../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['身份证', '护照', '临时身份证', '军官证'],
    index:0,
    codetext:'获取验证码',
    current:0
  },
  bindPickerChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  swiper:function(e){
    let that=this
    // if(that.data.current==1){
    //   if (!this.data.name || !this.data.idcard || !this.data.phone || !this.data.text) {
    //     if (!(/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(that.data.idcard)) && !(/^1[3456789]\d{9}$/.test(that.data.phone))) {
    //       that.setData({
    //         isYesno: true,
    //         current: e.detail.current
    //       })
    //       return false
    //     }
    //   }else{
    //     that.setData({
    //       isYesno: false,
    //       current: e.detail.current
    //     })
    //     return false
    //   }
    // }
    that.setData({
      current: e.detail.current
    })
    
  },
  code:function(){
    let that=this
    if (!that.data.phone||that.data.phone.split('').length!=11) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon:'none'
      })
      return false
    }
    that.data.codetext = 59
    let timer = setInterval(function () {
      if (that.data.codetext == 0) {
        clearTimeout(timer)
        that.setData({
          codetext: '重新发送'
        })
      }
      that.setData({
        codetext: that.data.codetext
      })
      that.data.codetext--
    }, 1000);
    wx.request({
      url: api.sendPhoneCode,
      data: { 'phone': that.data.phone},
      success(res){
        console.log(res)
      }
    })
  },
  photo:function(){
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        that.data.filePaths = res.tempFilePaths
        that.setData({
          photo: that.data.filePaths
        })
      }
    })
  },
  formSubmit:function(e){
    let that = this
    if (!that.data.photo){
      wx.showToast({
        title: '请选择照片',
        icon:'none'
      })
      return false
    }
    if (!e.detail.value.name || !e.detail.value.phone || !e.detail.value.idcard || !e.detail.value.text){
      wx.showToast({
        title: '请填写访客基本信息',
        icon: 'none'
      })
      return false
    }
    // if (!e.detail.value.code){
    //   wx.showToast({
    //     title: '请填写短信验证码',
    //     icon: 'none'
    //   })
    //   return false
    // }
    wx.showLoading({
      title: '正在提交',
    })
    wx.uploadFile({
      url: api.editorAlumniVisitor,
      filePath: that.data.photo[0],
      header: { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8', },
      name: 'file',
      formData: {
          'inviteUserId': wx.getStorageSync('loginId'),
          'visitorName': e.detail.value.name,
          'visitorPhone': e.detail.value.phone,
          'identity': that.data.array[parseInt(e.detail.value.type)],
          'IDNumber': e.detail.value.idcard,
          'phoneCode': e.detail.value.code,
          'visitorReason': e.detail.value.text,
          'remarks': '',
      },
      success(res) {
        wx.hideLoading()
        res = JSON.parse(res.data)
        if (res.errno == 0) {
          wx.redirectTo({
            url: '/pages/success/index',
          })
        }else{
          wx.showToast({
            title: res.data,
            icon: 'none'
          })
        }
      }
    })
  },
  ifYesno(){
    let that=this
    if (that.data.name || that.data.idcard || that.data.phone || that.data.text) {
      if ((/^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/.test(that.data.name))&&(/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(that.data.idcard)) && (/^1[3456789]\d{9}$/.test(that.data.phone))) {
        that.setData({
          isYesno: true
        })
      }else{
        that.setData({
          isYesno: false
        })
      }
    }else{
      that.setData({
        isYesno: false
      })
    }
  },
  name:function(e){
    this.setData({
      name:e.detail.value
    })
    this.ifYesno()
  },
  idcard: function (e) {
    this.setData({
      idcard: e.detail.value
    })
    this.ifYesno()
  },
  phone: function (e) {
    this.setData({
      phone: e.detail.value
    })
    this.ifYesno()
  },
  text: function (e) {
    this.setData({
      text: e.detail.value
    })
    this.ifYesno()
  },
  previewImage:function(){
    wx.previewImage({
      urls: [this.data.filePaths[0]] // 需要预览的图片http链接列表
    })
  },
  up:function(){
    this.data.current--
    this.setData({
      current: this.data.current,
      swipercurrent: this.data.current
    })
  },
  next:function(){
    debugger
    console.log(this.data)
    if(!this.data.photo){
      wx.showToast({
        title: '请先上传头像照',
        icon:'none'
      })
      return false
    }else if(this.data.current==1){
      if (!this.data.name || !this.data.idcard || !this.data.phone || !this.data.text) {
        wx.showToast({
          title: '资料请填写完整',
          icon: 'none'
        })
        return false
      }
      if (!(/^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/.test(this.data.name))){
        wx.showToast({
          title: '名称输入不合法',
          icon: 'none'
        })
        return false
      }
      if (!(/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(this.data.idcard))){
        wx.showToast({
          title: '身份证输入不合法',
          icon:'none'
        })
        return false
      }
      if (!(/^1[3456789]\d{9}$/.test(this.data.phone))){
        wx.showToast({
          title: '手机号输入不合法',
          icon: 'none'
        })
        return false
      }
    }
    this.data.current++
    this.setData({
      current: this.data.current,
      swipercurrent: this.data.current,
      isYesno: true
    })
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})