import { Button, ButtonSize, Dialog, Toast } from "@nutui/nutui-react-taro";
import { business } from "@/services";

interface IOrderFinalApproveProps {
  auditVO: BusinessAPI.AuditVO;
  size?: ButtonSize;
  onFinish?: () => void;
}

export default function OrderFinalApprove(props: IOrderFinalApproveProps) {
  const { auditVO, size = "normal", onFinish } = props;

  // 处理审批通过操作
  const handleApprove = () => {
    Dialog.open("dialog", {
      title: "审批通过",
      content: "确定要审批通过该采购订单吗？",
      confirmText: "确认",
      cancelText: "取消",
      onConfirm: async () => {
        await confirmApprove();
        Dialog.close("dialog");
      },
      onCancel: () => {
        Dialog.close("dialog");
      },
    });
  };

  // 确认审批通过
  const confirmApprove = async () => {
    try {
      // 调用最终审批API
      const { data } = await business.order.finalApproveOrder({
        orderId: auditVO.subjectId,
        auditId: auditVO.auditId,
      });

      if (data.success) {
        Toast.show("toast", {
          icon: "success",
          title: "提示",
          content: "审批通过成功",
        });
        onFinish?.();
      }
    } catch (error) {
      Toast.show("toast", {
        icon: "fail",
        title: "提示",
        content: error.message || "审批通过失败",
      });
    }
  };

  return (
    <Button
      block
      type={"primary"}
      size={size}
      onClick={(e) => {
        handleApprove();
        e.stopPropagation();
      }}
    >
      通过
    </Button>
  );
}
