import { MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';
import EmployeeShow from '@/pages/company/employees/modals/EmployeeShow';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import CodeInfo from '../../contract_archives/modals/Show';
import Read from './modals/Read';
import Show from './modals/Show';

export default function Index({ title = '合同借阅' }) {
  return (
    <ProTable
      {...MyProTableProps.props}
      request={async (params, sort) =>
        MyProTableProps.request(
          params,
          sort,
          Apis.Contract.ContractArchiveReads.List,
        )
      }
      headerTitle="合同借阅列表"
      toolBarRender={(action) => [
        <Read key="Read" reload={action?.reload} title={title} />,
      ]}
      columns={[
        MyColumns.ID({
          search: false,
        }),

        {
          title: '归档编号',
          dataIndex: ['contract_archive', 'code'],
          render: (_, item: any) => (
            <CodeInfo
              item={{ ...item, type: 'link' }}
              title={item?.contract_archive?.code}
            />
          ),
        },
        {
          title: '借阅信息',
          search: false,
          render: (_, item: any) => {
            if (
              !item?.contract_archive_read_files ||
              item.contract_archive_read_files.length === 0
            ) {
              return '-';
            }
            return (
              <div>
                {item.contract_archive_read_files.map(
                  (res: any, index: number) => (
                    <div key={`item_${index}`} style={{ marginBottom: 4 }}>
                      文件: {res?.contract_archive_file?.name}
                    </div>
                  ),
                )}
              </div>
            );
          },
        },
        {
          title: '借阅人',
          dataIndex: 'reader_name',
          render: (_, item: any) => (
            <EmployeeShow
              item={{ ...item, type: 'link', id: item?.reader_id }}
              title={item?.reader_name}
            />
          ),
        },
        {
          title: '借阅原因',
          dataIndex: 'read_reason',
          search: false,
        },
        {
          title: '借阅日期',
          dataIndex: 'read_date',
          search: false,
        },
        {
          title: '登记人',
          dataIndex: ['registrar', 'name'],
          search: false,
          render: (_, item: any) => (
            <EmployeeShow
              item={{ ...item, type: 'link', id: item?.registrar_id }}
              title={item?.registrar_name}
            />
          ),
        },
        // MyColumns.CreatedAt(),
        // MyColumns.UpdatedAt(),
        MyColumns.Option({
          width: 100,
          render: (_, item: any, index, action) => (
            <Space key={index}>
              <Show item={item} reload={action?.reload} title={title} />
            </Space>
          ),
        }),
      ]}
    />
  );
}
