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

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

  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: 'name',
          },
          {
            title: '机构简称',
            dataIndex: 'short_name',
            search: false,
          },
          // 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}>
                <CompanyShow item={item} title="查看" reload={action?.reload} />
                <CompanyUpdate
                  item={{ ...item, type: 'link' }}
                  title="机构"
                  reload={action?.reload}
                />
                <MyButtons.View
                  title="配置"
                  data-tooltip-position="top"
                  onClick={() => {
                    navigate(`/company/list/show/${item.id}`);
                  }}
                />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Company.Companies.Delete({ id: item.id }).then(() =>
                      action?.reload(),
                    )
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
