/*
 * @Author: code4eat awesomedema@gmail.com
 * @Date: 2022-05-27 14:17:59
 * @LastEditors: code4eat awesomedema@gmail.com
 * @LastEditTime: 2025-01-15 10:45:43
 * @FilePath: /KC-MiddlePlatform/src/pages/index/components/FastEntry/index.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

//快速入口模块
import { history, useModel } from 'umi';
import { useEffect, useState } from 'react';
import './style.less';
import { Empty, Modal } from 'antd';
import NavSelecter, { NavSelecterData, NavSelecterItemType } from '@/components/NavSelecter';
import { addFastEntry, AddFastEntryDataType, getUserPlatformNav } from '@/service/menu';
import { getAppAccess, removeFastEntrance } from '@/service';
import { createFromIconfontCN } from '@ant-design/icons';

export type FastEntryTabType = {
  id: string;
  name: string;
  path: string;
  systemId: string;
  type: number;
  contentType: number;
  menuId: number | string;
  url?: string;
};
export interface FastEntryType {
  data: FastEntryTabType[];
  onChange?: () => void;
}

const IconFont = createFromIconfontCN({
  scriptUrl: '',
});

export const FastEntry = (props: FastEntryType) => {
  const { initialState, setInitialState } = useModel('@@initialState');
  const [tabs, set_tabs] = useState<FastEntryTabType[]>([]);
  const [open, set_open] = useState(false);
  const [navSelecterData, set_navSelecterData] = useState<NavSelecterData[]>([]);

  const tabClickHandle = async (tab: FastEntryTabType) => {
    const { systemId = '', menuId = '', contentType, type, url } = tab;
    const resp = await getAppAccess(systemId.length > 0 ? systemId : menuId);
    if (!resp) {
      if (contentType != 7 && type != 1) {
        setInitialState((s: any) => ({ ...s, currentSelectedSys: tab as any, currentTab: tab }));
      }
      // if (type == 1 && contentType == 6) {
      //   //体系或第三方iframe
      //   history.push('/platform');
      // }else if(type == 1 && contentType == 7){
      //   window.open(url,'_blank');
      // }else {
      //   history.push(tab.path);
      // }
    } else {
      Modal.error({
        title: '当前系统未注册，请联系管理员处理！',
      });
    }
  };

  const getNavData = async () => {
    const navData = await getUserPlatformNav();
    set_navSelecterData(navData);
  };

  const onCheckedHandle = async (data: NavSelecterItemType[]) => {
    const { onChange } = props;
    const resp = await addFastEntry(data as AddFastEntryDataType[]);
    if (resp) {
      set_tabs(data);
      onChange && onChange();
    }
  };

  const addHandle = () => {
    getNavData();
    set_open(true);
  };

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

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

  return (
    <div className="fastEntry">
      <div className="topTitle">
        <span className="name">快速入口</span>
        <span className="addBtn" onClick={() => addHandle()}>
          管理
        </span>
      </div>

      {open && <NavSelecter type={2} resetHandleType="request" title="选择应用" data={navSelecterData} onVisibleChange={(bool) => set_open(bool)} value={tabs} onChecked={onCheckedHandle} />}

      <div className="wrap">
        {tabs.slice(0, 6).map((item, index) => {
          return (
            <div className="tab" onClick={() => tabClickHandle(item)} key={index}>
              <IconFont type="icontubiao1" style={{ marginRight: 8 }} />
              {item.name}
            </div>
          );
        })}
        {tabs.length == 0 && (
          <div className="empty">
            {/* <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} /> */}
            添加系统快速入口，方便再次进入
          </div>
        )}
      </div>
    </div>
  );
};
