import { MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';
import { HouseBillsBillStatusEnum, HouseBillsTypeEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space, Tooltip } from 'antd';
import { useEffect, useRef, useState } from 'react';
import BillCopy from '../../house_bills/modals/BillCopy';
import PayCreate from './modals/PayCreate';
import ShowQrCode from './modals/ShowQrCode';

export default function Index({ ...rest }) {
  const actionLooks = useRef<any>();
  const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);

  useEffect(() => {
    actionLooks?.current.reloadAndRest();
  }, [rest.loadmore]);

  const rowSelection = {
    onChange: (newSelectedRowKeys: React.Key[], selectedRows: any[]) => {
      setSelectedRowKeys(selectedRows);
    },
  };

  return (
    <>
      <ProTable<Record<any, any>>
        {...MyProTableProps.props}
        actionRef={actionLooks}
        headerTitle="欠费账单"
        request={async (params, sort) =>
          MyProTableProps.request(
            {
              ...params,
              ...(rest.item?.asset_car_ports_id
                ? { car_port_name: rest.item?.full_name || '' }
                : { asset_houses_id: rest.item?.asset_houses_id }),
              bill_status: [
                HouseBillsBillStatusEnum.PendingPayment.value,
                HouseBillsBillStatusEnum.PartiallyPaid.value,
                HouseBillsBillStatusEnum.Overdue.value,
              ],
            },
            sort,
            Apis.Bill.HouseBills.List,
          )
        }
        toolBarRender={(action) => [
          <ShowQrCode
            key="ShowQrCode"
            item={{
              list: selectedRowKeys,
              ...rest.item,
            }}
            title="生成收款二维码"
          />,
          <PayCreate
            key="Create"
            reload={action?.reload}
            title="收款"
            item={rest.item}
            selectedBillIds={selectedRowKeys}
            selectedRows={actionLooks.current?.selectedRows || []}
          />,
        ]}
        search={false}
        rowSelection={rowSelection}
        columns={[
          MyColumns.ID({
            search: false,
          }),
          MyColumns.EnumTag({
            title: '状态',
            dataIndex: 'bill_status',
            valueEnum: HouseBillsBillStatusEnum,
            search: false,
          }),
          {
            title: '账单月份',
            dataIndex: 'month',
            render: (_, record) => {
              return `${record.year}-${String(record.month).padStart(2, '0')}`;
            },
            search: false,
          },
          {
            title: '计费开始日期',
            dataIndex: 'start_date',
            search: false,
            hidden: true,
            sorter: true,
            defaultSortOrder: 'ascend',
          },
          MyColumns.EnumTag({
            title: '类型',
            dataIndex: 'type',
            valueEnum: HouseBillsTypeEnum,
            search: false,
          }),
          {
            title: '账单金额',
            dataIndex: 'amount',
            search: false,
          },
          {
            title: '优惠金额',
            dataIndex: 'discount_amount',
            search: false,
          },
          {
            title: '滞纳金',
            dataIndex: 'late_fee',
            search: false,
          },
          {
            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: 'receipt_account',
            render: (_, record) => {
              const text = `${record.receipt_account.company_account}｜${record.receipt_account?.company_name}｜${record.receipt_account?.company_bank}`;
              return (
                <Tooltip title={text}>
                  <span>
                    {text.length > 16 ? `${text.slice(0, 16)}...` : text}
                  </span>
                </Tooltip>
              );
            },
            search: false,
          },
          MyColumns.Option({
            width: 100,
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <BillCopy
                  key="Copy"
                  item={{
                    ...item,
                  }}
                  reload={action?.reload}
                  title="复制"
                />
              </Space>
            ),
          }),
        ]}
      />
    </>
  );
}
