import axios from '@utils/ajax'
import {
  initItems,
  delItems,
  batchDelItems,
  changeTitle,
  keepPushData,
  changeVisible
} from '@store/actions/tabTemplate';
import {DIAG_SHOW} from "@store/types/print";
import Notify from '@commonComp/Notify';
import store from '@store';
import {
  getAllDataList,
  getAllDataStringList
} from '@utils/tools';

export const initItemList = (type) => { //初始化数据
  let baseList = store.getState();
  let whichSign = baseList.typeConfig.typeConfig;
  let state = baseList.patInfo.message;

  return (dispatch) => {
    axios.json('/templateInfo/getByDoctorIdTemplates', {
      "doctorId": state.doctorId,
      "hospitalDeptId": state.hospitalDeptId,
      "hospitalId": state.hospitalId,
      "type": type || whichSign,
    }).then((res) => {
      const data = res.data;
      if (data.code == 0) {
        dispatch(initItems(data));
      } else {
        Notify.error(data.msg)
      }
    })
  }
};

export const saveTemplateDetail = (val,sex) => { //保存为模板
  let baseList = store.getState();
  let jsonData = getAllDataList(baseList);
  let jsonStr = getAllDataStringList(baseList);
  let whichSign = baseList.typeConfig.typeConfig;
  let state = baseList.patInfo.message;
  let preview = {
    "chief": jsonStr.chief,
    "present": jsonStr.present,
    "other": jsonStr.other,
    "vital": jsonStr.vital,
    "lis": jsonStr.lis,
    "pacs": jsonStr.pacs,
    "diag": jsonStr.diag,
    "advice": jsonStr.advice,
  }
  function getdata(idx){
    let tmpObj = {
      "doctorId": state.doctorId,
      "hospitalDeptId": state.hospitalDeptId,
      "hospitalId": state.hospitalId,
      "dataJson": JSON.stringify(jsonData),
      "modeName": val,
      "modeType": whichSign,
      "preview": idx ? JSON.stringify(preview):preview,
      "sex":sex
    }
    return tmpObj;
  }
  return (dispatch) => {
    axios.json('/templateInfo/saveTemplateInfo', getdata()).then((res) => {
      const data = res.data;
      if (data.code == 0) {
        Notify.success('模板保存成功');
        dispatch(initItemList());
        dispatch(keepPushData(getdata(1),'part'));
        dispatch({
          type: DIAG_SHOW,
          data:false
        });
      } else {
        if(data.msg == '该模板名存在'){         //存在不关闭弹窗
          dispatch({
            type: DIAG_SHOW,
            data:true
          });
        }else{
          dispatch({
            type: DIAG_SHOW,
            data:false
          });
        }
        Notify.error(data.msg);
      }
    })
  }
};

export const delItem = (id) => { //删除
  return (dispatch) => {
    axios.json('/templateInfo/cancelTemplateInfos', {
      ids: id
    }).then((res) => {
      let data = res.data;
      if (data.code == 0) {
        dispatch(delItems(id));
        Notify.success('模板删除成功');
      } else {
        Notify.error(data.msg);
      }
    })
  }
};
export const delBatchItem = (ids) => { //批量删除
  return (dispatch) => {
    axios.json('/templateInfo/cancelTemplateInfos', {
      ids: ids.join(",")
    }).then((res) => {
      let data = res.data;
      if (data.code == 0) {
        dispatch(batchDelItems(ids));
      } else {
        Notify.error(data.msg);
      }
    })
  }
};
export const changeTitleAsync = (obj) => { //改标题
  let baseList = store.getState();
  let whichSign = baseList.typeConfig.typeConfig;
  let state = baseList.patInfo.message;
  if (obj.title == '') {
    Notify.success('请输入模板名称');
    return;
  }
  return (dispatch) => {
    axios.json('/templateInfo/updateByIdUsNames', {
      "doctorId": state.doctorId,
      "hospitalDeptId": state.hospitalDeptId,
      "hospitalId": state.hospitalId,
      "id": obj.id,
      "modeName": obj.title,
      "type": whichSign
    }).then((res) => {
      let data = res.data;
      if (data.code == 0) {
        dispatch(changeTitle(obj));
        Notify.success('标题修改成功');
        store.dispatch(changeVisible(false))
      } else {
        console.log(data)
        Notify.error(data.msg)
      }
    })
  }
};