<?php
require_once __DIR__ . '/config/config.php';
$pdo = getDB();
$assetBase = (defined('BASE_URL') && BASE_URL && BASE_URL !== '/') ? rtrim(BASE_URL, '/') . '/' : '';
$techDir = BASE_PATH . '/uploads/tech_quality/';

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

try {
    $sql = "SELECT id, name, short_description, slug, detail_heading, detail_description, detail_content, image FROM quality_policy_items WHERE ";
    if ($slug) {
        $stmt = $pdo->prepare($sql . "slug = ? AND is_active = 1 LIMIT 1");
        $stmt->execute([$slug]);
        $item = $stmt->fetch(PDO::FETCH_ASSOC);
    }
    if (!$item && $id) {
        $stmt = $pdo->prepare($sql . "id = ? AND is_active = 1 LIMIT 1");
        $stmt->execute([$id]);
        $item = $stmt->fetch(PDO::FETCH_ASSOC);
    }
} catch (Exception $e) {
    $item = null;
    if ($slug || $id) {
        try {
            $sqlBasic = "SELECT id, name, short_description, slug, image FROM quality_policy_items WHERE ";
            if ($slug) {
                $stmt = $pdo->prepare($sqlBasic . "slug = ? AND is_active = 1 LIMIT 1");
                $stmt->execute([$slug]);
                $item = $stmt->fetch(PDO::FETCH_ASSOC);
            }
            if (!$item && $id) {
                $stmt = $pdo->prepare($sqlBasic . "id = ? AND is_active = 1 LIMIT 1");
                $stmt->execute([$id]);
                $item = $stmt->fetch(PDO::FETCH_ASSOC);
            }
        } catch (Exception $e2) { /* */ }
    }
}

if (!$item) {
    header('Location: ' . $assetBase . 'quality-policy.php');
    exit;
}

// Normalize keys (some DB drivers return different case)
$item = array_change_key_case($item, CASE_LOWER);

$heading = trim((string)($item['detail_heading'] ?? ''));
if ($heading === '') $heading = trim((string)($item['name'] ?? ''));
if ($heading === '') $heading = 'Quality Policy';

$pageTitle = $heading;
$imageSrc = null;
$img = trim((string)($item['image'] ?? ''));
if ($img !== '' && is_file($techDir . $img)) {
    $imageSrc = $assetBase . 'uploads/tech_quality/' . rawurlencode($img);
}

$lead = trim((string)($item['detail_description'] ?? ''));
$content = trim((string)($item['detail_content'] ?? ''));
$shortDesc = trim((string)($item['short_description'] ?? ''));
$bodyContent = $content !== '' ? $content : ($lead !== '' ? $lead : $shortDesc);

require_once __DIR__ . '/includes/header.php';
?>
<div class="page-content tech-quality-detail-page">
    <div class="container">
        <p class="tech-quality-back"><a href="<?php echo $assetBase; ?>quality-policy.php">&larr; Back to Quality policy</a></p>
        <article class="tech-quality-detail">
            <h1 class="tech-quality-detail-heading"><?php echo htmlspecialchars($heading); ?></h1>
            <?php if ($lead !== '' && $content !== ''): ?>
            <p class="tech-quality-detail-lead"><?php echo nl2br(htmlspecialchars($lead)); ?></p>
            <?php endif; ?>
            <?php if ($imageSrc): ?>
            <div class="tech-quality-detail-image-wrap">
                <img src="<?php echo htmlspecialchars($imageSrc); ?>" alt="" class="tech-quality-detail-img" loading="lazy">
            </div>
            <?php endif; ?>
            <div class="tech-quality-detail-content">
                <?php
                if ($bodyContent !== '') {
                    echo '<p>' . nl2br(htmlspecialchars($bodyContent)) . '</p>';
                } else {
                    echo '<p class="tech-quality-detail-muted">Content for this section will be updated soon. Please check back later.</p>';
                }
                ?>
            </div>
        </article>
    </div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
