import React from "react";
import { connect } from "react-redux";
import Banner from "../components/Banner";
import { SHOW_CONFIG_MODAL, CLOSE_CONFIG_MODAL, CONFIRM_TYPE } from "../store/types/typeConfig";
import {pushAllDataList} from '@utils/tools';
import {saveMode,getOtherHisRecord} from '@store/async-actions/fetchModules';
import { allCheckedShow } from '@store/actions/tabTemplate';
import {Notify} from '@commonComp';

//判断模块是否有数据
function ifHasData(data,saveText){
  if(saveText[0]||data.length>1||(data.length===1&&(data[0].value||(data[0].value!==''&&data[0].name)))){
    return true;
  }
  return false;
}

function mapStateToProps(state) {
  const main = state.mainSuit;
  const current = state.currentIll;
  const other = state.otherHistory;
  const typeConfig = state.typeConfig;
  return {
    typeConfig:typeConfig.typeConfig,
    visible:typeConfig.visible,
    hasMain:ifHasData(main.data,main.saveText),
    hasCurrent:ifHasData(current.data,current.saveText),
    hasOther:ifHasData(other.data,other.saveText),
    readMode:typeConfig.readMode,       //回读模式
  };
}

function mapDispatchToProps(dispatch) {
  return {
    showConfigModal: () => {
      dispatch({type: SHOW_CONFIG_MODAL})
    },
    closeConfigModal: () => {
      dispatch({type: CLOSE_CONFIG_MODAL})
    },
    confirmType: confirmType => {
      //保存模式切换结果
      dispatch(saveMode(confirmType)).then((res)=>{
        if(res.data.code=='0'&&res.data.data){
          //清空所有数据
          pushAllDataList('','clear');
          dispatch({type: CONFIRM_TYPE, confirmType});
          Notify.success("修改成功");
          dispatch(allCheckedShow(false))    //全选反选显示重置
        }else{
          Notify.error(res.data.msg+",请稍后再试");
        }
      });

    }
  };
}

const BannerContainer = connect(
  mapStateToProps,
  mapDispatchToProps
)(Banner);

export default BannerContainer;
