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

// Fetch DB content
$db_lang   = db_lang();
$db_cases  = db_get_all('cases', "c_lang=? AND c_show=1", [$db_lang], 'c_id DESC', '6');
$db_blogs  = db_get_all('blog', "b_lang=? AND b_show=1", [$db_lang], 'b_id DESC', '6');
$db_products = db_get_all('product', "p_lang=? AND p_show=1", [$db_lang], 'p_id DESC', '6');

// Load page content from DB (page_content table)
$home_data = page_data('home');

// --- Hero ---
$hot_prod = $home_data['hot_products'] ?? [];
$hero = $home_data['hero'] ?? [];
$hero_subtitle = $hero['subtitle'] ?? '';
$hero_title    = $hero['title'] ?? '';
$hero_desc     = $hero['content'] ?? '';
$hero_images   = $hero['images'] ?? ['assets/img/hero-equipment.svg','assets/img/hero-station.svg','assets/img/hero-structure.svg'];
$hero_btn1_txt = $hero['button_text'] ?: $hero['btn1_text'] ?: '';
$hero_btn1_url = $hero['button_url'] ?: $hero['btn1_url'] ?: 'products.php';
$hero_btn2_txt = $hero['button2_text'] ?? $hero['btn2_text'] ?? '';
$hero_btn2_url = $hero['button2_url'] ?? $hero['btn2_url'] ?? 'contact.php';

// --- About ---
$about = $home_data['about_us'] ?? [];
$about_subtitle = $about['subtitle'] ?? '';
$about_title    = $about['title'] ?? '';
$about_content  = $about['content'] ?? '' . "\n\n" . __('home_about_p2');
$about_image    = $about['image'] ?? '';
$about_tags     = $about['tags'] ?? [__('home_about_tag_1'),__('home_about_tag_2'),__('home_about_tag_3'),__('home_about_tag_4'),__('home_about_tag_5')];

// --- Company Strength ---
$strength = $home_data['company_strength'] ?? [];
$strength_subtitle = $strength['subtitle'] ?? '';
$strength_title    = $strength['title'] ?? '';
$strength_items    = $strength['items'] ?? [];

// --- Service Advantages ---
$service_adv = $home_data['service_advantages'] ?? [];
$sa_subtitle = $service_adv['subtitle'] ?? '';
$sa_title    = $service_adv['title'] ?? '';
$sa_items    = $service_adv['items'] ?? [];

// --- Qualifications ---
$quals = $home_data['qualifications'] ?? [];
$quals_subtitle = $quals['subtitle'] ?? '';
$quals_title    = $quals['title'] ?? '';
$quals_items    = $quals['items'] ?? [];

// --- OEM/ODM ---
$oem = $home_data['oem_odm'] ?? [];
$oem_subtitle = $oem['subtitle'] ?? '';
$oem_title    = $oem['title'] ?? '';
$oem_items    = $oem['items'] ?? [];
$oem_btn_txt  = $oem['button_text'] ?: '';
$oem_btn_url  = $oem['button_url'] ?: 'oem-odm.php';

// --- FAQ ---
$faq = $home_data['faq'] ?? [];
$faq_subtitle = $faq['subtitle'] ?? '';
$faq_title    = $faq['title'] ?? '';
$faq_items    = $faq['items'] ?? [];

// --- Cases & Blog (static titles from page_data, content auto) ---
$cases = $home_data['cases'] ?? [];
$cases_title = $cases['content'] ?? '';
$news_blog = $home_data['news_blog'] ?? [];
$nb_subtitle = $news_blog['subtitle'] ?? '';
$nb_title    = $news_blog['title'] ?? '';

$page_title       = site_seo('seotitle');
$page_description = site_seo('seodescription');
$page_keywords    = htmlspecialchars(site_seo('seokeywords'));
$page_canonical   = SITE_URL . '/';
$page_og_title    = __('site_name_short');
$page_schema      = '{"@context":"https://schema.org","@type":"Organization","name":"' . site_name() . '","url":"' . SITE_URL . '/","address":{"@type":"PostalAddress","addressLocality":"Chenzhou","addressRegion":"Hunan","addressCountry":"CN"},"email":"' . site_email() . '","description":"Petroleum equipment manufacturer and energy engineering service provider."}';
$page_extra_css   = ['assets/vendor/swiper/swiper-bundle.min.css'];
?>
<?php include 'includes/head.php'; ?>
</head>
<body>
<?php include 'includes/topbar.php'; ?>
<?php include 'includes/header.php'; ?>

