"use client"

import { useState } from "react";
import Swal from "sweetalert2";

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

export default function GetDiagnosticModal({
    chargepoint_id, onClose, token,
} : ClearAuthModalProps) {
    const [location, setLocation] = useState(""); 
    const [startTime, setStartTime] = useState(""); 
    const [endTime, setEndTime] = useState(""); 
    const [loading, setLoading] = useState(false);

    const handleGetDiagnostics = async () => {
        onClose();
        Swal.fire({
            title: "Processing Get Diagnostics...",
            text: "Please wait",
            allowOutsideClick: false,
            didOpen: () => {
            Swal.showLoading();
            },
        });
        try {
            setLoading(true);

            const fd = new FormData();

            fd.append("charge_point_id", chargepoint_id);
            fd.append("token", token); 
            fd.append("location", location); 
            fd.append("start_time", startTime + " 00:00:00"); 
            fd.append("end_time", endTime + " 00:00:00");

            const response = await fetch("/api/ocpp/diagnostics", {
                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 1",
                    allowOutsideClick: false,
                });
                return;
            }
            Swal.fire({
                icon: "success",
                title: "Success!",
                text: result.msg,
                allowOutsideClick: false,
            });
        } catch (err) {
            Swal.close();
            Swal.fire({
                icon: "error",
                title: "Error",
                text: "Something went wrong 2",
                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">Get Diagnostics - {chargepoint_id}</h5>
                            <button type="button" className="btn-close" onClick={onClose}></button>
                        </div>

                        <div className="modal-body">
                            <form className="space-y" onSubmit={(e) => { e.preventDefault(); handleGetDiagnostics(); }}>
                                <div>
                                    <label className="form-label">FTP Connection</label>
                                    <input type="text" name="ftp_location" className="form-control" placeholder="ftp://username:password@127.0.0.1:21/diagnostics/xyz/" onChange={(e) => setLocation(e.target.value) }/>
                                </div>
                                <div className="row">
                                    <div className="col-6">
                                        <label className="form-label">From</label>
                                        <input type="date" name="start_date" className="form-control" onChange={(e) => setStartTime(e.target.value) }/>
                                    </div>
                                    <div className="col-6">
                                        <label className="form-label">To</label>
                                        <input type="date" name="end_date" className="form-control" onChange={(e) => setEndTime(e.target.value) }/>
                                    </div>
                                </div>
                            </form>
                        </div>

                        <div className="modal-footer">
                            <button type="button" className="btn btn-danger btn-outline" onClick={onClose}>Cancel</button>
                            <button type="button" className="btn btn-success btn-outline" onClick={handleGetDiagnostics} disabled={loading}>
                                {loading ? ( 
                                    <><span className="spinner-border spinner-border-sm me-2" role="status" /> Getting Diagnostics... </> 
                                ) : ( 
                                    <> <i className="ti ti-http-get me-1" /> Get Diagnostics </> 
                                )}
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </>
    )
}