<?php
require_once __DIR__ . '/../config/auth.php';
require_once __DIR__ . '/../includes/functions.php';
requirePermission('product_management');

$pdo = getDB();
$product_id = isset($_GET['product_id']) ? (int)$_GET['product_id'] : 0;

if ($product_id) {
    $stmt = $pdo->prepare("
        SELECT p.*, c.category_name, b.brand_name 
        FROM products p
        LEFT JOIN categories c ON p.category_id = c.id
        LEFT JOIN brands b ON p.brand_id = b.id
        WHERE p.id = ?
    ");
    $stmt->execute([$product_id]);
    $product = $stmt->fetch();
    if (!$product) { header('Location: products.php'); exit; }
}

$all_products = $pdo->query("SELECT id, product_name, sku, barcode, selling_price FROM products WHERE status = 1 AND barcode IS NOT NULL AND barcode != '' ORDER BY product_name")->fetchAll();

$count = isset($_GET['count']) ? max(1, min(100, (int)$_GET['count'])) : 1;

include __DIR__ . '/../includes/header.php';
include __DIR__ . '/../includes/sidebar.php';
?>
<div class="content-wrapper">
    <div class="container-fluid">
        <h4 class="page-title"><i class="fas fa-barcode me-2"></i>Print Barcode</h4>

        <?php if (isset($_SESSION['error'])): ?><div class="alert alert-danger alert-dismissible"><?= $_SESSION['error']; unset($_SESSION['error']); ?></div><?php endif; ?>

        <div class="row mb-3">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-body">
                        <form method="GET" class="row g-3">
                            <div class="col-md-6">
                                <label class="form-label">Select Product</label>
                                <select name="product_id" class="form-select" onchange="this.form.submit()">
                                    <option value="">-- Select Product --</option>
                                    <?php foreach ($all_products as $ap): ?>
                                    <option value="<?= $ap['id'] ?>" <?= $product_id == $ap['id'] ? 'selected' : '' ?>>
                                        <?= htmlspecialchars($ap['product_name'] . ' (' . $ap['sku'] . ')') ?>
                                    </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-3">
                                <label class="form-label">Label Count</label>
                                <input type="number" name="count" class="form-control" value="<?= $count ?>" min="1" max="100">
                            </div>
                            <div class="col-md-3 d-flex align-items-end">
                                <button type="submit" class="btn btn-primary w-100"><i class="fas fa-sync me-1"></i>Update</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <?php if ($product): ?>
        <div class="text-end mb-3">
            <button class="btn btn-success" onclick="window.print()"><i class="fas fa-print me-1"></i>Print Labels</button>
        </div>
        <div class="barcode-labels">
            <?php for ($i = 0; $i < $count; $i++): ?>
            <div class="barcode-label">
                <div class="barcode-shop"><?= htmlspecialchars(getSetting('shop_name', 'DigiMobiz')) ?></div>
                <div class="barcode-name"><?= htmlspecialchars($product['product_name']) ?></div>
                <div class="barcode-sku">Code: <?= htmlspecialchars($product['product_code']) ?></div>
                <div class="barcode-number"><?= htmlspecialchars($product['barcode']) ?></div>
                <div class="barcode-price"><?= formatCurrency($product['selling_price']) ?></div>
            </div>
            <?php endfor; ?>
        </div>
        <?php else: ?>
        <div class="alert alert-info">Select a product to print barcode labels.</div>
        <?php endif; ?>
    </div>
</div>

<style>
@media print {
    .sidebar, .navbar, .footer, .card-header, .btn, .content-wrapper .card:first-child { display: none !important; }
    .barcode-labels { display: flex; flex-wrap: wrap; gap: 10px; }
    .barcode-label { width: 2.25in; height: 1.25in; border: 1px solid #000; padding: 6px; text-align: center; page-break-inside: avoid; font-family: 'Courier New', monospace; display: flex; flex-direction: column; justify-content: center; align-items: center; }
    .barcode-shop { font-size: 9px; font-weight: bold; margin-bottom: 2px; }
    .barcode-name { font-size: 10px; font-weight: bold; margin-bottom: 2px; }
    .barcode-sku { font-size: 7px; margin-bottom: 2px; }
    .barcode-number { font-size: 12px; font-weight: bold; letter-spacing: 1px; margin-bottom: 2px; font-family: 'Libre Barcode 39', 'Courier New', monospace; }
    .barcode-price { font-size: 10px; font-weight: bold; }
    body { margin: 0; padding: 0.25in; }
}
.barcode-labels { display: flex; flex-wrap: wrap; gap: 10px; }
.barcode-label { width: 2.25in; height: 1.25in; border: 1px solid #ccc; padding: 6px; text-align: center; page-break-inside: avoid; font-family: 'Courier New', monospace; display: flex; flex-direction: column; justify-content: center; align-items: center; }
.barcode-shop { font-size: 9px; font-weight: bold; margin-bottom: 2px; }
.barcode-name { font-size: 10px; font-weight: bold; margin-bottom: 2px; }
.barcode-sku { font-size: 7px; color: #666; margin-bottom: 2px; }
.barcode-number { font-size: 10px; font-weight: bold; letter-spacing: 1px; margin-bottom: 2px; }
.barcode-price { font-size: 10px; font-weight: bold; color: #000; }
</style>

<?php include __DIR__ . '/../includes/footer.php'; ?>
