import {
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} 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 { Radio } from 'antd';
import dayjs from 'dayjs';
import { useState } from 'react';
import Show from './modals/Show';
import Update from './modals/Update';
import UseSeal from './modals/UseSeal';
import SealsManage from './seals/SealsManage';

export default function Index({ title = '合同用印' }) {
  const navigate = useNavigate();
  const [isUseSeal, setIsUseSeal] = useState(false);

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="contract_seal_logs"
      tabLabel={title}
    >
      <ProTable
        key={isUseSeal ? 'used' : 'unused'}
        {...MyProTableProps.props}
        request={async (params, sort) => {
          return MyProTableProps.request(
            {
              ...params,
              is_use_seal: isUseSeal,
              ...(isUseSeal
                ? {}
                : { status: ContractsStatusEnum.Approved.value }),
            },
            sort,
            Apis.Contract.Contracts.List,
          );
        }}
        headerTitle={
          <Radio.Group
            value={isUseSeal}
            onChange={(e) => setIsUseSeal(e.target.value)}
            optionType="button"
            buttonStyle="solid"
          >
            <Radio.Button value={false}>未用印</Radio.Button>
            <Radio.Button value={true}>已用印</Radio.Button>
          </Radio.Group>
        }
        toolBarRender={() => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              manage: <SealsManage key="SealsManage" />,
            }}
          />,
        ]}
        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, __, action) => (
              <MyTableActions
                actions={{
                  useSeal: !isUseSeal ? (
                    <UseSeal item={item} reload={action?.reload} title="用印" />
                  ) : undefined,
                  show: isUseSeal ? <Show item={item} /> : undefined,
                  update: isUseSeal ? (
                    <Update
                      item={item}
                      reload={action?.reload}
                      title="合同用章"
                    />
                  ) : undefined,
                }}
              />
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
