import { MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';
import {
  ApprovalTemplatesTypeEnum,
  HouseBillsBillStatusEnum,
  HouseBillsTypeEnum,
  RefundsTypeEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import { useEffect, useRef } from 'react';
import BillRefund from '../../house_bills/modals/BillRefund';
import BillShow from './modals/BillShow';

export default function Index({ ...rest }) {
  const actionLooks = useRef<any>();
  useEffect(() => {
    actionLooks?.current.reloadAndRest();
  }, [rest.loadmore]);

  return (
    <>
      <ProTable<Record<any, any>>
        {...MyProTableProps.props}
        actionRef={actionLooks}
        headerTitle="已付账单"
        request={async (params, sort) =>
          MyProTableProps.request(
            {
              ...params,
              asset_houses_id: rest.item?.asset_houses_id,
              bill_status: [HouseBillsBillStatusEnum.Paid.value],
            },
            sort,
            Apis.Bill.HouseBills.List,
          )
        }
        search={false}
        columns={[
          MyColumns.ID({
            search: false,
          }),
          MyColumns.EnumTag({
            title: '状态',
            dataIndex: 'bill_status',
            valueEnum: HouseBillsBillStatusEnum,
          }),
          {
            title: '账单月份',
            render: (_, record) => {
              return `${record.year}-${String(record.month).padStart(2, '0')}`;
            },
          },
          MyColumns.EnumTag({
            title: '类型',
            dataIndex: 'type',
            valueEnum: HouseBillsTypeEnum,
          }),
          {
            title: '应收金额',
            dataIndex: 'total_payable_amount',
            search: false,
          },
          {
            title: '已收合计',
            dataIndex: 'total_paid_amount',
            search: false,
          },
          {
            title: '计费周期',
            dataIndex: 'start_date',
            render: (_, record) => {
              return `${record.start_date} 至 ${record.end_date}`;
            },
            search: false,
          },
          {
            title: '支付日期',
            dataIndex: 'paid_time',
            valueType: 'date',
            search: false,
          },
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <BillShow item={item} title="查看" reload={action?.reload} />
                <BillRefund
                  item={{
                    ...item,
                    // 跟据发起的页面传递的type来判断退款类型
                    type: RefundsTypeEnum.HouseBill.value,
                    // 审批模板的类型
                    approval_type: ApprovalTemplatesTypeEnum.Refund.value,
                    total_paid_amount: item.total_paid_amount,
                  }}
                  reload={action?.reload}
                  title="退款"
                />
              </Space>
            ),
          }),
        ]}
      />
    </>
  );
}
