import { getOutMedicalHistory,
  getOutMedicalDiagnosis,
  getOutMedicalAllergen,
  getOutMedicalVitalsigns,
  getOutMedicalPte, 
} from "@/request/medicalRecords";
export const handleSetMedicalData = async function (visitCardNo) {
  const res = await getOutMedicalHistory(visitCardNo);
  const DiagnosisRes = await getOutMedicalDiagnosis(visitCardNo);
  const Allergen = await getOutMedicalAllergen(visitCardNo);
  const Vitalsigns = await getOutMedicalVitalsigns(visitCardNo);
  const Pte = await getOutMedicalPte(visitCardNo);
  let MedicalInfo = {}
  if (res.code === 0 && res.data) {
    // 获取患者基本信息
    MedicalInfo.patientId = res.data.patientBase.healthCardNo || "";
    MedicalInfo.patientName = res.data.patientBase.patientName || "";
    MedicalInfo.sex = res.data.patientBase.gender || "";
    MedicalInfo.age = res.data.patientBase.age || "";
    MedicalInfo.year = res.data.patientBase.birthday || "";
    MedicalInfo.nation = res.data.patientBase.nation || "";
    MedicalInfo.profession = res.data.patientBase.profession || "";
    MedicalInfo.marriage = handleSetMarriage(
      res.data.patientBase.marriage
    );
    MedicalInfo.paymentType = res.data.patientBase.paymentType || "";
    MedicalInfo.workUnit = res.data.patientBase.workUnit ?
      res.data.patientBase.homeAddress :
      res.data.patientBase.workUni
    MedicalInfo.allergyCon = []
    MedicalInfo.allergyText = ''
    if (res.data.cert.allergy) {
      MedicalInfo.allergyText = "否认过敏史";
    } else {
      MedicalInfo.allergyCon = Allergen.data.allergen || [];
    }
    MedicalInfo.todayCheckRemark = Pte.data.ptePrint || ''
    MedicalInfo.date = (res.data.cert && res.data.cert.dateTime) ? res.data.cert.dateTime.slice(0, 16) : '';
    MedicalInfo.deptName = res.data.cert.deptName || "";
    // 主诉
    MedicalInfo.mainSuit = res.data.chiefComplaint || "";
    // 现病史
    MedicalInfo.historyIllness = res.data.hpi ?
      res.data.hpi.content || "" :
      "";
    // 既往史
    MedicalInfo.pastHistory = res.data.pmh ? res.data.pmh.content || "" : "";
    // 个人史
    MedicalInfo.personalHistory = res.data.pph ?
      res.data.pph.content || "" :
      "";
    // 家族史
    MedicalInfo.familyHistory = res.data.pfh ?
      res.data.pfh.content || "" :
      "";
    // 体格检查
    MedicalInfo.physicalExamination = res.data.ppe ?
      res.data.ppe.content || "" :
      "";
    // 月经/婚育史
    MedicalInfo.marryHistory = res.data.pm ? res.data.pm.content || "" : "";
     //现病史及病情
     MedicalInfo.hpi=res.data.hpi || {};
     //月经/婚育史
     MedicalInfo.pm=res.data.pm || {};
     //体格检查
     MedicalInfo.ppe=res.data.ppe || {};
    // 辅助检查结果
    // MedicalInfo.supplementaryExamination = res.data.pte || [];
    // MedicalInfo.todayCheckRemark = Pte.data.ptePrint || "";
    // 诊断
    // 重置诊断内容
    // console.log('诊断',res.data)
    MedicalInfo.diagnose = '';
    MedicalInfo.firstDiagnose = ''
    if (res.data.ppd && res.data.ppd.content) {
      MedicalInfo.diagnose += res.data.ppd.content + ";";
      MedicalInfo.firstDiagnose = res.data.ppd.content;
    }
    // 体格检查
    if (Vitalsigns.data.vitalsigns && Vitalsigns.data.vitalsigns.length) {
      let list = [];
      Vitalsigns.data.vitalsigns.forEach(item => {
        if(item.currentVisit) {
          list.push(item)
        }
      })
      let val = handleSetvitalSignsData(MedicalInfo.age, MedicalInfo.sex, list)
      let physicalExamination =
        (val.temperature > 0 ? "体温：" + val.temperature + "℃," : "") +
        (val.weight > 0 ? "体重:" + val.weight + "kg," : "") +
        (val.stature > 0 ? "身高:" + val.stature + "cm," : "") +
        (val.bmi ? "BMI:" + val.bmi + "kg/㎡," : "") +
        (val.bsa ? "BSA:" + val.bsa + "㎡," : "") +
        ((val.systolicPressure > 0 || val.diastolicPressure > 0) ? "血压:" + (val.systolicPressure > 0 ? val.systolicPressure : "") + "mmHg," : "") + "/" +  (val.diastolicPressure > 0 ? val.diastolicPressure : "")+
        (val.pulseRate ? "脉搏:" + val.pulseRate + "次/min," : "");
      // (val.respiratoryRate ? ('呼吸:'+val.respiratoryRate + '次/min,') :'');
      // +(val.odi ? ( '血氧饱和度:'+val.odi + '%,') :'')+(val.bloodSugar ? ( '血糖:'+val.bloodSugar + 'mmol/L。') :'');
      MedicalInfo.physicalExamination =
        physicalExamination + MedicalInfo.physicalExamination;
    }
    if (DiagnosisRes.data.diag && DiagnosisRes.data.diag.length) {
      DiagnosisRes.data.diag.forEach((item) => {
        MedicalInfo.diagnose +=((item.sort && item.diagnosesName) ? (item.sort+'.' +item.diagnosesName)  : "") + "  ";
      });
    }
    // 诊疗意见
    MedicalInfo.medicalOpinion = res.data.pdd ?
      res.data.pdd.content || "" :
      "";
  }
  return MedicalInfo
}
 // 获取患者有用的生命体征数据
