<?php
require_once 'connection.php';

$laptop_id = $_GET['id'] ?? 0;

$stmt = $pdo->prepare("
    SELECT l.*, c.name as category_name 
    FROM laptops l 
    LEFT JOIN laptop_categories c ON l.category_id = c.id 
    WHERE l.id = ?
");
$stmt->execute([$laptop_id]);
$laptop = $stmt->fetch();

if (!$laptop) {
    echo '<div class="alert alert-danger">Laptop not found</div>';
    exit();
}

// Get images
$stmt = $pdo->prepare("SELECT image_url FROM laptop_images WHERE laptop_id = ? ORDER BY sort_order");
$stmt->execute([$laptop_id]);
$images = $stmt->fetchAll();

$primary_image = !empty($images) ? $images[0]['image_url'] : 'assets/images/laptops/placeholder.jpg';
?>

<div class="row">
    <div class="col-md-5">
        <div class="text-center">
            <img src="<?php echo $primary_image; ?>" alt="<?php echo $laptop['brand'] . ' ' . $laptop['model']; ?>" class="img-fluid rounded" style="max-height: 300px; object-fit: contain;">
        </div>
        
        <?php if(count($images) > 1): ?>
        <div class="row mt-3">
            <div class="col-12">
                <div class="d-flex gap-2 justify-content-center">
                    <?php foreach(array_slice($images, 0, 4) as $img): ?>
                        <img src="<?php echo $img['image_url']; ?>" alt="Thumb" style="width: 60px; height: 60px; object-fit: cover; cursor: pointer; border-radius: 5px;" onclick="document.querySelector('.modal-body img').src = this.src">
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
        <?php endif; ?>
    </div>
    
    <div class="col-md-7">
        <h3><?php echo htmlspecialchars($laptop['brand'] . ' ' . $laptop['model']); ?></h3>
        <p class="text-muted">Category: <?php echo htmlspecialchars($laptop['category_name']); ?></p>
        
        <div class="price mb-3">
            <span class="display-6 text-success">$<?php echo number_format($laptop['discount_price'] ?? $laptop['price'], 2); ?></span>
            <?php if($laptop['discount_price']): ?>
                <span class="text-muted text-decoration-line-through ms-2">$<?php echo number_format($laptop['price'], 2); ?></span>
            <?php endif; ?>
        </div>
        
        <div class="specs-table">
            <table class="table table-bordered">
                <tr>
                    <td><strong>Processor</strong></td>
                    <td><?php echo htmlspecialchars($laptop['processor']); ?></td>
                </tr>
                <tr>
                    <td><strong>RAM</strong></td>
                    <td><?php echo htmlspecialchars($laptop['ram']); ?></td>
                </tr>
                <tr>
                    <td><strong>Storage</strong></td>
                    <td><?php echo htmlspecialchars($laptop['storage']); ?></td>
                </tr>
                <tr>
                    <td><strong>Display</strong></td>
                    <td><?php echo htmlspecialchars($laptop['display']); ?></td>
                </tr>
                <tr>
                    <td><strong>Graphics</strong></td>
                    <td><?php echo htmlspecialchars($laptop['graphics']); ?></td>
                </tr>
                <tr>
                    <td><strong>Battery</strong></td>
                    <td><?php echo htmlspecialchars($laptop['battery']); ?></td>
                </tr>
                <tr>
                    <td><strong>Operating System</strong></td>
                    <td><?php echo htmlspecialchars($laptop['os']); ?></td>
                </tr>
                <tr>
                    <td><strong>Warranty</strong></td>
                    <td><?php echo htmlspecialchars($laptop['warranty']); ?></td>
                </tr>
                <tr>
                    <td><strong>Condition</strong></td>
                    <td><span class="badge bg-info"><?php echo ucfirst($laptop['condition']); ?></span></td>
                </tr>
                <tr>
                    <td><strong>Stock Status</strong></td>
                    <td>
                        <?php if($laptop['stock_quantity'] > 0): ?>
                            <span class="badge bg-success">In Stock (<?php echo $laptop['stock_quantity']; ?> available)</span>
                        <?php else: ?>
                            <span class="badge bg-danger">Out of Stock</span>
                        <?php endif; ?>
                    </td>
                </tr>
            </table>
        </div>
        
        <?php if($laptop['description']): ?>
            <div class="mt-3">
                <strong>Description:</strong>
                <p><?php echo nl2br(htmlspecialchars($laptop['description'])); ?></p>
            </div>
        <?php endif; ?>
    </div>
</div>