<?php
require_once __DIR__ . '/config/config.php';
$pageTitle = 'Plant & Machinery';
$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);

$plantMain = ['heading' => 'Plant & Machinery', 'long_description' => '', 'image' => null];
$plantCards = [];
try {
    $row = $pdo->query("SELECT heading, long_description, image FROM plant_machinery_main LIMIT 1")->fetch(PDO::FETCH_ASSOC);
    if ($row) $plantMain = $row;
    $stmt = $pdo->query("SELECT name, short_description FROM plant_machinery_cards WHERE is_active = 1 ORDER BY sort_order, id");
    $plantCards = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) { /* */ }
if (empty($plantCards)) {
    $plantCards = [
        ['name' => 'Offset Printing Machine', 'short_description' => 'Used for high-quality printing on mono cartons and packaging materials.'],
        ['name' => 'Die Cutting Machine', 'short_description' => 'Used to cut packaging sheets into precise shapes and sizes.'],
        ['name' => 'Folder Gluer Machine', 'short_description' => 'Used for folding and pasting cartons to create finished packaging boxes.'],
        ['name' => 'Corrugation Machinery', 'short_description' => 'Used for manufacturing strong corrugated sheets and boxes.'],
        ['name' => 'Production Capability', 'short_description' => 'Efficient manufacturing processes for timely delivery across industries.'],
    ];
}

$plantImageSrc = null;
if (!empty($plantMain['image']) && is_file($techDir . ($plantMain['image'] ?? ''))) {
    $plantImageSrc = $assetBase . 'uploads/tech_quality/' . rawurlencode($plantMain['image']);
}

require_once __DIR__ . '/includes/header.php';
?>
<div class="page-content tech-quality-page">
    <div class="container">
        <h1 class="tech-quality-title"><?php echo htmlspecialchars($plantMain['heading']); ?></h1>
        <?php if ($plantImageSrc): ?>
        <div class="tech-quality-plant-image-wrap">
            <img src="<?php echo htmlspecialchars($plantImageSrc); ?>" alt="" class="tech-quality-plant-img" loading="lazy">
        </div>
        <?php endif; ?>
        <div class="tech-quality-plant-desc">
            <?php echo nl2br(htmlspecialchars($plantMain['long_description'] ?? '')); ?>
        </div>
        <h2 class="tech-quality-subheading">Our Key Machinery</h2>
        <div class="plant-machinery-cards">
            <?php foreach ($plantCards as $card): ?>
            <div class="plant-machinery-card">
                <h3 class="plant-machinery-card-title"><?php echo htmlspecialchars($card['name']); ?></h3>
                <p class="plant-machinery-card-desc"><?php echo nl2br(htmlspecialchars($card['short_description'] ?? '')); ?></p>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
