import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  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 Archive from './modals/Archive';
import Orther from './modals/Orther';

export default function Index({ title = '合同归档' }) {
  const navigate = useNavigate();

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="archives"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) => {
          return MyProTableProps.request(
            {
              ...params,
              status: ContractsStatusEnum.Signed.value,
            },
            sort,
            Apis.Contract.Contracts.List,
          );
        }}
        headerTitle="未归档合同"
        toolBarRender={(action) => [
          <MyButtons.Default
            key="finished"
            title="归档记录"
            type="primary"
            size="middle"
            onClick={() => {
              navigate('/contract/contract_archives/finished');
            }}
          />,
          <MyButtons.Default
            key="transfers"
            title="转移记录"
            type="primary"
            size="middle"
            onClick={() => {
              navigate('/contract/contract_archives/transfers');
            }}
          />,
          <Orther key="Orther" reload={action?.reload} title="其他合同" />,
        ]}
        columns={[
          MyColumns.ID({ search: false }),
          {
            title: '合同类型',
            dataIndex: 'contract_type_name',
            search: false,
          },
          {
            title: '合同名称',
            dataIndex: 'name',
          },
          {
            title: '合同编号',
            dataIndex: 'code',
            render: (_: any, 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: (_: any, item: any) => {
              return `${dayjs(item.start_time).format('YYYY-MM-DD')}至${dayjs(
                item.end_time,
              ).format('YYYY-MM-DD')}`;
            },
          },
          {
            title: '签订时间',
            dataIndex: 'sign_time',
            render: (_: any, item: any) => {
              return item.sign_time
                ? dayjs(item.sign_time).format('YYYY-MM-DD')
                : '-';
            },
            search: false,
          },
          MyColumns.Option({
            render: (_: any, item: any, _index: any, action: any) => (
              <Space>
                <Archive item={item} reload={action?.reload} title="归档" />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
