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

export default function Index({ title = '机构管理' }) {
  return (
    <MyPageContainer title={title}>
      <ProTable
        {...MyProTableProps.props}
        search={false}
        request={async (params, sort) =>
          MyProTableProps.request(params, sort, Apis.Company.Companies.List)
        }
        toolBarRender={(action) => [
          <Create key="Create" reload={action?.reload} title={title} />,
        ]}
        columns={[
          MyColumns.ID(),
          {
            title: '组织名称',
            dataIndex: 'name',
          },
          {
            title: '组织简称',
            dataIndex: 'short_name',
          },
          MyColumns.EnumTag({
            title: '类型',
            dataIndex: 'merchant_type',
            valueEnum: CompaniesMerchantTypeEnum,
          }),
          {
            title: '营业执照号',
            dataIndex: 'business_license_number',
          },
          {
            title: '联系人',
            dataIndex: 'contact_name',
          },
          {
            title: '手机号',
            dataIndex: 'contact_phone',
          },
          {
            title: '联系人邮箱',
            dataIndex: 'contact_email',
          },
          MyColumns.UpdatedAt(),
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <Update item={item} reload={action?.reload} title={title} />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Common.Admins.Delete({ id: item.id }).then(() =>
                      action?.reload(),
                    )
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
