<?php
$page_title = "Pemasok Batu Alam Premium";
$page_desc = "PT Namora Nexa Niaga menyediakan berbagai jenis batu alam berkualitas tinggi seperti marmer, travertine, andesit, batu candi, dan paras jogja.";
require_once 'includes/header.php';

// Fetch featured products
try {
    $stmt = $pdo->prepare("SELECT * FROM products WHERE is_featured = 1 LIMIT 3");
    $stmt->execute();
    $featured_products = $stmt->fetchAll();
} catch (Exception $e) {
    $featured_products = [];
}

// Fetch some gallery items for preview
try {
    $stmt = $pdo->prepare("SELECT * FROM gallery ORDER BY id DESC LIMIT 4");
    $stmt->execute();
    $gallery_preview = $stmt->fetchAll();
} catch (Exception $e) {
    $gallery_preview = [];
}
?>

<!-- Hero Section -->
<?php
$hero_images = [];
for ($i = 1; $i <= 3; $i++) {
    $field = 'hero_img_' . $i;
    $img = isset($settings[$field]) ? $settings[$field] : '';
    if (!empty($img)) {
        if (file_exists('uploads/' . $img)) {
            $hero_images[] = 'uploads/' . $img;
        } elseif (file_exists('assets/images/' . $img)) {
            $hero_images[] = 'assets/images/' . $img;
        } else {
            // fallback Unsplash images if files aren't physically present
            if ($i == 1) $hero_images[] = 'https://images.unsplash.com/photo-1600585154340-be6161a56a0c?auto=format&fit=crop&w=1200&q=80';
            if ($i == 2) $hero_images[] = 'https://images.unsplash.com/photo-1541888946425-d81bb19240f5?auto=format&fit=crop&w=1200&q=80';
            if ($i == 3) $hero_images[] = 'https://images.unsplash.com/photo-1586528116311-ad8dd3c8310d?auto=format&fit=crop&w=1200&q=80';
        }
    }
}
// Fallback if empty array
if (empty($hero_images)) {
    $hero_images[] = 'assets/images/hero_bg.jpg';
}
?>
<section class="hero">
    <div class="hero-slides">
        <?php foreach ($hero_images as $index => $img_path): ?>
            <div class="hero-slide <?php echo $index === 0 ? 'active' : ''; ?>" style="background-image: url('<?php echo $img_path; ?>');"></div>
        <?php endforeach; ?>
    </div>
    
    <div class="hero-content">
        <span class="hero-tag">PT Namora Nexa Niaga</span>
        <h1 class="hero-title">Material Konstruksi Kokoh untuk <span>Infrastruktur Indonesia</span></h1>
        <p class="hero-desc"><?php echo $settings['tagline']; ?> - Menyediakan batu split berkualitas tinggi dan agregat batuan crusher untuk kebutuhan pengecoran jalan, gedung, jembatan, dan pondasi proyek Anda.</p>
        <div class="hero-btns">
            <a href="products.php" class="btn-primary">Lihat Produk Material</a>
            <a href="contact.php" class="btn-secondary">Hubungi Sales</a>
        </div>
    </div>
</section>

<!-- About Section Preview -->
<section class="bg-alt">
    <div class="container">
        <div class="grid-2">
            <div class="about-image">
                <?php 
                $about_img_1 = 'assets/images/' . (isset($settings['about_img_1']) ? $settings['about_img_1'] : 'about_img.jpg');
                if (isset($settings['about_img_1']) && !empty($settings['about_img_1']) && file_exists('uploads/' . $settings['about_img_1'])) {
                    $about_img_1 = 'uploads/' . $settings['about_img_1'];
                }
                ?>
                <img src="<?php echo $about_img_1; ?>" alt="Tentang PT Namora Nexa Niaga" onerror="this.src='https://images.unsplash.com/photo-1600585154340-be6161a56a0c?auto=format&fit=crop&w=800&q=80'">
                <div class="about-experience-badge">
                    <span class="years">10+</span>
                    <span class="text">Tahun Kualitas</span>
                </div>
            </div>
            <div class="about-content">
                <span class="section-tag">TENTANG KAMI</span>
                <h2 class="section-title">Dedikasi Menghadirkan Material & Batu Split Terbaik</h2>
                <h3>PT Namora Nexa Niaga</h3>
                <p><?php echo $settings['about_short']; ?></p>
                <p>Kami bangga dapat berpartisipasi dalam mensuplai berbagai kebutuhan batu agregat untuk infrastruktur nasional. Mulai dari proyek jalan tol, gedung bertingkat, perumahan, hingga jembatan. Batuan kami diproses melalui crusher modern guna menjamin presisi ukuran gradasi batuan.</p>
                <ul class="about-list">
                    <li>Batu Split (1-2 cm & 2-3 cm) Kualitas Cor Tinggi</li>
                    <li>Abu Batu, Batu Screening, & Base Course A</li>
                    <li>Batu Alam Hias & Produk Terrazo Premium</li>
                    <li>Pengiriman Cepat dengan Armada Dump Truck Mandiri</li>
                </ul>
                <a href="about.php" class="btn-primary">Pelajari Selengkapnya</a>
            </div>
        </div>
    </div>
