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 {pushMessage} from '../store/async-actions/pushContainer';
import {getModules} from '../store/async-actions/fetchModules.js';
import {HIDE,RESET,CLICKCOUNT,ISREAD,SETDROPSHOW} from '@store/types/homePage';
import {billing} from '@store/async-actions/pushMessage';
import {getModule} from '@store/async-actions/fetchModules';
import {didPushParamChange,filterDataArr} from '@utils/tools';
import {Notify} from '@commonComp';

function mapStateToProps(state) {
  const {homePage,currentIll,mainSuit,diagnosticList,typeConfig} = state;
  const hasMain = filterDataArr(mainSuit.saveText);
    return {
        data:currentIll.data,//主诉模板
        emptyData:currentIll.emptyData,//空模板
        searchData:currentIll.searchDatas,//搜索结果
        focusIndex:currentIll.focusIndex,
        processModule:currentIll.processModule,//病程变化模板
        showArr:homePage.showDrop,
        span:currentIll.span,
        update:currentIll.update,//用于更新
        mainText:mainSuit.saveText,//主诉选中的数据
        mainData:mainSuit.data,//主诉使用的模板
        symptomFeature:mainSuit.symptomFeature,//主诉分词数据
        moduleNum:mainSuit.moduleNum,//主诉使用的模板
        type: typeConfig.mode,
        mainIds:mainSuit.mainIds,//主诉症状选中的id(去重用)
        mainTailIds:mainSuit.mainTailIds,//主诉症状选中的id(去重用)
        totalHide: homePage.totalHide,
        saveText:currentIll.saveText,
        editClear:currentIll.editClear,
        symptomIds:currentIll.symptomIds,//症状id，去重用
        isRead:homePage.isRead,
        fuzhen:diagnosticList.mainSuitStr,//诊断第一个复诊值
        allModules:homePage.allModules,
        // isChronic:!!diagnosticList.chronicMagItem,
        isChronic:mainSuit.chronicDesease?mainSuit.chronicDesease:diagnosticList.chronicMagItem,
        readMode:typeConfig.readMode,       //回读回来的模式（与当前模式并存）
        hasMain,      //是否有主诉
        typeConfig: state.typeConfig
    }
}


function mapDispatchToProps(dispatch) {
    return {
       insertProcess(obj,allModules){//点击病程变化
        // 埋点dispatch
        dispatch({
          type:CLICKCOUNT,
          data:{id:obj.id},
          clickType:'单击',
          num:1
        });
        dispatch({
          type:INSERT_PROCESS,
        });
        dispatch({
          type:ISREAD
        })
       },
       async setData(info){//设置现病史使用模板
        // let idsArr = info.mainIds;
        let idsArr = info.mainTailIds.filter((it,i)=>{return it});
        // let ids = idsArr.join(",");
        let labelModule = await getModules(idsArr);
        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('',2));       //参数意义：点到现病史时查体无数据则提前获取查体模板，避免点查体时模板返回慢
           }
         },500);
       },
        pushMessage: (data) => {
          dispatch(() => pushMessage(data))
        },
        //文本模式下推送
        fetchPushInfos(){
          //调右侧推送
          if(didPushParamChange()) {
            dispatch(billing());
          }
        },
      handleInput(obj){    //文本模式值保存
        dispatch({
          type:SETTEXTMODEVALUE,
          text:obj.text
        })
      },
        fetchModules(param){
            const {id,name,index,span,conceptId} = param;
            getModule(id).then((res)=>{
                if(res.data.code=='0'){
                  dispatch({
                      type:SELECT_SEARCHDATA,
                      index,
                      name,
                      data: res.data.data,
                      span,
                      isReplace:false,
                      conceptId
                  });
                  dispatch({
                    type:ISREAD
                  });
                  /*
                  * 选中后推送的入参可能变了，如输入心率搜索出心率不齐，
                  * 500ms内心率不齐标签未选中，系统会推送入参心率，
                  * 此时推送结果可能影响查体高亮结果且并不准确，
                  * 所以选中后要重新推送
                  * */
                  if(didPushParamChange()){     //操作后内容有变化才推送
                    dispatch(billing());
                  }
                }
            });
        },
        changeEditIll(bool){
            dispatch({
              type:CLEAR_CURRENT_EDIT,
              editClear:bool
            })
        },
      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;
