"use client"

import Swal from "sweetalert2";

interface ClearAuthModalProps {
    chargepoint_id: string;
    token: string;
    onClose: () => void;
}

export default function ClearAuthModal({
    chargepoint_id, onClose, token,
} : ClearAuthModalProps) {
    const handleClearAuthorization = async () => {
        onClose();
        Swal.fire({
            title: "Processing Clear Cache...",
            text: "Please wait",
            allowOutsideClick: false,
            didOpen: () => {
            Swal.showLoading();
            },
        });
        try {
            const fd = new FormData();
            fd.append("charge_point_id", chargepoint_id);
            fd.append("token", token);

            const response = await fetch("/api/ocpp/clear-cache", {
                method: "POST",
                body: fd,
            });

            if (!response.ok) {
                throw new Error("Failed to clear authorization");
            }

            Swal.close();
            const result = await response.json();

            if (!result.result) {
                Swal.fire({
                    icon: "error",
                    title: "Failed",
                    text: result.msg || "Something went wrong",
                    allowOutsideClick: false,
                });
                return;
            }
            Swal.fire({
                icon: "success",
                title: "Success!",
                text: result.data.status + " - " + result.msg,
                allowOutsideClick: false,
            });
        } catch (err) {
            Swal.close();
            Swal.fire({
                icon: "error",
                title: "Error",
                text: "Something went wrong",
                allowOutsideClick: false,
            });
        }
    };
    return (
        <>
            <div className="modal modal-blur fade show d-block" tabIndex={-1} role="dialog" style={{ backgroundColor: "rgba(0, 0, 0, 0.4)" }}>
                <div className="modal-dialog modal-dialog-centered" role="document">
                    <div className="modal-content">
                        <div className="modal-header">
                            <h5 className="modal-title">Clear Authorization Data</h5>
                            <button type="button" className="btn-close" onClick={onClose}></button>
                        </div>

                        <div className="modal-body">
                            <div className="d-flex">
                                <div className="me-3">
                                    <span className="avatar bg-warning-lt">
                                        <i className="ti ti-alert-triangle"></i>
                                    </span>
                                </div>

                                <div>
                                    <h4>Are you sure?</h4>
                                    <p className="text-secondary mb-0">This will clear the authorization data from this charger. This action cannot be undone.</p>
                                </div>
                            </div>
                        </div>

                        <div className="modal-footer">
                            <button type="button" className="btn btn-danger btn-outline" onClick={onClose}>Cancel</button>
                            <button type="button" className="btn btn-warning btn-outline" onClick={handleClearAuthorization}>
                                <i className="ti ti-trash me-1"></i> Clear Authorization Data
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </>
    )
}