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} from '@store/async-actions/tabTemplate';
import {saveClickNum,getInitModules} 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 {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,
        chronicMagItem: state.diagnosticList.chronicMagItem,//慢病疾病
        chronicDesease:state.mainSuit.chronicDesease,//主诉存的慢病
    });
}

function mapDispatchToProps(dispatch) {
    return {
        showPrintPreview: () => {
            dispatch({
                type: SHOW_PRINT_PREVIEW
            });
        },
        closePrintPreview: () => {
            dispatch({
                type: CLOSE_PRINT_PREVIEW
            });
        },
        showPreview: () => {
            dispatch({
                type: SHOW_PREVIEW
            });
        },
        closePreview: () => {
            dispatch({
                type: CLOSE_PREVIEW
            });
        },
        save: () => {
            dispatch(saveClickNum);
            dispatch(() => saveMessage())
        },
        clear: (flag) => {
            clearMessages();
            dispatch(getOtherHisRecord());      //清除后重新获取其他史记录
            // 清除时如果是慢病模板则重新获取模板
            if(flag){
                dispatch(getInitModules);
            }
        },
        saveDataAll(val,sex){
          dispatch(saveTemplateDetail(val,sex))
        },
        diagShowTmp(bool){
          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')):[];
            const deledSelects = localStorage.getItem('deletedSelects')?JSON.parse(localStorage.getItem('deletedSelects')):[];
            if(deledTags.length===0){
              Notify.info('暂无标签可还原');
              return ;
            }
            const arr = deledTags[0].delIndex.split("-");
            dispatch({
              type:maps[arr[0]],
              data:deledTags[0],
              select:deledSelects[0],
              index:arr[1]
            });
          dispatch({
            type:ISREAD
          });
          //右侧推送
          setTimeout(function(){      //延迟待确定后的数据更新后推送，避免获取的参数还是旧的
            if(didPushParamChange()){     //操作后内容有变化才推送
              dispatch(billing());
            }
          },200);

        }
    };
}

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

export default OperationComtainer;