<?php
require_once __DIR__ . '/db.php';

if (isset($_SESSION['user_id'])) {
    header('Location: dashboard.php');
    exit();
}

$bahagianList = audit_bahagian_list();
$logoVersion = @filemtime(__DIR__ . '/mainpp.png') ?: time();
?>
<!DOCTYPE html>
<html lang="ms">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Daftar - Dashboard Audit MAINPP</title>
    <link rel="stylesheet" href="styles.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <style>
        body {
            background: #f8fafc;
        }
        .auth-shell {
            min-height: 100vh;
            display: grid;
            grid-template-columns: minmax(360px, 0.9fr) minmax(520px, 1.1fr);
            background: #fff;
        }
        .auth-visual {
            position: relative;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
            background:
                radial-gradient(circle at 54% 76%, rgba(255, 230, 0, 0.28) 0, rgba(255, 230, 0, 0.08) 12%, transparent 29%),
                radial-gradient(circle at 20% 18%, rgba(0, 218, 67, 0.32) 0, transparent 32%),
                linear-gradient(145deg, #031512 0%, #06241f 42%, #0b5138 100%);
        }
        .auth-visual::before {
            content: "";
            position: absolute;
            inset: 0;
            background:
                linear-gradient(90deg, rgba(255,255,255,0.04) 1px, transparent 1px),
                linear-gradient(0deg, rgba(255,255,255,0.035) 1px, transparent 1px);
            background-size: 54px 54px;
            opacity: 0.28;
        }
        .auth-visual::after {
            content: "";
            position: absolute;
            left: 50%;
            bottom: 16%;
            width: 220px;
            height: 34px;
            border-radius: 999px;
            background: rgba(1, 8, 7, 0.42);
            filter: blur(18px);
            transform: translateX(-50%);
            animation: shadowPulse 4.8s ease-in-out infinite;
        }
        .visual-logo-wrap {
            position: relative;
            width: min(340px, 56vw);
            aspect-ratio: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            z-index: 1;
        }
        .visual-logo-glow {
            position: absolute;
            inset: 8%;
            border-radius: 50%;
            background:
                radial-gradient(circle, rgba(255, 230, 0, 0.22) 0, transparent 56%),
                radial-gradient(circle, rgba(0, 218, 67, 0.18) 0, transparent 68%);
            filter: blur(10px);
            animation: logoGlow 5.2s ease-in-out infinite;
        }
        .visual-logo-card {
            width: 100%;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 34px;
            animation: logoFloat 4.8s ease-in-out infinite;
            transform-origin: center;
        }
        .visual-logo-card img {
            width: 100%;
            height: 100%;
            object-fit: contain;
            filter: drop-shadow(0 26px 34px rgba(0, 0, 0, 0.32));
        }

        .visual-company-name {
            position: absolute;
            left: 50%;
            top: calc(50% + min(170px, 28vw));
            width: min(440px, 78%);
            transform: translateX(-50%);
            z-index: 2;
            color: rgba(255, 255, 255, 0.94);
            text-align: center;
            font-size: 22px;
            line-height: 1.32;
            font-weight: 900;
            letter-spacing: 0;
            text-shadow: 0 8px 24px rgba(0, 0, 0, 0.42);
        }
        .visual-company-name::after {
            content: "";
            display: block;
            width: 96px;
            height: 3px;
            margin: 12px auto 0;
            border-radius: 999px;
            background: linear-gradient(90deg, transparent, #ffe600, #00da43, transparent);
            opacity: 0.9;
        }
        @keyframes logoFloat {
            0%, 100% {
                transform: translateY(0) rotate(0deg) scale(1);
            }
            35% {
                transform: translateY(-14px) rotate(-1.2deg) scale(1.02);
            }
            70% {
                transform: translateY(6px) rotate(1deg) scale(0.995);
            }
        }
        @keyframes logoGlow {
            0%, 100% {
                opacity: 0.72;
                transform: scale(0.96);
            }
            50% {
                opacity: 1;
                transform: scale(1.08);
            }
        }
        @keyframes shadowPulse {
            0%, 100% {
                opacity: 0.52;
                transform: translateX(-50%) scaleX(0.88);
            }
            45% {
                opacity: 0.32;
                transform: translateX(-50%) scaleX(1.16);
            }
            70% {
                opacity: 0.46;
                transform: translateX(-50%) scaleX(0.98);
            }
        }
        .auth-panel {
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 42px 32px;
            background: #fbfcfd;
        }
        .auth-card {
            width: 100%;
            max-width: 720px;
        }
        .auth-brand {
            text-align: center;
            margin-bottom: 28px;
        }
        .auth-brand .logo-mark {
            margin: 0 auto 20px;
            width: 76px;
            height: 76px;
            border-radius: 50%;
            background: #f1f5f9;
            padding: 9px;
        }
        .auth-brand h1 {
            color: #090d12;
            font-size: 38px;
            line-height: 1.08;
            margin-bottom: 10px;
            letter-spacing: 0;
        }
        .auth-brand p {
            color: #5f6b7a;
            line-height: 1.55;
            font-size: 14px;
            max-width: 620px;
            margin: 0 auto;
        }
        .auth-card label {
            color: #1f2937;
            font-weight: 700;
            margin-bottom: 9px;
        }
        .auth-card input,
        .auth-card select {
            height: 48px;
            border: 1px solid #d8dee7;
            border-radius: 10px;
            background: #f7f8fa;
            box-shadow: 0 10px 18px rgba(15, 23, 42, 0.10);
            transition: border-color 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
        }
        .auth-card input:focus,
        .auth-card select:focus {
            outline: none;
            border-color: #16a34a;
            background: #fff;
            box-shadow: 0 0 0 4px rgba(22, 163, 74, 0.12), 0 12px 22px rgba(15, 23, 42, 0.12);
        }
        .auth-card .button-row {
            display: grid;
            grid-template-columns: 1fr;
            gap: 14px;
            margin-top: 28px;
        }
        .auth-card .primary-btn {
            width: 100%;
            border-radius: 999px;
            padding: 14px 18px;
            background: linear-gradient(180deg, #1c1c1c 0%, #050505 100%);
            color: #fff;
            box-shadow: 0 12px 22px rgba(0, 0, 0, 0.28);
        }
        .auth-card .primary-btn:hover {
            background: linear-gradient(180deg, #0f3d2c 0%, #061c16 100%);
        }
        .login-row {
            margin-top: 22px;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            color: #2d3748;
            font-size: 14px;
            font-weight: 600;
        }
        .auth-link {
            color: #0b7f3a;
            text-decoration: none;
            font-weight: 800;
        }
        .auth-link:hover {
            text-decoration: underline;
        }
        .password-container {
            position: relative;
        }
        .password-container input {
            padding-right: 45px;
        }
        .toggle-password {
            position: absolute;
            right: 15px;
            top: 50%;
            transform: translateY(-50%);
            cursor: pointer;
            color: #666;
            z-index: 10;
        }
        @media (max-width: 980px) {
            .auth-shell {
                grid-template-columns: 1fr;
            }
            .auth-visual {
                min-height: 280px;
            }
            .visual-logo-wrap {
                width: min(240px, 68vw);
            }
            .visual-company-name {
                top: calc(50% + 105px);
                font-size: 16px;
                width: min(350px, 88%);
            }

            .auth-panel {
                min-height: auto;
                padding: 34px 22px 42px;
            }
            .auth-brand h1 {
                font-size: 34px;
            }
        }
        @media (max-width: 620px) {
            .form-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
<div class="auth-shell">
    <div class="auth-visual" aria-hidden="true">
        <div class="visual-logo-wrap">
            <div class="visual-logo-glow"></div>
            <div class="visual-logo-card">
                <img src="mainpp.png?v=<?= urlencode((string) $logoVersion) ?>" alt="">
            </div>
            <div class="visual-company-name">Majlis Agama Islam Negeri Pulau Pinang</div>
        </div>
    </div>
    <div class="auth-panel">
        <div class="auth-card">
            <div class="auth-brand">
                <div class="logo-mark"><img src="mainpp.png?v=<?= urlencode((string) $logoVersion) ?>" alt="Logo MAINPP"></div>
                <h1>Daftar Akaun</h1>
                <p>Pendaftaran awam hanya untuk <b>Pengguna Biasa</b>. Akaun anda akan disemak oleh <b>Admin Audit</b> atau <b>Pentadbir Sistem</b> sebelum boleh login.</p>
            </div>

            <form action="register_process.php" method="POST">
                <div class="form-grid">
                    <div class="full-width">
                        <label for="nama">Nama</label>
                        <input type="text" id="nama" name="nama" required>
                    </div>
                    <div>
                        <label for="ic">No. Kad Pengenalan</label>
                        <input type="text" id="ic" name="ic" required>
                    </div>
                    <div>
                        <label for="emel">Emel</label>
                        <input type="email" id="emel" name="emel" required>
                    </div>
                    <div class="full-width">
                        <label for="jabatan">Bahagian / Unit</label>
                        <select id="jabatan" name="jabatan" required>
                            <option value="">-- Pilih Bahagian / Unit --</option>
                            <?php foreach ($bahagianList as $bahagian): ?>
                                <option value="<?= htmlspecialchars($bahagian) ?>"><?= htmlspecialchars($bahagian) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div>
                        <label for="password">Kata Laluan</label>
                        <div class="password-container">
                            <input type="password" id="password" name="password" required>
                            <i class="fa-solid fa-eye-slash toggle-password" onclick="togglePassword('password', this)"></i>
                        </div>
                    </div>
                    <div>
                        <label for="confirm_password">Sahkan Kata Laluan</label>
                        <div class="password-container">
                            <input type="password" id="confirm_password" name="confirm_password" required>
                            <i class="fa-solid fa-eye-slash toggle-password" onclick="togglePassword('confirm_password', this)"></i>
                        </div>
                    </div>
                </div>
                <div class="button-row">
                    <button type="submit" class="primary-btn">Daftar</button>
                </div>
                <div class="login-row">
                    <span>Sudah mempunyai akaun?</span>
                    <a href="login.php" class="auth-link">Log Masuk</a>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
function togglePassword(fieldId, icon) {
    const input = document.getElementById(fieldId);
    input.type = input.type === 'password' ? 'text' : 'password';
    icon.classList.toggle('fa-eye');
    icon.classList.toggle('fa-eye-slash');
}
</script>
</body>
</html>
