/*
 * @Descripttion: zx
 * @Company: 思宏科技
 * @version: v1.0
 * @Author: zx
 * @Date: 2021-11-08 14:05:22
 * @LastEditors: OBKoro1
 * @LastEditTime: 2022-01-07 15:28:45
 */
const app = getApp()
import {
  fhGetPatientRecord,
  fhSavePatientRecord
} from '../../../api/remote/index'
const pageOptions = {
  // 页面数据
  data: {
    isFirstOnShow: true, // 是否为首次执行onShow
    form: {
      sex: 0,
      bedNumber: app.globalData.hospitalInfo.bedNumber,
      sickroomId: app.globalData.hospitalInfo.sickroomId,
      hospitalId: app.globalData.hospitalInfo.hospitalId
    },
    hosiptal: {},
    hosiptalName: '',
    sickroomName: ''
  },

  //获取患者住院信息
  async fhGetPatientRecord() {
    const form = []
    const memberId = app.globalData.baseInfo.memberId
    const hospitalId = app.globalData.hospitalInfo.hospitalId
    try {
      const { results } = await fhGetPatientRecord(form, memberId, hospitalId)
      // this.setData({
      //   remoteVisitsList: results.results[0],
      // });
    } catch (e) {
      console.log('debug axios', e)
    }
  },
  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
    })
  },
  //创建患者住院信息
  async fhSavePatientRecord() {
    const { form } = this.data
    const memberId = app.globalData.baseInfo.memberId
    console.log(memberId)

    try {
      const { results } = await fhSavePatientRecord(form, memberId)
      const loginState = true
      const formhealth = wx.getStorageSync('form-health')
      console.log("我是健康页面来的", formhealth)
      
      if (formhealth) {
        wx.redirectTo({
          url: '/pages/health-record/index'
        })
        wx.removeStorageSync('form-health')
      } else {
        wx.navigateTo({
          url: `/remote/patient/home/index?loginState=${loginState}`
        })
      }
       
    } catch (e) {
      console.log('debug axios', e)
    }
  },
  //扫描二维码
  scan() {
    wx.scanCode({
      success: result => {
        console.log(result)
      },
      fail: () => {
        console.log(result)
      }
    })
  },
  // 页面载入时
  onLoad(e) {
    this.init(e)
    this.fhGetPatientRecord()
    this.setData({
      hosiptalName: app.globalData.hospitalAddress.poiName,
      sickroomName: app.globalData.hospitalAddress.sickroomName
    })
    console.log(app.globalData.baseInfo.memberId, '🐱‍🏍🐱‍🏍')
  },
  // 页面初始化
  init() {
    this.setData({
      hosiptal: {
        name: wx.getStorageSync('exactaddress'),
        ...wx.getStorageSync('hospital-info')
      }
    })
  },
  // 页面准备好时
  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,
    } */
  }
}

Page(pageOptions)
