import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  usePageTabs,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { CompaniesMerchantTypeEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space } from 'antd';
import ComponentsInfo from './components/ComponentsInfo';
import CompanyCreate from './modals/CompanyCreate';
import CompanyUpdate from './modals/CompanyUpdate';

export default function Index({ title = '机构列表' }) {
  const navigate = useNavigate();

  // 注册当前页面为标签页
  usePageTabs({
    tabKey: 'company-companies',
    tabLabel: title,
  });

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="company-companies"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(params, sort, Apis.Company.Companies.List)
        }
        toolBarRender={(action) => [
          <CompanyCreate key="Create" reload={action?.reload} title={title} />,
        ]}
        columns={[
          MyColumns.ID(),
          {
            title: '组织简称',
            dataIndex: 'short_name',
          },
          {
            title: '企业名称',
            dataIndex: 'name',
          },
          MyColumns.EnumTag({
            title: '类型',
            dataIndex: 'merchant_type',
            valueEnum: CompaniesMerchantTypeEnum,
            search: false,
          }),
          MyColumns.SoftDelete({
            onRestore: Apis.Company.Companies.Restore,
            onSoftDelete: Apis.Company.Companies.SoftDelete,
            search: false,
          }),
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <ComponentsInfo
                  item={item}
                  title="查看"
                  reload={action?.reload}
                />
                <CompanyUpdate
                  item={{ ...item, type: 'link' }}
                  title="机构"
                  reload={action?.reload}
                />
                <MyButtons.View
                  title="配置"
                  onClick={() => {
                    navigate(`/company/${item.id}`);
                  }}
                />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Common.Admins.Delete({ id: item.id }).then(() =>
                      action?.reload(),
                    )
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
