import React from 'react';
import {connect} from 'react-redux';
import MultSpread from "@components/MultSpread";
import {OTHERADDLABELITEM} from '@types/otherHistory';
import {ADDLABELITEM} from '@types/checkBody';
import {MAINADDLABELITEM} from '@store/types/mainSuit';
import {CURRENTADDLABELITEM} from '@store/types/currentIll';
import {HIDE,RESET,CLICKCOUNT,SETADDITEMINIT,ISREAD,SET_SELECTED_AREA,SET_CURRENT_MODULE} from '@store/types/homePage';
import store from '@store';
import {getMRAnalyse} from '@store/async-actions/pushMessage';

function mapStateToProps(state){
  return {
    select_start:state.homePage.select_start,
  }
}

const typeObj={
  1:MAINADDLABELITEM,
  2:CURRENTADDLABELITEM,
  3:OTHERADDLABELITEM,
  4:ADDLABELITEM
};

const getCopyData = (copyId,inx,boxMark)=>{
  return (dispatch,getStore)=>{
    const state = getStore();
    const copy = state.homePage.addItems[copyId];
    if(!copy){
      console.log("记录数据有误，未找到匹配");
      return;
    }
    dispatch({
      type:typeObj[+boxMark],
      data:JSON.stringify(copy),
      i:inx
    });
    setTimeout(function () {      //血压添加时后面逗号不显示bug
      dispatch({
        type:ISREAD
      })
    })
    //自由文本组件更新

  }
};

function mapDispatchToProps(dispatch){
  return {
    resetHide(){
      dispatch({
        type: RESET
      })
    },
    saveAddItem(id,data){      //保存有加号的标签原始数据
      dispatch({
        type:SETADDITEMINIT,
        id,
        data
      });
    },
    addLabelItem(param){
      const {copyId,ikey} = param;
      const boxMark = ikey.substr(0,1);
      const inx = ikey.split("-")[1];
      const storeState = store.getState()
      const {moduleName, moduleObj} = storeState.homePage
      //埋点记录
      dispatch({
        type:CLICKCOUNT,
        data:{id:copyId,isAddIcon:true},
        clickType:'单击',
        num:1
      });
      dispatch(getCopyData(copyId,inx,boxMark));
      if(moduleName != moduleObj[ikey[0]]) {
        dispatch({
          type: SET_CURRENT_MODULE,
          moduleName: moduleObj[ikey[0]]
        });
        dispatch(getMRAnalyse());
      }
    },
    handleClick(obj){
      dispatch({
        type:CLICKCOUNT,
        data:obj,
        clickType:'单击',
        num:1
      });
    },
    setSelectArea(data){
      dispatch({
        type:SET_SELECTED_AREA,
        data
      })
    }
  }
}

const MultSpreadCont = connect(mapStateToProps,mapDispatchToProps)(MultSpread);

export default MultSpreadCont;