// pages/login/login.ts
// @ts-ignore
import RSA from 'wxmp-rsa'
import { sendSms, userInfo, } from '../../apis/login'
import { bindPhone, bindPhoneList } from "../../apis/consumer";

const app = getApp<IAppOption>()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // username: '13408085719',
    phone: '',
    code: '',
    agree: false,
    publicKey: 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDP1WOya5Ut9LdR6sRdaDzFjXNH3UIv5m8sJAXqow+BOOGbSsDWTZgTdbGv7JzRo5YZVMC0wg+UwNH7zD3UVKJqtaHPUavh4+j/UIzD615X0gFDbVUCBusxCVNj68L4iXP/dqRZfqD6s72f4YPwhHsHvHLdQkGYBL9ls3/Ds9rXEQIDAQAB',
    sending: false,
    sendNum: 60,
    sendTimer: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('optionsLogin', options, options.source);
    if (options.source) {
      this.setData({
        source: options.source
      })
    }
    wx.removeStorageSync('QSource')
  },

  send() {
    console.log('send');

    if (this.data.sending) return
    // const rsa = new RSA()
    // rsa.setPublicKey(this.data.publicKey)
    const { phone } = this.data
    if (!phone) {
      wx.showToast({
        icon: 'none',
        title: '请输入手机号!'
      })
      return
    }
    if (!/^1[3|4|5|7|8][0-9]{9}$/.test(phone)) {
      wx.showToast({
        icon: 'none',
        title: '手机号格式错误!'
      })
      return
    }
    sendSms({
      phone,
    }).then(() => {
      wx.showToast({
        icon: 'none',
        title: '验证码发送成功'
      })
      const timer = setInterval(() => {
        const { sendNum, sendTimer } = this.data
        this.setData({
          sendNum: sendNum - 1
        })
        if (sendNum <= 0) {
          clearInterval(sendTimer)
          this.setData({
            sending: false,
            sendNum: 60,
            sendTimer: 0
          })
        }
      }, 1000)
      this.setData({
        sendTimer: timer,
        sending: true
      })
    })
  },

  defaultLogin() {
    const { phone, code } = this.data
    bindPhone({ phone, code }).then(res => {
      if (res.c === 200) {
        bindPhoneList().then(resp => {
          wx.setStorageSync('accountList', resp.d)
          // this.setData({
          //   userInfo: resp.d.find(item => item.phone === this.data.userInfo.phone)
          // })
          wx.setStorageSync('userInfo', resp.d.find(item => item.phone === this.data.phone))
          // wx.redirectTo({
          //   url: `/pages/my/my?phone=${phone}`
          // })
          wx.redirectTo({
            url: `/pages/my/my`
          })
        })

      }
    })

  },
  bindAgreeChange(e: WechatMiniprogram.CustomEvent) {
    const { value } = e.detail
    this.setData({
      agree: value
    })
  },

  jumpPrivacy() {
    wx.navigateTo({
      url: "/pages/privacy/privacy"
    })
  },
  jumpAgreement() {
    wx.navigateTo({
      url: "/pages/agreement/agreement"
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})