import React, { useState } from 'react';
import { router } from '@inertiajs/react';
import { HiOutlineLogout, HiX } from 'react-icons/hi';

interface LogoutConfirmProps {
  isOpen: boolean;
  onClose: () => void;
}

export default function LogoutConfirm({ isOpen, onClose }: LogoutConfirmProps) {
  if (!isOpen) return null;

  const [isLoading, setIsLoading] = useState(false);

  const handleLogout = () => {
    router.post('/logout', {}, {
      onStart: () => setIsLoading(true),
      onFinish: () => setIsLoading(false),
    });
  };

  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/80 backdrop-blur-sm animate-fade-in">
      
      {/* Overlay backdrop yang bisa diklik untuk menutup modal (disable saat loading) */}
      <div 
        onClick={() => !isLoading && onClose()} 
        className="absolute inset-0"
      />

      {/* Container Modal */}
      <div className="relative w-full max-w-sm bg-white border border-slate-200/80 rounded-2xl shadow-2xl overflow-hidden z-10 transition-all">

        <div className="p-6 text-center space-y-4">
          
          {/* Icon Logout dengan Aksen Merah Soft */}
          <div className="mx-auto w-12 h-12 bg-rose-50 text-rose-600 rounded-2xl border border-rose-100 flex items-center justify-center shadow-xs">
            <HiOutlineLogout className="w-6 h-6" />
          </div>

          {/* Judul & Deskripsi Konfirmasi */}
          <div>
            <h3 className="text-base font-bold text-slate-800">
              Konfirmasi Keluar
            </h3>
            <p className="text-xs text-slate-500 mt-1 leading-relaxed">
              Apakah Anda yakin ingin  keluar dari akun ini?
            </p>
          </div>

          {/* Action Buttons */}
          <div className="pt-2 flex items-center gap-3">
            <button
              type="button"
              onClick={onClose}
              disabled={isLoading}
              className="flex-1 py-2.5 px-4 bg-slate-100 hover:bg-slate-200 text-slate-700 font-semibold text-xs rounded-xl transition duration-200 cursor-pointer disabled:opacity-50"
            >
              Batal
            </button>

            <button
              type="button"
              onClick={handleLogout}
              disabled={isLoading}
              className="flex-1 py-2.5 px-4 bg-rose-600 hover:bg-rose-700 active:bg-rose-800 text-white font-semibold text-xs rounded-xl shadow-md shadow-rose-600/20 transition duration-200 flex items-center justify-center gap-2 cursor-pointer disabled:opacity-60 disabled:cursor-not-allowed"
            >
              {isLoading ? (
                <>
                  <svg className="animate-spin h-4 w-4 text-white" fill="none" viewBox="0 0 24 24">
                    <circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
                    <path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                  </svg>
                  <span>Keluar...</span>
                </>
              ) : (
                <span>Ya, Keluar</span>
              )}
            </button>
          </div>

        </div>
      </div>
    </div>
  );
}