import {get, post, json} from "../../utils/ajax";
import {GET_PATIENT_MESSAGE} from "../types/patInfo";
import {CONFIRM_TYPE} from "../types/typeConfig";
import {getInfos} from '@store/actions/getInfoByUuid';
import {getUrlArgObject,pushAllDataList} from '@utils/tools';
import {getInitModules,getCommonList} from '@store/async-actions/homePage.js';
import store from '@store'
import {ISREAD,MODI_LOADING} from "../types/homePage";
import { initItemList } from '@store/async-actions/tabTemplate';

const api = {
    getPatInfo: '/patientInfo/getTopPatientInfo',
    getPatBaseInfo: '/patientInfo/getPatientInfo'
};

//参数转换
export const initPersonInfo = (dispatch, getState) => {
    json('/tranFieldInfo/getInfoByUuid', {
        "uuid": getUrlArgObject('hospitalId'),
    }).then((res) => {
        const data = res.data;
        if (data.code == 0) {
            dispatch(getInfos(data.data));
            getPatientMessage(dispatch, getState);
        } else {
            console.log(res)
        }
    })
}

//默认回读
export const initHistoryDetails = (dispatch) => {
    let baseList = store.getState();
    let state = baseList.patInfo.message;
    return (dispatch) => {
        json('/inquiryInfo/readInquiry', {
            "doctorId": state.doctorId,
            "hospitalDeptId": state.hospitalDeptId,
            "hospitalId": state.hospitalId,
            "patientId": state.patientId, //患者id
            "inquiryCode": state.recordId,
        }).then((res) => {
            const data = res.data;
            if (data.code == 0) {
                const detail = data.data;
                pushAllDataList(detail.sign,'push',detail,'history');
                dispatch({
                  type:ISREAD
                });
                // 设置初始模式
                if(detail.sign == 1 || detail.sign == 0){
                    dispatch({
                        type: CONFIRM_TYPE,
                        confirmType: detail.sign
                    })
                    if(baseList.tabTemplate.items.length == 0){
                      dispatch(initItemList());        //模板列表没有就获取模板列表用于保存时做数据对比是否保存过
                    }
                }else{
                  dispatch(initItemList(baseList.typeConfig.typeConfig))
                }
                dispatch({type:MODI_LOADING,flag:false});
            } else {
                dispatch({type:MODI_LOADING,flag:false});
                console.log(res)
            }
        })
    }
}
//技术部需求，先获取第一个接口再去第二个
export async function getPatientMessage(dispatch, getState){
  const state = getState();
  const urlDatas = state.getInfoByUuid;
  const params = {
    hospitalCode:urlDatas.hospitalId,
    hospitalDeptCode: urlDatas.deptNo,
    doctorCode: urlDatas.doctorNo,
    patientCode:urlDatas.patientNo,
    recordId:urlDatas.recordId
  };
  let res1 = await json(api.getPatInfo, params);
  /*console.log(res1);
  console.log(1);*/
  let res2 = await json(api.getPatBaseInfo, params);
  /*console.log(res2);
  console.log(2);*/
  let data1 = {},data2={};
  if(res1.data.code=='0'){
    data1 = res1.data.data;
  }
  if(res2.data.code=='0'){
    data2 = res2.data.data;
  }
  dispatch({
    type: GET_PATIENT_MESSAGE,
    data:Object.assign(data1,data2)
  });
  //设置初始模式
  dispatch({
    type: CONFIRM_TYPE,
    confirmType:data1.modeValue
  });
  dispatch(getInitModules);     //确保病人信息获取以后再获取模板等数据，否则参数为空
  dispatch(getCommonList(5));     //获取常用化验标签列表
  dispatch(getCommonList(6));     //获取常用辅检标签列表if(allModules.length == 0){//模板为空才回读
  dispatch(initHistoryDetails());      //历史病历回读
}

/*
export const getPatientMessage = (dispatch, getState) => {
  const state = getState();
  const urlDatas = state.getInfoByUuid;
  const params = {
    hospitalCode:urlDatas.hospitalId,
    hospitalDeptCode: urlDatas.deptNo,
    doctorCode: urlDatas.doctorNo,
    patientCode:urlDatas.patientNo,
    recordId:urlDatas.recordId
  };
  // 获取医生，患者基本信息
  Promise.all([json(api.getPatInfo, params),json(api.getPatBaseInfo, params)])
    .then((data) => {
      let data1 = {},data2={};
      if(data[0].data.code=='0'){
        data1 = data[0].data.data;
      }
      if(data[1].data.code=='0'){
        data2 = data[1].data.data;
      }
      dispatch({
        type: GET_PATIENT_MESSAGE,
        data:Object.assign(data1,data2)
      });
      //设置初始模式
      dispatch({
        type: CONFIRM_TYPE,
        confirmType:data1.modeValue
      });
      dispatch(getInitModules);     //确保病人信息获取以后再获取模板等数据，否则参数为空
    })
};*/