export const  handleSetvitalSignsData = function (age,sex,arr) {
  let info = {},list = []
  arr.forEach((item) => {
    if (age && sex && (item.stature > 0 || item.weight > 0 || item.temperature > 0 || item.pulseRate > 0 || item.diastolicPressure > 0 || item.systolicPressure > 0)) {
      let { bmi, bsa } = handleSetBsa( age, sex, item.stature, item.weight);
      list.push({
        ...item,
        temperature: item.temperature || "",
        weight: item.weight > 0 ? item.weight : "",
        stature: item.stature > 0 ? item.stature : "",
        systolicPressure: item.systolicPressure || "",
        diastolicPressure: item.diastolicPressure || "",
        pulseRate: item.pulseRate || "",
        bmi: bmi,
        bsa: bsa,
      });
    }
  });
  info = list.length > 0 ? list[list.length -1] : {};
  return info
}
// 设置婚姻状况
export const  handleSetMarriage = function (marriage) {
  let marriageInfo = "";
  if (marriage == 1) {
    marriageInfo = "未婚";
  } else if (marriage == 2) {
    marriageInfo = "已婚";
  } else if (marriage == 3) {
    marriageInfo = "丧偶";
  } else if (marriage == 4) {
    marriageInfo = "离婚";
  } else {
    marriageInfo = "其他";
  }
  return marriageInfo;
}
export const  handleSetBsa = function (age, sex, stature, weight) {
  let info = {
    bsa: "",
    bmi: "",
  };
  if (age <= 14 && stature > 0 && weight > 0) {
    info.bsa = 0.0061 * stature + 0.0128 * weight;
  } else if (stature > 0 && weight > 0) {
    if (sex === "F") {
      info.bsa = 0.00586 * stature + 0.0126 * weight;
    } else {
      info.bsa = 0.00607 * stature + 0.0127 * weight;
    }
  }
  let statureTwo = Math.pow(stature / 100, 2);
  info.bsa = Math.floor(info.bsa * 100) / 100;
  if (stature > 1 && weight > 1) {
    info.bmi = Math.floor((weight / statureTwo) * 100) / 100;
  }
  info.bmi = info.bmi === Infinity ? "" : info.bmi;
  info.bsa = info.bsa === Infinity ? "" : info.bsa;
  return info;
}