<main>
  <section class="hero" aria-label="<?= htmlspecialchars($hero_title) ?>">
    <div class="swiper hero-swiper">
      <div class="swiper-wrapper">
<?php foreach ($hero_images as $img): ?>
        <div class="swiper-slide hero-slide"><img src="<?= htmlspecialchars($img) ?>" alt=""></div>
<?php endforeach; ?>
      </div>
    </div>
    <div class="container hero-content">
      <span class="eyebrow"><?= htmlspecialchars($hero_subtitle) ?></span>
      <h1><?= htmlspecialchars($hero_title) ?></h1>
      <p class="lead"><?= htmlspecialchars($hero_desc) ?></p>
      <div class="btn-row"><a class="btn primary" href="<?= htmlspecialchars($hero_btn1_url) ?>"><?= htmlspecialchars($hero_btn1_txt) ?></a><a class="btn outline" href="<?= htmlspecialchars($hero_btn2_url) ?>"><?= htmlspecialchars($hero_btn2_txt) ?></a></div>
    </div>
    <div class="swiper-pagination hero-pagination" aria-label="Hero slides"></div>
  </section>

  <!-- Products Section -->
  <section class="section" id="products">
    <div class="container">
      <span class="eyebrow"><?= !empty($hot_prod['subtitle']) ? htmlspecialchars($hot_prod['subtitle']) : __('home_products_eyebrow') ?></span>
      <h2><?= !empty($hot_prod['title']) ? htmlspecialchars($hot_prod['title']) : __('home_products_title') ?></h2>
      <div class="grid cols-3">
<?php if (!empty($db_products)): ?>
  <?php foreach ($db_products as $p): ?>
    <?php $thumb = db_thumbs($p['p_thumb']); $img = $thumb[0] ?? 'assets/img/product-skid.svg'; ?>
        <article class="card product-card"><img src="<?= htmlspecialchars($img) ?>" alt="<?= htmlspecialchars($p['p_title']) ?>"><div class="card-body"><h3><?= htmlspecialchars($p['p_title']) ?></h3><p><?= htmlspecialchars(truncate($p['p_desc'], 80)) ?></p><div class="btn-row"><a class="btn ghost" href="product-detail/<?= rawurlencode($p['p_url']) ?>.html"><?= __('product_card_learn_btn') ?></a><a class="btn primary" href="contact.php"><?= __('product_card_inquiry_btn') ?></a></div></div></article>
  <?php endforeach; ?>
  <?php endif; ?>
      </div>
    </div>
  </section>

  <!-- About Section -->
  <section class="section soft">
    <div class="container grid cols-2" style="align-items:center">
      <div class="media-frame">
<?php if (!empty($about_image)): ?>
        <img src="<?= htmlspecialchars($about_image) ?>" alt="<?= htmlspecialchars($about_title) ?>">
<?php endif; ?>
      </div>
      <div>
        <span class="eyebrow"><?= htmlspecialchars($about_subtitle) ?></span>
        <h2><?= htmlspecialchars($about_title) ?></h2>
        <p><?= nl2br(htmlspecialchars($about_content)) ?></p>
        <div class="tag-list"><?php foreach ($about_tags as $t): ?><span class="tag"><?= htmlspecialchars($t) ?></span><?php endforeach; ?></div>
      </div>
    </div>
  </section>

  <section class="section dark">
    <div class="container">
      <span class="eyebrow"><?= htmlspecialchars($strength_subtitle) ?></span>
      <h2><?= htmlspecialchars($strength_title) ?></h2>
      <div class="stat-grid">
