<?php
$pageTitle = 'Header & Footer Code';
require_once dirname(__DIR__) . '/includes/header.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $keys = ['header_code','footer_code','custom_css'];
    foreach ($keys as $key) {
        $val = $_POST[$key] ?? '';
        $st = db()->prepare("INSERT INTO site_settings (setting_key,setting_value) VALUES (?,?) ON DUPLICATE KEY UPDATE setting_value=?");
        $st->bind_param('sss', $key, $val, $val);
        $st->execute();
    }
    flash('Code saved successfully!','success');
    redirect(ADMIN_URL.'/pages/header-footer.php');
}

$raw = db()->query("SELECT setting_key,setting_value FROM site_settings WHERE setting_key IN ('header_code','footer_code','custom_css')")->fetch_all(MYSQLI_ASSOC);
$cfg = [];
foreach ($raw as $r) $cfg[$r['setting_key']] = $r['setting_value'];
?>
<div class="page-header">
    <h1>Header & Footer Code</h1>
    <div style="margin-left:auto;font-size:13px;color:var(--muted)">Injected into every page of the website</div>
</div>

<form method="POST">
<div style="display:flex;flex-direction:column;gap:20px">

<div class="card">
    <div class="card-header">
        <i class="fas fa-code" style="color:var(--brand)"></i>
        <h2>Custom &lt;head&gt; Code</h2>
        <span style="margin-left:auto;font-size:12px;color:var(--muted)">Injected before &lt;/head&gt;</span>
    </div>
    <div class="card-body">
        <div class="form-group" style="margin:0">
            <textarea name="header_code" class="form-control" rows="8" style="font-family:monospace;font-size:13px" placeholder="<!-- Google Analytics, meta tags, custom CSS links... -->"><?=htmlspecialchars($cfg['header_code']??'')?></textarea>
            <div class="form-hint">Add Google Analytics, Facebook Pixel, custom &lt;meta&gt; tags, or &lt;link&gt; tags here.</div>
        </div>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <i class="fas fa-code" style="color:var(--success)"></i>
        <h2>Footer Scripts</h2>
        <span style="margin-left:auto;font-size:12px;color:var(--muted)">Injected before &lt;/body&gt;</span>
    </div>
    <div class="card-body">
        <div class="form-group" style="margin:0">
            <textarea name="footer_code" class="form-control" rows="8" style="font-family:monospace;font-size:13px" placeholder="<!-- Chat widgets, tracking scripts... -->"><?=htmlspecialchars($cfg['footer_code']??'')?></textarea>
            <div class="form-hint">Add live chat widgets, WhatsApp buttons, or any JavaScript that should load after page content.</div>
        </div>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <i class="fas fa-paint-brush" style="color:var(--warn)"></i>
        <h2>Custom CSS</h2>
        <span style="margin-left:auto;font-size:12px;color:var(--muted)">Applied to all frontend pages</span>
    </div>
    <div class="card-body">
        <div class="form-group" style="margin:0">
            <textarea name="custom_css" class="form-control" rows="8" style="font-family:monospace;font-size:13px" placeholder="/* Your custom styles here */
body { font-family: 'MyFont', sans-serif; }
.hero { background: #your-color; }"><?=htmlspecialchars($cfg['custom_css']??'')?></textarea>
            <div class="form-hint">Pure CSS only — no &lt;style&gt; tags needed.</div>
        </div>
    </div>
</div>

</div>
<div style="margin-top:20px">
    <button type="submit" class="btn btn-primary" style="padding:12px 32px"><i class="fas fa-save"></i> Save Code</button>
</div>
</form>
<?php require_once dirname(__DIR__) . '/includes/footer.php'; ?>
