/*
 * @Author: code4eat awesomedema@gmail.com
 * @Date: 2022-05-30 09:50:41
 * @LastEditors: code4eat awesomedema@gmail.com
 * @LastEditTime: 2023-11-10 17:20:09
 * @FilePath: /KC-MiddlePlatform/src/pages/index/components/RecentlyVisited/index.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

//最近访问模块

import { useEffect, useState } from 'react';
import './style.less';
import { history, useModel } from 'umi';
import { Empty, Modal } from 'antd';
import { NavSelecterItemType } from '@/components/NavSelecter';
import { getAppAccess } from '@/service';

export type RecentlyVisitedItemsType = {
  name: string;
  path: string;
  menuId: string;
  type: number;
  systemId: string;
};
export interface RecentlyVisitedPropsType {
  data: RecentlyVisitedItemsType[];
}

export const RecentlyVisited = (props: RecentlyVisitedPropsType) => {
  const { data } = props;

  const { initialState, setInitialState } = useModel('@@initialState');

  const [tabs, set_tabs] = useState<RecentlyVisitedItemsType[]>([]);

  const clickHandle = async (val: NavSelecterItemType) => {
    //更新currentSelectedSys/currentTab触发导航跳转
    const { systemId = '', menuId = '' } = val;
    const resp = await getAppAccess(systemId.length > 0 ? systemId : menuId);

    if (!resp) {
      await setInitialState((s) => ({ ...s, currentSelectedSys: val as any, currentTab: val }));
      history.push(val.path);
    } else {
      Modal.error({
        title: '当前系统未注册，请联系管理员处理！',
      });
    }
  };

  useEffect(() => {
    set_tabs(data);
  }, [data]);

  return (
    <div className="RecentlyVisited">
      <div className="topTitle">
        <span className="name">最近访问</span>
      </div>
      <div className="wrap">
        {tabs.map((item, index) => {
          return (
            <div className="tab" onClick={() => clickHandle(item)} key={index}>
              {item.name}
            </div>
          );
        })}
        {tabs.length == 0 && (
          <div className="empty">
            <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
          </div>
        )}
      </div>
    </div>
  );
};
