import { MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';
import {
  ContractsContractNatureEnum,
  ContractsStatusEnum,
  ContractTemplatesIncomeExpenseTypeEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space } from 'antd';
import dayjs from 'dayjs';
import UseSeal from './modals/UseSeal';

export default function Index() {
  const navigate = useNavigate();

  return (
    <ProTable
      {...MyProTableProps.props}
      request={async (params, sort) => {
        return MyProTableProps.request(
          {
            ...params,
            status: ContractsStatusEnum.Approved.value,
            is_use_seal: false,
          },
          sort,
          Apis.Contract.Contracts.List,
        );
      }}
      headerTitle="未用印合同"
      columns={[
        MyColumns.ID({ search: false }),
        {
          title: '合同类型',
          dataIndex: 'contract_type_name',
          search: false,
        },
        {
          title: '合同名称',
          dataIndex: 'name',
        },
        {
          title: '合同编号',
          dataIndex: 'code',
          render: (_, item: any) => {
            return (
              <a
                onClick={() => {
                  navigate(`/contract/contracts/show/${item.id}`);
                }}
              >
                {item?.name}
              </a>
            );
          },
        },
        MyColumns.EnumTag({
          title: '状态',
          dataIndex: 'status',
          valueEnum: ContractsStatusEnum,
          search: false,
        }),
        MyColumns.EnumTag({
          title: '收支类型',
          dataIndex: 'income_expense_type',
          valueEnum: ContractTemplatesIncomeExpenseTypeEnum,
          search: false,
        }),
        MyColumns.EnumTag({
          title: '合同性质',
          dataIndex: 'contract_nature',
          valueEnum: ContractsContractNatureEnum,
          search: false,
        }),
        {
          title: '有效期',
          search: false,
          render: (_, item: any) => {
            return `${dayjs(item.start_time).format('YYYY-MM-DD')}至${dayjs(
              item.end_time,
            ).format('YYYY-MM-DD')}`;
          },
        },
        {
          title: '签订时间',
          dataIndex: 'sign_time',
          render: (_, item: any) => {
            return item.sign_time
              ? dayjs(item.sign_time).format('YYYY-MM-DD')
              : '-';
          },
          search: false,
        },
        MyColumns.Option({
          render: (_, item: any, index, action) => (
            <Space key={index}>
              <UseSeal item={item} reload={action?.reload} title="用印" />
            </Space>
          ),
        }),
      ]}
    />
  );
}
