/*
 * @Descripttion: zx
 * @Company: 思宏科技
 * @version: v1.0
 * @Author: zx
 * @Date: 2021-11-08 11:06:25
 * @LastEditors: OBKoro1
 * @LastEditTime: 2021-12-14 17:09:01
 */
const app = getApp()
const { Tips, Loading, hLoading } = getApp()
import { fhSendMsm } from '../../../api/notice/notice'
import { automaticPhone } from '../../../utils/auth'
import { fhSavePatientInfo, fhGetPatientInfo } from '../../../api/remote/index'
const pageOptions = {
  // 页面数据
  data: {
    countDownTime: 60 * 1000,
    countDownFlag: false,
    isFirstOnShow: true, // 是否为首次执行onShow
    certificateType: [
      '中国居民身份证',
      '护照',
      '港澳居民来往内地通行证',
      '台湾居民来往内地通行证'
    ],
    bloodArray: ['A', 'B', 'O', '其它'],
    form: {
      certificateType: 0,
      sex: 0
    }
  },
  //患者详情
  async fhGetPatientInfo() {
    const form = []
    const memberId = app.globalData.baseInfo.memberId
    try {
      const { results } = await fhGetPatientInfo(form, memberId)
      console.log(results, '❤❤❤❤')
    } catch (e) {
      console.log('debug axios', e)
    }
  },
  //保存患者认证
  async fhSavePatientInfo() {
    const { form } = this.data
    const memberId = app.globalData.baseInfo.memberId
    const emp = {
      0: 'ID_Card',
      1: 'Passport',
      2: 'HongKong_Macau_Pass',
      3: 'Taiwan_Pass'
    }
    form.certificateType = emp[form.certificateType]
    form.sickroomId = app.globalData.hospitalInfo.sickroomId
    form.hospitalId = app.globalData.hospitalInfo.hospitalId

    try {
      const { results } = await fhSavePatientInfo(form, memberId)
      app.globalData.patientInfo = results
      wx.navigateTo({
        url: `/remote/patient/hospitalInformation/index?memberId=${this.data.active}`
      })
    } catch (e) {
      console.log('debug axios', e)
    }
  },
  // 初始化表单
  initInfo(info = {}) {
    if (info && info.id) {
      this.tempHadInfo = true
    }
    this.setData({
      form: { sex: 1, ...info },
      loading: false
    })
  },
  // 输入
  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)
  },
  // 自动填入手机号或修改手机号
  async automaticPhoneFn(e) {
    const { type } = e.currentTarget.dataset
    if (type === 'change') {
      this.setData({
        changePhoneDialogShow: true,
        changePhoneForm: clone(this.data.form)
      })
      confirmFlag = false
      confirmResolve = null
      return
    }

    const { errMsg } = e.detail
    if (errMsg.includes('deny')) return

    const phone = await automaticPhone(e)
    this.setData({
      'form.phone': phone
    })
  },
  // 获取手机验证码
  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()
  },
  // 页面载入时
  onLoad(e) {
    this.init(e)
    this.fhGetPatientInfo()
  },
  // 页面初始化
  init(e) {
    const phone = wx.getStorageSync('auth-phone')
    if (phone) {
      this.setData({
        'form.phone': phone
      })
    }
  },
  // 页面准备好时
  onReady() {},
  // 页面显示时
  onShow() {
    const { isFirstOnShow } = this.data

    if (isFirstOnShow) {
      // 首次执行时
      this.setData({
        isFirstOnShow: false
      })
      return
    }
  },
  // 页面隐藏时
  onHide() {},
  // 页面卸载时
  onUnload() {},
  // 下拉页面时
  onPullDownRefresh() {},
  // 到达页面底部时
  onReachBottom() {},
  // 页面滚动时
  onPageScroll() {},
  // 分享时，注：onShareAppMessage不能为async异步函数，会导致不能及时取得返回值，使得分享设置无效
  onShareAppMessage() {
    /* const title = ''
    const path = ''
    const imageUrl = ``

    return {
      title,
      path,
      imageUrl,
    } */
  },
  // 倒计时开始
  start() {
    const countDown = this.selectComponent('#control-count-down')
    console.log(countDown)
    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
    })
  }
}

Page(pageOptions)
