<?php
require_once 'db.php';
$departments_list = getDepartmentList();
$position_map = getDepartmentPositionMap();
?>
<!DOCTYPE html>
<html lang="ms">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Daftar Akaun | MAINPP WORKSPACE</title>
    <style>
        html {
            min-height: 100%;
            margin: 0;
            padding: 0;
            background: #f5f8f7;
        }

        * {
            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;
            --text-dark: #17232b;
            --text-muted: #2d5367;
        }

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

        body::before,
        body::after {
            content: "";
            position: fixed;
            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(0, 119, 182, 0.16), transparent 44%);
            filter: blur(8px);
            opacity: 0.95;
        }

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

        .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); }
        }

        .register-panel {
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: clamp(18px, 3vw, 42px);
            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;
        }

        .register-card {
            width: 100%;
            max-width: 590px;
            border-radius: 22px;
            padding: 24px 28px;
            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: 18px;
        }

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

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

        .form-grid {
            display: grid;
            grid-template-columns: repeat(2, minmax(0, 1fr));
            gap: 12px 14px;
        }

        .form-group {
            position: relative;
        }

        .form-group.full {
            grid-column: 1 / -1;
        }

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

        input,
        select {
            width: 100%;
            height: 42px;
            padding: 9px 13px;
            border: 1px solid #dce5e4;
            border-radius: 7px;
            font-size: 0.9rem;
            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[type="password"],
        .pass-wrapper input[type="text"] {
            padding-right: 46px;
        }

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

        input:focus,
        select:focus {
            border-color: rgba(0, 180, 216, 0.95);
            outline: none;
            box-shadow: 0 0 0 4px rgba(0, 180, 216, 0.15);
            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);
        }

        .btn-submit {
            width: 100%;
            height: 46px;
            margin-top: 16px;
            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);
        }

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

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

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

        .error-msg {
            color: #dc3545;
            font-size: 0.8rem;
            margin-top: 4px;
            display: none;
        }

        @media (max-width: 980px) {
            .form-grid {
                grid-template-columns: 1fr;
            }
        }

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

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

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

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

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

        @media (max-width: 460px) {
            .register-card {
                padding: 26px 20px;
                border-radius: 22px;
            }
        }
    </style>
</head>
<body>
    <main class="register-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="register-panel" aria-label="Daftar akaun">
            <div class="register-card">
                <div class="form-heading">
                    <h1>Daftar Akaun</h1>
                    <p>MAINPP WORKSPACE</p>
                </div>

                <form action="process_register.php" method="POST" id="regForm">
                    <div class="form-grid">
                        <div class="form-group full">
                            <label for="fullname">Nama Penuh</label>
                            <input type="text" name="fullname" id="fullname" required>
                        </div>

                        <div class="form-group">
                            <label for="short_name">Nama Panggilan</label>
                            <input type="text" name="short_name" id="short_name" maxlength="100" required>
                        </div>

                        <div class="form-group">
                            <label for="ic_number">No. Kad Pengenalan</label>
                            <input type="text" name="ic_number" id="ic_number" inputmode="numeric" pattern="\d{12}" minlength="12" maxlength="12" title="No. Kad Pengenalan mesti mengandungi 12 digit nombor." required>
                        </div>

                        <div class="form-group full">
                            <label for="email">Emel Rasmi</label>
                            <input type="email" name="email" id="email" title="Gunakan emel rasmi MAINPP yang berakhir dengan @mainpp.gov.my" required>
                        </div>

                        <div class="form-group">
                            <label for="department">Bahagian / Unit</label>
                            <select name="department" id="department" required>
                                <option value="" disabled selected>-- Sila Pilih Bahagian --</option>
                                <?php foreach ($departments_list as $department): ?>
                                <option value="<?= htmlspecialchars($department) ?>"><?= htmlspecialchars($department) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="position">Jawatan</label>
                            <select name="position" id="position" required>
                                <option value="" disabled selected>-- Pilih Bahagian Dahulu --</option>
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="regPass">Kata Laluan</label>
                            <div class="pass-wrapper">
                                <input type="password" name="password" id="regPass" required>
                                <button type="button" class="toggle-pass" onclick="togglePass('regPass', 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="form-group">
                            <label for="regConfirmPass">Sahkan Kata Laluan</label>
                            <div class="pass-wrapper">
                                <input type="password" name="confirm_pass" id="regConfirmPass" required>
                                <button type="button" class="toggle-pass" onclick="togglePass('regConfirmPass', 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 class="error-msg" id="passError">Kata laluan tidak sepadan.</div>
                        </div>
                    </div>

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

                <div class="login-link">
                    Sudah mempunyai akaun? <a href="login.php">Log Masuk</a>
                </div>
            </div>
        </section>
    </main>

    <script>
        const positionMap = <?= json_encode($position_map, JSON_UNESCAPED_UNICODE) ?>;
        const departmentSelect = document.getElementById('department');
        const positionSelect = document.getElementById('position');

        function syncPositionOptions() {
            const department = departmentSelect.value;
            const positions = positionMap[department] || [];
            positionSelect.innerHTML = '<option value="" disabled selected>-- Sila Pilih Jawatan --</option>';
            positions.forEach(position => {
                const option = document.createElement('option');
                option.value = position;
                option.textContent = position;
                positionSelect.appendChild(option);
            });
            positionSelect.disabled = positions.length === 0;
        }
        departmentSelect.addEventListener('change', syncPositionOptions);

        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;
        }

        document.getElementById('regForm').addEventListener('submit', function(e) {
            const p1 = document.getElementById('regPass').value;
            const p2 = document.getElementById('regConfirmPass').value;
            const err = document.getElementById('passError');
            if (p1 !== p2) {
                e.preventDefault();
                err.style.display = 'block';
                document.getElementById('regConfirmPass').style.borderColor = '#dc3545';
            } else {
                err.style.display = 'none';
                document.getElementById('regConfirmPass').style.borderColor = '#dce5e4';
            }
        });
    </script>
</body>
</html>
