<?php
/**
 * Dynamic footer columns + bottom bar. Requires config.
 */
if (!function_exists('getDB')) return;
$pdo = getDB();
$assetBase = (defined('BASE_URL') && BASE_URL && BASE_URL !== '/') ? rtrim(BASE_URL, '/') . '/' : '';
$siteName = getSetting('site_name', 'CRIMSON');
$siteLogoFile = getSetting('site_logo', '');
$logoSrc = '';
if ($siteLogoFile && is_file(BASE_PATH . '/uploads/site/' . $siteLogoFile)) {
    $logoSrc = $assetBase . 'uploads/site/' . rawurlencode($siteLogoFile);
}
if ($logoSrc === '' && is_file(BASE_PATH . '/assets/images/logo.png')) {
    $logoSrc = $assetBase . 'assets/images/logo.png';
}
if ($logoSrc === '') {
    $logoSrc = $assetBase . 'assets/images/logo-dummy.svg';
}
$columns = [];
try {
    $stmt = $pdo->query("SELECT title, body FROM footer_columns WHERE is_active = 1 ORDER BY sort_order, id");
    $columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) { /* table missing */ }
$bottomHtml = getSetting('footer_bottom_html', '');
?>
<footer class="site-footer site-footer--dynamic">
    <div class="footer-main">
        <div class="container footer-grid">
            <div class="footer-col footer-brand">
                <a class="footer-brand-link" href="<?php echo htmlspecialchars($assetBase); ?>index.php" aria-label="<?php echo htmlspecialchars($siteName); ?>">
                    <img class="footer-logo" src="<?php echo htmlspecialchars($logoSrc); ?>" alt="<?php echo htmlspecialchars($siteName); ?>" width="220" height="56" loading="lazy">
                </a>
                <div class="footer-brand-name"><?php echo htmlspecialchars($siteName); ?></div>
                <?php
                $footerDesc = getSetting('footer_description', 'Providing reliable printing and packaging solutions with modern technology, strict quality control, and a commitment to customer satisfaction.');
                if (trim($footerDesc) !== ''):
                ?>
                <p class="footer-brand-desc"><?php echo nl2br(htmlspecialchars(trim($footerDesc))); ?></p>
                <?php endif; ?>
            </div>
            <?php if (!empty($columns)): ?>
                <?php foreach ($columns as $col): ?>
                <div class="footer-col">
                    <h3 class="footer-col-title"><?php echo htmlspecialchars($col['title']); ?></h3>
                    <div class="footer-col-body">
                        <?php
                        $body = trim($col['body'] ?? '');
                        if ($body === '') {
                            echo '';
                        } elseif (strpos($body, '<') !== false && strpos($body, '>') !== false) {
                            echo $body; // admin-controlled HTML
                        } else {
                            // Lines like "Label|url" or plain lines
                            $lines = preg_split('/\r\n|\r|\n/', $body);
                            echo '<ul class="footer-links">';
                            foreach ($lines as $line) {
                                $line = trim($line);
                                if ($line === '') continue;
                                if (strpos($line, '|') !== false) {
                                    list($label, $url) = explode('|', $line, 2);
                                    $label = trim($label);
                                    $url = trim($url);
                                    $href = (strpos($url, 'http') === 0) ? $url : $assetBase . ltrim($url, '/');
                                    echo '<li><a href="' . htmlspecialchars($href) . '">' . htmlspecialchars($label) . '</a></li>';
                                } else {
                                    echo '<li><span class="footer-plain">' . htmlspecialchars($line) . '</span></li>';
                                }
                            }
                            echo '</ul>';
                        }
                        ?>
                    </div>
                </div>
                <?php endforeach; ?>
            <?php else: ?>
            <div class="footer-col">
                <h3 class="footer-col-title"><?php echo htmlspecialchars($siteName); ?></h3>
            </div>
            <?php endif; ?>
        </div>
    </div>
    <div class="footer-bottom">
        <div class="container footer-bottom-inner">
            <?php if ($bottomHtml): ?>
            <div class="footer-bottom-custom"><?php echo $bottomHtml; ?></div>
            <?php endif; ?>
            <p class="footer-copyright">&copy; <?php echo date('Y'); ?> <?php echo htmlspecialchars($siteName); ?>. All rights reserved.</p>
        </div>
    </div>
</footer>
