import axios from '@utils/ajax';
import {
  pushAllDataList,
  didPushParamChange,
  pushHistoryDataList,
  getUrlArgObject,
} from '@utils/tools';
import { initHistory } from '@store/actions/historyTemplates';
import { HISTORY_GETHOSPITALINFO } from '@store/types/historyTemplates';
import { embedPush } from '@store/async-actions/pushMessage';
import Notify from '@commonComp/Notify';
import store from '@store';

export const initItemList = (item) => {
    let baseList = store.getState();
    const { homePage:{curHos}} = baseList
    let params = {
      sex: '',
      name: '',
      current: 1,
      size: 9999,
      desc : ['gmt_create'],
      hospitalId:curHos.hosId
    }; 
    if (item === 'up') {
      params.desc = ['gmt_create'];
    }else if(item === 'down'){
      delete params.desc;
      params.asc = ['gmt_create'];
    }
    return axios.json('/demo/templateInfo/getTemplatePageAlls', params);
};
export const getHistempDetail = (item) => {
  const param = {
      "id": item.id
    }
    return (dispatch) => {
       axios.json('/demo/templateInfo/getTemplatebyId',param).then((res)=>{        
            const data =res.data;
            if(data.code == 0){
                let tmpData = data.data
                pushHistoryDataList(tmpData);       //引用
                // 引用成功后默认推送
                store.dispatch(
                  embedPush({
                    action: 'patientia',
                    mode: 1,
                  })
                );
                
            }else{
                Notify.error(data.msg);
            }
        })
    }
};

export function setInitHistory(data){
  return (dispatch) => {
    dispatch(initHistory(data));
  }
}


export const asyncUpdateByIdUsNames = (editId, templateName) => {
  let baseList = store.getState();
  const { homePage:{curHos}} = baseList
  return (dispatch) => {
    return new Promise((resolve, reject)=>{
      let param = {
        id: editId,
        modeName: templateName,
        doctorId: '',
        hospitalId:curHos.hosId
      }
      axios.json('/demo/templateInfo/updateByIdUsNames', param).then(res=>{
        if (res.data.code === '0'){
          resolve(res)
        }else{
          reject(res.data)
        }
      })
    })
  }
}

export const asyncCancelTemplateInfos = (delId) => {
  let baseList = store.getState();
  const { homePage:{curHos}} = baseList
  return (dispatch) => {
    return new Promise((resolve, reject) => {
      let param = {
        ids: delId,
        hospitalId:curHos.hosId
      }
      axios.json('/demo/templateInfo/cancelTemplateInfos', param).then(res => {
        if (res.data.code === '0') {
          resolve(res)
        } else {
          reject(res.data)
        }
      })
    })
  }
}


// 获取医院信息
export const getHospitalInfo = () =>{
  return (dispatch) =>{
    let baseList = store.getState();
    const { homePage:{curHos}} = baseList
    axios.json('/tran/hospitalInfo/getHospitalInfoById', { id: curHos.hosId }).then(res => {
      if (res.data.code === '0') {
        dispatch({
          type: HISTORY_GETHOSPITALINFO,
          data: res.data.data,
        });
      }
    });
  }
  
}

