import { $http } from 'containers/config/https';
import { Util } from 'tools/index';
import React from 'react';
/**
 * 打开链接初始化
 * @param {*} urlName  // 具体页面名
 * @param {*} userData 登录用户数据
 * @param {*} otherData 其他参数
 * @returns
 */
export const initIframeUrl = (urlName, userData, otherData) => {
  const { protocol, host, hostname } = window.location;
  const titleData = React.$getSessionData('titleData');
  // const baseUrl = `https://172.16.1.6/EMRSys/#${urlName}`
  const baseUrl = ['localhost', '127.0.0.1'].includes(hostname)
    ? `http://localhost:5173/EMRSys/#${urlName}`
    : `${protocol}//${host}/EMRSys/#${urlName}`;
  const { historyAdmID, queID = '', paras, admID = '', token, recordID = '', hideSort = false, componentSource, areaRoomID } = otherData;
  const { patID = '', patName = '', } = titleData || {};
  const { hospCode, userCode, userName, locID, userID } = userData;
  const deptCode = otherData?.locID || locID;
  const params = paras?.params || componentSource?.paras;
  const groupCode = params ? Util.getValueByUrlStr(params, 'groupCode') : '';
  // 参数含义见飞书文档: https://dqqslxaq2y.feishu.cn/docx/YXNMd3FzZoz9TexPqpqcKbnznId
  return `${baseUrl}?admID=${historyAdmID || admID}&hospitalCode=${hospCode}&loginUserCode=${userCode}&userDesc=${userName}&deptCode=${deptCode}&token=${token}&groupCode=${groupCode}&userID=${userID}&queID=${queID}&areaRoomID=${areaRoomID}&recordID=${recordID}&hideSort=${hideSort}&patID=${patID}&patName=${patName}`;
}
/**
 * 通用获取token
 */
export const getEMRToken = (datetime = Date.now()) => {
  return new Promise((reslove, reject) => {
    let switchData = sessionStorage.getItem('newemr-switchData');
    if (switchData) {
      const { newEmrToken, useNewTime } = JSON.parse(switchData);
      reslove({
        newEmrToken,
        useNewEmr: useNewTime < new Date(datetime).getTime(),
      });
      return;
    }
    $http.post('urlS', this, {
      code: '06090011',
      data: {
        params: [{}],
      },
      success: (res) => {
        if (+res.errorCode === 0) {
          // SGSwitchDate 视光门诊切换时间
          const { token, SGSwitchDate } = res.result;
          const switchDate = SGSwitchDate && new Date(SGSwitchDate + ' 23:59:59').getTime();
          const data = {
            newEmrToken: token,
            useNewTime: switchDate,
            useNewEmr: switchDate < new Date(datetime).getTime(),
          }
          sessionStorage.setItem('newemr-switchData', JSON.stringify(data));
          reslove(data);
        }
      },
      fail: (err) => {
        console.error(err)
        reject(err);
      },
    })
  });
}
/**
 * 根据新病历数据对照返回老病历字段，页面不修改，防止后期不好维护
 * @param {*} data
 */
// https://dqqslxaq2y.feishu.cn/docx/FW98dhaUxoVwP7xOEHpcVmRzndb?302from=wiki
export const reverseData = (data) => {
  const obj = {
    dominantEye: "",
    mydriasisDrugName: "ComboBox12",
    optometryMethod: "",
    nakedVisionRight: "ComboBox2",
    sphericalMirrorRight: "field1",
    axialPositionRight: "field3",
    columnMirrorRight: "field2",
    nakedVisionLeft: "ComboBox9",
    sphericalMirrorLeft: "field6",
    columnMirrorLeft: "field7",
    axialPositionLeft: "field8",
    correctedVisionRight: "ComboBox7",
    correctedVisionLeft: "ComboBox8",
    prescriptionRemark: "ComboBox11",
    addValue: "field40",
    farCorrectedVisionRight: "ComboBox3",
    farPrismRight: "field15",
    farPrismBaseRight: "field16",
    farSphericalMirrorRight: "field11",
    farAxialPositionRight: "field14",
    farColumnMirrorRight: "field13",
    farCorrectedVisionLeft: "ComboBox4",
    farPrismLeft: "field20",
    farPrismBaseLeft: "field21",
    farSphericalMirrorLeft: "field17",
    farAxialPositionLeft: "field19",
    farColumnMirrorLeft: "field18",
    nearCorrectedVisionRight: "ComboBox5",
    nearPrismRight: "field26",
    nearPrismBaseRight: "field27",
    nearSphericalMirrorRight: "field23",
    nearAxialPositionRight: "field25",
    nearColumnMirrorRight: "field24",
    nearCorrectedVisionLeft: "ComboBox6",
    nearPrismLeft: "field31",
    nearPrismBaseLeft: "field32",
    nearSphericalMirrorLeft: "field28",
    nearAxialPositionLeft: "field30",
    nearColumnMirrorLeft: "field29",
    optometryRemark: "ComboBox10",
    optometryNo: "optometryNo",
    optometristDesc: "field39",
    specialLenses: "",
    farPupilDistance: "field42",
    farPupilDistanceLeft: "field43",
    nearSphericalMirror: "field44",
    nearSphericalMirrorLeft2: "field45",
    pupillaryHeight: "",
    handlingSuggestions: "",
    optometryDate: "field37",
    optometricState: 'optometryType',
  }
  const res = {};
  for (const key in data) {
    if (obj[key]) {
      res[obj[key]] = data[key]
    } else {
      res[key] = data[key];
    }
  }
  return res;
}
