import { MyButtons, MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';
import { ContractArchiveBorrowStatusEnum } from '@/gen/Enums';
import EmployeeShow from '@/pages/company/employees/modals/EmployeeShow';
import CodeInfo from '@/pages/contract/contract_archives/finished/modals/Show';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import Create from './modals/Create';
import Show from './modals/Show';
import Update from './modals/Update';

export default function Index({ title = '合同借用' }) {
  return (
    <ProTable
      {...MyProTableProps.props}
      request={async (params, sort) =>
        MyProTableProps.request(
          params,
          sort,
          Apis.Contract.ContractArchiveBorrows.List,
        )
      }
      headerTitle="合同借用列表"
      toolBarRender={(action) => [
        <Create key="Create" reload={action?.reload} title={title} />,
      ]}
      columns={[
        MyColumns.ID({
          search: false,
        }),
        MyColumns.EnumTag({
          title: '借用状态',
          dataIndex: 'borrow_status',
          valueEnum: ContractArchiveBorrowStatusEnum,
        }),
        // MyColumns.EnumTag({
        //   title: '归还状态',
        //   dataIndex: 'return_status',
        //   valueEnum: ContractArchiveFileReturnStatusEnum,
        // }),
        {
          title: '档案编号',
          dataIndex: ['contract_archive', 'code'],
          render: (_, item: any) => (
            <CodeInfo
              item={{ ...item, type: 'link', id: item?.contract_archives_id }}
              title={item?.contract_archive?.code}
            />
          ),
        },
        {
          title: '借用信息',
          search: false,
          render: (_, item: any) => {
            if (!item?.borrow_files || item.borrow_files.length === 0) {
              return '-';
            }
            return (
              <div>
                {item.borrow_files.map((res: any, index: number) => (
                  <div key={index} style={{ marginBottom: 4 }}>
                    文件: {res?.file?.name}，借用: {res?.borrow_number}，遗失:{' '}
                    {res?.lost_number}，损坏: {res?.damaged_number}
                  </div>
                ))}
              </div>
            );
          },
        },
        {
          title: '借用人',
          dataIndex: 'borrower_name',
          search: false,
          render: (_, item: any) => (
            <EmployeeShow
              item={{ ...item, type: 'link', id: item?.borrower_id }}
              title={item?.borrower_name}
            />
          ),
        },

        {
          title: '借用日期',
          dataIndex: 'borrow_date',
          search: false,
        },
        {
          title: '应归还日期',
          dataIndex: 'expected_return_date',
          search: false,
        },
        MyColumns.Boolean({
          title: '需要归还',
          dataIndex: 'is_need_return',
          search: false,
        }),
        {
          title: '登记人',
          dataIndex: ['registrar', 'name'],
          search: false,
          render: (_, item: any) => (
            <EmployeeShow
              item={{ ...item, type: 'link', id: item?.operator_id }}
              title={item?.operator_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} />
              <MyButtons.Default
                title="登记"
                type="primary"
                disabled={item?.borrow_status !== 'Approved'}
                isConfirm={true}
                description={`是否确认借用人已领取？`}
                onConfirm={() =>
                  Apis.Contract.ContractArchiveBorrows.Borrow({
                    id: item.id,
                  }).then(() => action?.reload())
                }
              />
              <Update item={item} reload={action?.reload} title={title} />
            </Space>
          ),
        }),
      ]}
    />
  );
}
