import {connect} from 'react-redux';
import {EditableSpan} from '@commonComp';
import {SETCHECKTEXT,SETSEARCHDATA,CHECK_FOCUS_INDEX,CHECKBODY_CLEAR} from '@types/checkBody';
import {SET_SEARCH,MAIN_FOCUS_INDEX,CLEAR_SEARCH,SETMAINTEXT} from '@types/mainSuit';
import {SETOTHERTEXT,SETOTHERSEARCHDATA,OTHER_FOCUS_INDEX,OTHERHIS_CLEAR} from '@types/otherHistory';
import {SET_CURRENT_SEARCH,CURRENT_CLEAR,SETCURRENTTEXT,CURRENT_FOCUS_INDEX} from '@types/currentIll';
import {getSearch} from '@store/async-actions/fetchModules.js';
import {billing} from '@store/async-actions/pushMessage';
import {didPushParamChange} from '@utils/tools.js';
import {SETTEXTFOCUS} from '@types/homePage';
import {RESET,SETDROPSHOW} from "@store/types/homePage";
import config from '@config/index.js';

function mapStateToProps(state){//console.log(state)
  return {
    mainSaveText:state.mainSuit.saveText,
    isRead:state.homePage.isRead,
  }
}

function mainSuitSearch(obj){
  return (dispatch)=>{
    const text = obj.text.replace(/\，/,'');
    getSearch({boxMark:1,inpStr:text,mainIds:obj.mainIds}).then((res)=>{
      if(res.data.code=='0'){
        dispatch({
          type:SET_SEARCH,
          data:res.data.data,
          inpStr:text //搜索输入的值
        })
      }
    });
  }
}

function currentSearch(obj){
  return (dispatch)=>{
    const text = obj.text.replace(/\，/,'');
    getSearch({boxMark:1,inpStr:text,mainIds:obj.mainIds}).then((res)=>{
      if(res.data.code=='0'){
        dispatch({
          type:SET_CURRENT_SEARCH,
          data:res.data.data,
          inpStr:text
        })
      }
    });
  }
}

function checkBodySearch(obj){
  return (dispatch)=>{
    const text = obj.text.replace(/\，/,'');
    getSearch({boxMark:4,inpStr:text}).then((res)=>{
      if(res.data.code=='0'){
        dispatch({
          type:SETSEARCHDATA,
          data:res.data.data,
          inpStr:text
        });
      }
    });
  }
}

function otherHisSearch(obj) {
  return (dispatch)=>{
    const text = obj.text.replace(/\，/,'');
    getSearch({boxMark:3,inpStr:text}).then((res)=>{
      if(res.data.code=='0'){
        dispatch({
          type:SETOTHERSEARCHDATA,
          data:res.data.data,
          inpStr:text
        });
      }
    });
  }
}

function mapDispatchToProps(dispatch,state) {
  return {
    setFocusIndex(obj){//将选中值插入相应位置
      const {i,boxMark} = obj;
      switch (+obj.boxMark){
        case 1:
          dispatch({
            type:MAIN_FOCUS_INDEX,
            index:i,
            $span:obj.dom
          });
          break;
        case 2:
          dispatch({
            type:CURRENT_FOCUS_INDEX,
            index:i,
            $span:obj.dom
          });
          break;
        case 3:
          dispatch({
            type:OTHER_FOCUS_INDEX,
            index:i,
            $span:obj.dom
          });
          break;
        case 4:
          dispatch({
            type:CHECK_FOCUS_INDEX,
            index:i,
            $span:obj.dom
          });
          break;
        default:
          dispatch({
            type:SETTEXTFOCUS,
            index:i
          });
          break;
        }

      //显示搜索框
      dispatch({
        type:SETDROPSHOW,
        data:{ikey:boxMark+i+'0'}
      });
      dispatch({
        type: RESET
      });
    },
    handleChange(obj){//存data值
      switch (+obj.boxMark){
        case 1:
          dispatch({
            type:SETMAINTEXT,
            text:obj.text1,
            i:obj.i
          });
          dispatch({
            type: RESET
          });
          break;
        case 2:
          dispatch({
            type:SETCURRENTTEXT,
            text:obj.text1,
            i:obj.i
          });
          break;
        case 3:
          dispatch({
            type:SETOTHERTEXT,
            text:obj.text1,
            i:obj.i
          });
          break;
        case 4:
          dispatch({
            type:SETCHECKTEXT,
            text:obj.text1,
            i:obj.i
          });
          break;
        default:
          break;
      }
      //右侧推送
      clearTimeout(timer);
      let timer = setTimeout(function(){      //延迟待确定后的数据更新后推送，避免获取的参数还是旧的
        if(didPushParamChange()){     //操作后内容有变化才推送
          dispatch(billing);
          clearTimeout(timer);
        }
      },config.delayPushTime);
    },
    handleSearch(obj){
      switch (+obj.boxMark){
        case 1:
          dispatch(mainSuitSearch(obj));
          break;
        case 2:
          dispatch(currentSearch(obj));
          break;
        case 3:
          dispatch(otherHisSearch(obj));
          break;
        case 4:
          dispatch(checkBodySearch(obj));
          break;
        default:
          break;
      }
    },
    handleClear(obj){//清空搜索结果
      switch (+obj.boxMark){
        case 1:
          dispatch({type:CLEAR_SEARCH});
          break;
        case 2:
          dispatch({type:CURRENT_CLEAR});
          break;
        case 3:
          dispatch({type:OTHERHIS_CLEAR});
          break;
        case 4:
          dispatch({type:CHECKBODY_CLEAR});
          break;
        default:
          break;
      }
    }
  }
}

const EditableSpanCont = connect(
  mapStateToProps,
  mapDispatchToProps
)(EditableSpan);

export default EditableSpanCont;