<?php
include 'includes/header.php';
$B  = BASE_PATH;
$db = site_db();

$id   = (int)($_GET['id']   ?? 0);
$slug = trim($_GET['slug']  ?? '');
$post = null;

if ($db) {
    if ($id)   $post = $db->query("SELECT * FROM blog_posts WHERE id=$id AND status='published'")->fetch_assoc();
    if (!$post && $slug) $post = $db->query("SELECT * FROM blog_posts WHERE slug='".htmlspecialchars($slug,ENT_QUOTES)."' AND status='published'")->fetch_assoc();
    if ($post) $db->query("UPDATE blog_posts SET views=views+1 WHERE id=".$post['id']);
}

if (!$post) {
    header('Location: '.$B.'blog.php');
    exit;
}

$page_title = htmlspecialchars($post['title']).' — CY Consultancy';
$related = [];
if ($db && $post['category']) {
    $r = $db->query("SELECT id,title,slug,featured_image,created_at FROM blog_posts WHERE status='published' AND category='".htmlspecialchars($post['category'],ENT_QUOTES)."' AND id!=".$post['id']." LIMIT 3");
    if ($r) $related = $r->fetch_all(MYSQLI_ASSOC);
}
?>
<div class="page-hero" style="<?=$post['featured_image']?'background-image:url(\''.htmlspecialchars(BASE_URL.'/'.$post['featured_image']).'\');background-size:cover;background-position:center':'background:linear-gradient(135deg,#0a0f1e,#1e3a8a)'?>">
  <h1 style="font-size:clamp(20px,3vw,36px)"><?=htmlspecialchars($post['title'])?></h1>
  <div class="breadcrumb"><a href="<?=$B?>index.php">Home</a><i class="fas fa-chevron-right"></i><a href="<?=$B?>blog.php">Blog</a><i class="fas fa-chevron-right"></i><span><?=htmlspecialchars(substr($post['title'],0,30))?>...</span></div>
</div>

