<?php
ob_start();
include("db/cn.php");
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
$search_name = isset($_GET['search']) ? trim($_GET['search']) : '';
$products = [];

if ($search_name !== '') {
    $stmt = $connection->prepare("SELECT product_id, product_name, product_unit, retail_price FROM products WHERE product_id LIKE CONCAT('%', ?, '%') LIMIT 1");
    $stmt->bind_param("s", $search_name);
    $stmt->execute();
    $result = $stmt->get_result();
    if ($result->num_rows > 0) {
        $products = $result->fetch_all(MYSQLI_ASSOC);
    }
} else {
    $query = "SELECT product_id, product_name, retail_price, product_unit FROM products";
    $result = mysqli_query($connection, $query);
    $products = mysqli_fetch_all($result, MYSQLI_ASSOC);
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Kashmir Bazar</title>
    <link href="../vendor/fontawesome-free/css/all.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@300;700&display=swap" rel="stylesheet">
    <link href="../css/sb-admin-2.min.css" rel="stylesheet">
    <style>
        body {
            font-family: 'Quicksand', sans-serif;
        }
        label {
            font-weight: 700;
        }
        .btn {
            font-weight: 700;
        }
        .bg {
            background: linear-gradient(270deg, #060606, #0A5064);
        }
        .product-card {
            background-color: #fff;
            color: black;
            border-radius: 10px;
            padding: 10px;
            text-align: center;
            font-weight: 700;
            margin: 5px;
            width: 48mm;
            height: 28mm;
            display: inline-block;
        }
        .product-card img {
            width: 90%;
            height: 15mm;
        }
@media print {
    @page {
        size: 50mm auto;
        margin: 0;
    }
    html, body {
        margin: 0;
        padding: 0;
        width: 80mm;
        height: 100%;
        font-size:28px;
        margin-top:-5%;
    }
    .no-print, .no-print * {
        display: none !important;
    }
    body * {
        visibility: hidden !important;
    }
    #print-area, #print-area * {
        visibility: visible !important;
    }
    #print-area {
        position: absolute;
        left: 0;
        top: 0;
        width: 50mm;
    }
    .product-card {
        width: 80mm;
        min-height: 40mm; /* forces enough space for vertical center */
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
       
        padding: 0 0 0 40px;
        margin-left:35%;
        
        box-sizing: border-box;
        text-align: center;
        page-break-after: always;
    }
    .product-id{
        font-size:65px;
        margin-top:-16%;
    }
}
    </style>
</head>
<body>
    <div class="container-fluid" style="max-width:100%">
        <div class="row bg no-print">
            <div class="col-sm-3" style="width:100%; height:69px;">
                <img src="../img/logo231.png" style="background:#FFF; border-radius:30px;margin-top:5px;" width="56" height="56">
            </div>
            <div class="col-sm-6">
                <h2 class="text-white text-center" style="line-height:60px;font-size:28px; text-transform:uppercase;font-weight:700;">Kashmir Bazar</h2>
            </div>
            <div class="col-sm-3">
                <a href="addproduct.php"><button type="button" class="btn btn-light" style="font-weight:700;float:right; margin-top:12px;">Back</button></a>
            </div>
        </div>
    </div>
    <div class="container mt-5">
        <div class="no-print mb-3">
            <form method="get" class="form-inline mb-2">
                <label class="mr-2">Search Label</label>
                <input type="text" name="search" class="form-control mr-2" placeholder="Enter product ID" style="font-weight:700;" value="<?= htmlspecialchars($search_name) ?>">
                <button type="submit" class="btn btn-primary">Search</button>
                <a href="<?= basename($_SERVER['PHP_SELF']) ?>" class="btn btn-secondary ml-2"><i class="fas fa-sync fa-1x"></i></a>
            </form>
            <button onclick="window.print()" class="btn btn-primary">Print Labels</button>
        </div>
        <div id="print-area">
            <?php if (count($products) > 0): ?>
                <?php foreach ($products as $row): ?>
                    <?php if (!empty($row['product_id']) && !empty($row['product_name']) && !empty($row['retail_price'])): ?>
                        <div class="product-card">
                            <div style="font-size: 1.2rem; font-weight: bold;">
                                <?= htmlspecialchars($row['product_name']) ?>
                            </div>
                            <p style="font-size:20px;">
                   
                           <img src="https://barcode.tec-it.com/barcode.ashx?data=<?= urlencode($row['product_id']) ?>&code=Code128&translate-esc=false" alt="Barcode" class="no-print">                            
                            
                            </p>
                            <div class="product-id " >
                               <?= htmlspecialchars('€ ' . $row['retail_price'] . '') ?>

                            </div>
                        </div>
                    <?php endif; ?>
                <?php endforeach; ?>
            <?php else: ?>
                <div class="alert alert-warning">No matching products found.</div>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>