"use strict";
const common_vendor = require("../common/vendor.js");
const menuClick = async (e, _this, skipWay = "navigateTo") => {
  let itemParent = e.currentTarget ? e.currentTarget.dataset.itemParent : {};
  let item = e.currentTarget ? e.currentTarget.dataset.item : e;
  let url = item.Url;
  if (getApp().globalData.hasDistrict && item.MenuName == "预约挂号") {
    url = `/pagesPatient/st1/business/otherService/hospitalDistrict/hospitalDistrict`;
  }
  if (itemParent && itemParent.IsUse && itemParent.IsUse != "1" || item.IsUse && item.IsUse != "1") {
    url = `/pages/st1/business/otherService/building/building`;
    common_vendor.goToUrl(url);
    return;
  }
  if (item.MenuName == "当日挂号") {
    try {
      const limitDistance = getApp().globalData.config.pageConfiguration.signInList_config.signDistance || 1;
      await getHospitalLocationDistance(limitDistance);
    } catch (err) {
      await new Promise((resolve) => {
        common_vendor.showModal("需到院后才能挂当日号，且不支持无就诊卡进行挂号操作", resolve, {
          title: "温馨提示"
        });
      });
    }
  }
  if (item.MenuName == "来院导航") {
    getLocation();
    return;
  }
  if (item.AppId) {
    common_vendor.wx$1.navigateToMiniProgram({
      appId: item.AppId,
      path: item.Url
    });
    return;
  }
  if (url.startsWith("https")) {
    common_vendor.goToUrl(`/pages/st1/business/h5/h5?url=${encodeURIComponent(url)}`);
    return;
  }
  if (!common_vendor.isEmpty(item.Children)) {
    let queryBean = JSON.stringify(item);
    url = `${item.Url}`;
    getApp().globalData.selectUrl_x = queryBean;
  }
  let needMemberMenu = [
    "rechargeMoney",
    // 充值缴费
    "reportIndex",
    // 报告查询
    "appointmentRecord",
    // 预约记录
    "outpatientCosts",
    // 门诊清单
    "hospitalCosts",
    // 住院清单
    "cardRecord",
    // 门诊记录
    "hosRecord",
    // 住院记录
    "supplement",
    // 互联网医院提交咨询
    "enquireList",
    // 互联网医院咨询记录
    "doctorLike",
    // 互联网医院关注医生
    "signInList",
    // 在线签到
    "queueList",
    // 候诊查询
    "orderPayment",
    // 门诊结算
    "prescriptionList",
    // 当日处方
    "paymentRecord",
    // 缴费记录
    "personalCenter",
    // 个人中心
    "continuationList",
    // 在线续方
    "drugCredentials",
    // 取药凭证
    "settlementRecord",
    // 结算记录
    "waitRecord",
    // 候补记录
    "dischargeMedication",
    // 出院带药
    "home",
    // 我的随访
    "chat-room",
    // AI智能就医助手
    "topUpRecord",
    //充值记录
    "newPrescriptionList",
    //当日处方2.0
    "prescriptionRecipe",
    //处方取药2.0
    "medicalTech",
    // 医技预约
    "outpatientRefund",
    //在线退费
    "selfProject",
    // 自助开单
    "mdtList",
    // MDT列表
    "recordList",
    // MDT记录
    "diseaseIndex"
    // 罕见病
  ];
  let has = false;
  for (let i of needMemberMenu) {
    if (url.indexOf(i) != "-1") {
      has = true;
      break;
    }
  }
  if (has) {
    if (common_vendor.useIsToAuthPage()) {
      return;
    }
    if (await toAddMemberOrNot(url)) {
      return;
    }
  }
  common_vendor.goToUrl(url, {
    skipWay
  });
};
const getLocation = () => {
  let hospitalInfo = getApp().globalData.hospitalInfo;
  let data = {
    latitude: Number(hospitalInfo.Wd),
    //要去的纬度-地址
    longitude: Number(hospitalInfo.Jd),
    //要去的经度-地址
    name: hospitalInfo.HospitalAlias,
    address: hospitalInfo.HospitalAddress
  };
  locationIsauthorization("gcj02", () => {
    common_vendor.index.openLocation(data);
  });
};
const locationIsauthorization = (type, callBack) => {
  common_vendor.index.getLocation({
    type,
    success: function(res) {
      callBack(res);
    },
    fail: function(err) {
      if (err.errMsg.includes("1")) {
        common_vendor.showToast("访问个人地址失败，请查看是否开启地理位置权限");
      } else {
        common_vendor.showModal(
          "位置信息获取失败，请打开应用权限",
          () => {
            common_vendor.index.openSetting({
              success: function(res) {
                res.authSetting["scope.userLocation"];
              }
            });
          },
          {
            cancelText: "取消",
            confirmText: "打开"
          }
        );
      }
    }
  });
};
const getHospitalLocationDistance = async (limitDistance = 1) => {
  const { hospitalInfo } = getApp().globalData;
  if (common_vendor.isEmpty(hospitalInfo)) {
    common_vendor.showModal(`获取医院信息失败`);
    return;
  }
  const { Wd: hosLat, Jd: hosLng } = hospitalInfo;
  if (common_vendor.isEmpty(hosLat) || common_vendor.isEmpty(hosLng)) {
    common_vendor.showModal(`获取医院配置失败`);
    return;
  }
  return new Promise((resolve, reject) => {
    locationIsauthorization("gcj02", (res) => {
      let distance = common_vendor.getDistance(res.latitude, res.longitude, hosLat, hosLng);
      distance > limitDistance ? reject() : resolve(true);
    });
  });
};
const toAddMemberOrNot = async (url) => {
  let cardType = "1";
  if (getApp().globalData.withoutCard) {
    let needCardArr = ["appointmentRecord", "signInList", "settlementRecord"];
    for (let i of needCardArr) {
      if (url.indexOf(i) != "-1") {
        cardType = "";
        break;
      }
    }
  }
  let needHosArr = [
    "rechargeMoney?pageType=zyjf",
    "reportIndex?queryType=2",
    "hospitalCosts",
    "hosRecord",
    "dischargeMedication"
  ];
  for (let i of needHosArr) {
    if (url.indexOf(i) != "-1") {
      cardType = "14";
      break;
    }
  }
  let member = await common_vendor.useGetDefaultMember({
    isKeepPage: true,
    cardType,
    url
  });
  if (common_vendor.isEmpty(member)) {
    if (member !== null) {
      getApp().globalData.toUrl = url;
      getApp().globalData.cardType = cardType;
      common_vendor.goToUrl(
        `/pagesPersonal${common_vendor.index.getStorageSync("wx_Slb") ? "Slb" : ""}/st1/business/patientManagement/addMember/addMember`
      );
    }
    return true;
  }
  return false;
};
exports.menuClick = menuClick;
//# sourceMappingURL=../../.sourcemap/mp-weixin/utils/index.js.map
