import React from 'react';
import {connect} from 'react-redux';
import RadioInpDrop from "@components/RadioInpDrop";
import {SETRADIO,CLEARSELECTED,CONFIRMSELECTED,CHANGEOTHERTEXTLABEL,SET_OT_RADIO_INPUT_VAL} from '@types/otherHistory';
import {SETSELECTED4,CHANGECHECKTEXTLABEL,SET_CK_RADIO_INPUT_VAL} from '@types/checkBody';
import {SETDROPSHOW,HIDE,RESET,HIDEDROP,CLICKCOUNT,ISREAD} from '@types/homePage.js';
import {RADIO_SELECT,CHANGE_LABELVAL,SET_MS_RADIO_INPUT_VAL} from '@store/types/mainSuit.js';
import {CURRENT_RADIO,CURRENT_TEXT_LABEL,SET_RADIO_INPUT_VALUE} 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 {ikey,id,values} = params;
  //let index = getLabelIndex(ikey);
  dispatch({
    type:SET_MS_RADIO_INPUT_VAL,
    data:{
      ikey,
      id,
      values
    }
  });
}

// 现病史选中事件
function currentSelect(dispatch,params){
  const {ikey,id,values} = params;
  //let index = getLabelIndex(ikey);
  dispatch({
    type:SET_RADIO_INPUT_VALUE,
    data:{
      ikey,
      id,
      values
    }
  });
}


//其他史选中事件
function otherSelect(dispatch,params){
  const {ikey,id,values} = params;
  //let index = getLabelIndex(ikey);
  dispatch({
    type:SET_OT_RADIO_INPUT_VAL,
    data:{
      ikey,
      id,
      values
    }
  });
}
//查体单选下拉选中事件
function checkSelect(dispatch,params){
  const {ikey,id,values} = params;
  dispatch({
    type:SET_CK_RADIO_INPUT_VAL,
    data:{
      ikey,
      id,
      values
    }
  });
}
//在不同模块（主诉、现病史等）下拉选中调用不同事件
function handleModuleDiff(dispatch,store,params){
  const {mainSaveText,values,value} = params;
  const type = params.ikey.substr(0,1);    //当前所在的项目
  switch (+type){
    case 1:
      if(values){//有选值才需要判断，清空不需要处理
        let str='',temp='';
        for(let i in values){
          temp = values[i];
          if(typeof temp=='object'){
            str+=temp.value;
          }else{
            str+=temp;
          }
        }
        let mainText = filterDataArr(mainSaveText);
        let lengths = value?mainText.length - value.length + str.length:mainText.length + str.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
};
//对应模块单选带输入存值事件
const InputActions = {
  1:SET_MS_RADIO_INPUT_VAL,
  2:SET_RADIO_INPUT_VALUE,
  3:SET_OT_RADIO_INPUT_VAL,
  4:SET_CK_RADIO_INPUT_VAL
};

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
      });
    },
    handleSaveInp(params){
      const {boxMark} = params;
      const index = params.ikey;
      let ikey = getLabelIndex(index);
      dispatch({
        type:InputActions[+boxMark],
        data:{values:params.values,ikey:params.ikey}
      });
      dispatch(billing());
      dispatch({
        type:ISREAD
      })
    },
    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 RadioInpDropCont = connect(mapStateToProps,mapDispatchToProps)(RadioInpDrop);

export default RadioInpDropCont;
