import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { Form, message } from 'antd';
import { useState } from 'react';

export default function DepositRefund(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  const [billId, setBillId] = useState<number | null>(null);

  const handleOpenChange = (open: boolean) => {
    if (open) {
      form.resetFields();
      setBillId(null);
      Apis.Renovation.RenovationApplies.Bills({
        id: props?.item?.id,
      }).then((res: any) => {
        const bills = res?.data || [];
        const depositBill = bills.find(
          (b: any) => b.type === 'RenovationDeposit',
        );
        if (depositBill?.id) {
          setBillId(depositBill.id);
          form.setFieldsValue({
            refund_amount: depositBill?.amount,
          });
        }
      });
    }
  };

  return (
    <BetaSchemaForm<ApiTypes.Bill.BillRefunds.Store>
      {...MyModalFormProps.props}
      title={`装修押金退款`}
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 20 }}
      width="600px"
      layout="horizontal"
      trigger={
        <MyButtons.Default
          title={`押金退款`}
          size="small"
          disabled={!props?.item?.deposit_amount}
        />
      }
      onOpenChange={handleOpenChange}
      key={new Date().getTime()}
      form={form}
      onFinish={async (values) => {
        if (!billId) {
          message.error('未找到押金账单，无法退款');
          return false;
        }
        return Apis.Bill.BillRefunds.Store({
          ...values,
          bill_id: billId,
        })
          .then(() => {
            props.reload?.();
            message.success('押金退款申请提交成功');
            return true;
          })
          .catch(() => false);
      }}
      columns={[
        {
          key: 'refund_amount',
          title: '退款金额',
          valueType: 'digit',
          colProps: { span: 24 },
          fieldProps: {
            style: { width: '100%' },
            addonAfter: '元',
          },
          formItemProps: { ...rulesHelper.number },
        },
        {
          key: 'refund_reason',
          title: '退款原因',
          valueType: 'textarea',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            placeholder: '请详细描述退款原因',
            rows: 4,
          },
        },
        {
          key: 'refund_notes',
          title: '退款备注',
          valueType: 'textarea',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            placeholder: '请填写退款备注',
            rows: 4,
          },
        },
        MyFormItems.UploadImages({
          key: 'pay_certificate',
          title: '退款凭证',
          tooltip: '最多上传5张图片',
          max: 5,
          colProps: { span: 24 },
          formItemProps: { required: false },
        }),
      ]}
    />
  );
}
