/*
 * @Descripttion: zx
 * @Company: 思宏科技
 * @version: v1.0
 * @Author: zx
 * @Date: 2021-11-04 14:27:47
 * @LastEditors: HJ
 * @LastEditTime: 2021-11-25 10:13:24
 */
const app = getApp();
import { fhGetRemoteVisitsDetail } from "../../../api/remote/index";
const pageOptions = {
  // 页面数据
  data: {
    isFirstOnShow: true, // 是否为首次执行onShow
    memberType: "PATIENT",
    detail: {},
    // role: "presenter", // 用户角色 presenter(可发言) audience（仅收听）
    tapTime: "", //防抖动参数
    visitTime: "", //探视时间
    roomID: "",
    userID: "",
    template: "grid",
    familyId: null,
    memberType: null, //角色
    // privacySwitch:false //隐私开关
  },
  joinRoom: function () {
    // 防止两次点击操作间隔太快
    const nowTime = new Date();
    if (nowTime - this.tapTime < 1000) {
      return;
    }
    const url = `/remote/room/room?roomID=${app.globalData.id}&userID=${app.globalData.baseInfo.memberId}&template=${this.data.template}&memberType=PATIENT`;
    this.tapTime = nowTime;
    this.checkDeviceAuthorize()
      .then((result) => {
        console.log("授权成功", result);
        wx.navigateTo({ url: url });
      })
      .catch((error) => {
        console.log("没有授权", error);
      });
  },
  joinFamilyRoom: function () {
    // 防止两次点击操作间隔太快
    const nowTime = new Date();
    if (nowTime - this.tapTime < 1000) {
      return;
    }
    const url = `/remote/room/room?roomID=${app.globalData.id}&userID=${app.globalData.baseInfo.memberId}&template=${this.data.template}&memberType=FAMILY_MEMBER`;
    this.tapTime = nowTime;
    this.checkDeviceAuthorize()
      .then((result) => {
        console.log("授权成功", result);
        wx.navigateTo({ url: url });
      })
      .catch((error) => {
        console.log("没有授权", error);
      });
  },
  checkDeviceAuthorize: function () {
    this.hasOpenDeviceAuthorizeModal = false;
    return new Promise((resolve, reject) => {
      if (!wx.getSetting || !wx.getSetting()) {
        // 微信测试版 获取授权API异常，目前只能即使没授权也可以通过
        resolve();
      }
      wx.getSetting().then((result) => {
        console.log("getSetting", result);
        this.authorizeMic = result.authSetting["scope.record"];
        this.authorizeCamera = result.authSetting["scope.camera"];
        if (
          result.authSetting["scope.camera"] &&
          result.authSetting["scope.record"]
        ) {
          // 授权成功
          resolve();
        } else {
          // 没有授权，弹出授权窗口
          // 注意： wx.authorize 只有首次调用会弹框，之后调用只返回结果，如果没有授权需要自行弹框提示处理
          console.log("getSetting 没有授权，弹出授权窗口", result);
          wx.authorize({
            scope: "scope.record",
          })
            .then((res) => {
              console.log("authorize mic", res);
              this.authorizeMic = true;
              if (this.authorizeCamera) {
                resolve();
              }
            })
            .catch((error) => {
              console.log("authorize mic error", error);
              this.authorizeMic = false;
            });
          wx.authorize({
            scope: "scope.camera",
          })
            .then((res) => {
              console.log("authorize camera", res);
              this.authorizeCamera = true;
              if (this.authorizeMic) {
                resolve();
              } else {
                this.openConfirm();
                reject(new Error("authorize fail"));
              }
            })
            .catch((error) => {
              console.log("authorize camera error", error);
              this.authorizeCamera = false;
              this.openConfirm();
              reject(new Error("authorize fail"));
            });
        }
      });
    });
  },
  openConfirm: function () {
    if (this.hasOpenDeviceAuthorizeModal) {
      return;
    }
    this.hasOpenDeviceAuthorizeModal = true;
    return wx.showModal({
      content: "您没有打开麦克风的权限，是否去设置打开？",
      confirmText: "确认",
      cancelText: "取消",
      success: (res) => {
        this.hasOpenDeviceAuthorizeModal = false;
        console.log(res);
        // 点击“确认”时打开设置页面
        if (res.confirm) {
          console.log("用户点击确认");
          wx.openSetting({
            success: (res) => {},
          });
        } else {
          console.log("用户点击取消");
        }
      },
    });
  },
  async getRemoteVisitsDetail() {
    const id = app.globalData.id;
    try {
      const { results } = await fhGetRemoteVisitsDetail(id);
      results.remoteVisitMemberDTOList.forEach((item) => {
          this.setData({
            familyId: item.memberId,
          });
       
      });
      this.setData({
        detail: results,
        visitTime: results.visitTime ,
        privacySwitch:results.privacySwitch
      });
    } catch (e) {
      wx.navigateTo({
        url: '/remote/patientsMember/management/index',  
      });
      console.log('退出');
      console.log("debug axios", e);
    }
    
  },
  // 页面载入时
  onLoad(e) {
    this.init(e);
 
  },
  // 页面初始化
  init(e) {},
  // 页面准备好时
  onReady() {},
  // 页面显示时
  onShow() {
    this.getRemoteVisitsDetail();
  },
  // 页面隐藏时
  onHide() {},
  // 页面卸载时
  onUnload() {},
  // 下拉页面时
  onPullDownRefresh() {},
  // 到达页面底部时
  onReachBottom() {},
  // 页面滚动时
  onPageScroll() {},
  // 分享时，注：onShareAppMessage不能为async异步函数，会导致不能及时取得返回值，使得分享设置无效
  onShareAppMessage() {
    /* const title = ''
    const path = ''
    const imageUrl = ``

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

Page(pageOptions);