<?php if (!empty($strength_items)): ?>
  <?php foreach ($strength_items as $item): ?>
    <?php if (!empty($item['title'])): ?>
        <div class="stat"><strong><?= htmlspecialchars($item['title']) ?></strong><span><?= htmlspecialchars($item['content'] ?? '') ?></span></div>
    <?php endif; ?>
  <?php endforeach; ?>
<?php endif; ?>
      </div>
    </div>
  </section>

  <section class="section">
    <div class="container">
      <span class="eyebrow"><?= htmlspecialchars($sa_subtitle) ?></span>
      <h2><?= htmlspecialchars($sa_title) ?></h2>
<?php if (!empty($sa_items)): ?>
      <div class="grid cols-3">
  <?php $i = 0; foreach ($sa_items as $item): $i++; ?>
        <div class="card icon-card"><div class="icon"><?= str_pad($i, 2, '0', STR_PAD_LEFT) ?></div><h3><?= htmlspecialchars($item['title'] ?? '') ?></h3><p><?= htmlspecialchars($item['content'] ?? '') ?></p></div>
  <?php endforeach; ?>
      </div>
<?php endif; ?>
    </div>
  </section>

  <section class="section soft">
    <div class="container">
      <div class="section-heading">
        <div>
          <span class="eyebrow"><?= htmlspecialchars($quals_subtitle) ?></span>
          <h2><?= htmlspecialchars($quals_title) ?></h2>
        </div>
<?php if (!empty($quals_items)): ?>
        <div class="carousel-controls" aria-label="Certificate carousel controls">
          <button class="carousel-btn" type="button" data-carousel-prev aria-label="Previous certificates">‹</button>
          <button class="carousel-btn" type="button" data-carousel-next aria-label="Next certificates">›</button>
        </div>
<?php endif; ?>
      </div>
<?php if (!empty($quals_items)): ?>
      <div class="swiper certificate-swiper">
        <div class="swiper-wrapper certificate-track">
  <?php foreach ($quals_items as $item): ?>
          <div class="swiper-slide certificate">
    <?php if (!empty($item['image'])): ?>
            <img src="<?= htmlspecialchars($item['image']) ?>" alt="<?= htmlspecialchars($item['title'] ?? '') ?>" style="width:100%;aspect-ratio:4/5;object-fit:contain;margin-bottom:18px;border:8px solid #f4a449;border-radius:4px;">
    <?php else: ?>
            <div class="certificate-visual" aria-hidden="true"><span></span></div>
    <?php endif; ?>
            <h3><?= htmlspecialchars($item['title'] ?? '') ?></h3>
            <p><?= htmlspecialchars($item['content'] ?? '') ?></p>
          </div>
  <?php endforeach; ?>
        </div>
        <div class="swiper-pagination certificate-pagination"></div>
      </div>
<?php endif; ?>
    </div>
  </section>

  <section class="section">
    <div class="container">
      <span class="eyebrow"><?= htmlspecialchars($oem_subtitle) ?></span>
      <h2><?= htmlspecialchars($oem_title) ?></h2>
<?php if (!empty($oem_items)): ?>
      <div class="steps">
  <?php foreach ($oem_items as $item): ?>
        <div class="step"><h3><?= htmlspecialchars($item['title'] ?? '') ?></h3><p><?= htmlspecialchars($item['content'] ?? '') ?></p></div>
  <?php endforeach; ?>
      </div>
<?php endif; ?>
      <div class="btn-row" style="margin-top:28px"><a class="btn primary" href="<?= htmlspecialchars($oem_btn_url) ?>"><?= htmlspecialchars($oem_btn_txt) ?></a></div>
    </div>
  </section>

  <!-- Case Section -->
  <section class="section soft">
    <div class="container">
      <span class="eyebrow"><?= __('home_case_eyebrow') ?></span>
      <h2><?= htmlspecialchars($cases_title) ?></h2>
      <div class="grid cols-3">
