<?php
require_once __DIR__ . '/config/config.php';
$pageTitle = 'Quality Policy';
$pdo = getDB();
$assetBase = (defined('BASE_URL') && BASE_URL && BASE_URL !== '/') ? rtrim(BASE_URL, '/') . '/' : '';
$techDir = BASE_PATH . '/uploads/tech_quality/';
if (!is_dir($techDir)) @mkdir($techDir, 0755, true);

$qualityMain = ['heading' => 'Quality Policy', 'short_description' => ''];
$qualityItems = [];
try {
    $row = $pdo->query("SELECT heading, short_description FROM quality_policy_main LIMIT 1")->fetch(PDO::FETCH_ASSOC);
    if ($row) $qualityMain = $row;
    $stmt = $pdo->query("SELECT id, name, short_description, slug FROM quality_policy_items WHERE is_active = 1 ORDER BY sort_order, id");
    $qualityItems = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) { /* tables missing */ }
if (empty($qualityItems)) {
    $qualityItems = [
        ['id' => 1, 'name' => 'Quality commitment', 'short_description' => 'We are committed to delivering packaging solutions that meet the highest standards.', 'slug' => 'quality-commitment'],
        ['id' => 2, 'name' => 'Quality control process', 'short_description' => 'Strict quality control at every stage of production.', 'slug' => 'quality-control-process'],
        ['id' => 3, 'name' => 'Inspection standards', 'short_description' => 'Rigorous inspection standards for every product we deliver.', 'slug' => 'inspection-standards'],
        ['id' => 4, 'name' => 'Continues Improvement', 'short_description' => 'We continuously improve our processes and standards.', 'slug' => 'continues-improvement'],
    ];
}

require_once __DIR__ . '/includes/header.php';
?>
<div class="page-content tech-quality-page">
    <div class="container">
        <h1 class="tech-quality-title"><?php echo htmlspecialchars($qualityMain['heading']); ?></h1>
        <?php if (!empty($qualityMain['short_description'])): ?>
        <p class="tech-quality-lead"><?php echo nl2br(htmlspecialchars($qualityMain['short_description'])); ?></p>
        <?php endif; ?>
        <div class="tech-quality-cards tech-quality-cards--four">
            <?php foreach ($qualityItems as $item): ?>
            <div class="tech-quality-card">
                <h2 class="tech-quality-card-title"><?php echo htmlspecialchars($item['name']); ?></h2>
                <p class="tech-quality-card-desc"><?php echo nl2br(htmlspecialchars($item['short_description'] ?? '')); ?></p>
                <a href="<?php echo $assetBase; ?>quality-policy-detail.php?<?php if (!empty($item['slug'])) echo 'slug=' . rawurlencode($item['slug']) . '&'; ?>id=<?php echo (int)$item['id']; ?>" class="btn btn-primary tech-quality-card-btn">More details</a>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
