/*
 * @Descripttion: zx
 * @Company: 思宏科技
 * @version: v1.0
 * @Author: zx
 * @Date: 2021-11-04 11:18:33
 * @LastEditors: OBKoro1
 * @LastEditTime: 2022-01-23 17:05:29
 */
const dayjs = require('../../../src/js/dayjs/index')
const app = getApp()
import {
  fhGetFamilyMembers,
  fhGetPatientInfo,
  fhGetPatientRecord,
  fhGetRemoteVisitsDetail
} from '../../../api/remote/index'
const pageOptions = {
  // 页面数据
  data: {
    isFirstOnShow: true, // 是否为首次执行onShow
    addressDetail: null,
    memberType: 'PATIENT',
    remoteVisitsList: [],
    show: false,
    hospitalForm: [],
    basicForm: [],
    familyList: [], // 家属列表
    visitState: null, // 状态
    // 二维码展示弹窗
    actionVisibled: false,
    // 当前点击的卡片
    currRemoteData: {},
    isEmpty: true
  },
  toVisit() {
    wx.navigateTo({
      url: `/remote/patient/visitApplication/index`
    })
  },
  nav() {
    wx.navigateTo({
      url: `/remote/my-remote-visitation/index`
    })
  },
  // 获取患者家属列表
  async fhGetFamilyMembers() {
    const form = []
    const memberId = app.getMemberId()
    try {
      const { results } = await fhGetFamilyMembers(form, memberId)
      this.setData({
        familyList: results.results
      })
    } catch (e) {
      console.log('debug axios', e)
    }
  },
  // 远程探视详情
  async getRemoteVisitsDetail() {
    const id = app.globalData.id
    try {
      const { results } = await fhGetRemoteVisitsDetail(id)
      // wx.$setGlobalData("patientInfo", results);
      this.setData({
        visitState: results.visitState
      })
      console.log(results, '✔✔✔this.data.form')
    } catch (e) {
      console.log('debug axios', e)
    }
  },
  // 获取患者详情
  async fhGetPatientInfo() {
    const form = []
    const memberId = app.getMemberId()
    try {
      const { results } = await fhGetPatientInfo(form, memberId)

      this.setData({
        basicForm: results
      })
      this.fhGetPatientRecord()
    } catch (e) {
      console.log('debug axios', e)
    }
  },
  // 获取患者住院信息
  async fhGetPatientRecord() {
    const form = []
    const memberId = app.getMemberId()
    const hospitalId = app.globalData.hospitalInfo.hospitalId
    try {
      const { results } = await fhGetPatientRecord(form, memberId, hospitalId)
      this.setData({
        hospitalForm: results,
        addressDetail: results.sickArea + results.bedNumber
      })
    } catch (e) {
      // console.log("debug axios", e);
    }
  },
  // 小程序卡片中的快捷按钮被点击
  cardAction({ detail }) {
    const { visitState } = detail
    if (visitState === 'TO_BE_CONFIRM') {
      this.setData({
        actionVisibled: true,
        currRemoteData: detail
      })
    }
  },
  // 关闭二维码展示弹窗
  actionClose() {
    this.setData({
      actionVisibled: false
    })
  },
  // 页面载入时
  onLoad(e) {
    this.trtcComponent = this.selectComponent('#visit-list')
    this.options = e
    this.init(e)
  },
  // 页面初始化
  init(e) {},
  // 页面准备好时
  onReady() {},
  // 页面显示时
  onShow() {
    const { isFirstOnShow } = this.data

    this.fhGetPatientInfo()
    this.fhGetFamilyMembers()
    console.log(this.trtcComponent.init)
    const tag  =  'remote'
    this.trtcComponent.init(tag).then(({ isEmpty }) => {
      this.setData({ isEmpty })
    })

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

    return {
      title,
      path,
      imageUrl,
    } */
  }
}

Page(pageOptions)
