import {
  MyBetaModalFormProps,
  MyButtons,
  MyColumns,
  MyProTableProps,
} from '@/common';
import { Apis } from '@/gen/Apis';
import {
  ContractBillsAuditStatusEnum,
  ContractBillsCostTypeEnum,
  HouseOrdersOrderStatusEnum,
} from '@/gen/Enums';
import CompleteApply from '@/pages/contract/contract_bills/modals/CompleteApply';
import PayBills from '@/pages/contract/contract_bills/modals/PayBills';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space } from 'antd';

export default function Index(props: MyBetaModalFormProps) {
  const navigate = useNavigate();
  return (
    <ProTable<Record<any, any>>
      {...MyProTableProps.props}
      request={async (params, sort) =>
        MyProTableProps.request(
          { ...params, contracts_id: props?.item?.id },
          sort,
          Apis.Contract.ContractBills.List,
        )
      }
      headerTitle="账单列表"
      toolBarRender={(action) => [
        <MyButtons.Create
          key="ContractBillsCreate"
          onClick={() => {
            navigate(
              `/contract/contract_bills/pages/create?id=${props?.item?.id}`,
            );
          }}
          disabled={
            props?.item?.status === 'TemporaryStorage' ||
            props?.item?.status === 'UnderApproval' ||
            props?.item?.status === 'Rejected' ||
            props?.item?.status === 'Terminating' ||
            props?.item?.status === 'Terminated' ||
            props?.item?.status === 'Voided' ||
            props?.item?.status === 'Canceled'
          }
          title="添加账单"
        />,
      ]}
      columns={[
        MyColumns.ID({ search: false }),
        {
          title: '账单月',
          search: false,
          render: (_, item: any) => {
            return `${item?.year || 0}-${
              item?.month < 10 ? `0${item?.month}` : item?.month
            }`;
          },
        },
        {
          title: '期数',
          dataIndex: 'period_num',
          search: false,
        },

        MyColumns.EnumTag({
          title: '支付状态',
          dataIndex: 'payment_status',
          valueEnum: HouseOrdersOrderStatusEnum,
        }),
        MyColumns.EnumTag({
          title: '费用类型',
          dataIndex: 'cost_type',
          valueEnum: ContractBillsCostTypeEnum,
        }),
        {
          title: '账单金额',
          search: false,
          render: (_, item: any) => {
            return `¥${item?.amount || 0}`;
          },
        },
        MyColumns.EnumTag({
          title: '付款审核',
          dataIndex: 'audit_status',
          valueEnum: ContractBillsAuditStatusEnum,
        }),
        MyColumns.CreatedAt(),
        MyColumns.Option({
          render: (_, item: any, index, action) => (
            <Space key={index}>
              <CompleteApply
                item={item}
                reload={action?.reload}
                title="申请付款"
              />
              <PayBills item={item} reload={action?.reload} title="登记" />
              <MyButtons.Default
                key="Create"
                size="small"
                type="primary"
                disabled={
                  item?.payment_status === 'Paid' ||
                  item?.audit_status === 'Approved' ||
                  item?.audit_status === 'Pending'
                }
                onClick={() => {
                  navigate(
                    `/contract/contract_bills/pages/update?id=${item.id}`,
                  );
                }}
                title="编辑"
              />
              <MyButtons.Delete
                disabled={item?.payment_status === 'Paid'}
                onConfirm={() =>
                  Apis.Contract.ContractBills.Delete({ id: item.id }).then(() =>
                    action?.reload(),
                  )
                }
              />
            </Space>
          ),
        }),
      ]}
    />
  );
}
