<?php
require_once 'auth.php';

if (!empty($_SESSION['logged_in']) && $_SESSION['logged_in'] === true) {
    if (isSessionExpired()) {
        destroyActiveSession();
    } else {
        $_SESSION['last_activity'] = time();
        header('Location: dashboard.php');
        exit();
    }
}

$saved_emel = isset($_COOKIE['user_email']) ? htmlspecialchars($_COOKIE['user_email']) : '';
$saved_pass = isset($_COOKIE['user_password']) ? $_COOKIE['user_password'] : '';
$remember_checked = ($saved_emel !== '') ? 'checked' : '';
?>
<!DOCTYPE html>
<html lang="ms">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Log Masuk | MAINPP WORKSPACE</title>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            font-family: 'Segoe UI', 'Inter', Tahoma, Geneva, Verdana, sans-serif;
        }

        :root {
            --main-blue: #0077b6;
            --deep-blue: #023e5f;
            --soft-blue: #00b4d8;
            --accent-blue: #00b4d8;
            --text-dark: #17232b;
            --text-muted: #2d5367;
        }

        body {
            min-height: 100vh;
            color: var(--text-dark);
            background: #f5f8f7;
            overflow-x: hidden;
        }

        body::before,
        body::after {
            content: "";
            position: fixed;
            inset: auto;
            pointer-events: none;
            z-index: 0;
        }

        body::before {
            width: 54vw;
            height: 54vw;
            left: -18vw;
            bottom: -25vw;
            background:
                linear-gradient(155deg, rgba(255, 255, 255, 0.11), rgba(255, 255, 255, 0.01)),
                repeating-linear-gradient(0deg, transparent 0 32px, rgba(255, 255, 255, 0.08) 33px 34px);
            border: 1px solid rgba(255, 255, 255, 0.12);
            border-radius: 38px;
            transform: rotate(-10deg);
            box-shadow: 0 30px 120px rgba(0, 0, 0, 0.16);
        }

        body::after {
            width: 34vw;
            height: 34vw;
            left: 12vw;
            top: 10vw;
            background:
                radial-gradient(circle, rgba(0, 180, 216, 0.28), transparent 58%),
                radial-gradient(circle, rgba(24, 163, 177, 0.16), transparent 44%);
            filter: blur(8px);
            opacity: 0.95;
        }

        .login-shell {
            position: relative;
            z-index: 1;
            min-height: 100vh;
            display: grid;
            grid-template-columns: minmax(340px, 1fr) minmax(340px, 1fr);
            align-items: stretch;
            width: 100%;
            margin: 0;
            padding: 0;
        }

        .brand-panel {
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: clamp(36px, 7vw, 92px);
            text-align: center;
            background:
                linear-gradient(135deg, rgba(2, 62, 95, 0.82), rgba(0, 119, 182, 0.58)),
                radial-gradient(circle at 22% 24%, rgba(0, 180, 216, 0.38), transparent 28%),
                radial-gradient(circle at 56% 74%, rgba(0, 180, 216, 0.24), transparent 24%);
            overflow: hidden;
        }

        .brand-mark {
            position: relative;
            display: flex;
            align-items: center;
            justify-content: center;
            width: min(340px, 54vw);
            aspect-ratio: 1;
            border-radius: 50%;
            background:
                radial-gradient(circle, rgba(255, 255, 255, 0.18), rgba(255, 255, 255, 0.03) 58%, transparent 62%),
                conic-gradient(from 0deg, rgba(255, 255, 255, 0.02), rgba(0, 180, 216, 0.3), rgba(255, 255, 255, 0.02), rgba(0, 119, 182, 0.28), rgba(255, 255, 255, 0.02));
            box-shadow: 0 34px 95px rgba(0, 0, 0, 0.32);
        }

        .brand-mark::before {
            content: "";
            position: absolute;
            inset: 11%;
            border: 1px solid rgba(255, 255, 255, 0.16);
            border-radius: 50%;
        }

        .brand-mark::after {
            content: "";
            position: absolute;
            width: 118%;
            height: 118%;
            border-radius: 50%;
            border: 1px dashed rgba(255, 255, 255, 0.2);
            animation: orbitSpin 12s linear infinite;
        }

        .brand-mark img {
            position: relative;
            z-index: 1;
            width: clamp(155px, 19vw, 245px);
            height: auto;
            filter: drop-shadow(0 18px 28px rgba(0, 0, 0, 0.34));
            animation: logoRotate 9s linear infinite;
            transform-origin: center;
        }
        .brand-name {
            max-width: 360px;
            margin-top: 34px;
            color: #fff;
            font-size: clamp(1.05rem, 2vw, 1.45rem);
            line-height: 1.35;
            font-weight: 800;
            text-align: center;
            text-shadow: 0 12px 28px rgba(0, 0, 0, 0.35);
        }

        @keyframes logoRotate {
            from { transform: rotateY(0deg); }
            to { transform: rotateY(360deg); }
        }

        @keyframes orbitSpin {
            from { transform: rotate(0deg); }
            to { transform: rotate(360deg); }
        }

        .login-panel {
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: clamp(28px, 6vw, 74px);
            background:
                linear-gradient(135deg, rgba(255, 255, 255, 0.48), rgba(0, 180, 216, 0.12)),
                url('assets/auth-bg.png') center/cover no-repeat;
        }

        .login-card {
            width: 100%;
            max-width: 430px;
            border-radius: 22px;
            padding: 38px;
            background: rgba(255, 255, 255, 0.30);
            border: 1px solid rgba(255, 255, 255, 0.42);
            box-shadow: 0 30px 90px rgba(2, 38, 60, 0.26);
            backdrop-filter: blur(24px) saturate(1.25);
            -webkit-backdrop-filter: blur(24px) saturate(1.25);
        }

        .form-heading {
            text-align: center;
            margin-bottom: 28px;
        }

        .form-heading h1 {
            color: #005f99;
            font-size: clamp(1.78rem, 3vw, 2.25rem);
            line-height: 1.12;
            font-weight: 800;
            margin-bottom: 10px;
        }

        .form-heading p {
            color: #355f73;
            font-size: 0.94rem;
            font-weight: 500;
            line-height: 1.5;
        }

        .form-group {
            margin-bottom: 18px;
            position: relative;
        }

        label {
            display: block;
            margin-bottom: 8px;
            color: var(--text-dark);
            font-weight: 700;
            font-size: 0.9rem;
        }

        input[type="email"],
        input[type="password"],
        input[type="text"] {
            width: 100%;
            height: 48px;
            padding: 12px 46px 12px 15px;
            border: 1px solid #dce5e4;
            border-radius: 7px;
            font-size: 0.95rem;
            color: #12313e;
            transition: border-color 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
            background: #fff;
            box-shadow: 0 1px 0 rgba(16, 50, 47, 0.03);
        }

        input::placeholder {
            color: #526b79;
        }

        input:focus {
            border-color: rgba(0, 180, 216, 0.95);
            outline: none;
            box-shadow: 0 0 0 4px rgba(0, 180, 216, 0.18);
            background: #fff;
        }

        .pass-wrapper {
            position: relative;
        }

        .toggle-pass {
            position: absolute;
            right: 13px;
            top: 50%;
            transform: translateY(-50%);
            cursor: pointer;
            background: none;
            border: none;
            padding: 3px;
            display: inline-flex;
            align-items: center;
            justify-content: center;
        }

        .toggle-pass svg {
            width: 20px;
            height: 20px;
            fill: #627781;
            transition: fill 0.2s ease;
        }

        .toggle-pass:hover svg {
            fill: var(--main-blue);
        }

        .options {
            display: flex;
            justify-content: space-between;
            align-items: center;
            gap: 18px;
            margin: 4px 0 24px;
            font-size: 0.86rem;
        }

        .remember {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            cursor: pointer;
            color: var(--text-muted);
            margin-bottom: 0;
            font-weight: 650;
        }

        .remember input {
            cursor: pointer;
            width: 16px;
            height: 16px;
            accent-color: var(--accent-blue);
        }

        .forgot,
        .back-link a {
            color: var(--main-blue);
            text-decoration: none;
            font-weight: 700;
        }

        .forgot:hover,
        .forgot:focus,
        .back-link a:hover,
        .back-link a:focus {
            text-decoration: none;
        }

        .btn-submit {
            width: 100%;
            height: 50px;
            padding: 0 18px;
            background: linear-gradient(90deg, #0077b6, #00b4d8);
            color: #fff;
            border: 1px solid rgba(0, 119, 182, 0.28);
            border-radius: 999px;
            font-size: 0.95rem;
            font-weight: 850;
            letter-spacing: 0.04em;
            cursor: pointer;
            box-shadow: 0 14px 28px rgba(0, 119, 182, 0.26);
            transition: transform 0.2s ease, box-shadow 0.2s ease, filter 0.2s ease;
        }

        .btn-submit:hover {
            transform: translateY(-1px);
            filter: brightness(1.03);
            box-shadow: 0 18px 34px rgba(0, 119, 182, 0.34);
        }

        .back-link {
            text-align: center;
            margin-top: 22px;
            font-size: 0.9rem;
            color: var(--text-muted);
        }

        @media (max-width: 860px) {
            body {
                overflow-y: auto;
            }

            .login-shell {
                grid-template-columns: 1fr;
                width: 100%;
                min-height: 100vh;
            }

            .brand-panel {
                min-height: 42vh;
                padding: 40px 24px;
            }

            .brand-mark {
                width: min(280px, 72vw);
            }
            .brand-name {
                margin-top: 24px;
                font-size: 1.05rem;
            }

            .login-panel {
                min-height: auto;
                padding: 34px 18px 44px;
            }
        }

        @media (max-width: 460px) {
            .login-card {
                padding: 26px 20px;
                border-radius: 22px;
            }

            .options {
                align-items: flex-start;
                flex-direction: column;
                gap: 10px;
            }
        }
    </style>
</head>
<body>
    <main class="login-shell">
        <section class="brand-panel" aria-label="Logo MAINPP">
            <div class="brand-mark">
                <img src="mainpp.png" alt="Logo MAINPP">
            </div>
            <p class="brand-name">Majlis Agama Islam Negeri Pulau Pinang</p>
        </section>

        <section class="login-panel" aria-label="Log masuk akaun">
            <div class="login-card">
                <div class="form-heading">
                    <h1>Log Masuk</h1>
                    <p>MAINPP WORKSPACE</p>
                </div>

                <form action="process_login.php" method="POST">
                    <input type="hidden" name="redirect" value="<?= htmlspecialchars($_GET['redirect'] ?? '', ENT_QUOTES) ?>">
                    <div class="form-group">
                        <label for="loginEmail">Emel</label>
                        <input type="email" name="email" id="loginEmail" value="<?= $saved_emel ?>" placeholder="Masukkan emel anda" required>
                    </div>

                    <div class="form-group">
                        <label for="loginPass">Kata Laluan</label>
                        <div class="pass-wrapper">
                            <input type="password" name="password" id="loginPass" value="<?= $saved_pass ?>" placeholder="Masukkan kata laluan" required>
                            <button type="button" class="toggle-pass" onclick="togglePass('loginPass', this)" aria-label="Papar atau sembunyi kata laluan">
                                <svg viewBox="0 0 24 24"><path d="M12 7c2.76 0 5 2.24 5 5 0 .65-.13 1.26-.36 1.83l2.92 2.92c1.51-1.26 2.7-2.89 3.43-4.75-1.73-4.39-6-7.5-11-7.5-1.4 0-2.74.25-3.98.7l2.16 2.16C10.74 7.13 11.35 7 12 7zM2 4.27l2.28 2.28.46.46C3.08 8.3 1.78 10.02 1 12c1.73 4.39 6 7.5 11 7.5 1.55 0 3.03-.3 4.38-.84l.42.42L19.73 22 21 20.73 3.27 3 2 4.27zM7.53 9.8l1.55 1.55c-.05.21-.08.43-.08.65 0 1.66 1.34 3 3 3 .22 0 .44-.03.65-.08l1.55 1.55c-.67.33-1.41.53-2.2.53-2.76 0-5-2.24-5-5 0-.79.2-1.53.53-2.2zm4.31-.78l3.15 3.15.02-.16c0-1.66-1.34-3-3-3l-.17.01z"/></svg>
                            </button>
                        </div>
                    </div>

                    <div class="options">
                        <label class="remember">
                            <input type="checkbox" name="remember" value="1" <?= $remember_checked ?>> Ingat Saya
                        </label>
                        <a href="forgot_password.php" class="forgot">Lupa kata laluan?</a>
                    </div>

                    <button type="submit" class="btn-submit">LOG MASUK</button>
                </form>

                <div class="back-link">
                    <a href="index.php">&larr; Kembali ke halaman utama</a>
                </div>
            </div>
        </section>
    </main>

    <script>
        function togglePass(inputId, btn) {
            const input = document.getElementById(inputId);
            const isPass = input.type === 'password';
            input.type = isPass ? 'text' : 'password';
            const openIcon = '<svg viewBox="0 0 24 24"><path d="M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-8c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z"/></svg>';
            const closedIcon = '<svg viewBox="0 0 24 24"><path d="M12 7c2.76 0 5 2.24 5 5 0 .65-.13 1.26-.36 1.83l2.92 2.92c1.51-1.26 2.7-2.89 3.43-4.75-1.73-4.39-6-7.5-11-7.5-1.4 0-2.74.25-3.98.7l2.16 2.16C10.74 7.13 11.35 7 12 7zM2 4.27l2.28 2.28.46.46C3.08 8.3 1.78 10.02 1 12c1.73 4.39 6 7.5 11 7.5 1.55 0 3.03-.3 4.38-.84l.42.42L19.73 22 21 20.73 3.27 3 2 4.27zM7.53 9.8l1.55 1.55c-.05.21-.08.43-.08.65 0 1.66 1.34 3 3 3 .22 0 .44-.03.65-.08l1.55 1.55c-.67.33-1.41.53-2.2.53-2.76 0-5-2.24-5-5 0-.79.2-1.53.53-2.2zm4.31-.78l3.15 3.15.02-.16c0-1.66-1.34-3-3-3l-.17.01z"/></svg>';
            btn.innerHTML = isPass ? openIcon : closedIcon;
        }

        window.onload = function() {
            const urlParams = new URLSearchParams(window.location.search);
            if (urlParams.get('success') === 'pending_approval') {
                alert("Pendaftaran Berjaya Dihantar!\n\nAkaun anda sedang menunggu kelulusan Admin.\nSila cuba log masuk semula setelah diluluskan.");
            }
            if (urlParams.has('session')) {
                const sessionType = urlParams.get('session');
                if (sessionType === 'logged_out') alert("Anda telah berjaya log keluar.");
            }
            if (urlParams.has('error')) {
                const errorType = urlParams.get('error');
                if (errorType === 'wrong_password') alert("Kata laluan salah!");
                else if (errorType === 'system') alert("Ralat sistem. Sila cuba lagi.");
                else if (errorType === 'invalid') alert("Emel atau Kata Laluan salah!");
                else if (errorType === 'notfound') alert("Akaun tidak dijumpai!");
                else if (errorType === 'empty') alert("Sila isi semua medan!");
                else if (errorType === 'pending') alert("Akaun anda masih dalam proses kelulusan Admin.");
                else if (errorType === 'rejected') alert("Pendaftaran anda telah ditolak oleh Admin.Akaun anda telah dinyahaktifkan. Sila hubungi pentadbir sistem untuk mendapatkan bantuan.");
                else if (errorType === 'account_blocked') alert("Akaun anda tidak lagi aktif. Sila hubungi pentadbir.");
            }

            if (window.history && window.history.replaceState && window.location.search) {
                const cleanUrl = new URL(window.location.href);
                ['success', 'session', 'error'].forEach(param => cleanUrl.searchParams.delete(param));
                window.history.replaceState({}, document.title, cleanUrl.pathname + cleanUrl.search + cleanUrl.hash);
            }
        }
    </script>
</body>
</html>
