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

    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <style>
        :root {
            --lib-navy: #0f172a;
            --color-primary: #06b6d4; /* Warna Cyan khas pengembalian */
        }

        * { user-select: none; cursor: none; }

        body { 
            font-family: 'Plus Jakarta Sans', sans-serif; 
            background-color: var(--lib-navy);
            background-image: radial-gradient(circle at center, rgba(30, 41, 59, 0.8), rgba(15, 23, 42, 1)), url("{{ asset('images/utama.jpg') }}"); 
            background-size: cover;
            background-position: center;
            height: 100vh;
            width: 100vw;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            overflow: hidden;
            margin: 0;
            padding: 20px;
        }

        /* Kontainer Utama yang Fleksibel */
        .main-wrapper {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            flex-grow: 1;
            width: 100%;
            max-width: 1200px;
            animation: fadeInScale 1s ease-out;
        }

        @keyframes fadeInScale {
            from { transform: scale(0.95); opacity: 0; }
            to { transform: scale(1); opacity: 1; }
        }

        /* Tipografi Responsif */
        .big-text {
            font-size: clamp(3rem, 15vw, 8.5rem);
            font-weight: 900;
            line-height: 0.85;
            background: linear-gradient(to bottom, #ffffff 30%, #94a3b8 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            letter-spacing: -0.04em;
            text-align: center;
            margin: 1rem 0;
        }

        .sub-text {
            font-size: clamp(0.75rem, 2.5vw, 1.25rem);
            font-weight: 600;
            color: var(--color-primary);
            text-transform: uppercase;
            letter-spacing: 0.4em;
            transition: color 0.5s ease;
        }

        /* Responsif Badge Logo */
        .header-badge {
            position: absolute;
            top: clamp(20px, 5vh, 40px);
            display: flex;
            align-items: center;
            gap: 12px;
            background: rgba(255, 255, 255, 0.05);
            padding: 8px 20px 8px 8px;
            border-radius: 100px;
            backdrop-filter: blur(20px);
            border: 1px solid rgba(255, 255, 255, 0.15);
            box-shadow: 0 10px 40px rgba(0,0,0,0.4);
        }

        .logo-circle {
            width: clamp(32px, 5vw, 45px);
            height: clamp(32px, 5vw, 45px);
            background: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: clamp(4px, 1vw, 8px);
        }

        .auto-reset-bar {
            position: fixed;
            bottom: 0;
            left: 0;
            height: 6px;
            background: var(--color-primary);
            width: 100%;
            /* Disinkronkan menjadi 3 detik */
            animation: shrinkBar 3s linear forwards;
        }

        @keyframes shrinkBar { from { width: 100%; } to { width: 0%; } }

        .divider { width: 1px; height: 18px; background: rgba(255, 255, 255, 0.2); }
    </style>
</head>
<body id="main-body">

    <div class="header-badge">
        <div class="flex gap-2">
            <div class="logo-circle">
                <img src="{{ asset('images/kartu.png') }}" class="w-full h-full object-contain" alt="Logo">
            </div>
            <div class="logo-circle">
                <img src="{{ asset('images/bakorwil.png') }}" class="w-full h-full object-contain" alt="Logo">
            </div>
        </div>
        
        <div class="divider"></div>

        <div class="flex flex-col">
            <span class="text-white font-black tracking-widest text-[9px] md:text-[11px]">NAWASENA</span>
            <span class="text-white/50 font-bold text-[6px] md:text-[7px] uppercase">Bakorwil III Malang</span>
        </div>
    </div>

    <main class="main-wrapper">
        <div id="status-label" class="sub-text">Pengembalian Berhasil</div>
        
        <h1 class="big-text">
            TERIMA<br>KASIH
        </h1>
        
        <div class="flex items-center gap-4 mt-4 opacity-60">
            <div class="h-[1px] w-8 md:w-16 bg-gradient-to-r from-transparent to-white"></div>
            <p id="instruction-text" class="text-white text-[10px] md:text-xs font-light tracking-[0.3em] uppercase whitespace-nowrap">
                Buku Telah Berhasil Dikembalikan
            </p>
            <div class="h-[1px] w-8 md:w-16 bg-gradient-to-l from-transparent to-white"></div>
        </div>

        <div class="absolute bottom-10 flex flex-col items-center gap-2">
            <!-- Teks disesuaikan menjadi 3 detik -->
            <p class="text-white/20 text-[8px] md:text-[10px] tracking-[0.4em] uppercase italic">
                Sistem kembali otomatis dalam 3 detik
            </p>
        </div>
    </main>

    <div class="auto-reset-bar"></div>

<script>
    const client = mqtt.connect(
        'ws://' + window.location.hostname + ':9001'
    );

    // ===============================
    // INIT PAGE
    // ===============================
    function initPage() {
        // Kirim sinyal standby dan sukses ke broker MQTT
        client.publish('rfid/mode/set', 'standby');
        client.publish('rfid/status', 'finish_success');
    }

    // ===============================
    // CLOSE SESSION
    // ===============================
    function closeSession() {
        // Hapus sisa-sisa data transaksi lokal
        localStorage.removeItem('trx_buku');
        localStorage.removeItem('last_borrower');
        localStorage.removeItem('trx');
        localStorage.removeItem('trx_type');
        localStorage.removeItem('nawasena_hasil_akhir');

        fetch('/api/esp/reset')
            .finally(() => {
                window.location.href = '/transaksi/scan-barcode';
            });
    }

    // ===============================
    // START
    // ===============================
    document.addEventListener('DOMContentLoaded', initPage);

    // Hitung mundur otomatis diubah menjadi 3000ms (3 detik)
    setTimeout(closeSession, 3000);
</script>
</body>
</html>