<?php
/**
 * Catalog Viewer — opens in new tab, shows PDF / Drive embed
 * Path: catalog.php?comp=ID
 */
require_once 'function.php';

$comp_id = intval($_GET['comp'] ?? 0);
$company = get_company_by_id($comp_id);
$site_name = get_site_setting('site_name');

if (!$company || empty($company['catalog_url'])) {
    http_response_code(404);
    echo '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Catalog Not Found</title>
    <script src="https://cdn.tailwindcss.com"></script></head>
    <body class="bg-slate-100 min-h-screen flex items-center justify-center p-6">
    <div class="bg-white rounded-2xl shadow-lg p-10 text-center max-w-md">
        <p class="text-gray-600 mb-4">Catalog not available for this company.</p>
        <a href="products.php" class="text-sky-600 font-bold">← Back to Products</a>
    </div></body></html>';
    exit;
}

$raw_url   = trim($company['catalog_url']);
$embed_url = catalog_embed_url($raw_url);
$company_name = $company['name'];
$is_pdf = (bool) preg_match('/\.pdf($|\?)/i', $raw_url) || stripos($raw_url, 'drive.google') !== false;
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo htmlspecialchars($company_name); ?> Catalog — <?php echo htmlspecialchars($site_name); ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        html, body { height: 100%; margin: 0; }
        .viewer-frame { height: calc(100vh - 64px); }
    </style>
</head>
<body class="bg-slate-950 text-white flex flex-col min-h-screen">

    <header class="h-16 flex-shrink-0 bg-slate-900 border-b border-slate-800 flex items-center justify-between px-4 sm:px-6">
        <div class="flex items-center gap-3 min-w-0">
            <span class="w-9 h-9 rounded-lg bg-sky-500/20 text-sky-400 flex items-center justify-center flex-shrink-0">
                <i class="fa-solid fa-book-open"></i>
            </span>
            <div class="min-w-0">
                <p class="text-[10px] uppercase tracking-wider text-slate-400 font-bold">Product Catalog</p>
                <h1 class="text-sm sm:text-base font-black truncate"><?php echo htmlspecialchars($company_name); ?></h1>
            </div>
        </div>
        <div class="flex items-center gap-2 flex-shrink-0">
            <a href="<?php echo htmlspecialchars($raw_url); ?>" target="_blank" rel="noopener"
               class="hidden sm:inline-flex items-center gap-2 text-xs font-bold uppercase bg-slate-800 hover:bg-slate-700 px-3 py-2 rounded-lg transition-colors">
                <i class="fa-solid fa-arrow-up-right-from-square"></i> Open Original
            </a>
            <button type="button" onclick="window.close()"
                class="text-xs font-bold uppercase bg-sky-500 hover:bg-sky-400 text-slate-950 px-3 py-2 rounded-lg transition-colors">
                Close
            </button>
        </div>
    </header>

    <main class="flex-1 viewer-frame bg-slate-900 relative">
        <iframe
            src="<?php echo htmlspecialchars($embed_url); ?>"
            class="absolute inset-0 w-full h-full border-0"
            title="<?php echo htmlspecialchars($company_name); ?> Catalog"
            allow="autoplay"
        ></iframe>
    </main>

</body>
</html>
