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,MODI_LOADING,SETDROPSHOW,SET_CURRENT_MODULE} from '@store/types/homePage.js';
import {getCommSymptoms,getCommSymptomPush} from '@store/async-actions/mainSuit.js'
import {CLEAR_DIAGNOSE} from '@store/types/diagnosticList';
import {getSearch,getFeature,autoFillModules} from '@store/async-actions/fetchModules';
import {billing,getMRAnalyse} 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 {SHOW_REFER_RECORD} from '@store/types/diagnosticList';
import { SET_READ_MODE } from "@store/types/typeConfig";
import store from '@store';

function mapStateToProps(state) {
  const {mainSuit,homePage,diagnosticList,typeConfig} = state;
  return {
    //showDrop:mainSuit.showDrop,//用于更新
    update:mainSuit.update,
    CommonSymptoms:mainSuit.CommonSymptoms,//常见症状
    datas:mainSuit.data,//数据源
    saveText:mainSuit.saveText,
	  searchData:mainSuit.searchDatas,//搜索结果
    type: typeConfig.mode,
    showArr:homePage.showDrop,
    totalHide:homePage.totalHide,
    focusIndex:mainSuit.focusIndex,
    fuzhen:diagnosticList.mainSuitStr,//诊断第一个复诊值
    span:mainSuit.span,
    editClear:mainSuit.editClear,
    clearAction:mainSuit.clearAction,//清除
    mainIds:mainSuit.mainIds,//选中症状的id，搜索去重用
    isRead:homePage.isRead,
    commSymHide:homePage.commSymHide,
    currentData:state.currentIll.data,
    allModules:homePage.allModules,
    chronicDesease:mainSuit.chronicDesease?mainSuit.chronicDesease:diagnosticList.chronicMagItem,
    items:state.historyTemplates.items,
    showHisBox:mainSuit.showHisBox,
    readMode:typeConfig.readMode,       //回读回来的模式（与当前模式并存）
    typeConfig: state.typeConfig
  }
}

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,
          })
          dispatch({
            type:ISREAD
          })
        },
        insertMain:(item)=>{//将选中的常见症状插入
          // 判断字数限制
          const items = item.select;
          if(items.length>1){
            /*for(let i=1; i<items.length; i++){
              items[i].name = '、'+items[i].name;
              items[i].value = items[i].value?'、'+items[i].value:items[i].name;
            }*/ 
            // 计算字数
            let tempTotal = "";
            for(let j in items){
              tempTotal += items[j].name ;
            }
            if((tempTotal.length+ items.length-1) >= config.limited){
              Notify.info(config.limitText);
              return ;
            }
          }
          dispatch({//主诉使用模板
            type: SET_DATA
          })
          dispatch({
            type: INSERT_MAIN,
            item
          });
          //右侧推送
          setTimeout(function(){      //延迟待确定后的数据更新后推送，避免获取的参数还是旧的
            if(didPushParamChange()){     //操作后内容有变化才推送
              dispatch(billing());
            }
          },500);
        },
        /*getData(){//主诉使用模板
          dispatch({
            type: SET_DATA
          })
        },*/
      pushMessage(data) {//结构模式右侧推送
        if(didPushParamChange()) {
          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(){//文本模式下推送
        //调右侧推送
        if(didPushParamChange()) {
          dispatch(billing());
        }
      },
      handleInput(obj){   //文本模式值保存
          dispatch({
            type:SETTEXTMODEVALUE,
            text:obj.text
          })
      },
      clearDiagnose(){//清除复诊
        dispatch({
            type:CLEAR_DIAGNOSE,
          })
      },
      getSymptomFeature(data){
        dispatch({
          type:SET_FEATURE,
          data:data
        })
      },
      async saveChronic(item,isTextMode){//储存慢病信息
        // loading
        dispatch({type:MODI_LOADING,flag:true});
        // 存慢病
        dispatch({
          type:SAVE_CHRONIC,
          data:item
        })
        if(isTextMode){
          dispatch({type: SET_READ_MODE, readMode: 0});
        }
        // 取模板
        await dispatch(getInitModules);
        // 模板回来关闭；loading
        dispatch({type:MODI_LOADING,flag:false});
        dispatch({type:SHOW_REFER_RECORD});
      },
      pureSaveChronic(item){//引用病例时储存
        // 存慢病
        dispatch({
          type:SAVE_CHRONIC,
          data:item
        })
      },
      autoFillModules() {//自动填充模板
        dispatch(autoFillModules())
      },
      handleShow(obj) {
        const storeState = store.getState()
        const {moduleName, moduleObj} = storeState.homePage
        dispatch({
          type:SETDROPSHOW,
          data:obj
        });
        if(moduleName != moduleObj[obj.ikey]) {
          dispatch({
            type: SET_CURRENT_MODULE,
            moduleName: moduleObj[obj.ikey]
          });
          dispatch(getMRAnalyse());
        }
        dispatch({
          type: RESET
        });
      },
    }
}

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