import React from 'react';
import {connect} from 'react-redux';
import RadioDrop from "@components/RadioDrop";
import {SETRADIO,CLEARSELECTED,CONFIRMSELECTED,CHANGEOTHERTEXTLABEL} from '@types/otherHistory';
import {SETSELECTED4,CHANGECHECKTEXTLABEL} from '@types/checkBody';
import {SETDROPSHOW,HIDE,RESET,HIDEDROP,CLICKCOUNT,ISREAD} from '@types/homePage.js';
import {RADIO_SELECT,CHANGE_LABELVAL} from '@store/types/mainSuit.js';
import {CURRENT_RADIO,CURRENT_TEXT_LABEL} from '@store/types/currentIll.js';
import {Notify} from '@commonComp';
import {filterArr,didPushParamChange,filterDataArr} from '@utils/tools.js';
import {billing} from '@store/async-actions/pushMessage';
import {getLabelIndex,fullfillText} from '@common/js/func.js';
import config from '@config/index.js';

function mapStateToProps(state){
  return {
    mainSaveText:state.mainSuit.saveText,
  }
}


// 主诉选中事件
function mainSelect(dispatch,params){
  const {id,ikey,text} = params;
  dispatch({
    type:RADIO_SELECT,
    id,
    ikey,
    text:text||''
  });
}

// 现病史选中事件
function currentSelect(dispatch,params){
  const {id,ikey,text} = params;
  dispatch({
    type:CURRENT_RADIO,
    id,
    ikey,
    text:text||''
  });
}


//其他史选中事件
function otherSelect(dispatch,params){
  const {id,ikey,text} = params;
  dispatch({
    type:SETRADIO,
    id,
    ikey,
    text:text||''
  });
}
//查体单选下拉选中事件
function checkSelect(dispatch,params){
  const {id,ikey,text} = params;
  dispatch({
    type:SETSELECTED4,
    id,
    ikey,
    text:text||''
  });
}
//在不同模块（主诉、现病史等）下拉选中调用不同事件
function handleModuleDiff(dispatch,store,params){
  const {mainSaveText,value,text} = params;
  const type = params.ikey.substr(0,1);    //当前所在的项目
  switch (+type){
    case 1:
      if(text){//有选值才需要判断，清空不需要处理
        let mainText = filterDataArr(mainSaveText);
        let lengths = value?mainText.length - value.length + text.length:mainText.length + text.length;
        if(lengths >= 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:
  }
}
/**************标签双击输入action types**************/
const dbEditActions = {
  1:CHANGE_LABELVAL,
  2:CURRENT_TEXT_LABEL,
  3:CHANGEOTHERTEXTLABEL,
  4:CHANGECHECKTEXTLABEL
};

function mapDispatchToProps(dispatch,store){
  return {
    handleSelect(params) {
      handleModuleDiff(dispatch, store, params);
      //单选选中隐藏弹窗
      dispatch({
        type: HIDEDROP
      });

      //右侧推送
      setTimeout(function(){      //延迟待确定后的数据更新后推送，避免获取的参数还是旧的
        if(didPushParamChange()){     //操作后内容有变化才推送
          dispatch(billing());
        }
      },500);
    },
    handleDbclick(obj){//双击统计
      dispatch({
        type:CLICKCOUNT,
        data:obj,
        clickType:'双击',
        num:1
      });
    },
    handleLabelEdit(params){
      const {type} = params;
      const index = params.ikey;
      let ikey = getLabelIndex(index);
      dispatch({
        type:dbEditActions[+type],
        data:{changeVal:params.changeVal,ikey:ikey}
      });
      dispatch(billing());
      dispatch({
        type:ISREAD
      })
    },
    handleShow(obj) {
      dispatch({
        type:CLICKCOUNT,
        data:obj,
        clickType:'单击',
        num:1
      });
      dispatch({
        type:SETDROPSHOW,
        data:obj
      });
      dispatch({
        type: RESET
      });
    }
  }
}

const RadioDropCont = connect(mapStateToProps,mapDispatchToProps)(RadioDrop);

export default RadioDropCont;
