<?php 
require_once 'db/config.php';
$slug = $_GET['slug'] ?? 'safety-socks';

$stmt = db()->prepare("SELECT * FROM products WHERE slug = ?");
$stmt->execute([$slug]);
$product = $stmt->fetch();

if (!$product) {
    header("Location: /");
    exit;
}

$pageTitle = $product['name'];
include 'header.php'; 
?>

<div class="container py-5">
    <!-- Breadcrumb -->
    <nav aria-label="breadcrumb" class="mb-5">
      <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="/" class="text-decoration-none text-muted">Home</a></li>
        <li class="breadcrumb-item active"><?php echo htmlspecialchars($product['name']); ?></li>
      </ol>
    </nav>

    <!-- Hero Story Section -->
    <div class="row align-items-center mb-5">
        <div class="col-md-6 order-2 order-md-1">
            <h1 class="display-4 fw-bold mb-4"><?php echo htmlspecialchars($product['name']); ?></h1>
            <p class="lead mb-4"><?php echo htmlspecialchars($product['short_description']); ?></p>
            <p class="h2 text-gold mb-4">$<?php echo number_format($product['price'], 2); ?> 
                <?php if ($product['original_price']): ?>
                    <small class="text-muted fs-5 text-decoration-line-through">$<?php echo number_format($product['original_price'], 2); ?></small>
                <?php endif; ?>
            </p>
            
            <div class="d-flex gap-3 mt-4">
                <button class="btn btn-gold btn-lg px-4" onclick="addToCart(<?php echo $product['id']; ?>, '<?php echo addslashes($product['name']); ?>', <?php echo $product['price']; ?>, '<?php echo $product['image_url']; ?>')">Add to Cart</button>
                <button class="btn btn-outline-gold btn-lg px-4" data-bs-toggle="modal" data-bs-target="#inquiryModal">Learn More</button>
            </div>
        </div>
        <div class="col-md-6 mb-4 mb-md-0 order-1 order-md-2 text-center">
             <img src="<?php echo htmlspecialchars($product['image_url']); ?>" alt="<?php echo htmlspecialchars($product['name']); ?>" class="img-fluid rounded shadow-lg border border-gold p-1" style="max-height: 500px; object-fit: cover;">
        </div>
    </div>

    <!-- Details Section -->
    <div class="row justify-content-center py-5">
        <div class="col-md-8">
            <h3 class="fw-bold mb-4">The Story Behind the Design</h3>
            <div class="fs-5 text-muted mb-5" style="line-height: 1.8;">
                <?php echo nl2br(htmlspecialchars($product['description'])); ?>
            </div>

            <!-- Benefits Box (Charcoal Style like About Page) -->
            <div class="p-5 bg-charcoal text-white rounded my-5 shadow">
                <h4 class="text-gold mb-4">Why It Matters</h4>
                <ul class="list-unstyled">
                    <?php 
                    $benefits = json_decode($product['benefits'], true);
                    if ($benefits):
                        foreach ($benefits as $benefit): ?>
                            <li class="mb-3 d-flex align-items-start">
                                <i class="bi bi-check-circle-fill text-gold me-3 mt-1"></i>
                                <span class="fs-5"><?php echo htmlspecialchars($benefit); ?></span>
                            </li>
                        <?php endforeach; 
                    endif; ?>
                </ul>
            </div>

            <div class="text-center mt-5">
                <div class="alert alert-light border small text-muted d-inline-block px-4">
                    <i class="bi bi-shield-check me-2"></i> <strong>ElderEase Guarantee:</strong> 30-day hassle-free returns on all safety items.
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Inquiry Modal -->
<div class="modal fade" id="inquiryModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow-lg">
            <div class="modal-header bg-charcoal text-white border-0">
                <h5 class="modal-title">Product Inquiry</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-close="modal"></button>
            </div>
            <div class="modal-body p-4">
                <p class="text-muted mb-4">Have questions about how this product can help? Our team is here to support you.</p>
                <form id="inquiryForm" action="/api/inquiry.php" method="POST">
                    <input type="hidden" name="product_id" value="<?php echo htmlspecialchars($product['slug']); ?>">
                    <div class="mb-3">
                        <label class="form-label small fw-bold">Name</label>
                        <input type="text" name="name" class="form-control form-control-lg" placeholder="Your Name" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label small fw-bold">Email Address</label>
                        <input type="email" name="email" class="form-control form-control-lg" placeholder="your@email.com" required>
                    </div>
                    <div class="mb-4">
                        <label class="form-label small fw-bold">Message</label>
                        <textarea name="message" class="form-control" rows="3" placeholder="How can we help?"></textarea>
                    </div>
                    <button type="submit" class="btn btn-gold btn-lg w-100">Send Message</button>
                </form>
            </div>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>