<!DOCTYPE html>
<html lang="id">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Layar Informasi Poliklinik</title>
    <link rel="shortcut icon" type="image/png" href="<?= base_url('assets/images/logo-mini.png') ?>">

    <!-- Tailwind CSS -->
    <script src="https://cdn.tailwindcss.com"></script>

    <!-- Google Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Montserrat:wght@700&display=swap" rel="stylesheet">

    <style>
        /* Custom styles to complement Tailwind */
        body {
            font-family: 'Inter', sans-serif;
            overflow: hidden;
            /* Mencegah scroll di body */
            background-color: #f0f4f8;
            /* Warna latar belakang body */
        }

        /* Container utama yang akan diskalakan */
        .scale-container {
            width: 1920px;
            height: 1080px;
            position: absolute;
            top: 0;
            left: 0;
            transform-origin: top left;
            background-color: #ffffff;
        }

        /* Menambahkan efek marquee pada footer */
        .marquee-content {
            display: inline-block;
            white-space: nowrap;
            animation: marquee 30s linear infinite;
            padding-left: 100%;
        }

        @keyframes marquee {
            0% {
                transform: translateX(0%);
            }

            100% {
                transform: translateX(-100%);
            }
        }
    </style>
</head>

<body class="bg-gray-100" onload="init()">

    <div class="scale-container">
        <div class="relative w-full h-full">
            <!-- Background Image -->
            <!-- PERHATIAN: Ganti 'src' di bawah dengan path gambar Anda: 'assets_/assets/img/backgroundhd1.jpg' -->
            <!-- Posisi gambar diatur ke 'object-top' untuk menampilkan bagian atas gambar -->
            <img class="absolute inset-0 w-full h-full object-cover object-top z-0" src="assets_/assets/img/backgroundhd1.jpg" onerror="this.onerror=null;this.src='https://images.unsplash.com/photo-1576091160550-2173dba999ef?q=80&w=2070&auto=format&fit=crop';" alt="Background Medis" />
            <!-- Overlay untuk memberikan opacity 40% pada gambar (60% opaque) -->
            <div class="absolute inset-0 bg-white/60 z-1"></div>

            <!-- Header -->
            <header class="absolute top-0 left-0 right-0 h-[157px] bg-white/80 backdrop-blur-sm shadow-md z-10 flex items-center justify-between px-14 py-8">
                <!-- Logo -->
                <img class="h-20 w-auto" src="<?= base_url('assets_/assets/img/and.png') ?>" alt="Logo Rumah Sakit" />

                <!-- Judul Utama -->
                <div class="text-center">
                    <h1 class="font-bold text-5xl text-blue-800" style="font-family: 'Montserrat', sans-serif;">LAYAR INFORMASI KUOTA POLIKLINIK</h1>
                    <p class="text-2xl text-gray-600 mt-1">(Layar Update Antrian dan Informasi)</p>
                </div>

                <!-- Tanggal dan Waktu -->
                <div class="text-right">
                    <div id="date-display" class="text-xl font-semibold text-gray-700">MEMUAT...</div>
                    <div id="time-display" class="text-4xl font-bold text-gray-900">00:00</div>
                </div>
            </header>

            <!-- Judul Rumah Sakit di bawah Header (Warna diubah menjadi hijau) -->
            <div class="absolute top-[170px] left-1/2 -translate-x-1/2 z-10 bg-green-600 text-white px-8 py-2 rounded-full shadow-lg">
                <h2 class="text-3xl font-bold whitespace-nowrap">RUMAH SAKIT UMUM ANANDA PURWOKERTO</h2>
            </div>


            <!-- Konten Utama -->
            <main class="absolute top-[235px] bottom-[60px] left-3 right-3 z-10 flex gap-6">
                <!-- Kolom Kiri: Tabel Informasi -->
                <div class="w-1/2 flex flex-col bg-white rounded-xl shadow-lg p-4">
                    <h3 class="text-2xl font-bold text-center text-blue-700 mb-3 flex-shrink-0">INFORMASI KUOTA POLIKLINIK</h3>
                    <div class="flex-grow overflow-hidden rounded-md" id="table-scroll-container">
                        <table class="w-full text-left border-collapse">
                            <thead class="text-base text-white uppercase bg-blue-600 sticky top-0 z-20">
                                <tr>
                                    <!-- Lebar kolom telah disesuaikan -->
                                    <th scope="col" class="px-4 py-4 border-2 border-blue-400" style="width: 34%;">Nama Dokter</th>
                                    <th scope="col" class="px-4 py-4 text-center border-2 border-blue-400" style="width: 24%;">Klinik</th>
                                    <th scope="col" class="px-4 py-4 text-center border-2 border-blue-400" style="width: 16%;">Jam</th>
                                    <th scope="col" class="px-4 py-4 text-center border-2 border-blue-400" style="width: 9%;">Kapasitas</th>
                                    <th scope="col" class="px-4 py-4 text-center border-2 border-blue-400" style="width: 9%;">Terdaftar</th>
                                    <th scope="col" class="px-4 py-4 text-center border-2 border-blue-400" style="width: 8%;">Sisa</th>
                                </tr>
                            </thead>
                            <!-- Ukuran font pada isi tabel diperbesar menjadi 'text-xl' -->
                            <tbody id="table-body" class="text-xl">
                                <!-- Data akan diisi oleh JavaScript dari server -->
                            </tbody>
                        </table>
                    </div>
                </div>

                <!-- Kolom Kanan: Video Informasi -->
                <div class="w-1/2 flex flex-col bg-white rounded-xl shadow-lg p-4">
                    <h3 class="text-2xl font-bold text-center text-blue-700 mb-3 flex-shrink-0">MEDIA INFORMASI</h3>
                    <div class="flex-grow rounded-md overflow-hidden bg-black">
                        <!-- PERHATIAN: Parameter 'mute=0' memungkinkan suara, TAPI dapat memblokir autoplay di beberapa browser. -->
                        <!-- Browser modern umumnya hanya mengizinkan autoplay untuk video tanpa suara. -->
                        <iframe class="w-full h-full" src="https://www.youtube.com/embed/13ZEshSNQ5s?autoplay=1&mute=0&loop=1&playlist=13ZEshSNQ5s&controls=0" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
                    </div>
                </div>
            </main>

            <!-- Footer -->
            <footer class="absolute bottom-0 left-0 right-0 h-[50px] bg-green-600 text-white z-10 flex items-center overflow-hidden">
                <div class="marquee-content">
                    <span class="text-xl italic font-medium mx-16">“Rumah Sakit Umum ANANDA Purwokerto“</span>
                    <span class="text-xl italic font-medium mx-16">“Terima Kasih Atas Kunjungan Anda, Kesehatan Anda Kebahagiaan Kami”</span>
                    <span class="text-xl italic font-medium mx-16">“Semoga Sehat Selalu“</span>
                    <span class="text-xl italic font-medium mx-16">“RSU ANANDA Purwokerto Berkomitmen Untuk Memberikan Pelayanan Bermutu Secara Komprehensif“</span>
                    <span class="text-xl italic font-medium mx-16">“Support by Instalasi Sistem Informasi RSU ANANDA Purwokerto“</span>
                </div>
            </footer>

        </div>
    </div>

    <script>
        // --- Konfigurasi ---
        const REFRESH_INTERVAL_MS = 5000;
        const SCROLL_SPEED_PX_PER_S = 15;

        // --- Variabel Global ---
        let scrollInterval;
        let currentData = ""; // Menyimpan data terakhir untuk mencegah reload yang tidak perlu

        /**
         * Menampilkan status loading di dalam tabel.
         */
        function showLoadingState() {
            const tableBody = document.getElementById('table-body');
            if (scrollInterval) clearInterval(scrollInterval);
            tableBody.innerHTML = `
                <tr>
                    <td colspan="6" class="text-center p-8">
                        <div class="flex justify-center items-center text-gray-600">
                            <svg class="animate-spin h-6 w-6 mr-3 text-blue-600" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
                                <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                                <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                            </svg>
                            <span class="font-semibold">Memperbarui data jadwal...</span>
                        </div>
                    </td>
                </tr>`;
        }

        /**
         * Mengambil data HTML dari controller dan memperbarui tabel.
         */
        function updateTableData() {
            const tableBody = document.getElementById('table-body');
            const xhttp = new XMLHttpRequest();

            xhttp.onload = function() {
                let htmlContent = this.responseText;
                if (htmlContent && htmlContent.trim() !== "" && htmlContent !== currentData) {
                    currentData = htmlContent;
                    tableBody.innerHTML = htmlContent + htmlContent;
                    startAutoScroll();
                } else if (!htmlContent || htmlContent.trim() === "") {
                    tableBody.innerHTML = `<tr><td colspan="6" class="text-center p-8 text-gray-500 font-semibold">Tidak ada data jadwal poliklinik saat ini.</td></tr>`;
                    if (scrollInterval) clearInterval(scrollInterval);
                }
            };
            xhttp.onerror = function() {
                tableBody.innerHTML = `<tr><td colspan="6" class="text-center p-8 text-red-600 font-bold">Gagal terhubung ke server. Mencoba lagi...</td></tr>`;
                if (scrollInterval) clearInterval(scrollInterval);
            };

            // PENTING: Kode ini akan memanggil backend Anda.
            // Ini akan menyebabkan error 'Invalid URL' di lingkungan pratinjau,
            // namun akan berfungsi dengan benar saat dijalankan di server CodeIgniter Anda.
            xhttp.open("GET", "Poli/layarutama");
            xhttp.send();

            /* PENTING UNTUK ANDA:
            Agar tampilan data dari API Anda rapi seperti yang diinginkan (dengan border, dll.),
            pastikan file PHP 'Poli/antreandis' Anda menghasilkan output HTML dengan struktur seperti ini:

            <tr class="bg-white">
                <td class="px-4 py-3 font-medium text-gray-900 border border-gray-300">NAMA DOKTER DARI API</td>
                <td class="px-4 py-3 text-center border border-gray-300">NAMA POLI DARI API</td>
                <td class="px-4 py-3 text-center border border-gray-300">JADWAL DARI API</td>
                <td class="px-4 py-3 text-center font-semibold text-2xl border border-gray-300">KAPASITAS DARI API</td>
                <td class="px-4 py-3 text-center font-semibold text-2xl border border-gray-300">TERDAFTAR DARI API</td>
                <td class="px-4 py-3 text-center font-bold text-2xl text-green-600 border border-gray-300">SISA DARI API</td>
            </tr>
            <tr class="bg-gray-100">
                <td class="px-4 py-3 font-medium text-gray-900 border border-gray-300">...DATA BERIKUTNYA...</td>
                ... dan seterusnya ...
            </tr>

            Tambahkan class Tailwind CSS tersebut ke dalam loop 'foreach' di file PHP Anda.
            */
        }

        /**
         * Menangani logika auto-scroll.
         */
        function startAutoScroll() {
            const container = document.getElementById('table-scroll-container');
            if (scrollInterval) clearInterval(scrollInterval);
            if (container.scrollHeight <= container.clientHeight) return;

            let scrollPosition = 0;
            const scrollAmount = SCROLL_SPEED_PX_PER_S / 60;

            scrollInterval = setInterval(() => {
                scrollPosition += scrollAmount;
                if (scrollPosition >= container.scrollHeight / 2) {
                    scrollPosition = 0;
                }
                container.scrollTop = scrollPosition;
            }, 1000 / 60);
        }

        /**
         * Memperbarui tampilan tanggal dan waktu secara terus-menerus.
         */
        function updateDateTime() {
            const now = new Date();
            const options = {
                timeZone: 'Asia/Jakarta',
                weekday: 'long',
                year: 'numeric',
                month: 'long',
                day: 'numeric'
            };
            const tanggal = now.toLocaleDateString('id-ID', options).toUpperCase();
            const jam = now.toLocaleTimeString('id-ID', {
                timeZone: 'Asia/Jakarta',
                hour: '2-digit',
                minute: '2-digit',
                hour12: false
            }).replace(/\./g, ':');

            document.getElementById('date-display').textContent = tanggal;
            document.getElementById('time-display').textContent = jam;
        }

        /**
         * Menyesuaikan skala konten agar pas dengan jendela browser.
         */
        function scaleToFit() {
            const container = document.querySelector('.scale-container');
            const targetWidth = 1920;
            const targetHeight = 1080;

            const windowWidth = window.innerWidth;
            const windowHeight = window.innerHeight;

            const scaleX = windowWidth / targetWidth;
            const scaleY = windowHeight / targetHeight;

            container.style.transform = `scale(${scaleX}, ${scaleY})`;
        }

        /**
         * Menginisialisasi aplikasi.
         */
        function init() {
            scaleToFit();
            updateDateTime();
            showLoadingState();
            updateTableData();

            window.addEventListener('resize', scaleToFit);
            setInterval(updateDateTime, 1000);
            setInterval(updateTableData, REFRESH_INTERVAL_MS);
        }
    </script>
</body>

</html>