import Button from "@mui/material/Button";
import Dialog from "@mui/material/Dialog";
import DialogActions from "@mui/material/DialogActions";
import DialogContent from "@mui/material/DialogContent";
import DialogTitle from "@mui/material/DialogTitle";
import TextField from "@mui/material/TextField";
import { useEffect, useState } from "react";
import { AppIdentity } from "@/shared/ui/AppIdentity.jsx";
import { UploadField } from "@/shared/ui/UploadField.jsx";

export function FinanceAuditDialog({
  application,
  apps = [],
  loading,
  onClose,
  onSubmit,
  open,
  requireApprovalImage = false,
  requireRejectRemark = false,
  reviewLabel = "财务",
}) {
  const [remark, setRemark] = useState("");
  const [auditImageUrl, setAuditImageUrl] = useState("");
  const decision = application?.decision || "approved";
  const needsImage = decision === "approved" && requireApprovalImage;
  const needsRejectRemark = decision === "rejected" && requireRejectRemark;
  const isOperationsRejectRetry =
    decision === "rejected" && application?.operationsStatus === "rejecting" && application?.reviewStage === "operations";
  const submitDisabled = loading || (needsImage && !auditImageUrl) || (needsRejectRemark && !remark.trim());

  useEffect(() => {
    if (open) {
      // rejecting 已持久化了原拒绝原因；重试必须默认复用该原因，避免补偿动作改写审核语义。
      setRemark(isOperationsRejectRetry ? application?.operationsAuditRemark || "" : "");
      setAuditImageUrl("");
    }
  }, [application?.operationsAuditRemark, isOperationsRejectRetry, open]);

  const submit = () => {
    onSubmit({ auditImageUrl, decision, remark });
  };

  return (
    <Dialog fullWidth maxWidth="sm" open={open} onClose={loading ? undefined : onClose}>
      <DialogTitle>{decision === "approved" ? `通过${reviewLabel}审核` : `拒绝${reviewLabel}审核`}</DialogTitle>
      <DialogContent className="finance-audit-dialog">
        <div className="finance-audit-summary">
          <AppIdentity
            app={apps.find((app) => app.appCode === application?.appCode)}
            appCode={application?.appCode}
            appName={application?.appName}
            size="small"
          />
          <strong>{application?.withdrawMethod || "用户提现"}</strong>
          <span>{application?.userId || "-"}</span>
        </div>
        <TextField
          // 后端会固定沿用首次 claim 的原因；只读展示可避免用户误以为重试能修改审核事实。
          disabled={loading || isOperationsRejectRetry}
          label="审核备注"
          minRows={4}
          multiline
          value={remark}
          onChange={(event) => setRemark(event.target.value)}
        />
        {needsRejectRemark && !remark.trim() ? <span className="finance-audit-hint">拒绝时必须填写原因</span> : null}
        {needsImage ? (
          <UploadField
            disabled={loading}
            hideType
            kind="image"
            label="打款凭证图片"
            uploadKind="image"
            value={auditImageUrl}
            onChange={setAuditImageUrl}
          />
        ) : null}
      </DialogContent>
      <DialogActions>
        <Button disabled={loading} onClick={onClose}>
          取消
        </Button>
        <Button disabled={submitDisabled} variant="contained" onClick={submit}>
          确认
        </Button>
      </DialogActions>
    </Dialog>
  );
}
