import React from 'react';
import {connect} from 'react-redux';
import NumberDrop from "@components/NumberDrop";
import {SETNUMBER,CHANGEOTHERTEXTLABEL} from '@types/otherHistory';
import {SETNUMBER4} from '@types/checkBody';
import {SETDROPSHOW,CLICKCOUNT,HIDE,RESET,HIDEDROP,ISREAD,SET_SELECTED_AREA,SET_CURRENT_MODULE} from '@types/homePage';
import {NUMBER_SELECT} from '@store/types/mainSuit';
import {CURRENT_NUMBER} from '@store/types/currentIll';
import {billing,getMRAnalyse} from '@store/async-actions/pushMessage';
import {Notify} from '@commonComp';
import {filterArr,didPushParamChange,filterDataArr} from '@utils/tools';
import config from '@config/index';
import storeGlobal from '@store';

function mapStateToProps(state){
  return {
    mainSaveText:state.mainSuit.saveText,
    select_start:state.homePage.select_start,
  }
}
//查体数字键盘选中
function checkSelect(dispatch,params){
  dispatch({
    type:SETNUMBER4,
    params
  });
}
//其他史数字键盘选中
function otherSelect(dispatch,params){
  dispatch({
    type:SETNUMBER,
    params
  });
}
//主诉
function mainSelect(dispatch,params){
  dispatch({
    type:NUMBER_SELECT,
    params
  });
}
//现病史
function currentSelect(dispatch,params){
  dispatch({
    type:CURRENT_NUMBER,
    params
  });
}

function handleModuleDiff(dispatch,params){
  const {mainSaveText} = params;
  const type = params.ikey.substr(0,1);    //当前所在的项目
  switch (+type){
    case 1:
      let text = filterDataArr(mainSaveText);
      if(text.length >= config.limited){
        Notify.info(config.limitText);
        return
      }
      mainSelect(dispatch,params);
      break;
    case 2:
      currentSelect(dispatch,params);
      break;
    case 3:
      otherSelect(dispatch,params);
      break;
    case 4:
      checkSelect(dispatch,params);
      break;
    default:
  }
}

function mapDispatchToProps(dispatch,store){
  return {
    handleSelect(params){
      handleModuleDiff(dispatch,params);

      //右侧推送
      setTimeout(function(){      //延迟待确定后的数据更新后推送，避免获取的参数还是旧的
        if(didPushParamChange()){     //操作后内容有变化才推送
          dispatch(billing('',params.ikey.substr(0,1)));
        }
      },config.delayPushTime);
    },
    handleHide(){
      dispatch({
        type:HIDEDROP
      })
    },
    handleShow(params) {
      const storeState = storeGlobal.getState()
      const {moduleName, moduleObj} = storeState.homePage
      dispatch({
        type:CLICKCOUNT,
        data:params,
        clickType:'单击',
        num:1
      });
      dispatch({
        type:SETDROPSHOW,
        data:params
      });
      if(moduleName != moduleObj[params.ikey[0]]) {
        dispatch({
          type: SET_CURRENT_MODULE,
          moduleName: moduleObj[params.ikey[0]]
        });
        dispatch(getMRAnalyse());
      }
      /*dispatch({
        type: RESET
      });*/
    },
    setSelectArea(data){
      dispatch({
        type:SET_SELECTED_AREA,
        data
      })
    }
  }
}

const NumberDropCont = connect(mapStateToProps,mapDispatchToProps)(NumberDrop);

export default NumberDropCont;