import {
  sendPhoneCode,
  getPayrollId
} from '../../services/index.js'
import {
  showToast
} from '../../utils/tips.js'
import {
  imgServerUrl
} from '../../config/config.js'
import { testPhone } from '../../utils/util'
import $wuxCountDown from '../../utils/coutdown.js'
var app = getApp()
Page({

  data: {
    imgServerUrl: imgServerUrl,
    iphone: "",
    yzm: '',
    phone: '',
    url: '',
    reset: false
  },
  onLoad: function (options) {
    if (options.phone) {
      this.setData({
        phone: options.phone,
        url: options.url,
        auto_send: options.auto_send
      })
      if (options.title) {
        wx.setNavigationBarTitle({
          title: options.title,
        })
      }
    }
    // console.log(opts.approveState);
    // this.setData({
    //   approveState: opts.approveState
    // })
    // if (opts.approveState == 0) {
    //   wx.showModal({
    //     title: '您还未进行实名认证',
    //     content: '认证后即可使用查询功能',
    //     cancelText: '以后再说',
    //     confirmText: '立即认证',
    //     success(res) {
    //       if (res.confirm) {
    //         wx.redirectTo({
    //           url: '../identification/index',
    //         })
    //       } else if (res.cancel) {
    //         wx.navigateBack({
    //           delta: 1
    //         })
    //       }
    //     }
    //   })
    // }
  },
  //下一步提交
  next(e) {
    if (this.data.approveState != 3) {
      showToast('身份认证成功后才可进行查询！！！')
      return
    }
    if (this.data.iphone.length != 11) {
      showToast('请输入11位手机号码！')
      return
    }
    if (this.data.yzm.length != 6) {
      showToast('请输入6位验证码！')
      return
    }
    let paramsObj = {
      phone: this.data.iphone,
      code: this.data.yzm,
      user_id: app.globalData.userId,
      user_token: app.globalData.userToken,
      member_id: app.globalData.memberId
    }
    Object.assign(paramsObj);
    getPayrollId(paramsObj).then(data => {
      wx.redirectTo({
        url: '../wages/index?idNum=' + data.data.hpMember.idCardNumber + "&realName=" + data.data.hpMember.realName,
      })
    })
  },
  // 手机号输入
  bindPhoneInput(e) {
    this.setData({
      iphone: e.detail.value
    })
  },
  // 验证码号输入
  bindCodeInput(e) {
    this.setData({
      yzm: e.detail.value
    })

  },
  //倒计时
  vcode() {
    if (this.djs && this.djs.interval) return !1
    this.djs = new $wuxCountDown({
      date: +(new Date) + 60000,
      onEnd() {
        this.setData({
          djs: '获取验证码',
        })
      },
      render(date) {
        const sec = this.leadingZeros(date.sec, 2) + ' 秒 '
        date.sec !== 0 && this.setData({
          djs: '剩余' + sec + '',
        })
      },
    })
  },
  // 验证码发送
  sendCode(e) {
    if (this.data.approveState != 3) {
      showToast('身份认证成功后才可进行查询！！！')
      return
    }
    if (this.data.iphone.length != 11) {
      showToast('请输入11位手机号码！')
      return
    }
    if (this.data.djs && this.data.djs !== '重新获取验证码') {
      return false
    }
    sendPhoneCode({
      phone: this.data.iphone
    })
    this.vcode()
  },
  send(data) {
    sendPhoneCode({
      phone: this.data.phone,
      ...data.detail
    })
  },
  toList(e) {
    if (testPhone(this.data.phone)) {
      let paramsObj = {
        phone: this.data.phone,
        code: e.detail.code,
        user_id: app.globalData.userId,
        user_token: app.globalData.userToken,
        member_id: app.globalData.memberId
      }
      getPayrollId(paramsObj).then(data => {
        wx.setStorage({
          key: 'hpMember',
          data: data.data.hpMember,
          success: res => {
            wx.redirectTo({
              url: this.data.url,
            })
          }
        })
      }).catch(data => {
        this.setData({
          reset: !this.data.reset
        })
      })
    } else {
      wx.showToast({
        icon: 'none',
        title: '电话号码格式错误',
      })
    }
  }
})