</section>

<!-- Stats Section -->
<section class="stats">
    <div class="container">
        <div class="stats-grid">
            <div class="stat-item">
                <span class="number">100+</span>
                <span class="label">Proyek Terlayani</span>
            </div>
            <div class="stat-item">
                <span class="number">6+</span>
                <span class="label">Varian Agregat</span>
            </div>
            <div class="stat-item">
                <span class="number">200k+</span>
                <span class="label">M³ Terdistribusi</span>
            </div>
            <div class="stat-item">
                <span class="number">99%</span>
                <span class="label">Ketepatan Pengiriman</span>
            </div>
        </div>
    </div>
</section>

<!-- Featured Products Section -->
<?php if (isset($settings['menu_products_visible']) && $settings['menu_products_visible'] == 1): ?>
<section class="bg-alt">
    <div class="container">
        <div class="section-header">
            <span class="section-tag">PRODUK UNGGULAN</span>
            <h2 class="section-title">Batu Alam Pilihan Terbaik</h2>
            <p class="section-desc">Eksplorasi koleksi batu alam premium kami yang dirancang untuk memperindah dan memperkokoh struktur bangunan Anda.</p>
        </div>
        
        <div class="grid-3">
            <?php if (count($featured_products) > 0): ?>
                <?php foreach ($featured_products as $product): ?>
                    <div class="product-card">
                        <div class="product-img-wrapper">
                            <span class="product-category"><?php echo htmlspecialchars($product['category']); ?></span>
                            <?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="product-info">
                            <h3 class="product-title"><?php echo htmlspecialchars($product['name']); ?></h3>
                            <p class="product-description"><?php echo htmlspecialchars($product['description']); ?></p>
                            <div class="product-specs">
                                <span>Finishing: <strong><?php echo htmlspecialchars($product['finishing']); ?></strong></span>
                                <span>Ukuran: <strong><?php echo htmlspecialchars($product['size_available']); ?></strong></span>
                            </div>
                            <a href="product-detail.php?id=<?php echo $product['id']; ?>" class="product-link">Detail Produk</a>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php else: ?>
                <p style="grid-column: 1/-1; text-align: center; color: var(--text-muted);">Belum ada produk unggulan yang tersedia saat ini.</p>
            <?php endif; ?>
        </div>
        
        <div style="text-align: center; margin-top: 50px;">
            <a href="products.php" class="btn-primary">Lihat Semua Material</a>
        </div>
    </div>
</section>
<?php endif; ?>

<!-- Gallery Preview Section -->
<?php if (isset($settings['menu_gallery_visible']) && $settings['menu_gallery_visible'] == 1 && count($gallery_preview) > 0): ?>
<section>
    <div class="container">
        <div class="section-header">
            <span class="section-tag">GALERI PROYEK</span>
            <h2 class="section-title">Dokumentasi Logistik & Pengiriman</h2>
            <p class="section-desc">Dokumentasi pengiriman armada dump truck batu split dan aktivitas pemasok agregat di lapangan proyek.</p>
        </div>
        
        <div class="gallery-grid">
            <?php foreach ($gallery_preview as $item): ?>
                <?php 
                $img_src = 'assets/images/' . $item['image'];
                if (!empty($item['image']) && file_exists('uploads/' . $item['image'])) {
                    $img_src = 'uploads/' . $item['image'];
                }
                ?>
                <div class="gallery-item">
                    <img src="<?php echo $img_src; ?>" alt="<?php echo htmlspecialchars($item['title']); ?>" onerror="this.src='https://images.unsplash.com/photo-1600585154526-990dced4db0d?auto=format&fit=crop&w=600&q=80'">
                    <div class="gallery-overlay">
                        <h4><?php echo htmlspecialchars($item['title']); ?></h4>
                        <p><?php echo htmlspecialchars($item['description']); ?></p>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
        
        <div style="text-align: center; margin-top: 50px;">
            <a href="gallery.php" class="btn-secondary" style="border-color: var(--primary); color: var(--primary);">Lihat Seluruh Galeri</a>
        </div>
    </div>
</section>
<?php endif; ?>

