/*
 * @Descripttion: HJ
 * @Company: 思宏科技
 * @version: v1.0
 * @Author: HJ
 * @Date: 2021-06-15 09:57:13
 * @LastEditors: zhangzhuobin 1093998108@qq.com
 * @LastEditTime: 2022-05-31 13:42:59
 */
import {
  computedAge
} from '../../utils/util'
import {
  fhIsVerityPatientVerty
} from '../../api/sign/index'
const {
  watch,
  Tips
} = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    authedFlag: false,
    authedImg: "../../images/authedFlag.png",
    avatarName: {}
  },
  onShow() {
    const avatarName = wx.getStorageSync('avatar-name') ? wx.getStorageSync('avatar-name') : {}
    // 获取患者memberId
    const memberId = getApp().globalData.baseInfo.memberId
    // 获取患者是否认证状态
    const isAuthedFlag = wx.getStorageSync('authedFlag')

    this.setData({
      authedFlag: isAuthedFlag,
      avatarName
    })

    // 如果有患者id，才能进行验证实名认证
    const patientInfo = getApp().globalData.patientInfo
    if (patientInfo.id !== null) {
      // 患者是否认证函数
      this.handleAuthedIdent()
    }
  },

  onReady() {
    watch('patientInfo', this.initInfo.bind(this))
  },
  ucenterClick({
    currentTarget: {
      dataset
    }
  }) {
    const ifAuth = wx.getStorageSync('anflag')
    const {
      path,
      type
    } = dataset
    const ispatientInfo = getApp().globalData.patientInfo

    if (ifAuth) {
      wx.$routerTo('/pages/authorization/authorization')
      return
    }

    // if (
    //   ispatientInfo &&
    //   !ispatientInfo.id &&
    //   type === 'health' &&
    //   !wx.getStorageSync('__isReadPrivacyKey__')
    // ) {
    //   wx.setStorageSync('form-health', true)
    //   wx.$routerTo('/pages/pages-patient/patient-login/patient-login?type=back')
    //   return
    // }

    if (path) {
      wx.$routerTo(path)
    } else {
      Tips('敬请期待')
    }
  },
  signManagement() {
    // 获取用户是否登录信息
    const ifAuth = wx.getStorageSync('anflag')
    // 用户未登录,进行登录
    if (ifAuth) {
      wx.$routerTo('/pages/authorization/authorization')
      return
    }
    wx.switchTab({
      url: '../sign/sign'
    })
  },
  // 初始化表单
  initInfo(info = {}) {
    let age = null
    if (info.birthday) {
      age = computedAge(info.birthday)
    }
    this.setData({
      info: {
        ...info,
        age
      }
    })
  },

  // 前往微信客服
  toCustomerService() {
    wx.openCustomerServiceChat({
      extInfo: {
        url: 'https://work.weixin.qq.com/kfid/kfcee4256adc5536f89'
      },
      corpId: 'ww1f41003d877e916f',
    })
  },
  toNursingOrder({ currentTarget: { dataset } }) {
    // 获取用户是否登录信息
    const ifAuth = wx.getStorageSync('anflag')
    const { path, type } = dataset
    // 用户未登录,进行登录
    if (ifAuth) {
      wx.$routerTo('/pages/authorization/authorization')
      return
    }
    if (path) {
      wx.$routerTo(path)
    } else {
      Tips('敬请期待')
    }
  },

  /***
   * 患者是否认证过
   */
  async handleAuthedIdent() {
    wx.showLoading({
      title: "正在加载中",
      mask: true
    })
    try {
      const params = {
        patientId: getApp().globalData.baseInfo.memberId
      }
      const {
        results
      } = await fhIsVerityPatientVerty(params);
      wx.setStorageSync('authedFlag', results.flag)
      // 存储法大大返回的实名认证信息
      wx.setStorageSync('authedInformation', results)

      this.setData({
        authedFlag: results
      })
    } catch (e) {
      console.log(e)
    } finally {
      wx.hideLoading()
    }
  },
})