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

function mapStateToProps(state) {
  return {
    disVisible:state.copyRight.disVisible,
    copyVisible:state.copyRight.copyVisible,
    //failed:state.typeConfig.failed,
    typeConfig:state.typeConfig.typeConfig||config.defaultValue.mode,
    visible:state.typeConfig.visible
  };
}

function mapDispatchToProps(dispatch) {
  return {
    showConfigModal: () => {
      dispatch({type: SHOW_CONFIG_MODAL})
    },
    closeConfigModal: () => {
      dispatch({type: CLOSE_CONFIG_MODAL})
    },
    changeType: typeConfig => {
      dispatch({type: CHANGE_TYPE, typeConfig})
    },
    confirmType: confirmType => {
      //保存模式切换结果
      dispatch(saveMode(confirmType)).then((res)=>{
        if(res.data.code=='0'&&res.data.data){
          //清空所有数据
          pushAllDataList('','clear');
          dispatch({type: CONFIRM_TYPE, confirmType});
          //切换模式是要重新获取其他史数据
          dispatch({
            type:SETOTHERHISTORY,
            data:[],
            save:[],
            otherIsHis:false
          });
          dispatch(getOtherHisRecord());        //获取其他史最近记录
          Notify.success("模式切换成功");
          dispatch(initItemList(confirmType));
          dispatch(allCheckedShow(false))    //全选反选显示重置
        }else{
          Notify.error(res.data.msg+",请稍后再试");
          //dispatch({type:MODE_CHANGE_FAIL,data:true});      //保存失败弹窗提示
          //alert('模式保存失败')
        }
      });

    }
  };
}

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

export default BannerContainer;
