<?php
require_once __DIR__ . '/config/config.php';
$pageTitle = 'About us';
$pdo = getDB();
$assetBase = (defined('BASE_URL') && BASE_URL && BASE_URL !== '/') ? rtrim(BASE_URL, '/') . '/' : '';
$aboutDir = BASE_PATH . '/uploads/about/';
if (!is_dir($aboutDir)) {
    @mkdir($aboutDir, 0755, true);
}

// Default taglines (used if DB tagline empty)
$defaultTaglines = [
    'Innovative Packaging Solutions for Every Business',
    'Premium Printing & Packaging Solutions Since 2015',
    'Reliable Mono Carton & Corrugated Packaging Manufacturer',
    'Smart Packaging That Protects Your Product & Brand',
    'Delivering Quality Packaging with Precision',
];

// --- Company intro (single row)
$company = [
    'company_name' => 'Crimson Uni Quepack Pvt. Ltd.',
    'company_details' => 'We are a leading manufacturer of mono cartons and corrugated packaging solutions, serving businesses across industries since 2015. Our state-of-the-art facility combines precision printing with robust packaging to deliver products that protect your goods and elevate your brand.',
    'image' => null,
    'image_position' => 'right',
    'tagline' => 'Premium Printing & Packaging Solutions Since 2015',
];
try {
    $stmt = $pdo->query("SELECT company_name, company_details, image, image_position, tagline FROM about_company WHERE is_active = 1 LIMIT 1");
    if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $company = array_merge($company, $row);
    }
} catch (Exception $e) { /* table missing */ }
if (empty($company['tagline'])) $company['tagline'] = $defaultTaglines[1];
$companyImageSrc = null;
if (!empty($company['image']) && is_file($aboutDir . $company['image'])) {
    $companyImageSrc = $assetBase . 'uploads/about/' . rawurlencode($company['image']);
}

// --- Company growth (main, future_goal, current_growth)
$growthMain = $growthFuture = $growthCurrent = null;
try {
    $stmt = $pdo->query("SELECT section_type, heading, description, image FROM about_growth WHERE is_active = 1 ORDER BY sort_order, id");
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $imgSrc = null;
        if (!empty($row['image']) && is_file($aboutDir . $row['image'])) {
            $imgSrc = $assetBase . 'uploads/about/' . rawurlencode($row['image']);
        }
        $row['image_src'] = $imgSrc;
        if ($row['section_type'] === 'main') $growthMain = $row;
        elseif ($row['section_type'] === 'future_goal') $growthFuture = $row;
        elseif ($row['section_type'] === 'current_growth') $growthCurrent = $row;
    }
} catch (Exception $e) { /* table missing */ }
if (!$growthMain) $growthMain = ['heading' => 'Our Growth Journey', 'description' => 'From a small packaging unit to a trusted name in mono carton and corrugated solutions.', 'image_src' => null];
if (!$growthFuture) $growthFuture = ['heading' => 'Future Goal', 'description' => 'Expanding capacity and product range to become the preferred packaging partner.', 'image_src' => null];
if (!$growthCurrent) $growthCurrent = ['heading' => 'Current Growth', 'description' => 'Serving 30+ customers with 1000+ projects delivered and 50+ professionals.', 'image_src' => null];

// --- Owner (single row)
$owner = [
    'name' => 'Leadership',
    'designation' => 'Founder & Managing Director',
    'description' => 'Our leadership brings decades of experience in packaging and print. Under their guidance, we have built a culture of quality, innovation, and customer-first delivery.',
    'image' => null,
];
try {
    $stmt = $pdo->query("SELECT name, designation, description, image FROM about_owner WHERE is_active = 1 LIMIT 1");
    if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $owner = array_merge($owner, $row);
    }
} catch (Exception $e) { /* table missing */ }
$ownerImageSrc = null;
if (!empty($owner['image']) && is_file($aboutDir . $owner['image'])) {
    $ownerImageSrc = $assetBase . 'uploads/about/' . rawurlencode($owner['image']);
}

// --- Team (4 members)
$teamHeading = getSetting('about_team_heading', 'Meet Our Team');
$teamSubtext = getSetting('about_team_subtext', 'The people behind our quality and your trust.');
$team = [];
try {
    $stmt = $pdo->query("SELECT name, designation, image FROM about_team WHERE is_active = 1 ORDER BY sort_order, id LIMIT 8");
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $imgSrc = null;
        if (!empty($row['image']) && is_file($aboutDir . $row['image'])) {
            $imgSrc = $assetBase . 'uploads/about/' . rawurlencode($row['image']);
        }
        $team[] = ['name' => $row['name'], 'designation' => $row['designation'], 'image_src' => $imgSrc];
    }
} catch (Exception $e) { /* table missing */ }
if (empty($team)) {
    $team = [
        ['name' => 'Team Member One', 'designation' => 'Operations Head', 'image_src' => null],
        ['name' => 'Team Member Two', 'designation' => 'Production Manager', 'image_src' => null],
        ['name' => 'Team Member Three', 'designation' => 'Quality Assurance', 'image_src' => null],
        ['name' => 'Team Member Four', 'designation' => 'Client Relations', 'image_src' => null],
    ];
}

