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} from '@store/async-actions/homePage.js';
import store from '@store'
import {ISREAD} from "../types/homePage";

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

//参数转换
export const initPersonInfo = (dispatch, getState) => {
    json('/api/icss/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('/api/icss/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){
                    dispatch({
                        type: CONFIRM_TYPE,
                        confirmType: 1
                    })
                }else if(detail.sign == 0){
                    dispatch({
                        type: CONFIRM_TYPE,
                        confirmType: 0
                    })
                }
            } else {
                console.log(res)
            }
        })
    }
}

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
      });
    })
};