<?php
/**
 * OPAC Handler untuk Bebas Pustaka
 * Path: plugins/bebas_pustaka/opac_handler.php
 * 
 * Menangani halaman p=bebas_pustaka dengan layout Multi OPAC
 */

if (!defined('INDEX_AUTH')) {
    die("Direct access not allowed");
}

// ============================================================
// 1. AMBIL PARAMETER
// ============================================================
$lib_slug = $_GET['lib'] ?? 'pusat';
$p = $_GET['p'] ?? '';

// 🔥 DEBUG: Cek semua parameter URL
$log_file = __DIR__ . '/debug_opac.log';
file_put_contents($log_file, date('Y-m-d H:i:s') . " - URL: " . $_SERVER['REQUEST_URI'] . "\n", FILE_APPEND);
file_put_contents($log_file, date('Y-m-d H:i:s') . " - GET: " . print_r($_GET, true) . "\n", FILE_APPEND);

// Cek apakah ada parameter ajukan_ulang
if (isset($_GET['ajukan_ulang'])) {
    file_put_contents($log_file, date('Y-m-d H:i:s') . " - ✅ AJUKAN_ULANG DETECTED: " . $_GET['ajukan_ulang'] . "\n", FILE_APPEND);
} else {
    file_put_contents($log_file, date('Y-m-d H:i:s') . " - ❌ AJUKAN_ULANG NOT DETECTED\n", FILE_APPEND);
}
// ============================================================
// 2. JIKA BUKAN BEBAS PUSTAKA, LANJUTKAN
// ============================================================
if ($p !== 'bebas_pustaka') {
    return; // Biarkan SLiMS memproses halaman lain
}

// ============================================================
// 3. SET GLOBAL VARIABLES UNTUK TEMPLATE
// ============================================================
$GLOBALS['nav_lib_slug'] = $lib_slug;
$GLOBALS['current_p'] = 'bebas_pustaka';

// ============================================================
// 4. LOAD HELPERS
// ============================================================
require_once __DIR__ . '/src/helper.php';

// Cari helpers Multi OPAC
$multi_opac_helpers = [
    dirname(__DIR__, 2) . '/multi_opac_path/src/helpers.php',
    dirname(__DIR__, 3) . '/multi_opac_path/src/helpers.php',
    __DIR__ . '/../../multi_opac_path/src/helpers.php'
];

foreach ($multi_opac_helpers as $path) {
    if (file_exists($path)) {
        require_once $path;
        break;
    }
}

// ============================================================
// 5. AMBIL DATA LIBRARY & THEME
// ============================================================
$library = getLibraryBySlug($lib_slug);
$library_id = $library['library_id'] ?? 1;
$library_name = $library['library_name'] ?? 'Perpustakaan';

$theme = function_exists('getLibraryTheme') ? getLibraryTheme($library_id) : ['primary_color' => '#0d6efd'];
$primaryColor = $theme['primary_color'] ?? '#0d6efd';
$logo = $theme['logo'] ?? '';

// Set untuk template
$GLOBALS['nav_library_name'] = $library_name;
$GLOBALS['nav_theme'] = $theme;
$GLOBALS['library_slug'] = $lib_slug;