<?php if (!empty($db_cases)): ?>
  <?php foreach ($db_cases as $c): ?>
    <?php $cimg = db_thumbs($c['c_thumb']); $cimg = $cimg[0] ?? 'assets/img/case-project.svg'; ?>
        <article class="card case-card"><img src="<?= htmlspecialchars($cimg) ?>" alt="<?= htmlspecialchars($c['c_title']) ?>"><div class="card-body"><h3><?= htmlspecialchars($c['c_title']) ?></h3><p><?= htmlspecialchars(truncate($c['c_desc'], 100)) ?></p><a class="btn ghost" href="case-detail/<?= rawurlencode($c['c_url']) ?>.html"><?= __('case_card_view_btn') ?></a></div></article>
  <?php endforeach; ?>
<?php endif; ?>
      </div>
    </div>
  </section>

  <section class="section">
    <div class="container form-wide-layout">
      <div>
        <span class="eyebrow"><?= htmlspecialchars($faq_subtitle) ?></span>
        <h2><?= htmlspecialchars($faq_title) ?></h2>
<?php if (!empty($faq_items)): ?>
  <?php $first = true; foreach ($faq_items as $item): ?>
        <div class="faq-item<?= $first ? ' active' : '' ?>"><button class="faq-question" type="button" aria-expanded="<?= $first ? 'true' : 'false' ?>"><?= htmlspecialchars($item['question'] ?? '') ?><span>+</span></button><div class="faq-answer"><p><?= htmlspecialchars($item['answer'] ?? '') ?></p></div></div>
    <?php $first = false; endforeach; ?>
<?php endif; ?>
      </div>
      <form class="form inquiry-form" action="submit-inquiry.php" method="post">
        <input type="hidden" name="c_form" value="home">
        <input type="text" name="website" style="display:none" autocomplete="off" tabindex="-1">
        <div class="field-row"><label><?= __('form_name') ?><input name="name" autocomplete="name" required></label><label><?= __('form_phone') ?><input type="tel" name="phone" autocomplete="tel"></label></div>
        <div class="field-row"><label><?= __('form_email') ?><input type="email" name="email" autocomplete="email" required></label><label><?= __('form_whatsapp') ?><input name="whatsapp" autocomplete="tel"></label></div>
        <div class="field-row"><label><?= __('form_company') ?><input name="company" autocomplete="organization"></label><label><?= __('form_country') ?><input name="country" autocomplete="country-name"></label></div>
        <label><?= __('form_interest') ?><select name="interest">
<?php $_interest_cats = db_get_all('product_cls', 'pc_lang=? AND pc_show=1', [db_lang()], 'pc_sort ASC'); foreach ($_interest_cats as $_ic): ?>
          <option value="<?= htmlspecialchars($_ic['pc_title']) ?>"><?= htmlspecialchars($_ic['pc_title']) ?></option>
<?php endforeach; ?>
        </select></label>
        <label><?= __('form_message') ?><textarea name="message" required></textarea></label>
        <button class="btn primary" type="submit"><?= __('form_submit') ?></button>
      </form>
    </div>
  </section>

  <!-- Blog Section -->
  <section class="section soft">
    <div class="container">
      <span class="eyebrow"><?= htmlspecialchars($nb_subtitle) ?></span>
      <h2><?= htmlspecialchars($nb_title) ?></h2>
      <div class="grid cols-3">
<?php if (!empty($db_blogs)): ?>
  <?php foreach ($db_blogs as $b): ?>
    <?php $bimg = db_thumbs($b['b_thumb']); $bimg = $bimg[0] ?? 'assets/img/product-skid.svg'; ?>

        <article class="card case-card"><img src="<?= htmlspecialchars($bimg) ?>" alt="<?= htmlspecialchars($b['b_title']) ?>"><div class="card-body"><h3><?= htmlspecialchars($b['b_title']) ?></h3><p><?= htmlspecialchars(truncate($b['b_desc'], 100)) ?></p><a class="btn ghost" href="news-detail/<?= rawurlencode($b['b_url']) ?>.html" ><?= __('btn_read_more') ?></a></div></article>
  <?php endforeach; ?>
<?php endif; ?>
      </div>
    </div>
  </section>
</main>

<?php include 'includes/footer.php'; ?>
<?php
$page_extra_js_before = ['assets/vendor/swiper/swiper-bundle.min.js'];
include 'includes/scripts.php';
?>
</body>
</html>