<!-- Latest Articles Section -->
<?php 
try {
    $stmt = $pdo->query("SELECT * FROM articles ORDER BY id DESC LIMIT 3");
    $latest_articles = $stmt->fetchAll();
} catch (Exception $e) {
    $latest_articles = [];
}
if (isset($settings['menu_blog_visible']) && $settings['menu_blog_visible'] == 1 && count($latest_articles) > 0): 
?>
<section class="bg-alt">
    <div class="container">
        <div class="section-header">
            <span class="section-tag">ARTIKEL & BLOG</span>
            <h2 class="section-title">Edukasi Material Terbaru</h2>
            <p class="section-desc">Dapatkan wawasan seputar konstruksi, panduan agregat batuan, dan tips batu alam dari kami.</p>
        </div>
        
        <div class="grid-3">
            <?php foreach ($latest_articles as $art): ?>
                <div style="background: #ffffff; border-radius: 12px; overflow: hidden; box-shadow: var(--shadow-sm); border: 1px solid var(--border-color); display: flex; flex-direction: column;">
                    <div style="height: 180px; overflow: hidden; position: relative;">
                        <?php 
                        $img_src = '';
                        if (!empty($art['image']) && file_exists('uploads/' . $art['image'])) {
                            $img_src = 'uploads/' . $art['image'];
                        } elseif (!empty($art['image']) && file_exists('assets/images/' . $art['image'])) {
                            $img_src = 'assets/images/' . $art['image'];
                        } else {
                            $img_src = get_article_fallback_image($art['slug']);
                        }
                        ?>
                        <img src="<?php echo $img_src; ?>" alt="<?php echo htmlspecialchars($art['title']); ?>" style="width: 100%; height: 100%; object-fit: cover;">
                    </div>
                    <div style="padding: 20px; flex-grow: 1; display: flex; flex-direction: column;">
                        <span style="font-size: 11px; color: var(--text-muted); margin-bottom: 8px; display: block; font-weight: 600;"><?php echo date('d M Y', strtotime($art['created_at'])); ?> | Oleh <?php echo htmlspecialchars($art['author']); ?></span>
                        <h3 style="font-size: 16px; font-weight: 700; line-height: 1.4; margin-bottom: 12px; color: var(--accent); flex-grow: 1;">
                            <a href="blog-detail.php?slug=<?php echo htmlspecialchars($art['slug']); ?>" style="color: inherit; text-decoration: none;">
                                <?php echo htmlspecialchars($art['title']); ?>
                            </a>
                        </h3>
                        <a href="blog-detail.php?slug=<?php echo htmlspecialchars($art['slug']); ?>" style="color: var(--primary); font-weight: 700; font-size: 13px; text-decoration: none; text-transform: uppercase; letter-spacing: 0.5px; display: inline-flex; align-items: center; gap: 4px;">
                            Baca Artikel <i class="fas fa-arrow-right" style="font-size: 10px;"></i>
                        </a>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
        
        <div style="text-align: center; margin-top: 40px;">
            <a href="blog.php" class="btn-secondary" style="border-color: var(--primary); color: var(--primary);">Lihat Semua Artikel</a>
        </div>
    </div>
</section>
<?php endif; ?>

<!-- Call to Action -->
<section style="background-color: var(--secondary); color: var(--text-light); text-align: center; padding: 80px 0; position: relative; overflow: hidden;">
    <div style="position: absolute; top:0; left:0; width:100%; height:100%; background: radial-gradient(circle at 70% 30%, rgba(197, 168, 128, 0.15) 0%, transparent 60%); z-index:1;"></div>
    <div class="container" style="position: relative; z-index:2; max-width: 800px;">
        <h2 style="font-size: 36px; font-weight: 800; margin-bottom: 20px;">Butuh Pasokan Batu Split Berkapasitas Besar?</h2>
        <p style="color: rgba(255, 255, 255, 0.7); font-size: 18px; margin-bottom: 40px;">Kami melayani kontrak suplai jangka panjang untuk batching plant, proyek jalan tol, pembangunan real estate, dengan jaminan volume murni, armada mandiri, dan harga langsung dari tambang.</p>
        <?php 
        $cta_link = 'contact.php';
        if (!isset($settings['menu_contact_visible']) || $settings['menu_contact_visible'] == 0) {
            $cta_link = 'https://wa.me/' . $settings['whatsapp_number'] . '?text=Halo%20PT%20Namora%20Nexa%20Niaga,%20saya%20tertarik%20dengan%20produk%20batu%20split%20dan%20material%20konstruksi.';
        }
        ?>
        <a href="<?php echo $cta_link; ?>" class="btn-primary">Hubungi Layanan Sales & Quotation</a>
    </div>
</section>

<!-- JSON-LD Structured Data Schema -->
<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "LocalBusiness",
  "name": "<?php echo htmlspecialchars($settings['company_name']); ?>",
  "image": "<?php echo (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST']; ?>/assets/images/hero_bg.jpg",
  "@id": "<?php echo (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST']; ?>/#localbusiness",
  "url": "<?php echo (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST']; ?>/index.php",
  "telephone": "<?php echo htmlspecialchars($settings['phone']); ?>",
  "email": "<?php echo htmlspecialchars($settings['email']); ?>",
  "address": {
    "@type": "PostalAddress",
    "streetAddress": "<?php echo htmlspecialchars($settings['address']); ?>",
    "addressLocality": "Jakarta Selatan",
    "addressRegion": "DKI Jakarta",
    "postalCode": "12000",
    "addressCountry": "ID"
  },
  "geo": {
    "@type": "GeoCoordinates",
    "latitude": -6.285817812543781,
    "longitude": 106.77977464016766
  },
  "openingHoursSpecification": {
    "@type": "OpeningHoursSpecification",
    "dayOfWeek": [
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday"
    ],
    "opens": "08:00",
    "closes": "17:00"
  },
  "sameAs": [
    "<?php echo htmlspecialchars($settings['facebook']); ?>",
    "<?php echo htmlspecialchars($settings['instagram']); ?>"
  ]
}
</script>

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