require_once __DIR__ . '/includes/header.php';
?>
<div class="page-content about-page">
    <div class="container">

        <!-- Company intro: name, details, image (left/right), tagline -->
        <section class="about-section about-company-intro" aria-labelledby="about-company-title">
            <h1 id="about-company-title" class="about-page-title"><?php echo htmlspecialchars($company['company_name']); ?></h1>
            <p class="about-tagline"><?php echo htmlspecialchars($company['tagline']); ?></p>
            <div class="about-company-wrap about-image-<?php echo $company['image_position'] === 'left' ? 'left' : 'right'; ?>">
                <div class="about-company-copy">
                    <div class="about-company-details">
                        <?php echo nl2br(htmlspecialchars($company['company_details'])); ?>
                    </div>
                </div>
                <?php if ($companyImageSrc): ?>
                <div class="about-company-image-wrap">
                    <img src="<?php echo htmlspecialchars($companyImageSrc); ?>" alt="<?php echo htmlspecialchars($company['company_name']); ?>" class="about-section-img" loading="lazy">
                </div>
                <?php else: ?>
                <div class="about-company-image-wrap about-placeholder">
                    <div class="about-img-placeholder" aria-hidden="true">Image</div>
                </div>
                <?php endif; ?>
            </div>
        </section>

        <!-- Company growth: heading, description, image -->
        <section class="about-section about-growth-main" aria-labelledby="about-growth-title">
            <h2 id="about-growth-title" class="about-section-heading"><?php echo htmlspecialchars($growthMain['heading']); ?></h2>
            <div class="about-growth-main-wrap">
                <div class="about-growth-copy">
                    <p class="about-growth-desc"><?php echo nl2br(htmlspecialchars($growthMain['description'])); ?></p>
                </div>
                <?php if (!empty($growthMain['image_src'])): ?>
                <div class="about-growth-image-wrap">
                    <img src="<?php echo htmlspecialchars($growthMain['image_src']); ?>" alt="" class="about-section-img" loading="lazy">
                </div>
                <?php else: ?>
                <div class="about-growth-image-wrap about-placeholder">
                    <div class="about-img-placeholder" aria-hidden="true">Growth</div>
                </div>
                <?php endif; ?>
            </div>
        </section>

        <!-- Future Goal + Current Growth (side by side or stacked) -->
        <div class="about-growth-cards">
            <section class="about-growth-card" aria-labelledby="about-future-title">
                <h2 id="about-future-title" class="about-card-heading"><?php echo htmlspecialchars($growthFuture['heading']); ?></h2>
                <?php if (!empty($growthFuture['image_src'])): ?>
                <div class="about-card-image">
                    <img src="<?php echo htmlspecialchars($growthFuture['image_src']); ?>" alt="" loading="lazy">
                </div>
                <?php else: ?>
                <div class="about-card-image about-placeholder">
                    <div class="about-img-placeholder">Future</div>
                </div>
                <?php endif; ?>
                <p class="about-card-desc"><?php echo nl2br(htmlspecialchars($growthFuture['description'])); ?></p>
            </section>
            <section class="about-growth-card" aria-labelledby="about-current-title">
                <h2 id="about-current-title" class="about-card-heading"><?php echo htmlspecialchars($growthCurrent['heading']); ?></h2>
                <?php if (!empty($growthCurrent['image_src'])): ?>
                <div class="about-card-image">
                    <img src="<?php echo htmlspecialchars($growthCurrent['image_src']); ?>" alt="" loading="lazy">
                </div>
                <?php else: ?>
                <div class="about-card-image about-placeholder">
                    <div class="about-img-placeholder">Current</div>
                </div>
                <?php endif; ?>
                <p class="about-card-desc"><?php echo nl2br(htmlspecialchars($growthCurrent['description'])); ?></p>
            </section>
        </div>

        <!-- Owner details -->
        <section class="about-section about-owner" aria-labelledby="about-owner-title">
            <h2 id="about-owner-title" class="about-section-heading">Leadership</h2>
            <div class="about-owner-wrap">
                <?php if ($ownerImageSrc): ?>
                <div class="about-owner-image-wrap">
                    <img src="<?php echo htmlspecialchars($ownerImageSrc); ?>" alt="<?php echo htmlspecialchars($owner['name']); ?>" class="about-section-img" loading="lazy">
                </div>
                <?php else: ?>
                <div class="about-owner-image-wrap about-placeholder">
                    <div class="about-img-placeholder">Owner</div>
                </div>
                <?php endif; ?>
                <div class="about-owner-copy">
                    <h3 class="about-owner-name"><?php echo htmlspecialchars($owner['name']); ?></h3>
                    <p class="about-owner-designation"><?php echo htmlspecialchars($owner['designation']); ?></p>
                    <div class="about-owner-desc"><?php echo nl2br(htmlspecialchars($owner['description'])); ?></div>
                </div>
            </div>
        </section>

        <!-- Meet our team -->
        <section class="about-section about-team" aria-labelledby="about-team-title">
            <h2 id="about-team-title" class="about-section-heading"><?php echo htmlspecialchars($teamHeading); ?></h2>
            <?php if ($teamSubtext): ?>
            <p class="about-team-sub"><?php echo htmlspecialchars($teamSubtext); ?></p>
            <?php endif; ?>
            <div class="about-team-grid">
                <?php foreach (array_slice($team, 0, 4) as $member): ?>
                <div class="about-team-card">
                    <div class="about-team-card-image-wrap">
                        <?php if (!empty($member['image_src'])): ?>
                        <img src="<?php echo htmlspecialchars($member['image_src']); ?>" alt="<?php echo htmlspecialchars($member['name']); ?>" loading="lazy">
                        <?php else: ?>
                        <div class="about-img-placeholder about-team-placeholder"><?php echo htmlspecialchars(mb_substr($member['name'], 0, 1)); ?></div>
                        <?php endif; ?>
                    </div>
                    <h3 class="about-team-name"><?php echo htmlspecialchars($member['name']); ?></h3>
                    <p class="about-team-designation"><?php echo htmlspecialchars($member['designation']); ?></p>
                </div>
                <?php endforeach; ?>
            </div>
        </section>

    </div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