<section class="sec" style="background:var(--bgf)">
  <div class="blog-layout">
    <div class="blog-main">
      <article style="background:#fff;border-radius:16px;overflow:hidden;box-shadow:0 2px 12px rgba(0,0,0,.06)">
        <?php if ($post['featured_image']): ?>
        <img src="<?=htmlspecialchars(BASE_URL.'/'.$post['featured_image'])?>" alt="<?=htmlspecialchars($post['title'])?>" style="width:100%;max-height:400px;object-fit:cover">
        <?php endif; ?>
        <div style="padding:32px">
          <div style="display:flex;gap:14px;align-items:center;flex-wrap:wrap;margin-bottom:20px;font-size:13px;color:#6b7280">
            <?php if($post['category']): ?><span style="background:#dbeafe;color:#1e40af;padding:4px 12px;border-radius:20px;font-weight:600;font-size:11px"><?=htmlspecialchars($post['category'])?></span><?php endif; ?>
            <span><i class="far fa-calendar" style="margin-right:4px"></i><?=date('F j, Y',strtotime($post['created_at']))?></span>
            <span><i class="fas fa-user" style="margin-right:4px"></i><?=htmlspecialchars($post['author']??'CY Consultancy')?></span>
            <?php if($post['views']): ?><span><i class="fas fa-eye" style="margin-right:4px"></i><?=$post['views']?> views</span><?php endif; ?>
          </div>
          <div style="font-size:15px;line-height:1.9;color:#374151;font-family:'DM Sans',sans-serif">
            <?= $post['content'] ?>
          </div>
          <div style="margin-top:32px;padding-top:24px;border-top:1px solid #e5e7eb;display:flex;gap:12px;align-items:center;flex-wrap:wrap">
            <span style="font-size:13px;color:#9ca3af;font-weight:600">Share:</span>
            <a href="https://www.facebook.com/sharer/sharer.php?u=<?=urlencode(BASE_URL.'/blog-post.php?id='.$post['id'])?>" target="_blank" style="width:36px;height:36px;border-radius:50%;background:#1877f2;color:#fff;display:flex;align-items:center;justify-content:center;font-size:14px"><i class="fab fa-facebook-f"></i></a>
            <a href="https://wa.me/?text=<?=urlencode($post['title'].' '.BASE_URL.'/blog-post.php?id='.$post['id'])?>" target="_blank" style="width:36px;height:36px;border-radius:50%;background:#25d366;color:#fff;display:flex;align-items:center;justify-content:center;font-size:14px"><i class="fab fa-whatsapp"></i></a>
            <a href="<?=$B?>blog.php" style="margin-left:auto;display:inline-flex;align-items:center;gap:6px;color:#1a56db;font-size:13px;font-weight:600;text-decoration:none"><i class="fas fa-arrow-left"></i> Back to Blog</a>
          </div>
        </div>
      </article>

      <?php if (!empty($related)): ?>
      <div style="margin-top:32px">
        <h3 style="font-size:20px;font-weight:800;margin-bottom:20px;font-family:'Montserrat',sans-serif">Related Posts</h3>
        <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(240px,1fr));gap:16px">
          <?php foreach($related as $rp): ?>
          <a href="blog-post.php?slug=<?=htmlspecialchars($rp['slug'])?>" style="text-decoration:none;background:#fff;border-radius:12px;overflow:hidden;border:1px solid #e5e7eb;transition:transform .2s;display:block" onmouseover="this.style.transform='translateY(-4px)'" onmouseout="this.style.transform='none'">
            <?php if($rp['featured_image']): ?>
            <img src="<?=htmlspecialchars(BASE_URL.'/'.$rp['featured_image'])?>" alt="" style="width:100%;height:150px;object-fit:cover">
            <?php else: ?>
            <div style="height:150px;background:linear-gradient(135deg,#dbeafe,#eff6ff);display:flex;align-items:center;justify-content:center;font-size:36px">📰</div>
            <?php endif; ?>
            <div style="padding:14px">
              <div style="font-weight:700;color:#111827;font-size:14px;line-height:1.4"><?=htmlspecialchars(substr($rp['title'],0,60).(strlen($rp['title'])>60?'...':''))?></div>
              <div style="font-size:12px;color:#9ca3af;margin-top:6px"><?=date('M j, Y',strtotime($rp['created_at']))?></div>
            </div>
          </a>
          <?php endforeach; ?>
        </div>
      </div>
      <?php endif; ?>
    </div>

    <div class="blog-sidebar">
      <div class="sb-box">
        <h4>About This Post</h4>
        <?php if($post['category']): ?><p style="font-size:13px;color:#6b7280;margin-bottom:8px"><strong>Category:</strong> <?=htmlspecialchars($post['category'])?></p><?php endif; ?>
        <p style="font-size:13px;color:#6b7280;margin-bottom:8px"><strong>Published:</strong> <?=date('F j, Y',strtotime($post['created_at']))?></p>
        <p style="font-size:13px;color:#6b7280"><strong>By:</strong> <?=htmlspecialchars($post['author']??'CY Consultancy')?></p>
      </div>
      <div class="sb-box" style="background:linear-gradient(135deg,#e76f51,#f4a261);border:none">
        <h4 style="color:#fff;border-color:rgba(255,255,255,.2)">Free Consultation</h4>
        <p style="font-size:12.5px;color:rgba(255,255,255,.9);margin-bottom:14px;line-height:1.7">Ready to start your study abroad journey? Our experts are here to help!</p>
        <a href="<?=$B?>contact.php" class="btn" style="background:#fff;color:#e76f51;width:100%;justify-content:center;font-weight:700"><i class="fas fa-arrow-right"></i> Get Free Advice</a>
      </div>
    </div>
  </div>
</section>
<?php include 'includes/footer.php'; ?>
