import React from 'react';
import { connect } from 'react-redux';
import CurrentIll from '@components/CurrentIll';
import {INSERT_PROCESS,SET_CURRENT_DATA,SETTEXTMODEVALUE,SET_LABEL_MODULE,SELECT_SEARCHDATA,CLEAR_CURRENT_EDIT,SAVE_CURR_FREE} from '@store/types/currentIll';
import {SET_CURRENT_ILL} from '@store/types/fillInfo'
import {pushMessage} from '../store/async-actions/pushContainer';
import {getModules} from '../store/async-actions/fetchModules.js';
import {SETDROPSHOW} from '@types/homePage.js';
import {HIDE,RESET,CLICKCOUNT,ISREAD,SEARCH_DROP_LOCATION} from '@store/types/homePage.js';
import {billing} from '@store/async-actions/pushMessage';
import {getModule} from '@store/async-actions/fetchModules.js';
import {didPushParamChange} from '@utils/tools.js';


function mapStateToProps(state) { //console.log(111,state);
  const {homePage} = state;
    return {
        data:state.currentIll.data,//主诉模板
        emptyData:state.currentIll.emptyData,//空模板
        searchData:state.currentIll.searchDatas,//搜索结果
        focusIndex:state.currentIll.focusIndex,
        processModule:state.currentIll.processModule,//病程变化模板
        showArr:homePage.showDrop,
        span:state.currentIll.span,
        update:state.currentIll.update,//用于更新
        mainText:state.mainSuit.saveText,//主诉选中的数据
        mainData:state.mainSuit.data,//主诉使用的模板
        symptomFeature:state.mainSuit.symptomFeature,//主诉分词数据
        moduleNum:state.mainSuit.moduleNum,//主诉使用的模板
        type: state.typeConfig.typeConfig,
        mainIds:state.mainSuit.mainIds,//主诉症状选中的id(去重用)
        fillInfo: state.fillInfo,
        showArr:homePage.showDrop,
        totalHide: homePage.totalHide,
        saveText:state.currentIll.saveText,
        selecteds:state.currentIll.selecteds,       //普通多选选中状态
        editClear:state.currentIll.editClear,
        symptomIds:state.currentIll.symptomIds,//症状id，去重用
        isRead:state.homePage.isRead,
        fuzhen:state.diagnosticList.mainSuitStr,//诊断第一个复诊值
        boxTop:state.homePage.boxTop,
        boxLeft:state.homePage.boxLeft,
        allModules:state.homePage.allModules,
        // isChronic:!!state.diagnosticList.chronicMagItem,
        isChronic:state.mainSuit.chronicDesease,

    }
}


function mapDispatchToProps(dispatch) {
    return {
       insertProcess(obj,allModules){//点击病程变化
        // 埋点dispatch
        dispatch({
          type:CLICKCOUNT,
          data:{id:obj.id},
          clickType:'单击',
          num:1
        });
        dispatch({
          type:INSERT_PROCESS,
          id:obj.relationModule,
          allModules:allModules
        });
        dispatch({
          type:ISREAD
        })
       },
       async setData(info){//设置现病史使用模板
        let idsArr = info.mainIds;
        let ids = idsArr.join(",");
        let labelModule = await getModules(ids);
        if(labelModule.data.code==0){//根据id获取标签模板
          dispatch({
            type:SET_LABEL_MODULE,
            data:labelModule.data.data
          })
        }
        dispatch({
          type:SET_CURRENT_DATA,
          info
        })
         //右侧推送
         setTimeout(function(){      //延迟待确定后的数据更新后推送，避免获取的参数还是旧的
           if(didPushParamChange()){     //操作后内容有变化才推送
             dispatch(billing());
           }
         },500);
       },
        setCurrentIll: value => {
          dispatch({type: SET_CURRENT_ILL, value})
        },
        pushMessage: (data) => {
          dispatch(() => pushMessage(data))
        },
        //文本模式下推送
        fetchPushInfos(){
          //调右侧推送
          dispatch(billing());
        },
      handleInput(obj){    //文本模式值保存
        dispatch({
          type:SETTEXTMODEVALUE,
          text:obj.text
        })
      },
        fetchModules(param){
            const {id,name,index,span} = param;
            getModule(id).then((res)=>{
                if(res.data.code=='0'){
                  dispatch({
                      type:SELECT_SEARCHDATA,
                      index,
                      name,
                      data: res.data.data,
                      span,
                      isReplace:false
                  })
                  dispatch({
                    type:ISREAD
                  })
                }
            });
          //右侧推送
          setTimeout(function(){ 
            dispatch(billing());
          },200);
        },
        changeEditIll(bool){
            dispatch({
              type:CLEAR_CURRENT_EDIT,
              editClear:bool
            })
        },
        
      getSearchLocation(top,left){
        dispatch({
          type:SEARCH_DROP_LOCATION,
          top:top,
          left:left,
          dis:0
        })
      },
      freeText(item){//自由输入
        dispatch({
          type: SAVE_CURR_FREE,
          data:item
        })
        //右侧推送
          setTimeout(function(){ 
            if(didPushParamChange()){
              dispatch(billing());
            }
          },500);
      }
    }
}

const CurrentIllContainer = connect(
    mapStateToProps,
    mapDispatchToProps
)(CurrentIll);
export default CurrentIllContainer;
