/*
 * @Descripttion: 完善信息表单
 * @Company: 思宏科技
 * @version: v1.0
 * @Author: HJ
 * @Date: 2021-11-27 23:08:03
 * @LastEditors: OBKoro1
 * @LastEditTime: 2022-01-10 14:41:51
 */
import { automaticPhone } from '../../../utils/auth'
import { fhSendMsm } from '../../../api/notice/notice'
const { Tips, Loading, hLoading } = getApp()
const componentOptions = {
  // 组件选项
  options: {
    multipleSlots: true
  },
  behaviors: [],
  properties: {},
  // 组件数据
  data: {
    index: 0,
    isPageHidden: false, // 页面是否处于隐藏状态4
    changePhoneDialogShow: false,
    countDownTime: 60 * 1000,
    form: {
      name: null,
      sex: null,
      birthday: null,
      certificateType: 'ID_Card',
      identityCardNumber: null,
      phone: null,
      captcha: null,
      height: null,
      weight: null,
      bloodType: null
    },
    certificateType: [
      {
        type: 'ID_Card',
        text: '中国居民身份证'
      },
      {
        type: 'Passport',
        text: '护照'
      },
      {
        type: 'HongKong_Macau_Pass',
        text: '港澳居民来往内地通行证'
      },
      {
        type: 'Taiwan_Pass',
        text: '台湾居民来往大陆通行证'
      }
    ],
    timeData: {},
    countDownFlag: false,
    sex: ['男', '女'],
    bloodArray: ['A', 'B', 'O', '其它']
  },
  // 数据监听器
  observers: {},
  // 组件方法
  methods: {
    init() {},
    updateForm(form) {
      this.setData({
        form
      })
      this.setData({
        index: this.data.certificateType.findIndex(
          item => item.type === form.certificateType
        )
      })
    },
    //填写表单
    Change({ currentTarget: { dataset }, detail }) {
      console.log(dataset, 123)
      const { key, formKey = 'form' } = dataset
      const emo = this.data.certificateType[detail.value].type
      this.setData({
        index: detail.value,
        [`${formKey}.${key}`]: emo
      })
      wx.$setGlobalData('Form', this.data.form)
    },
    // 输入
    onInputChange({ currentTarget: { dataset }, detail }) {
      const { key, formKey = 'form' } = dataset
      let { value } = detail
      if (key === 'sex') value = +value
      this.setData({
        [`${formKey}.${key}`]: typeof detail === 'string' ? detail : value
      })
      console.log(this.data.form)
      wx.$setGlobalData('Form', this.data.form)
    },
    // 自动填入手机号或修改手机号
    async automaticPhoneFn(e) {
      try {
        const phone = await automaticPhone(e)
        this.setData({
          'form.phone': phone
        })
        console.log(phone)
      } catch (error) {
        console.log(error)
      }
    },
    // 获取手机验证码
    async getMsmCode() {
      const { form, countDownFlag } = this.data
      const { phone } = form
      if (countDownFlag) return
      if (!phone) {
        Tips('请输入手机号')
        return
      }
      Loading('获取手机验证码中...')
      try {
        await fhSendMsm({ phone })
        Tips('获取验证码成功，请留意手机短信')
        this.setData(
          {
            countDownFlag: true
          },
          () => {
            this.start()
          }
        )
      } catch (e) {
        console.log('debug axios', e)
      }
      hLoading()
    },
    // 校验表单
    validateForm() {
      return this.data.form
    },
    // 倒计时开始
    start() {
      const countDown = this.selectComponent('#control-count-down')
      countDown.start()
    },
    // 倒计时暂停
    pause() {
      const countDown = this.selectComponent('#control-count-down')
      countDown.pause()
    },
    // 倒计时重置
    reset() {
      const countDown = this.selectComponent('#control-count-down')
      countDown.reset()
    },
    finished() {
      this.reset()
      this.setData({
        countDownFlag: false
      })
    },
    onChange(e) {
      this.setData({
        timeData: e.detail
      })
    },
    showPhoneModal() {
      this.setData({
        changePhoneDialogShow: true
      })
    }
  },
  // 组件生命周期
  lifetimes: {
    created() {},
    attached() {
      this.init()
    },
    ready() {},
    moved() {},
    detached() {}
  },
  definitionFilter() {},
  // 页面生命周期
  pageLifetimes: {
    // 页面被展示
    show() {
      const { isPageHidden } = this.data

      // show事件发生前，页面不是处于隐藏状态时
      if (!isPageHidden) {
        return
      }

      // 重新执行定时器等操作
    },
    // 页面被隐藏
    hide() {
      this.setData({
        isPageHidden: true
      })

      // 清除定时器等操作
    },
    // 页面尺寸变化时
    resize() {}
  }
}

Component(componentOptions)
