function Login({ onConnectConfig }) {
    const [email, setEmail] = React.useState('');
    const [password, setPassword] = React.useState('');
    const [showPassword, setShowPassword] = React.useState(false);
    const [loading, setLoading] = React.useState(false);
    const [isSignUp, setIsSignUp] = React.useState(false);
    const [error, setError] = React.useState(null);
    const [message, setMessage] = React.useState(null);

    const handleAuth = async (e) => {
        e.preventDefault();
        setLoading(true);
        setError(null);
        setMessage(null);

        const client = window.SupabaseUtils.getClient();
        if (!client) {
            setError("Koneksi database belum diinisialisasi.");
            setLoading(false);
            return;
        }

        try {
            if (isSignUp) {
                const { data, error } = await client.auth.signUp({
                    email,
                    password,
                });
                if (error) throw error;
                setMessage("Pendaftaran berhasil! Silakan cek email Anda untuk konfirmasi (jika diaktifkan) atau login langsung.");
                setIsSignUp(false);
            } else {
                const { data, error } = await client.auth.signInWithPassword({
                    email,
                    password,
                });
                if (error) throw error;
                // Session will be handled by onAuthStateChange in App.js
            }
        } catch (err) {
            setError(err.message || "Terjadi kesalahan saat otentikasi.");
        } finally {
            setLoading(false);
        }
    };

    return (
        <div className="min-h-screen flex items-center justify-center bg-gray-100 p-4" data-name="login-page" data-file="components/Login.js">
            <InstallPwa />
            <div className="bg-white p-8 rounded-lg shadow-md w-full max-w-sm">
                <div className="text-center mb-6">
                    {window.AppAssets && window.AppAssets.logo ? (
                        <img 
                            src={window.AppAssets.logo}
                            alt="Logo Pondok" 
                            className="w-24 h-24 object-contain mx-auto mb-4" 
                            onError={(e) => {
                                e.target.onerror = null; 
                                e.target.style.display = 'none';
                                e.target.nextSibling.style.display = 'flex';
                            }}
                        />
                    ) : null}
                    {/* Fallback Icon if Image Fails or Missing */}
                    <div 
                        className="w-24 h-24 mx-auto mb-4 bg-teal-600 rounded-full flex items-center justify-center text-white shadow-lg"
                        style={{ display: (window.AppAssets && window.AppAssets.logo) ? 'none' : 'flex' }}
                    >
                        <div className="icon-school text-4xl"></div>
                    </div>

                    <h2 className="text-2xl font-bold text-gray-800">
                        {isSignUp ? 'Daftar Akun Baru' : 'Login SIMPEG'}
                    </h2>
                    <p className="text-sm text-gray-500">
                        {isSignUp ? 'Buat akun untuk mengelola data.' : 'Masuk untuk mengakses sistem kepegawaian.'}
                    </p>
                </div>

                {error && (
                    <div className="bg-red-50 text-red-600 p-3 rounded mb-4 text-sm border border-red-200">
                        {error}
                    </div>
                )}

                {message && (
                    <div className="bg-green-50 text-green-600 p-3 rounded mb-4 text-sm border border-green-200">
                        {message}
                    </div>
                )}

                <form onSubmit={handleAuth} className="space-y-4">
                    <div>
                        <label className="block text-sm font-medium text-gray-700 mb-1">Email</label>
                        <input 
                            type="email" 
                            required
                            className="w-full p-2 border border-gray-300 rounded focus:ring-2 focus:ring-teal-500 outline-none"
                            placeholder="nama@sekolah.com"
                            value={email}
                            onChange={(e) => setEmail(e.target.value)}
                        />
                    </div>
                    <div>
                        <label className="block text-sm font-medium text-gray-700 mb-1">Password</label>
                        <div className="relative">
                            <input 
                                type={showPassword ? "text" : "password"}
                                required
                                minLength="6"
                                className="w-full p-2 border border-gray-300 rounded focus:ring-2 focus:ring-teal-500 outline-none pr-10"
                                placeholder="******"
                                value={password}
                                onChange={(e) => setPassword(e.target.value)}
                            />
                            <button
                                type="button"
                                onClick={() => setShowPassword(!showPassword)}
                                className="absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-400 hover:text-gray-600 focus:outline-none"
                            >
                                <div className={`icon-${showPassword ? 'eye-off' : 'eye'} text-lg`}></div>
                            </button>
                        </div>
                    </div>
                    
                    <button 
                        type="submit" 
                        disabled={loading}
                        className="w-full bg-[var(--primary-color)] hover:bg-[var(--primary-hover)] text-white py-2 rounded transition-colors flex justify-center items-center gap-2"
                    >
                        {loading ? 'Memproses...' : (isSignUp ? 'Daftar Sekarang' : 'Masuk')}
                        {!loading && <div className={`icon-${isSignUp ? 'user-plus' : 'log-in'} text-lg`}></div>}
                    </button>
                </form>

                <div className="mt-6 text-center text-sm text-gray-400 italic">
                    <p>Silakan hubungi administrator jika belum memiliki akun.</p>
                </div>
            </div>
        </div>
    );
}