<?php
require_once 'includes/db.php';

if (!isset($settings['menu_products_visible']) || $settings['menu_products_visible'] == 0) {
    header("Location: index.php");
    exit;
}

$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$product = null;

if ($id > 0) {
    try {
        $stmt = $pdo->prepare("SELECT * FROM products WHERE id = ?");
        $stmt->execute([$id]);
        $product = $stmt->fetch();
    } catch (Exception $e) {
        $product = null;
    }
}

if (!$product) {
    header("Location: products.php");
    exit;
}

$page_title = $product['name'];
$page_desc = substr(strip_tags($product['description']), 0, 160);
require_once 'includes/header.php';
?>

<!-- Page Header Banner -->
<section style="background: linear-gradient(rgba(15, 23, 42, 0.8), rgba(15, 23, 42, 0.8)), url('assets/images/hero_bg.jpg') no-repeat center center/cover; padding: 120px 0 60px; text-align: center; color: var(--text-light);">
    <div class="container">
        <h1 style="font-size: 36px; font-weight: 800; margin-bottom: 10px;"><?php echo htmlspecialchars($product['name']); ?></h1>
        <p style="color: var(--primary); font-weight: 600; letter-spacing: 2px; text-transform: uppercase; font-size: 14px;">Detail Produk</p>
    </div>
</section>

<!-- Product Detail View -->
<section class="bg-alt">
    <div class="container">
        <div style="margin-bottom: 30px;">
            <a href="products.php" style="color: var(--primary); font-weight: 600; font-size: 15px;"><i class="fas fa-arrow-left" style="margin-right: 8px;"></i> Kembali ke Produk</a>
        </div>
        
        <div class="detail-grid">
            <div class="detail-img-container">
                <?php 
                $img_src = '';
                if (!empty($product['image']) && file_exists('uploads/' . $product['image'])) {
                    $img_src = 'uploads/' . $product['image'];
                } elseif (!empty($product['image']) && file_exists('assets/images/' . $product['image'])) {
                    $img_src = 'assets/images/' . $product['image'];
                } else {
                    $img_src = get_product_fallback_image($product['category'], $product['name']);
                }
                ?>
                <img src="<?php echo $img_src; ?>" alt="<?php echo htmlspecialchars($product['name']); ?>">
            </div>
            
            <div class="detail-content">
                <span class="detail-category"><?php echo htmlspecialchars($product['category']); ?></span>
                <h2 class="detail-title"><?php echo htmlspecialchars($product['name']); ?></h2>
                
                <div class="detail-desc">
                    <p><?php echo nl2br(htmlspecialchars($product['description'])); ?></p>
                </div>
                
                <h3 style="font-size: 18px; font-weight: 700; color: var(--accent); margin-bottom: 15px;">Spesifikasi Produk</h3>
                <table class="detail-specs-table">
                    <tr>
                        <td class="spec-name">Kategori Batu</td>
                        <td class="spec-val"><?php echo htmlspecialchars($product['category']); ?></td>
                    </tr>
                    <tr>
                        <td class="spec-name">Finishing Tersedia</td>
                        <td class="spec-val"><?php echo htmlspecialchars($product['finishing']); ?></td>
                    </tr>
                    <tr>
                        <td class="spec-name">Ukuran Standar</td>
                        <td class="spec-val"><?php echo htmlspecialchars($product['size_available']); ?></td>
                    </tr>
                </table>
                
                <!-- CTA WhatsApp Inquiry -->
                <?php 
                $wa_message = "Halo PT Namora Nexa Niaga, saya ingin berkonsultasi / menanyakan penawaran harga untuk produk: *" . $product['name'] . "* (Kategori: " . $product['category'] . "). Terima kasih.";
                $wa_url = "https://wa.me/" . $settings['whatsapp_number'] . "?text=" . urlencode($wa_message);
                ?>
                <a href="<?php echo $wa_url; ?>" class="btn-whatsapp" target="_blank" rel="noopener noreferrer">
                    <i class="fab fa-whatsapp"></i> Hubungi via WhatsApp untuk Penawaran Harga
                </a>
            </div>
        </div>
    </div>
</section>

<?php require_once 'includes/footer.php'; ?>
