import { MyPageContainer, usePageTabs } from '@/common';
import { Apis } from '@/gen/Apis';
import { ProCard } from '@ant-design/pro-components';
import { useParams } from '@umijs/max';
import { Tabs } from 'antd';
import { useEffect, useState } from 'react';
import ComponentsInfo from './components/ComponentsInfo';
import Employees from './components/Employees';
import Organizations from './components/Organizations';
import Positions from './components/Positions';
import Projects from './components/Projects';
import ReceiptAccounts from './components/ReceiptAccounts';

export default function Show({ title }: { title?: string } = {}) {
  const { id } = useParams<{ id: string }>();
  const [data, setShow] = useState<any>({});

  // 注册当前页面为标签页
  const { addTab } = usePageTabs({
    tabKey: `company-show-${id}`,
    tabLabel: data?.short_name || title || '资产详情',
  });
  const loadShow = () => {
    let paramsId: any = { id: id ?? 0 };
    Apis.Company.Companies.Show(paramsId).then((res) => {
      setShow(res?.data);
      // 更新标签页名称为API返回的name
      if (res?.data?.short_name) {
        addTab({
          key: `company-show-${id}`,
          label: res.data.short_name,
          path: `/company/${id}`,
        });
      }
    });
  };
  useEffect(() => {
    loadShow();
  }, [id]);

  let items = [
    {
      label: '项目管理',
      key: '1',
      closable: false,
      children: <Projects item={data} />,
    },
    {
      label: '组织管理',
      key: '2',
      closable: false,
      children: <Organizations item={data} />,
    },
    {
      label: '岗位管理',
      key: '3',
      closable: false,
      children: <Positions item={data} />,
    },
    {
      label: '员工管理',
      key: '4',
      closable: false,
      children: <Employees item={data} />,
    },
    {
      label: '收款账号',
      key: '5',
      closable: false,
      children: <ReceiptAccounts item={data} />,
    },
  ];
  return (
    <MyPageContainer title={title}>
      <ComponentsInfo item={data} />
      <ProCard>
        <Tabs type="card" items={data?.id ? items : []} />
      </ProCard>
    </MyPageContainer>
  );
}
