<?php
require_once __DIR__ . '/config.php';
requireAdmin();
$pdo = getDB();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!empty($_POST['delete_id'])) {
        $pdo->prepare("DELETE FROM footer_columns WHERE id = ?")->execute([(int)$_POST['delete_id']]);
    } elseif (isset($_POST['action']) && $_POST['action'] === 'add') {
        $pdo->prepare("INSERT INTO footer_columns (title, body, sort_order) VALUES (?, ?, ?)")->execute([
            trim($_POST['title'] ?? ''),
            trim($_POST['body'] ?? ''),
            (int)($_POST['sort_order'] ?? 0)
        ]);
    } elseif (isset($_POST['action']) && $_POST['action'] === 'save' && !empty($_POST['id'])) {
        $pdo->prepare("UPDATE footer_columns SET title=?, body=?, sort_order=?, is_active=? WHERE id=?")->execute([
            trim($_POST['title'] ?? ''),
            trim($_POST['body'] ?? ''),
            (int)($_POST['sort_order'] ?? 0),
            isset($_POST['is_active']) ? 1 : 0,
            (int)$_POST['id']
        ]);
    }
    if (isset($_POST['footer_bottom_html'])) {
        $pdo->prepare("INSERT INTO site_settings (setting_key, setting_value) VALUES ('footer_bottom_html', ?) ON DUPLICATE KEY UPDATE setting_value = ?")->execute([
            $_POST['footer_bottom_html'], $_POST['footer_bottom_html']
        ]);
    }
    if (isset($_POST['footer_description'])) {
        $val = trim($_POST['footer_description'] ?? '');
        $pdo->prepare("INSERT INTO site_settings (setting_key, setting_value) VALUES ('footer_description', ?) ON DUPLICATE KEY UPDATE setting_value = ?")->execute([$val, $val]);
    }
    header('Location: footer-content.php');
    exit;
}

try {
    $cols = $pdo->query("SELECT * FROM footer_columns ORDER BY sort_order, id")->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) { $cols = []; }
$bottomHtml = getSetting('footer_bottom_html', '');
$footerDesc = getSetting('footer_description', 'Providing reliable printing and packaging solutions with modern technology, strict quality control, and a commitment to customer satisfaction.');
$pageTitle = 'Footer content';
require_once __DIR__ . '/includes/header.php';
?>
<div class="admin-page">
    <h1>Footer columns</h1>
    <p>Body: one link per line as <code>Label|page.php</code> or plain lines. Or use HTML if you need rich content.</p>
    <?php if (empty($cols)): ?>
    <p class="error">Import <code>database/client_footer.sql</code> if table missing.</p>
    <?php endif; ?>
    <form method="post" class="admin-form inline-form" style="margin-bottom:16px;">
        <input type="hidden" name="action" value="add">
        <input type="text" name="title" placeholder="Column title" required>
        <input type="number" name="sort_order" placeholder="Order" value="0" style="width:70px;">
        <button type="submit" class="btn">Add column</button>
    </form>
    <?php foreach ($cols as $c): ?>
    <form method="post" class="admin-form" style="margin-bottom:24px;padding:16px;border:1px solid #ddd;border-radius:8px;">
        <input type="hidden" name="action" value="save">
        <input type="hidden" name="id" value="<?php echo (int)$c['id']; ?>">
        <label>Title</label>
        <input type="text" name="title" value="<?php echo htmlspecialchars($c['title']); ?>">
        <label>Body</label>
        <textarea name="body" rows="8"><?php echo htmlspecialchars($c['body']); ?></textarea>
        <label>Sort order</label>
        <input type="number" name="sort_order" value="<?php echo (int)$c['sort_order']; ?>">
        <label><input type="checkbox" name="is_active" value="1" <?php echo $c['is_active'] ? 'checked' : ''; ?>> Active</label>
        <button type="submit" class="btn">Save</button>
    </form>
    <form method="post" onsubmit="return confirm('Delete column?');" style="margin:-16px 0 24px;">
        <input type="hidden" name="delete_id" value="<?php echo (int)$c['id']; ?>">
        <button type="submit" class="link-btn">Delete this column</button>
    </form>
    <?php endforeach; ?>

    <h2>Footer brand description</h2>
    <p class="muted">Shown under the company name and tagline in the footer (first column).</p>
    <form method="post" class="admin-form" style="margin-bottom:24px;">
        <textarea name="footer_description" rows="4" placeholder="e.g. Providing reliable printing and packaging solutions..."><?php echo htmlspecialchars($footerDesc); ?></textarea>
        <button type="submit" class="btn">Save description</button>
    </form>

    <h2>Footer bottom (optional HTML)</h2>
    <form method="post" class="admin-form">
        <textarea name="footer_bottom_html" rows="4" placeholder="Optional HTML above copyright"><?php echo htmlspecialchars($bottomHtml); ?></textarea>
        <button type="submit" class="btn">Save bottom block</button>
    </form>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
