import React from 'react';
import { connect } from 'react-redux';
import MainSuit from '@components/MainSuit';
import {CLEAR_COMSYMPTOMS,SHOW_TAIL,INSERT_MAIN,CLEAR_SEARCH,SET_SEARCH,SAVE_FREE,SET_DATA,INSERT_SEARCH,SETTEXTMODEVALUE,SET_FEATURE,SAVE_CHRONIC,SHOW_HISTORY_BOX,HIDE_HISTORY_BOX} from '@store/types/mainSuit';
import {RESET,CLICKCOUNT,ISREAD,SEARCH_DROP_LOCATION,MODI_LOADING} from '@store/types/homePage.js';
import {getCommSymptoms,getCommSymptomPush} from '@store/async-actions/mainSuit.js'
import {SET_MAIN_SUIT} from '@store/types/fillInfo';
import {CLEAR_DIAGNOSE} from '@store/types/diagnosticList';
import {getSearch,getFeature,setOtherHisModule,autoFillModules} from '@store/async-actions/fetchModules';
import {billing} from '@store/async-actions/pushMessage';
import {didPushParamChange,filterDataArr,inspectAndAssist} from '@utils/tools.js';
import config from '@config/index.js';
import {Notify} from '@commonComp';
import {getInitModules} from '@store/async-actions/homePage.js';
import {CURRENT_CHRONIC} from '@store/types/currentIll';
import {SET} from '@types/checkBody';
import {SHOW_REFER_RECORD} from '@store/types/diagnosticList';

function mapStateToProps(state) {//console.log(11,state);
  return {
    showDrop:state.mainSuit.showDrop,//用于更新
    update:state.mainSuit.update,
    CommonSymptoms:state.mainSuit.CommonSymptoms,//常见症状
    datas:state.mainSuit.data,//数据源
    selecteds:state.mainSuit.selecteds,//数据源
    saveText:state.mainSuit.saveText,
	  searchData:state.mainSuit.searchDatas,//搜索结果
    type: state.typeConfig.typeConfig,
    fillInfo: state.fillInfo,
    showArr:state.homePage.showDrop,
    totalHide:state.homePage.totalHide,
    focusIndex:state.mainSuit.focusIndex,
    fuzhen:state.diagnosticList.mainSuitStr,//诊断第一个复诊值
    span:state.mainSuit.span,
    editClear:state.mainSuit.editClear,
    clearAction:state.mainSuit.clearAction,//清除
    mainIds:state.mainSuit.mainIds,//选中症状的id，搜索去重用
    isRead:state.homePage.isRead,
    commSymHide:state.homePage.commSymHide,
    boxTop:state.homePage.boxTop,
    boxLeft:state.homePage.boxLeft,
    currentData:state.currentIll.data,
    allModules:state.homePage.allModules,
    chronicDesease:state.mainSuit.chronicDesease,
    items:state.historyTemplates.items,
    showHisBox:state.mainSuit.showHisBox,
  }
}

function mapDispatchToProps(dispatch) {
    return {
        reTotalHide:()=>{//重置隐藏
          dispatch({
            type:RESET
          })
        },
        getCommonSymptoms:()=>{//获取常见症状
          dispatch(getCommSymptoms());
        },
        clearCommS:()=>{
          dispatch({
            type: CLEAR_COMSYMPTOMS
          })
        },
        handleTailClick:(obj,saveText,allModules)=>{//点击带尾巴的标签，如添加病程变化
          let text = filterDataArr(saveText);
          if(text.length >= config.limited){
            Notify.info(config.limitText);
            return
          }
          // 埋点dispatch
          dispatch({
            type:CLICKCOUNT,
            data:{id:obj.id},
            clickType:'单击',
            num:1
          });
          dispatch({
            type: SHOW_TAIL,
            info:obj.questionMapping,
            id:obj.relationModule,
            allModules:allModules
          })
          dispatch({
            type:ISREAD
          })
        },
        insertMain:(item)=>{//将选中的常见症状插入
          dispatch({
            type: INSERT_MAIN,
            item
          });
          //右侧推送
          setTimeout(function(){      //延迟待确定后的数据更新后推送，避免获取的参数还是旧的
            if(didPushParamChange()){     //操作后内容有变化才推送
              dispatch(billing());
            }
          },500);
        },
        getData(){//主诉使用模板
          dispatch({
            type: SET_DATA
          })
        },
      setMainSuit: (value) => {
        dispatch({type: SET_MAIN_SUIT, value})
      },
      pushMessage(data) {//结构模式右侧推送
        // console.log("右侧推送接口");
        dispatch(billing(data));
      },
      getSearchData:(item)=>{
        getSearch(item).then((res)=>{
            let result = res.data;
            if(+result.code == 0){
              let data = result.data;
              dispatch({
                type:SET_SEARCH,
                data:data
              })
            }else{
              console.log(result.msg);
            }
        });
      },
      insertSearch(obj){
        dispatch({
          type: INSERT_SEARCH,
          info:obj.item,
          span:obj.span
        })
        dispatch({
          type:ISREAD
        })
        //右侧推送
        setTimeout(function(){ 
          dispatch(billing());
        },200);
      },
      clearSearch:()=>{
        dispatch({
          type: CLEAR_SEARCH
        })
      },
      freeText(item){//自由输入
        dispatch({
          type: SAVE_FREE,
          data:item
        })
        //右侧推送
          setTimeout(function(){ 
            if(didPushParamChange()){
              dispatch(billing());
            }
          },500);
      },
      fetchPushInfos(){//文本模式下推送
        //调右侧推送
        console.log('右侧推送内容');
        dispatch(billing());
      },
      handleInput(obj){   //文本模式值保存
          dispatch({
            type:SETTEXTMODEVALUE,
            text:obj.text
          })
      },
      clearDiagnose(){//清除复诊
        dispatch({
            type:CLEAR_DIAGNOSE,
          })
      },
      getSearchLocation(top,left){
        dispatch({
          type:SEARCH_DROP_LOCATION,
          top:top,
          left:left,
          dis:0
        })
      },
      async getSymptomFeature(item){
        const it = item&&item.trim();
        const symptomFeature = await getFeature(it);
        if(symptomFeature.data.code==0){
          const data = symptomFeature.data.data;
          dispatch({
            type:SET_FEATURE,
            data:data
          })
        }
      },
      async saveChronic(item){//储存慢病信息
        // 页面已有数据不再请求
        
        // loading
        dispatch({type:MODI_LOADING,flag:true});
        // 存慢病
        dispatch({
          type:SAVE_CHRONIC,
          data:item
        })
        // 取模板
        await dispatch(getInitModules);
        // 模板回来关闭；loading
        dispatch({type:MODI_LOADING,flag:false});
        // 弹窗是否引用历史病历？
        // dispatch({type:SHOW_HISTORY_BOX});
        dispatch({type:SHOW_REFER_RECORD});

      },
      pureSaveChronic(item){//引用病例时储存
        // 存慢病
        dispatch({
          type:SAVE_CHRONIC,
          data:item
        })
      },
      closeHisBox(){
        dispatch({type:HIDE_HISTORY_BOX})
      },
      getBilling: () => {
        dispatch(billing())
      },
      autoFillModules() {//自动填充模板
        dispatch(autoFillModules())
      }
    }
}

const MainSuitContainer = connect(
  mapStateToProps,
  mapDispatchToProps
)(MainSuit);
export default MainSuitContainer;
