/*
 * @Descripttion: zx
 * @Company: 思宏科技
 * @version: v1.0
 * @Author: zx
 * @Date: 2021-11-22 09:12:10
 * @LastEditors: OBKoro1
 * @LastEditTime: 2022-01-23 17:05:40
 */
const app = getApp()
import {
  fhCreateQr,
  fhGetPatientInfo,
  fhGetPatientRecord
} from '../../../api/remote/index'
import { sceneEncode } from '../../../utils/util'
const pageOptions = {
  // 页面数据
  data: {
    isFirstOnShow: true, // 是否为首次执行onShow
    basicForm: [], //患者详情
    addressDetail: null,
    qrImage: null,
    id: 0
  },

  // 获取患者详情(只用于页面展示患者基本信息)
  async fhGetPatientInfo() {
    const form = []
    const memberId = app.getMemberId()
    try {
      const { results } = await fhGetPatientInfo(form, memberId)

      this.setData({
        basicForm: results
      })
      this.getPatientRecord()
    } catch (e) {
      console.log('debug axios', e)
    }
  },

  //获取患者详情
  async getPatientRecord() {
    const form = []
    const memberId = app.globalData.baseInfo.memberId
    const hospitalId = app.globalData.hospitalInfo.hospitalId
    try {
      const { results } = await fhGetPatientRecord(form, memberId, hospitalId)
      this.setData({
        id: results.id
      })
      return results
    } catch (e) {
      return e
    }
  },
  // 获取分享二维码
  async getQrImg() {
    try {
      app.Loading('二维码加载中...')
      const scene = sceneEncode({ rid: this.data.id })
      const { results } = await fhCreateQr({
        page: 'pages/pages-patient/patient-login/patient-login',
        scene
      })
      app.hLoading()
      this.setData({
        qrImage: results
      })
    } catch (e) {
      console.log('debug axios', e)
    }
  },
  //保存相册
  save() {
    wx.getSetting({
      success: res => {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {}
          })
        } else {
          this.saveQrcode()
        }
      }
    })
  },

  saveQrcode() {
    wx.downloadFile({
      url: this.data.qrImage,
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: () => {
            wx.showToast({ icon: 'none', title: '保存成功' })
            onClose()
          },
          fail: () => {
            wx.showToast({ icon: 'none', title: '保存失败' })
          }
        })
      },
      fail: () => {
        wx.showToast({
          icon: 'none',
          title: '下载失败～请检查一下相关下载配置'
        })
      }
    })
  },

  // 页面载入时
  async onLoad(e) {
    await this.getPatientRecord()
    this.getQrImg()
    this.init(e)
    // this.getPatientInfo();
  },
  // 页面初始化
  init(e) {
    this.fhGetPatientRecord()
  },
  async fhGetPatientRecord() {
    const form = []
    const memberId = app.getMemberId()
    const hospitalId = app.globalData.hospitalInfo.hospitalId
    try {
      const { results } = await fhGetPatientRecord(form, memberId, hospitalId)
      this.setData({
        addressDetail: results.sickArea + results.bedNumber
      })
    } catch (e) {
      // console.log("debug axios", e);
    }
  },
  // 页面准备好时
  onReady() {
    // this.getQrImg()
  },
  // 页面显示时
  onShow() {
    const { isFirstOnShow } = this.data
    this.fhGetPatientInfo()
    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)
