import { MyBetaModalFormProps, MyButtons } from '@/common';
import { Apis } from '@/gen/Apis';
import { Modal } from 'antd';
import { useEffect, useState } from 'react';
import MyInfo from '../components/info';
import AuditReview from './AuditReview';

export default function Review(props: MyBetaModalFormProps) {
  const [open, setOpen] = useState(false);
  const [data, setData] = useState<any>({});
  const [loading, setLoading] = useState(false);

  useEffect(() => {
    if (open && props.item?.id) {
      setLoading(true);
      Apis.Renovation.RenovationApplies.Show({ id: Number(props.item.id) })
        .then((res) => {
          setData(res?.data || {});
        })
        .finally(() => {
          setLoading(false);
        });
    }
  }, [open, props.item?.id]);

  const handleAuditSuccess = () => {
    props.reload?.();
    setOpen(false);
  };

  return (
    <>
      <MyButtons.Default
        type="primary"
        size="small"
        title="审核"
        onClick={() => setOpen(true)}
      />
      <Modal
        title="装修申请审核"
        open={open}
        onCancel={() => setOpen(false)}
        width={1000}
        footer={
          <div style={{ display: 'flex', justifyContent: 'flex-end', gap: 8 }}>
            <MyButtons.Default
              size="middle"
              title="关闭"
              onClick={() => setOpen(false)}
            />
            <AuditReview item={data} reload={handleAuditSuccess} title="审核" />
          </div>
        }
        destroyOnClose
      >
        <MyInfo item={{ ...data, loading }} />
      </Modal>
    </>
  );
}
