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

export default function Index({ title = '印章管理' }) {
  return (
    <ProTable
      {...MyProTableProps.props}
      request={async (params, sort) =>
        MyProTableProps.request(
          params,
          sort,
          Apis.CompanySeal.CompanySeals.List,
        )
      }
      toolBarRender={(action) => [
        <Create key="Create" reload={action?.reload} title={title} />,
      ]}
      columns={[
        MyColumns.ID({ search: false }),
        {
          title: '企业名称',
          dataIndex: ['company_supplier', 'name'],
          search: { transform: (value) => ({ company_supplier_name: value }) },
        },
        MyColumns.EnumTag({
          title: '类型',
          dataIndex: 'type',
          valueEnum: CompanySealsTypeEnum,
        }),
        {
          title: '印章图片',
          search: false,
          render: (_, item: any) => {
            if (!item?.seal_image || !item.seal_image[0]?.url) {
              return '-';
            }
            return <Image src={item.seal_image[0].url} height="40px" />;
          },
        },
        {
          title: '保管人',
          search: false,
          render: (_, item: any) => {
            return `${item.company_employee?.name || ''}_${
              item.company_employee?.phone || ''
            }`;
          },
        },
        MyColumns.Boolean({
          dataIndex: 'is_enabled',
          title: '是否启用',
          search: false,
        }),
        {
          title: '启用日期',
          dataIndex: 'use_date',
          search: false,
        },
        // MyColumns.UpdatedAt(),
        // MyColumns.CreatedAt(),
        MyColumns.Option({
          render: (_, item: any, index, action) => (
            <Space key={index}>
              <Update item={item} reload={action?.reload} title={title} />
              <MyButtons.Default
                title={item.is_enabled ? '禁用' : '启用'}
                type={item.is_enabled ? 'default' : 'primary'}
                danger={item.is_enabled}
                isConfirm={true}
                description={
                  item.is_enabled ? `确定要禁用印章吗？` : `确定要启用印章吗？`
                }
                onConfirm={() =>
                  Apis.CompanySeal.CompanySeals.Update({
                    id: item.id ?? 0,
                    is_enabled: item.is_enabled ? 0 : 1,
                    type: item.type,
                    company_suppliers_id: item.company_suppliers_id,
                  }).then(() => action?.reload())
                }
              />
              <MyButtons.Delete
                onConfirm={() =>
                  Apis.CompanySeal.CompanySeals.Delete({ id: item.id }).then(
                    () => action?.reload(),
                  )
                }
              />
            </Space>
          ),
        }),
      ]}
    />
  );
}
