<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Daftar Akun - Pustaka Nawasena</title>
    <link rel="icon" href="{{ asset('images/kartu.png') }}" type="image/png">

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">

    <style>
        /* Paksa halaman tetap fit ke layar */
        html, body {
            height: 100vh;
            margin: 0;
            padding: 0;
            overflow: hidden; 
        }

        body {
            font-family: 'Inter', sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            background: url('{{ asset("images/utama.jpg") }}') no-repeat center center / cover;
            position: relative;
        }

        body::before {
            content: '';
            position: absolute;
            inset: 0;
            background: rgba(0, 0, 0, 0.45);
            backdrop-filter: blur(5px);
            z-index: 0;
        }

        .auth-card {
            position: relative;
            z-index: 1;
            background: #fff;
            border-radius: 16px;
            padding: 20px 30px; 
            width: 90%;
            max-width: 380px; 
            box-shadow: 0 15px 35px rgba(0,0,0,0.3);
            opacity: 0;
            transform: translateY(20px);
            animation: fadeInCard 0.6s ease-out forwards;
        }

        @keyframes fadeInCard {
            to { opacity: 1; transform: translateY(0); }
        }

        .logo-wrapper {
            display: flex;
            justify-content: center;
            margin-bottom: 10px; 
        }
        .logo-wrapper img {
            width: 60px; 
            height: 60px;
            object-fit: contain;
            border-radius: 50%;
            background: #fff;
            box-shadow: 0 4px 10px rgba(249, 115, 22, 0.2);
        }

        .header-bar {
            text-align: center;
            margin-bottom: 15px; 
        }
        .header-bar h5 {
            color: #1e3a8a;
            font-weight: 800;
            font-size: 1.2rem; 
            margin-bottom: 0;
        }
        .header-bar small {
            color: #6b7280;
            font-size: 0.8rem;
        }

        .form-label {
            font-size: 0.85rem; 
            margin-bottom: 4px;
        }

        input.form-control {
            border-radius: 10px;
            padding: 8px 12px; 
            font-size: 0.9rem;
            border: 1.5px solid #eee;
        }

        .mb-3 { margin-bottom: 0.7rem !important; } 

        .password-wrapper {
            position: relative;
        }
        .password-wrapper i {
            position: absolute;
            right: 12px;
            top: 50%;
            transform: translateY(-50%);
            cursor: pointer;
            font-size: 0.9rem;
            color: #888;
        }

        button.btn-primary {
            background: #f97316;
            border: none;
            border-radius: 10px;
            padding: 10px;
            font-weight: 700;
            font-size: 0.95rem;
            margin-top: 5px;
        }
        button.btn-primary:hover {
            background: #ea580c;
        }

        .text-small {
            font-size: 0.8rem;
            margin-top: 12px;
        }
        .text-small a {
            color: #f97316;
            text-decoration: none;
            font-weight: 600;
        }

        /* Styling tombol rekomendasi password */
        .btn-recommend {
            font-size: 0.75rem;
            color: #f97316;
            text-decoration: none;
            cursor: pointer;
            font-weight: 600;
            display: inline-block;
            margin-top: 2px;
        }
        .btn-recommend:hover {
            text-decoration: underline;
        }

        @media (max-height: 650px) {
            .auth-card { padding: 15px 25px; }
            .logo-wrapper img { width: 45px; height: 45px; }
            .mb-3 { margin-bottom: 0.4rem !important; }
        }
    </style>
</head>
<body>

