import React from 'react';
import {connect} from 'react-redux';
import OtherHistory from "../components/OtherHistory";
import {SETSELECTED,CLEARSELECTED,CONFIRMSELECTED,SELECTOTHERSEARCHDATA,SETDATA,SETTEXTMODEVALUE,OTHEREDICLEAR} from '@types/otherHistory';
import {HIDE,RESET,ISREAD,SEARCH_DROP_LOCATION} from '@store/types/homePage.js';
import {getModule,setOtherHisModule} from '@store/async-actions/fetchModules.js';
import {billing} from '@store/async-actions/pushMessage';
import {fullfillText} from '@common/js/func';
import {didPushParamChange,filterDataArr} from '@utils/tools.js';

function mapStateToProps(state){
  const {otherHistory,homePage,typeConfig,mainSuit} = state;
  const hasMain = filterDataArr(mainSuit.saveText);//||mainSuit.data.length;
  return {
    data: otherHistory.data,
    isEmpty:otherHistory.isEmpty,
    //initData:state.homePage.initData.otherHis,
    update:otherHistory.update,   //用于触发更新
    showArr:homePage.showDrop,
    totalHide:homePage.totalHide,
    saveText:otherHistory.saveText,
    type: typeConfig.typeConfig,
    hasMain,//主诉是否有数据
    searchData:otherHistory.searchData,    //延迟搜索结果
    focusTextIndex:otherHistory.focusIndex,    //聚焦的自由文本标签index
    span:otherHistory.span,
    searchInEnd:otherHistory.searchInEnd,      //是否在搜索末尾插入结果
    selecteds:otherHistory.selecteds,       //普通多选选中状态
    editClear:otherHistory.editClear,       //编辑状态
    isRead:state.homePage.isRead,
    boxTop:state.homePage.boxTop,
    boxLeft:state.homePage.boxLeft,
  }
}

function mapDispatchToProps(dispatch,store){
  return {
    setInitData(){
      //先获取最近记录，没有的话显示模板
      dispatch(setOtherHisModule());
      //右侧推送
      setTimeout(function(){      //延迟待确定后的数据更新后推送，避免获取的参数还是旧的
        if(didPushParamChange()){     //操作后内容有变化才推送
          dispatch(billing());
        }
      },500);
    },
    fetchModules(param){
      const {id,name,index,span,searchInEnd} = param;
      getModule(id).then((res)=>{
        if(res.data.code=='0'){
          dispatch({
            type:SELECTOTHERSEARCHDATA,
            index,
            name,
            data: res.data.data,
            span,
            isReplace:false,
            searchInEnd
          });
          dispatch({
            type:ISREAD
          });
        }
      });
    },
    //右侧推送
    fetchPushInfos(){
      //调右侧推送
      dispatch(billing());
    },
    handleInput(obj){    //文本模式值保存
      dispatch({
        type:SETTEXTMODEVALUE,
        text:obj.text
      })
    },
    changeEditClear(bool){    //文本编辑模式
      dispatch({
        type:OTHEREDICLEAR,
        bool:bool
      })
    },
    getSearchLocation(top,left){
      dispatch({
        type:SEARCH_DROP_LOCATION,
        top:top,
        left:left,
        dis:0
      })
    }
  }
}

const OtherHistoryCont = connect(mapStateToProps,mapDispatchToProps)(OtherHistory);

export default OtherHistoryCont;