import React from 'react';
import {connect} from 'react-redux';
import NumberDrop from "@components/NumberDrop";
import {SETNUMBER,CHANGEOTHERTEXTLABEL,CHANGEOTHERTEXTLABEL_NUMBER} from '@types/otherHistory';
import {SETNUMBER4,CHANGECHECKTEXTLABEL,CHANGECHECKTEXTLABEL_NUMBER} from '@types/checkBody.js';
import {SETDROPSHOW,CLICKCOUNT,HIDE,RESET,HIDEDROP,ISREAD} from '@types/homePage.js';
import {NUMBER_SELECT,CHANGE_LABELVAL,CHANGE_LABELVAL_NUMBER} from '@store/types/mainSuit.js';
import {getLabelIndex} from '@common/js/func.js';
import {CURRENT_NUMBER,CURRENT_TEXT_LABEL,CURRENT_TEXT_LABEL_NUMBER} from '@store/types/currentIll.js';
import {billing} from '@store/async-actions/pushMessage';
import {Notify} from '@commonComp';
import {filterArr,didPushParamChange,filterDataArr} from '@utils/tools.js';
import config from '@config/index.js';

function mapStateToProps(state){
  return {
    mainSaveText:state.mainSuit.saveText,
  }
}
//查体数字键盘选中
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 mainSuitLabel(dispatch,params){
  const index = params.ikey;
  let ikey = getLabelIndex(index);
  const {changeVal,totalVal} = params;
  dispatch({
    // type:CHANGE_LABELVAL,
    type:CHANGE_LABELVAL_NUMBER,
    data:Object.assign({},params,{ikey})
  })
}
// 现病史
function currentLabel(dispatch,params){
  const index = params.ikey;
  let ikey = getLabelIndex(index);
  dispatch({
    // type:CURRENT_TEXT_LABEL,
    type:CURRENT_TEXT_LABEL_NUMBER,
    data:Object.assign({},params,{ikey})
  });
  dispatch({
    type:ISREAD
  })
}

//其他史
function otherHisLabelEdit(dispatch,params){
  const index = params.ikey;
  //const {changeVal,totalVal} = params;
  let ikey = getLabelIndex(index);
  dispatch({
    // type:CHANGEOTHERTEXTLABEL,
    type:CHANGEOTHERTEXTLABEL_NUMBER,
    data:Object.assign({},params,{ikey})
  })
}

//查体
function checkBodyLabelEdit(dispatch,params){
  const index = params.ikey;
  let ikey = getLabelIndex(index);
  //const {changeVal,totalVal} = params;
  dispatch({
    // type:CHANGECHECKTEXTLABEL,
    type:CHANGECHECKTEXTLABEL_NUMBER,
    data:Object.assign({},params,{ikey})
  })
}
function mapDispatchToProps(dispatch,store){
  return {
    handleSelect(params){
      handleModuleDiff(dispatch,params);

      //右侧推送
      clearTimeout(timer);
      let timer = setTimeout(function(){      //延迟待确定后的数据更新后推送，避免获取的参数还是旧的
        if(didPushParamChange()){     //操作后内容有变化才推送
          dispatch(billing());
          clearTimeout(timer);
        }
      },config.delayPushTime);
    },
    handleDbclick(obj){
      dispatch({
        type:CLICKCOUNT,
        data:obj,
        clickType:'双击',
        num:1
      });
    },
    handleHide(){
      dispatch({
        type:HIDEDROP
      })
    },
    handleShow(params) {
      dispatch({
        type:CLICKCOUNT,
        data:params,
        clickType:'单击',
        num:1
      });
      dispatch({
        type:SETDROPSHOW,
        data:params
      });
      /*dispatch({
        type: RESET
      });*/
    },
    handleLabelChange(params){
      const {type} = params;
      switch (+type){
        case 1:
          mainSuitLabel(dispatch,params);
          break;
        case 2:
          currentLabel(dispatch,params);
          break;
        case 3:
          otherHisLabelEdit(dispatch,params);
          break;
        case 4:
          checkBodyLabelEdit(dispatch,params);
          break;
        default:
      }
    }
  }
}

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

export default NumberDropCont;