<div class="auth-card">
    <div class="logo-wrapper">
        <img src="{{ asset('images/kartu.png') }}" alt="Logo">
    </div>

    <div class="header-bar">
        <h5>Pustaka Nawasena</h5>
        <small>Pendaftaran Akun Baru</small>
    </div>

    @if($errors->any())
        <div class="alert alert-danger py-1 px-2 small text-center">{{ $errors->first() }}</div>
    @endif

    <form method="POST" action="{{ route('register') }}" onsubmit="return validatePassword()">
        @csrf

        <div class="mb-3">
            <label class="form-label fw-semibold">Nama Lengkap</label>
            <input type="text" name="name" id="name" class="form-control" placeholder="Nama Anda" required>
        </div>

        <div class="mb-3">
            <label class="form-label fw-semibold">Email</label>
            <input type="email" name="email" class="form-control" placeholder="Email aktif" required>
        </div>

        <div class="mb-3">
            <div class="d-flex justify-content-between align-items-center">
                <label class="form-label fw-semibold mb-0">Kata Sandi</label>
                <span class="btn-recommend" onclick="generatePassword()"><i class="bi bi-magic"></i> Rekomendasi Sandi</span>
            </div>
            <div class="password-wrapper mt-1">
                <input type="password" id="password" name="password" class="form-control" placeholder="••••••••" required>
                <i class="bi bi-eye-slash" id="togglePassword"></i>
            </div>
            <small class="form-text text-muted d-block mt-1" style="font-size: 0.72rem; line-height: 1.2;">
                *Minimal 8 karakter (kombinasi huruf kapital, kecil, dan tanda baca . ,)
            </small>
        </div>

        <div class="mb-3">
            <label class="form-label fw-semibold">Konfirmasi Sandi</label>
            <div class="password-wrapper">
                <input type="password" id="password_confirmation" name="password_confirmation" class="form-control" placeholder="••••••••" required>
                <i class="bi bi-eye-slash" id="toggleConfirm"></i>
            </div>
        </div>
        
        <button type="submit" class="btn btn-primary w-100">Daftar</button>

        <p class="text-center text-small mb-0 text-muted">
            Sudah punya akun? <a href="{{ route('login') }}">Masuk</a>
        </p>
    </form>
</div>

<script>
    const nameInput = document.getElementById('name');
    const password = document.getElementById('password');
    const confirmPassword = document.getElementById('password_confirmation');
    const togglePassword = document.getElementById('togglePassword');
    const toggleConfirm = document.getElementById('toggleConfirm');

    const setupToggle = (btn, field) => {
        btn.addEventListener('click', () => {
            field.type = field.type === 'password' ? 'text' : 'password';
            btn.classList.toggle('bi-eye');
            btn.classList.toggle('bi-eye-slash');
        });
    };

    setupToggle(togglePassword, password);
    setupToggle(setupToggle, confirmPassword); // Diperbaiki agar mereferensikan fungsi dengan benar
    setupToggle(toggleConfirm, confirmPassword);

    // Fungsi Rekomendasi Password Berdasarkan Nama Pengguna
    function generatePassword() {
        const nameValue = nameInput.value.trim();
        
        // Validasi jika nama belum diisi
        if (!nameValue) {
            alert('Silakan isi "Nama Lengkap" terlebih dahulu untuk menghasilkan rekomendasi sandi!');
            nameInput.focus();
            return;
        }

        // Mengambil kata pertama dan membersihkan karakter selain huruf
        let cleanName = nameValue.split(' ')[0].replace(/[^a-zA-Z]/g, '');
        
        // Memastikan format huruf pertama kapital dan sisanya kecil (memenuhi syarat kombinasi huruf)
        if (cleanName.length > 0) {
            cleanName = cleanName.charAt(0).toUpperCase() + cleanName.slice(1).toLowerCase();
        } else {
            cleanName = "Nawasena"; // Cadangan jika input nama berupa karakter aneh/angka saja
        }

        const specialChars = ".,";
        const numbers = "0123456789";
        
        // Mengambil simbol acak dan angka acak
        let randomSpecial = specialChars.charAt(Math.floor(Math.random() * specialChars.length));
        let randomNum1 = numbers.charAt(Math.floor(Math.random() * numbers.length));
        let randomNum2 = numbers.charAt(Math.floor(Math.random() * numbers.length));
        
        // Menggabungkan komponen: Nama + Simbol + Angka
        let result = cleanName + randomSpecial + randomNum1 + randomNum2;

        // Validasi panjang karakter: Jika masih kurang dari 8, tambahkan angka acak lagi di ekornya
        while (result.length < 8) {
            result += numbers.charAt(Math.floor(Math.random() * numbers.length));
        }
        
        // Set nilai ke input field password dan konfirmasinya
        password.value = result;
        confirmPassword.value = result;
        
        // Mengubah input menjadi teks biasa agar pengguna bisa membaca sandi tersebut
        password.type = 'text';
        confirmPassword.type = 'text';
        
        // Menyelaraskan ikon mata indikator
        togglePassword.className = "bi bi-eye";
        toggleConfirm.className = "bi bi-eye";
    }

    function validatePassword() {
        const regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[.,]).{8,}$/;
        
        if (!regex.test(password.value)) {
            alert('Kata sandi harus minimal 8 karakter dan mengandung kombinasi huruf besar, huruf kecil, serta tanda baca (. atau ,)!');
            return false;
        }

        if (password.value !== confirmPassword.value) {
            alert('Kata sandi tidak cocok!');
            return false;
        }
        return true;
    }
</script>

</body>
</html>

