// 处理时间轴不同类型的内容数据
export const handleSortData = function (data,loginCode) {
    /*
     * examination: "检查检验"; inspectionReport: "既往检查报告"; diagnose: "诊断"; medicalHistory: "既往病史"; appointment: "门诊预约"; mdt: "MDT";
     * follow: "随访"; progressNote: "病程记录"; inHospital: "入院"; outHospital: "出院"; operation: "手术史";
     * operationDoctorAdvice: "手术医嘱"; radiotherapy: "放疗史"; chemotherapy: "化疗史";  drug: "药物";drugMedicine: "当日药物医嘱"
     * otherAdvice: "其他医嘱"; image: "影像"; pathology: "病理";
     */
    const hData = data.map(item => {
      let info = {
        title: "",
        des: "",
        time: "",
        urls: "",
        noticed: '',
        noticeMsg: '',
        schemeOut: '',
        list: [],
        extUrl: '',
        other: {},
        data: {},
        fullTime: '',
        dateTime: '',
        category: '',
        subCategory: '',
        drugAliasList: [],
        dcmUrl: ''
      };
      info.dcmUrl = item.dcmUrl || '';
      info.title = item.title ? item.title : "";
      info.type = item.type ? item.type : "";
      info.extUrl = (item.data && item.data.externalUrl) ? item.data.externalUrl : "";
      info.data = item.data || {}
      info.dateTime = item.dateTime 
      info.date = item.dateTime.slice(0,10)
      info.category = item.category
      info.subCategory = item.subCategory 
      info.drugAliasList= item.drugAliasList? item.drugAliasList:[]
      return handleItem(info,item,loginCode)
    })
    hData.sort(function (a, b) {
        return new Date(b.dateTime).getTime() - new Date(a.dateTime).getTime();
    });
    return changeToArr(hData)
    return hData
  }
  // 处理时间获取当前时间
  export const handleConverDate = function () {
    let date = new Date();
    var chinese = [
      "",
      "一",
      "二",
      "三",
      "四",
      "五",
      "六",
      "七",
      "八",
      "九",
      "十",
      "十一",
      "十二",
    ];
    var y = date.getFullYear().toString();
    var m = date.getMonth().toString();
    var d = date.getDate().toString();
    var result = {};
    result.year = y;
    if (m.length == 2) {
      if (m.charAt(0) == "1") {
        result.month = "十" + chinese[Number(m.charAt(1)) + 1] + "月";
      }
    } else {
      m = Number(m);
      result.month = chinese[m + 1] + "月";
    }
    if (d.length == 2) {
      result.day =
        chinese[d.charAt(0)] === "一" ?
        "十" + chinese[d.charAt(1)] :
        chinese[d.charAt(0)] + "十" + chinese[d.charAt(1)];
    } else {
      result.day = chinese[d.charAt(0)];
    }
    return result;
  }
  // 住院病历预览
  const medicalPreviewUrl = function (data,loginCode) {
    return `http://10.72.2.60:8080/centerweb/freeLogin.jsp?jsessionids=Mobile001db2&code=${loginCode}&sendurl=http://10.72.2.60:8080/emrweb/new_version/print_view.html?id=${data.medicalId}&patientId=${data.patientId}&vaa07=${data.inpatientId}`;
  }
  // 每一项特殊处理
  const handleItem = function(data,item,loginCode) {
    if (item.category !== "writer") {
        // 判断是否打开弹窗/跳转页面  category !== "other_check_report" ||
        switch (item.subCategory) {
          /** 门诊/住院诊疗记录 */
          case "diagnose": // 诊断
            data.des = item.data ? item.data.diagnosesName || "" : "";
            data.urls = item.data ? item.data.reportUrl || "" : "";
            break;
          case "medicalHistory": // 既往病史
            data.des = item.data ? item.data.content || "" : "";
            data.urls = item.data ? item.data.reportUrl || "" : "";
            break;
          case "appointment": // 门诊预约
            data.title = item.data ?
              (item.data.deptName ? item.data.deptName : "") +
              (item.data.doctorName ?  "-" + item.data.doctorName : "") :
              "";
            data.urls = item.data ? item.data.reportUrl || "" : "";
            break;
          case "mdt": // MDT
            data.des =  item.data.reportUrl ? 'MDT咨询建议书' : item.data.conclusion;
            data.urls = item.data ? item.data.reportUrl || "" : "";
            break;
          case "follow": // 随访
            data.des = item.data ? item.data.content || "" : "";
            data.urls = item.data ? item.data.reportUrl || "" : "";
            break;
          case "progressNote": // 病程记录
            data.urls = item.data ? item.data.url || "" : "";
            data.urls = item.data ? item.data.reportUrl || "" : "";
            break;
          case "inHospital": // 入院
            data.des = item.data ? item.data.inHospitalDiag || "" : "";
            data.urls = item.data ? item.data.reportUrl || "" : "";
            break;
          case "outHospital": // 出院
            data.des = item.data ? item.data.inHospitalDiag || "" : "";
            data.urls = item.data ? item.data.reportUrl || "" : "";
            break;
            /** 手术/操作*/
          case "operation": // 手术史
            data.des = item.data ? item.data.desc || "" : "";
            data.urls = item.data ? item.data.reportUrl || "" : "";
            break;
          case "operationDoctorAdvice": // 手术医嘱
            data.des = item.data ? item.data.desc || "" : "";
            break;
            /** 治疗 */
          case "radiotherapy": // 放疗史
            data.urls = item.data ? item.data.reportUrl || "" : "";
            break;
          case "radiationTherapy": // 放射治疗
            data.schemeOut = item.schemeOut === '1' ? true : false;
            break;
          case "chemotherapy": // 化疗史
            data.urls = item.data ? item.data.reportUrl || "" : "";
            break;
          case "drug": // 药品
            data.des = item.data ?
              (item.data.drugName ? item.data.drugName : "") +
              (item.data.drugPerDose ?
                "每次" + item.data.drugPerDose + "," :
                "") +
              (item.data.route ? item.data.route + "," : "") +
              (item.data.drugUsingFreq || "") :
              "";
            data.urls = item.data ? item.data.reportUrl || "" : "";
            data.noticed = item.noticed || ''
            data.noticeMsg = item.noticeMsg || ''
            break;
          case "drugMedicine": // 药品医嘱
            data.des = item.data ? item.data.drugName || "" : "";
            data.noticed = item.noticed || ''
            data.noticeMsg = item.noticeMsg || ''
            break;
          case "otherAdvice": // 其他医嘱
            data.des = item.data ? item.data.content || "" : "";
            break;
            /** 影像 */
          case "image": // 影像*
            data.urls = item.data ? item.data.reportUrl || "" : "";
            data.des = item.data ? item.data.diagnosis || "" : "";
            break;
            /** 病理报告 */
          case "pathology": // 病理
            data.urls = item.data&&item.data.length ? item.data.reportUrl || "" : item.reportUrl || "";
            data.des = item.data ? item.data.diagnosis || "" : item.diagnosis || "";
            break;
            /** 检查报告 */
          case "examination":
            if(item.reportUrl!=''){
              data.urls =  item.reportUrl || "";
            }else{
              data.urls = item.data ? item.data.reportUrl || "" : "";
            }
            data.des = item.data ? item.data.diagnosis || "" : "";
            data.other = item.other ? item.other  : {};
            break;
            // 检验报告
          case "inspectionReport":
            data.urls = item.data ? item.data.reportUrl || "" : "";
            data.des = item.data ? item.data.result || "" : "";
            data.inspectionReportData = item.inspectionReportData;
            data.reportData = item.reportData;
            break;
          case "otherReport": // 既往报告
            data.urls = item.data ? item.data.reportUrl || "" : "";
            data.des = item.data ? item.data.result || "" : "";
            break;
        }
      } else {
        // 住院病历、门诊病历-his
        if ((item.subCategory !== "outpatients_emergency_case" && item.other && item.type !='外院') || (item.subCategory === "outpatients_emergency_case" && item.other && item.data.subType === 'case_his' )) { // his产生的病历
          let MedicalUrl = medicalPreviewUrl(item.other,loginCode);
          data.des = item.data ? item.data.content || "" : "";
          data.urls = MedicalUrl || "";
        // 门诊病历-实时门诊病历
        } else if (item.subCategory === "outpatients_emergency_case" && item.other && item.data && (item.data.writerType === 'outpatients_emergency_case'|| item.data.writerType === 'consultation_apply_form'|| item.data.writerType === 'consultation_record')) { // composer显示的实时门诊病历
          data.other.inpatientId = item.other.inpatientId;
          data.other.deptName = item.other.deptName;
          data.other.deptId = item.other.deptId;
          data.other.writerType = item.data.writerType;
          data.des = item.data ? item.data.content || "" : "";
        } else { // 门诊病历 || cm上传的url
          data.urls = item.data ? item.data.url || "" : "";
          data.des = item.data ? item.data.content || "" : "";
        }
      }
      return data
  }
  //按照分类排序
  const typeArr = ['image_report','pathology_report','other_check_report','outpatients_hospital_record','operationf','treatment','writer']
  const sortByCatalog = function(data){
    let arr = []
    typeArr.forEach(item => {
      if(item == 'outpatients_hospital_record'){
        const a = data.filter(i => i.category == item)
        let b = a.filter(i => i.subCategory == 'diagnose')
        let c = a.filter(i => i.subCategory != 'diagnose')
        b.reverse().forEach(i => {
          if (i.data.diagnosticType == "次要诊断") {
            let firstIndex = c.findIndex(
              option => option.data.visitCardNo == i.data.inpatientId
            );
            c.splice(firstIndex + 1, 0, i);
          }
        });
        let d = b.reverse().filter(i => i.data.diagnosticType == "主要诊断");
        d.forEach(i => {
          let firstIndex = c.findIndex(
            option => option.data.visitCardNo == i.data.inpatientId
          );
          c.splice(firstIndex + 1, 0, i);
        });
        // 过滤出外院诊断
        let e = b
        .reverse()
        .filter(i => i.data.diagnosticType === null);
        arr = arr.concat([...c,...e])
        // const d = b.filter(i => i.data.diagnosticType == "主要诊断")
        // const e = b.filter(i => i.data.diagnosticType != "主要诊断")
        // arr = arr.concat(c).concat(d).concat(e)
        // arr = arr.concat(data.filter(i => i.category == item))
      } else {
        arr = arr.concat(data.filter(i => i.category == item))
      }
    })
    return arr
  }
  const changeToArr = function(data) {
    let map = {}, result = [];
    for(let i = 0; i < data.length; i++){
        const ai = data[i];
        if(!map[ai.date]){
            result.push({
                date: ai.date,
                child: [ai]
            });
            map[ai.date] = ai;
        }else{
            for(let j = 0; j < result.length; j++){
                const dj = result[j];
                if(dj.date == ai.date){
                    dj.child.push(ai);
                    break;
                }
            }
        }
    }
    for(let k=0; k<result.length; k++){
      const child = result[k].child
      if(child.length > 1){
        result[k].child = sortByCatalog(child)
      }
    }
    return result
  }