import React from 'react';
import {connect} from 'react-redux';
import MultSpread from "@components/MultSpread";
import {SETSELECTED,CLEARSELECTED,CONFIRMSELECTED,CHANGEOTHERTEXTLABEL,OTHERADDLABELITEM} from '@types/otherHistory';
import {CHANGECHECKTEXTLABEL,ADDLABELITEM} from '@types/checkBody.js';
import {CHANGE_LABELVAL,MAINADDLABELITEM} from '@store/types/mainSuit.js';
import {CURRENTADDLABELITEM} from '@store/types/currentIll.js';
import {HIDE,RESET,CLICKCOUNT,SETADDITEMINIT,ISREAD} from '@store/types/homePage.js';

function mapStateToProps(state){
  return {}
}

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];
    dispatch({
      type:typeObj[+boxMark],
      data:JSON.stringify(copy),
      i:inx
    });
    //自由文本组件更新
    dispatch({
      type:ISREAD
    })
  }
};

/**************************双击标签输入*********************************/

function mainSuitLabel(dispatch,params){
  const index = params.ikey;
  let ikey = index.substr(1);
  const {changeVal,totalVal} = params;
  dispatch({
    type:CHANGE_LABELVAL,
    data:{changeVal:changeVal,ikey:ikey,totalVal}
  })
}
//其他史
function otherHisLabelEdit(dispatch,params){
  const index = params.ikey;
  const {changeVal,totalVal} = params;
  let ikey = index.substr(1);
  dispatch({
    type:CHANGEOTHERTEXTLABEL,
    data:{changeVal:changeVal,ikey:ikey,totalVal}
  })
}

//查体
function checkBodyLabelEdit(dispatch,params){
  const index = params.ikey;
  let ikey = index.substr(1);
  const {changeVal,totalVal} = params;
  dispatch({
    type:CHANGECHECKTEXTLABEL,
    data:{changeVal:changeVal,ikey:ikey,totalVal}
  })
}
function mapDispatchToProps(dispatch){
  return {
    resetHide(){
      dispatch({
        type: RESET
      })
    },
    handleDbclick(obj){
      dispatch({
        type:CLICKCOUNT,
        data:obj,
        clickType:'双击',
        num:1
      });
    },
    saveAddItem(id,data){       //保存有加号的标签原始数据
      dispatch({
        type: SETADDITEMINIT,
        id,
        data
      });
    },
    addLabelItem(param){
      const {copyId,ikey} = param;
      const boxMark = ikey.substr(0,1);
      const inx = ikey.substr(1);
      //埋点记录
      dispatch({
        type:CLICKCOUNT,
        data:{id:copyId,isAddIcon:true},
        clickType:'单击',
        num:1
      });
      dispatch(getCopyData(copyId,inx,boxMark));
    },
    saveEditText(params){
      const {type} = params;
      switch (+type){
        case 1:
          mainSuitLabel(dispatch,params);
          break;
        case 2:
          break;
        case 3:
          otherHisLabelEdit(dispatch,params);
          break;
        case 4:
          checkBodyLabelEdit(dispatch,params);
          break;
        default:
      }
    },
    handleClick(obj){
      dispatch({
        type:CLICKCOUNT,
        data:obj,
        clickType:'单击',
        num:1
      });
    }
    /*handleConfirm(checks,key){
      dispatch({
        type: CONFIRMSELECTED,
        checks,
        key
      })
    }*/
  }
}

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

export default MultSpreadCont;