import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { CompanySuppliersCounterpartyEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import Create from './modals/Create';
import Update from './modals/Update';

export default function Index({ title = '企业信息' }) {
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="company_internal_list"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            {
              ...params,
              counterparty:
                CompanySuppliersCounterpartyEnum.InternalCompany.value,
            },
            sort,
            Apis.CompanySupplier.CompanySuppliers.List,
          )
        }
        toolBarRender={(action) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              add: (
                <Create key="Create" reload={action?.reload} title={title} />
              ),
            }}
          />,
        ]}
        columns={[
          MyColumns.ID({ search: false }),
          {
            title: '企业名称',
            dataIndex: 'name',
          },
          {
            title: '营业执照',
            dataIndex: 'tax_number',
          },
          {
            title: '所在城市',
            search: false,
            render: (_, item: any) => {
              return `${item.province} ${item.city || ''} `;
            },
          },
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, __, action) => (
              <MyTableActions
                actions={{
                  update: (
                    <Update item={item} reload={action?.reload} title={title} />
                  ),
                  delete: (
                    <MyButtons.Delete
                      onConfirm={() =>
                        Apis.CompanySupplier.CompanySuppliers.Delete({
                          id: item.id,
                        }).then(() => action?.reload())
                      }
                    />
                  ),
                }}
              />
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
