<?php
// modules/[nama_file].php
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Path BENAR untuk file di folder modules/
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../config/app.php';

checkAuth();

$conn = getConnection();

// Daftar SPO dengan link Google Drive
$spo_list = [
    [
        'kode' => 'SPO-001',
        'nama' => 'Standar Prosedur Operasional Pemasangan Infus',
        'kategori' => 'Tindakan Keperawatan',
        'drive_link' => 'https://drive.google.com/file/d/example1/view',
        'embed_link' => 'https://drive.google.com/file/d/example1/preview'
    ],
    [
        'kode' => 'SPO-002',
        'nama' => 'Standar Prosedur Operasional Pemberian Obat Injeksi',
        'kategori' => 'Tindakan Keperawatan',
        'drive_link' => 'https://drive.google.com/file/d/example2/view',
        'embed_link' => 'https://drive.google.com/file/d/example2/preview'
    ],
    [
        'kode' => 'SPO-003',
        'nama' => 'Standar Prosedur Operasional Perawatan Luka',
        'kategori' => 'Tindakan Keperawatan',
        'drive_link' => 'https://drive.google.com/file/d/example3/view',
        'embed_link' => 'https://drive.google.com/file/d/example3/preview'
    ],
    [
        'kode' => 'SPO-004',
        'nama' => 'Standar Prosedur Operasional Pemasangan NGT',
        'kategori' => 'Tindakan Keperawatan',
        'drive_link' => 'https://drive.google.com/file/d/example4/view',
        'embed_link' => 'https://drive.google.com/file/d/example4/preview'
    ],
    [
        'kode' => 'SPO-005',
        'nama' => 'Standar Prosedur Operasional Perawatan Pasien Isolasi',
        'kategori' => 'PPI',
        'drive_link' => 'https://drive.google.com/file/d/example5/view',
        'embed_link' => 'https://drive.google.com/file/d/example5/preview'
    ],
    [
        'kode' => 'SPO-006',
        'nama' => 'Standar Prosedur Operasional Cuci Tangan 6 Langkah',
        'kategori' => 'PPI',
        'drive_link' => 'https://drive.google.com/file/d/example6/view',
        'embed_link' => 'https://drive.google.com/file/d/example6/preview'
    ],
];

$kategori = $_GET['kategori'] ?? '';
$search = $_GET['search'] ?? '';

// Filter
$filtered_spo = $spo_list;
if ($kategori) {
    $filtered_spo = array_filter($filtered_spo, function($spo) use ($kategori) {
        return $spo['kategori'] == $kategori;
    });
}
if ($search) {
    $filtered_spo = array_filter($filtered_spo, function($spo) use ($search) {
        return stripos($spo['nama'], $search) !== false || stripos($spo['kode'], $search) !== false;
    });
}

// Get unique categories
$categories = array_unique(array_column($spo_list, 'kategori'));
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Daftar SPO Keperawatan</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        body { font-family: 'Poppins', sans-serif; background: #f8fafc; }
        .spo-card {
            transition: all 0.3s;
        }
        .spo-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
        }
    </style>
</head>
<body class="p-6">
    <div class="mb-6">
        <h1 class="text-2xl font-bold text-gray-800">Daftar SPO Keperawatan</h1>
        <p class="text-gray-600">Standar Prosedur Operasional - Google Drive Viewer</p>
    </div>
    
    <!-- Filter dan Search -->
    <div class="bg-white rounded-xl shadow-lg p-6 mb-6">
        <form method="GET" class="grid grid-cols-1 md:grid-cols-3 gap-4">
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Kategori</label>
                <select name="kategori" class="w-full px-4 py-2 border border-gray-300 rounded-lg">
                    <option value="">Semua Kategori</option>
                    <?php foreach ($categories as $cat): ?>
                    <option value="<?= $cat ?>" <?= $kategori == $cat ? 'selected' : '' ?>><?= $cat ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Cari</label>
                <input type="text" name="search" value="<?= htmlspecialchars($search) ?>" placeholder="Kode atau nama SPO..." class="w-full px-4 py-2 border border-gray-300 rounded-lg">
            </div>
            <div class="flex items-end">
                <button type="submit" class="w-full bg-gradient-to-r from-blue-600 to-indigo-600 text-white px-4 py-2 rounded-lg hover:from-blue-700 hover:to-indigo-700">
                    <i class="fas fa-search mr-2"></i> Filter
                </button>
            </div>
        </form>
    </div>
    
    <!-- Daftar SPO -->
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 mb-8">
        <?php foreach ($filtered_spo as $spo): ?>
        <div class="spo-card bg-white rounded-xl shadow-lg overflow-hidden cursor-pointer" onclick="viewSPO('<?= $spo['embed_link'] ?>', '<?= $spo['nama'] ?>')">
            <div class="bg-gradient-to-r from-blue-600 to-indigo-600 px-4 py-3">
                <h3 class="text-white font-semibold"><?= $spo['kode'] ?></h3>
            </div>
            <div class="p-4">
                <h4 class="font-semibold text-gray-800 mb-2"><?= $spo['nama'] ?></h4>
                <div class="flex items-center justify-between">
                    <span class="px-2 py-1 bg-blue-100 text-blue-800 text-xs rounded-full"><?= $spo['kategori'] ?></span>
                    <a href="<?= $spo['drive_link'] ?>" target="_blank" class="text-blue-600 hover:text-blue-800" onclick="event.stopPropagation()">
                        <i class="fas fa-external-link-alt"></i>
                    </a>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
    
    <!-- Google Drive Viewer Modal -->
    <div id="spoModal" class="fixed inset-0 bg-black bg-opacity-50 hidden items-center justify-center z-50" onclick="closeModal()">
        <div class="bg-white rounded-xl shadow-2xl w-11/12 h-5/6 max-w-6xl" onclick="event.stopPropagation()">
            <div class="flex justify-between items-center p-4 border-b">
                <h3 id="modalTitle" class="text-lg font-semibold text-gray-800">Viewer SPO</h3>
                <button onclick="closeModal()" class="text-gray-500 hover:text-gray-700">
                    <i class="fas fa-times text-xl"></i>
                </button>
            </div>
            <div class="p-4 h-[calc(100%-80px)]">
                <iframe id="spoIframe" src="" class="w-full h-full border-0 rounded-lg"></iframe>
            </div>
        </div>
    </div>
    
    <!-- Jika tidak ada SPO -->
    <?php if (empty($filtered_spo)): ?>
    <div class="bg-yellow-50 border-l-4 border-yellow-500 p-4 rounded">
        <p class="text-yellow-700">Tidak ada SPO yang ditemukan.</p>
    </div>
    <?php endif; ?>
    
    <script>
        function viewSPO(embedLink, title) {
            document.getElementById('modalTitle').textContent = title;
            document.getElementById('spoIframe').src = embedLink;
            document.getElementById('spoModal').classList.remove('hidden');
            document.getElementById('spoModal').classList.add('flex');
        }
        
        function closeModal() {
            document.getElementById('spoModal').classList.add('hidden');
            document.getElementById('spoModal').classList.remove('flex');
            document.getElementById('spoIframe').src = '';
        }
        
        // Close modal with Escape key
        document.addEventListener('keydown', function(e) {
            if (e.key === 'Escape') {
                closeModal();
            }
        });
    </script>
</body>
</html>