import {
    json
} from "../../utils/ajax";
import store from '@store';
import Notify from '@commonComp/Notify';
import {
    getAllDataList,
    getAllDataStringList,
    pushAllDataList,
    filterDataArr
} from '@utils/tools';

export const saveMessage = (bool) => {
    let baseList = store.getState();
    let state = baseList.patInfo.message;
    let whichSign = baseList.typeConfig.typeConfig;
    const pushMessage = baseList.pushMessage;
    let jsonStr = getAllDataStringList(baseList);
    let jsonData = getAllDataList(baseList);
    const lisData = baseList.inspect.pushItem;
    const lisArr = transferIndexData(lisData);
    const evaluation={
      htmlContent:JSON.stringify(getEvaluations(baseList.assessResult,pushMessage)),
      //scaleList:[]
    };//getEvaluation();
    //console.log(baseList)
    // console.log(jsonStr.diag,'保存历史病历')
    json('/inquiryInfo/saveInquiry', {
        "doctorId": state.doctorId,
        "hospitalDeptId": state.hospitalDeptId,
        "hospitalId": state.hospitalId,
        "patientId": state.patientId,         //患者id              
        "dataJson": JSON.stringify(getAllDataList(baseList)),
        "indexData":lisArr,
        "inquiryEvaluation":evaluation,
        "detailList": [{
            "content": jsonStr.chief,
            "contentValue":filterDataArr(JSON.parse(jsonStr.chief)),
            "type": 1
        }, {
            "content": jsonStr.present,
            "contentValue":filterDataArr(JSON.parse(jsonStr.present)),
            "type": 2
        }, {
            "content": jsonStr.other,
            "contentValue":filterDataArr(JSON.parse(jsonStr.other)),
            "type": 3
        }, {
            "content": jsonStr.vital,
            "contentValue":filterDataArr(JSON.parse(jsonStr.vital)),
            "type": 4
        }, {
            "content": jsonStr.lis,
            "contentValue":jsonStr.lis,
            "type": 5
        }, {
            "content": jsonStr.pacs,
            "contentValue":jsonStr.pacs,
            "type": 6
        }, {
            "content": jsonStr.diag,
            "contentValue":jsonStr.diag,
            "type": 7
        }, {
            "content": jsonStr.advice,
            "contentValue":jsonStr.advice,
            "type": 8
        }],
        "sign": whichSign, //类型0结构化 1文本
        "diagnose": (jsonData.diag.length > 0 ? jsonData.diag[0].name : ''), //诊断
        "inquiryCode": state.recordId, //就诊序列号
        "drugList":baseList.pushMessage.advice.drugList || [],//药品列表
        // "regVisitedState": 0,      //就诊状态
        // "type": 0,
    }).then((res) => {
        let data = res.data
        if (data.code == 0) {
            if(!bool){
                Notify.success('历史病历保存成功');
            }
        } else {
            console.log(res)
        }
    })
    if(bool){
        json('/printRecord/savePrintRecords', {
            "dataJson": JSON.stringify(getAllDataList(baseList)),
            "doctorId": state.doctorId,
            "hospitalDeptId": state.hospitalDeptId,
            "hospitalId": state.hospitalId,
            "inquiryCode": state.recordId, //就诊序列号
            "name": (jsonData.diag.length > 0 ? jsonData.diag[0].name : ''), //诊断
            "patientId": state.patientId, //患者id              
            "type": whichSign, //类型0结构化 1文本
            "content": {
                "advice": jsonStr.advice,
                "chief": jsonStr.present,
                "diag": jsonStr.diag,
                "lis": jsonStr.lis,
                "other": jsonStr.other,
                "pacs": jsonStr.pacs,
                "present": jsonStr.present,
                "vital": jsonStr.vital
            },
        }).then((res) => {
            let data = res.data
            if (data.code == 0) {
                console.log('打印记录保存成功')
            } else {
                console.log(res)
            }
        })
    }
};

//转换化验指标保存需要的数据
function transferIndexData(data) {
    const arr=[];
   data.map((it)=>{
       if(it.uniqueName&&it.value!=''){
           arr.push({
             creatTime:it.time+':00',
             indexUnique:it.uniqueName,
             indexUnit:it.units,
             indexValue:it.value,
             isAbnormal:(it.maxValue||it.minValue)?(it.value<it.minValue||it.value>it.maxValue?1:0):0
           });
       }
   });
   return arr;
}

function getEvaluations(base,pushMessage){
    let obj = [];
    Object.keys(base.wholeIndexs).map((i)=>{
      obj[i]=pushMessage.chronicPushItems[i];
    });
    return {
      data:base.data,
      chooseSelecteds:base.chooseSelecteds,
      indexTimeTypes:base.indexTimeTypes,
      wholeAssess:obj,
      wholeAssessText:base.wholeAssessText,
      wholeIndexs:base.wholeIndexs,
      scaleInfo:pushMessage.scaleInfo,
      scaleResult:pushMessage.formulaResult,
      wholeResults:base.wholeResults
    };
}

export const clearMessages = () => {
    let baseList = store.getState();
    let whichSign = baseList.typeConfig.typeConfig;
    pushAllDataList(whichSign,'clear');
};