import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  ContractsContractNatureEnum,
  ContractsStatusEnum,
  ContractTemplatesIncomeExpenseTypeEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate, useSearchParams } from '@umijs/max';
import dayjs from 'dayjs';

import Archives from '../contract_archives/modals/Archive';
import UseSeal from '../contract_seal_logs/modals/UseSeal';
import Terminated from './modals/Terminated';
import Voided from './modals/Voided';

export default function Index({ title = '补充协议' }) {
  const navigate = useNavigate();
  const [searchParams, setSearchParams]: any = useSearchParams();

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="contracts_list"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) => {
          setSearchParams(params);
          return MyProTableProps.request(
            {
              params,
              is_agreement: true,
            },
            sort,
            Apis.Contract.Contracts.List,
          );
        }}
        headerTitle="补充协议"
        // toolBarRender={(action) => [toolBarRender(action)]}
        columns={[
          MyColumns.ID({ search: false }),
          Selects?.AssetProjects({
            title: '关联项目',
            key: 'asset_projects_id',
            hidden: true,
          }),

          MyColumns.EnumTag({
            title: '合同状态',
            dataIndex: 'status',
            valueEnum: ContractsStatusEnum,
            fieldProps: {
              defaultValue: searchParams?.get('status') || undefined,
            },
          }),
          {
            title: '合同编号',
            dataIndex: 'code',
          },
          {
            title: '合同名称',
            dataIndex: 'name',
            // render: (_, item: any) => {
            //   return (
            //     <a
            //       onClick={() => {
            //         navigate(`/contract/contracts/show/${item.id}`);
            //       }}
            //     >
            //       {item?.name}
            //     </a>
            //   );
            // },
          },
          {
            title: '合同类型',
            dataIndex: 'contract_type_name',
            search: false,
          },

          MyColumns.EnumTag({
            title: '收支类型',
            dataIndex: 'income_expense_type',
            valueEnum: ContractTemplatesIncomeExpenseTypeEnum,
          }),
          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: ['asset_project', 'name'],

            search: false,
          },
          {
            title: '合同对接人',
            dataIndex: 'contract_liaison',
            search: false,
          },
          {
            title: '用印日期',
            dataIndex: 'sign_time',
            valueType: 'date',
            render: (_, item: any) => {
              return item.sign_time
                ? dayjs(item.sign_time).format('YYYY-MM-DD')
                : '-';
            },
          },
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, __, action) => (
              <MyTableActions
                actions={{
                  view: (
                    <MyButtons.View
                      key="configInfo"
                      title="主合同"
                      onClick={() => {
                        navigate(`/contract/contracts/show/${item.parent_id}`);
                      }}
                    />
                  ),
                  update: (
                    <MyButtons.Default
                      key="Update"
                      size="small"
                      type="primary"
                      disabled={
                        item.status !== 'TemporaryStorage' &&
                        item.status !== 'Rejected'
                      }
                      onClick={() => {
                        navigate(
                          `/contract/contracts/pages/contract_add_update?id=${item.id}`,
                        );
                      }}
                      title="编辑"
                    />
                  ),
                  useSeal: (
                    <UseSeal
                      key="UseSeal"
                      item={{ ...item, disabled: true }}
                      reload={action?.reload}
                      title="申请用印"
                    />
                  ),
                  archives: (
                    <Archives
                      key="Archives"
                      item={{ ...item, disabled: true }}
                      reload={action?.reload}
                      title="归档"
                    />
                  ),
                  Terminated: (
                    <Terminated
                      key="Terminated"
                      item={item}
                      reload={action?.reload}
                      title="解除"
                    />
                  ),
                  voided: (
                    <Voided
                      key="Voided"
                      item={item}
                      reload={action?.reload}
                      title="撤销"
                    />
                  ),
                  delete: (
                    <MyButtons.Default
                      key="Delete"
                      title="取消"
                      disabled={
                        item?.status !== 'TemporaryStorage' &&
                        item?.status !== 'Voided'
                      }
                      isConfirm={true}
                      description={`您正在操作取消「${item.name}」，取消后，合同不能做任何操作，是否继续？`}
                      onConfirm={() =>
                        Apis.Contract.Contracts.SoftDelete({
                          id: item.id,
                        }).then(() => action?.reload())
                      }
                    />
                  ),
                }}
              />
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
