import {connect} from 'react-redux';
import CheckBody from '@components/CheckBody';
import {getModule,getInitData,pregetCheckbodyData} from '@store/async-actions/fetchModules.js';
import {HIDE,RESET,SETDROPSHOW,ISREAD,MODI_LOADING,HIDEDROP,SET_CURRENT_MODULE} from '@store/types/homePage.js';
import {SELECTSEARCHDATA,REPUSH_CHECK_LABELS} from "@store/types/checkBody";
import {billing,getMRAnalyse} from '@store/async-actions/pushMessage';
import {didPushParamChange,filterDataArr} from '@utils/tools.js';
import store from '@store';

function mapStateToProps(state){ 
  const {homePage,mainSuit,checkBody,diagnosticList} = state;
  const hasMain = filterDataArr(mainSuit.saveText);//||mainSuit.data.length;
  let diagnC = diagnosticList.chronicMagItem;
  return {
    data:checkBody.data,
    defaultShowAll:checkBody.showAll,
    isEmpty:checkBody.isEmpty,
    update:checkBody.update,   //用于触发更新
    showArr:homePage.showDrop,
    totalHide:homePage.totalHide,
    saveText:checkBody.saveText,
    searchData:checkBody.searchData,    //延迟搜索结果
    hasMain,//主诉选中的数据
    focusTextIndex:checkBody.focusIndex,    //聚焦的自由文本标签index
    span:checkBody.span,
    searchInEnd:checkBody.searchInEnd,      //是否在搜索末尾插入结果
    importLabel:checkBody.importLabel,    //需高亮的标签id
    typeConfig: state.typeConfig,  //bug2764
    isChronic:diagnC?diagnC:state.mainSuit.chronicDesease,
  }
}

function mapDispatchToProps(dispatch){
  return {
    getInit(){
      //dispatch({type:MODI_LOADING,flag:true});
      dispatch(getInitData());
    },
    fetchModules(param){
      const {id,name,index,span,searchInEnd} = param;
      getModule(id).then((res)=>{
        if(res.data.code=='0'){
          dispatch({
            type:SELECTSEARCHDATA,
            index,
            name,
            data: res.data.data,
            span,
            isReplace:false,
            searchInEnd
          });
          dispatch({
            type:ISREAD
          });
          //调右侧推送
          if(didPushParamChange()) {
            dispatch(billing());
          }
        }
      });
    },
    rePush(){
      const storeState = store.getState()
      const {moduleName} = storeState.homePage
      dispatch(pregetCheckbodyData(false,true));
      if(moduleName != '查体') {
        dispatch({
          type: SET_CURRENT_MODULE,
          moduleName: '查体'
        });
        dispatch(getMRAnalyse());
      }
    },
    hideAllDrop(){
      dispatch({
        type:HIDEDROP
      });
    },
  }
}

const CheckBodyCont = connect(mapStateToProps,mapDispatchToProps)(CheckBody);

export default CheckBodyCont;