import { MyPageContainer, usePageTabs } from '@/common';
import { Apis } from '@/gen/Apis';
import { ProCard } from '@ant-design/pro-components';
import { useParams } from '@umijs/max';
import { Space, Tabs } from 'antd';
import { useEffect, useState } from 'react';

import Assets from './components/Assets';
import CompanyApps from './components/CompanyApps';
import ComponentsInfo from './components/ComponentsInfo';
import Employees from './components/Employees';
import Organizations from './components/Organizations';
import Positions from './components/Positions';
import ReceiptAccounts from './components/ReceiptAccounts';
import CompanyUpdate from './modals/CompanyUpdate';

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: '1-项目配置',
      key: '1',
      closable: false,
      children: <Assets item={data} />,
    },
    {
      label: '2-组织配置',
      key: '2',
      closable: false,
      children: <Organizations item={data} />,
    },
    {
      label: '3-岗位配置',
      key: '3',
      closable: false,
      children: <Positions item={data} />,
    },
    {
      label: '4-员工配置',
      key: '4',
      closable: false,
      children: <Employees item={data} />,
    },
    {
      label: '5-账号配置',
      key: '5',
      closable: false,
      children: <ReceiptAccounts item={data} />,
    },
    {
      label: '6-应用配置',
      key: '6',
      closable: false,
      children: <CompanyApps item={data} />,
    },
  ];
  return (
    <MyPageContainer title={title}>
      {/* <ComponentsInfo item={data} /> */}
      <ProCard
        title={`${data?.name} (${data?.short_name})`}
        extra={
          <Space>
            <ComponentsInfo
              item={{ ...data, type: 'primary' }}
              title="查看"
              reload={loadShow}
            />
            <CompanyUpdate item={data} title="机构" reload={loadShow} />
          </Space>
        }
      >
        <div>
          * 您可在以下页签中对应配置：项目、组织、岗位、员工、账号、应用；
        </div>
      </ProCard>
      <ProCard>
        <Tabs type="card" items={data?.id ? items : []} />
      </ProCard>
    </MyPageContainer>
  );
}