// ============================================================
// 6. LOAD TEMPLATE MULTI OPAC
// ============================================================
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bebas Pustaka - <?= htmlspecialchars($library_name) ?></title>
    
    <!-- Bootstrap CSS -->
    <link href="/template/default/assets/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="/plugins/multi_opac_path/assets/css/all.min.css">
    <!-- CSS Kustom -->
    <link rel="stylesheet" href="/plugins/multi_opac_path/assets/css/opac-style.css">
    
    <style>
        :root {
            --primary-color: <?= $primaryColor ?>;
        }
        .bebas-container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        .bebas-card {
            background: white;
            border-radius: 12px;
            box-shadow: 0 2px 12px rgba(0,0,0,0.08);
            padding: 24px;
            margin-bottom: 20px;
        }
        .bebas-card .card-header {
            background: #f8f9fa;
            padding: 12px 16px;
            border-bottom: 2px solid #e9ecef;
            border-radius: 8px 8px 0 0;
            margin: -24px -24px 20px -24px;
            padding: 16px 24px;
        }
        .btn-primary-custom {
            background-color: <?= $primaryColor ?>;
            border-color: <?= $primaryColor ?>;
        }
        .btn-primary-custom:hover {
            background-color: <?= $primaryColor ?>dd;
            border-color: <?= $primaryColor ?>dd;
        }
        .badge-status {
            font-size: 12px;
            padding: 4px 10px;
            border-radius: 20px;
        }
        .badge-pending { background: #ffc107; color: #000; }
        .badge-verified { background: #17a2b8; color: #fff; }
        .badge-approved { background: #007bff; color: #fff; }
        .badge-printed { background: #28a745; color: #fff; }
        .badge-rejected { background: #dc3545; color: #fff; }
    </style>
</head>
<body>

<!-- ============================================================
NAVBAR: PAKAI _navbar_library.inc.php
============================================================ -->
<?php 
// Load navbar library dari Multi OPAC
$navbar_file = dirname(__DIR__, 2) . '/multi_opac_path/template/_navbar_library.inc.php';
if (file_exists($navbar_file)) {
    require_once $navbar_file;
} else {
    // Fallback navbar sederhana
    echo '<nav class="navbar navbar-expand-lg navbar-dark" style="background-color: ' . $primaryColor . '; padding:12px 0;">';
    echo '<div class="container">';
    echo '<a class="navbar-brand" href="index.php?p=multi-opac&lib=' . $lib_slug . '">';
    echo '<i class="fa-solid fa-book-open me-2"></i> ' . htmlspecialchars($library_name);
    echo '</a>';
    echo '</div>';
    echo '</nav>';
}
?>

<!-- ============================================================
KONTEN UTAMA
============================================================ -->
<div class="container bebas-container">
    <?php
    // Load konten Bebas Pustaka
    if (isset($_SESSION['mid']) || isset($_SESSION['m_id'])) {
        // 🔥 PASTIKAN PARAMETER GET DITERUSKAN
        $_GET['ajukan_ulang'] = $_GET['ajukan_ulang'] ?? null;
        $_GET['id'] = $_GET['id'] ?? null;
        require_once __DIR__ . '/pages/member.php';
    } else {
        echo '<div class="bebas-card text-center py-5">';
        echo '<i class="fa-solid fa-lock fa-3x text-muted mb-3"></i>';
        echo '<h4>Silakan login terlebih dahulu</h4>';
        echo '<p class="text-muted">Anda harus login sebagai member untuk mengakses halaman ini.</p>';
        echo '<a href="index.php?p=member-login&lib=' . $lib_slug . '" class="btn btn-primary btn-primary-custom">';
        echo '<i class="fa-solid fa-right-to-bracket me-2"></i> Login';
        echo '</a>';
        echo '</div>';
    }
    ?>
</div>

<!-- ============================================================
FOOTER: PAKAI library_page.tpl (jika ada)
============================================================ -->
<?php
$footer_file = dirname(__DIR__, 2) . '/multi_opac_path/template/library_page.tpl';
if (file_exists($footer_file)) {
    // Ambil bagian footer dari library_page.tpl
    $content = file_get_contents($footer_file);
    if (preg_match('/<footer.*?>(.*?)<\/footer>/is', $content, $matches)) {
        echo '<footer class="text-center py-4 mt-4" style="background: white; border-top: 1px solid #e9ecef;">';
        echo $matches[1];
        echo '</footer>';
    } else {
        echo '<footer class="text-center py-4 mt-4" style="background: white; border-top: 1px solid #e9ecef;">';
        echo '<div class="container">';
        echo '<p class="mb-0 text-muted small">&copy; ' . date('Y') . ' ' . htmlspecialchars($library_name) . ' | Bebas Pustaka</p>';
        echo '</div>';
        echo '</footer>';
    }
} else {
    echo '<footer class="text-center py-4 mt-4" style="background: white; border-top: 1px solid #e9ecef;">';
    echo '<div class="container">';
    echo '<p class="mb-0 text-muted small">&copy; ' . date('Y') . ' ' . htmlspecialchars($library_name) . ' | Bebas Pustaka</p>';
    echo '</div>';
    echo '</footer>';
}
?>

<!-- Scripts -->
<script src="/template/default/assets/js/jquery.min.js"></script>
<script src="/template/default/assets/js/bootstrap.bundle.min.js"></script>

</body>
</html>
<?php
// Stop eksekusi agar SLiMS tidak memproses lebih lanjut
exit;