<?php
declare(strict_types=1);
require_once 'db/config.php';

// Fetch items on promotion
$stmt = db()->query("SELECT * FROM stock_items WHERE is_promotion = 1 AND (promotion_end IS NULL OR promotion_end >= CURDATE()) ORDER BY name_en ASC");
$items = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Promotional Catalog - A4</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&family=Noto+Sans+Arabic:wght@400;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <style>
        :root {
            --primary-color: #2c3e50;
            --accent-color: #e74c3c;
            --success-color: #27ae60;
        }
        body {
            font-family: 'Inter', sans-serif;
            background-color: #f4f7f6;
            color: var(--primary-color);
        }
        .ar {
            font-family: 'Noto Sans Arabic', sans-serif;
            direction: rtl;
        }
        .catalog-container {
            width: 210mm;
            min-height: 297mm;
            margin: 10px auto;
            background: white;
            padding: 10mm;
            box-shadow: 0 0 20px rgba(0,0,0,0.1);
            position: relative;
        }
        .header-section {
            border-bottom: 2px solid var(--primary-color);
            margin-bottom: 15px;
            padding-bottom: 10px;
        }
        .promo-title {
            font-weight: 700;
            letter-spacing: 0.5px;
            color: var(--primary-color);
            text-transform: uppercase;
        }
        .item-card {
            border: 1px solid #edf2f7;
            border-radius: 8px;
            padding: 10px;
            text-align: center;
            height: 100%;
            display: flex;
            flex-direction: column;
            position: relative;
            background: #fff;
            transition: all 0.2s ease;
        }
        .item-image-wrapper {
            height: 120px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 8px;
            background: #fdfdfd;
            border-radius: 6px;
            overflow: hidden;
        }
        .item-image {
            max-width: 100%;
            max-height: 100%;
            object-fit: contain;
        }
        .promo-tag {
            position: absolute;
            top: 5px;
            right: 5px;
            background: var(--accent-color);
            color: white;
            padding: 2px 8px;
            border-radius: 50px;
            font-size: 0.65rem;
            font-weight: 700;
            z-index: 10;
        }
        .item-name-en {
            font-weight: 600;
            font-size: 0.8rem;
            margin-bottom: 2px;
            color: #1a202c;
            line-height: 1.2;
        }
        .item-name-ar {
            font-size: 0.75rem;
            color: #718096;
            margin-bottom: 8px;
            line-height: 1.2;
        }
        .price-section {
            margin-top: auto;
            border-top: 1px dashed #e2e8f0;
            padding-top: 6px;
        }
        .price-old {
            text-decoration: line-through;
            color: #a0aec0;
            font-size: 0.75rem;
        }
        .price-new {
            color: var(--success-color);
            font-weight: 700;
            font-size: 1rem;
        }
        .currency {
            font-size: 0.75rem;
            font-weight: 400;
        }
        @page {
            size: A4;
            margin: 0;
        }
        @media print {
            body {
                background: none;
                padding: 0;
            }
            .catalog-container {
                margin: 0;
                box-shadow: none;
                width: 100%;
                height: 100%;
            }
            .no-print {
                display: none;
            }
        }
        .footer-note {
            position: absolute;
            bottom: 10mm;
            left: 0;
            right: 0;
            text-align: center;
            font-size: 0.7rem;
            color: #a0aec0;
        }
    </style>
</head>
<body>

    <div class="container my-4 no-print d-flex justify-content-center gap-3">
        <button onclick="window.print()" class="btn btn-dark px-4 py-2 fw-bold">
            <i class="bi bi-printer"></i> Print Catalog (PDF)
        </button>
        <a href="index.php" class="btn btn-outline-secondary px-4 py-2">
            Return to Dashboard
        </a>
    </div>

    <div class="catalog-container">
        <div class="header-section d-flex justify-content-between align-items-end">
            <div>
                <h1 class="promo-title h5 mb-1">Weekly Special Offers</h1>
                <p class="text-muted small mb-0" style="font-size: 0.7rem;">High Quality Products at Best Prices</p>
            </div>
            <div class="text-end">
                <h2 class="ar h6 mb-1">عروضنا الخاصة الأسبوعية</h2>
                <p class="text-muted small mb-0 ar" style="font-size: 0.7rem;">أفضل المنتجات بأقل الأسعار</p>
            </div>
        </div>

        <div class="row g-3">
            <?php foreach ($items as $item): 
                $originalPrice = (float)$item['sale_price'];
                $discountPercent = (float)$item['promotion_percent'];
                $newPrice = $originalPrice * (1 - ($discountPercent / 100));
                $image = !empty($item['image_path']) && file_exists($item['image_path']) ? $item['image_path'] : 'https://placehold.co/400x400?text=No+Image';
            ?>
            <div class="col-3">
                <div class="item-card">
                    <div class="promo-tag">-<?= round($discountPercent) ?>%</div>
                    <div class="item-image-wrapper">
                        <img src="<?= $image ?>" class="item-image" alt="<?= htmlspecialchars($item['name_en']) ?>">
                    </div>
                    <div class="item-name-en"><?= htmlspecialchars($item['name_en']) ?></div>
                    <div class="item-name-ar ar"><?= htmlspecialchars($item['name_ar']) ?></div>
                    <div class="price-section">
                        <div class="price-old"><?= number_format($originalPrice, 3) ?> <span class="currency">OMR</span></div>
                        <div class="price-new"><?= number_format($newPrice, 3) ?> <span class="currency">OMR</span></div>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>

        <?php if (empty($items)): ?>
            <div class="text-center py-5 mt-5">
                <h3 class="text-muted">No promotional items found.</h3>
                <p>Please enable promotions for items in the stock management section.</p>
            </div>
        <?php endif; ?>

        <div class="footer-note">
            <p class="mb-0">Prices are inclusive of VAT | الأسعار تشمل ضريبة القيمة المضافة</p>
            <p class="mb-0">Generated on <?= date('d/m/Y H:i') ?></p>
        </div>
    </div>

</body>
</html>