import React from "react";
import { connect } from "react-redux";
import Operation from "../components/Operation";
import {
  SHOW_PRINT_PREVIEW,
  CLOSE_PRINT_PREVIEW,
  SHOW_PREVIEW,
  CLOSE_PREVIEW,
  DIAG_SHOW
} from "../store/types/print";
import { saveMessage, clearMessages } from "../store/async-actions/print";
import { saveTemplateDetail, getDepartments, saveAdminTemplateDetail, getFloderList,newFloder,orderFloder } from '@store/async-actions/tabTemplate';
import { getDptLis,folderModal,folderOrder } from '@store/actions/tabTemplate';
import { saveClickNum } from '@store/async-actions/homePage';
/*import { getOtherHisRecord } from '@store/async-actions/fetchModules';*/
import { RECOVER_TAG_MAIN } from '@store/types/mainSuit';
import { RECOVER_TAG_CURRENT } from '@store/types/currentIll';
import { RECOVER_TAG_OTHER } from '@store/types/otherHistory';
import { RECOVER_TAG_CHECK } from '@store/types/checkBody';
import { SETMEDICALNAME, CLEARMEDICALNAME } from '@store/types/patInfo';
import { Notify } from '@commonComp';
import { didPushParamChange } from '@utils/tools.js';
import { billing } from '@store/async-actions/pushMessage';
import { ISREAD } from '@store/types/homePage';

// function mapStateToProps({print}) {
function mapStateToProps(state) {
  const { print, homePage } = state;
  return ({
    print,
    winWidth: homePage.windowWidth,
    admin: homePage.admin,
    chronicMagItem: state.diagnosticList.chronicMagItem,//慢病疾病
    chronicDesease: state.mainSuit.chronicDesease,//主诉存的慢病
    departLis: state.tabTemplate.departLis,
    floderListAdmin:state.tabTemplate.floderListAdmin,
    folderModalShow:state.tabTemplate.folderModalShow,
    folderOrderShow:state.tabTemplate.folderOrderShow,
    adminItems: state.tabTemplate.adminItems,
    patInfo: state.patInfo.message,
  });
}

function mapDispatchToProps(dispatch) {
  return {
    folderOrder(show){ 
      dispatch(folderOrder(show))
    },
    saveOrderFolder(folderList){
      dispatch(orderFloder(folderList))
    },
    folderModal(show){
      dispatch(folderModal(show))
    },
    newFloder(name){
      dispatch(newFloder(name))
    },
    getFloderList() {
      dispatch(getFloderList())
    },
    handleClearValue: () => {
      dispatch(dispatch(getDptLis([])))
    },
    handleChangeValue: (val) => {
      val && dispatch(getDepartments(val))
    },
    showPrintPreview: () => {
      dispatch({
        type: SHOW_PRINT_PREVIEW
      });
    },
    closePrintPreview: () => {
      dispatch({
        type: CLOSE_PRINT_PREVIEW
      });
    },
    showPreview: () => {
      dispatch({
        type: SHOW_PREVIEW
      });
    },
    closePreview: () => {
      dispatch({
        type: CLOSE_PREVIEW
      });
    },
    setmedicalName: (val) =>{
      dispatch({
        type: SETMEDICALNAME,
        params: val
      });
    },
    clearmedicalName: (val) => {
      dispatch({
        type: CLEARMEDICALNAME,
      });
    },
    save: () => {
      // 埋点事件，点击保存时调用
      // dispatch(saveClickNum);

      dispatch(() => saveMessage())
    },
    clear: () => {
      clearMessages();

    },
    saveDataAlls(val, sex, id,fstName,folderId,folderName) {
      dispatch(saveAdminTemplateDetail(val, sex, id,fstName,folderId,folderName))
    },
    saveDataAll(val, sex, spell) {
      dispatch(saveTemplateDetail(val, sex, spell))
    },
    hideAllSearch(){
        dispatch({
            type:'HIDE_ALL_SEARCH'
        });
    },
    diagShowTmp(bool) {
      dispatch({
          type:'HIDE_ALL_SEARCH'
      });
      dispatch({
        type: DIAG_SHOW,
        data: bool
      });
    },
    cancelDelTag() {
      const maps = {
        '1': RECOVER_TAG_MAIN,
        '2': RECOVER_TAG_CURRENT,
        '3': RECOVER_TAG_OTHER,
        '4': RECOVER_TAG_CHECK
      };
      const deledTags = localStorage.getItem('deletedTags') ? JSON.parse(localStorage.getItem('deletedTags')) : [];
      if (deledTags.length === 0) {
        Notify.info('暂无标签可还原');
        return;
      }
      const arr = deledTags[0].delIndex.split("-");
      const item = deledTags[0];
      dispatch({
        type: maps[arr[0]],
        data: item.tagType ? item : item.tags,
        index: arr[1]
      });
      dispatch({
        type: ISREAD
      });
      //删除文字选中状态
      window.getSelection().empty();
      //右侧推送
      setTimeout(function () {      //延迟待确定后的数据更新后推送，避免获取的参数还是旧的
        if (didPushParamChange()) {     //操作后内容有变化才推送
          dispatch(billing());
        }
      }, 200);
    }
  };
}

const OperationComtainer = connect(
  mapStateToProps,
  mapDispatchToProps
)(Operation);

export default OperationComtainer;