<?php
/**
 * Rig N Dig - Content Editor
 * Manage landing page text and marketing content
 */

$adminTitle = 'Content Editor';
require_once __DIR__ . '/includes/header.php';

// Handle settings update
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_content'])) {
    foreach ($_POST['s'] as $key => $value) {
        Database::execute("UPDATE settings SET setting_value = ? WHERE setting_key = ?", [$value, $key]);
    }
    $_SESSION['flash']['success'] = "Content updated successfully!";
    header("Location: content.php");
    exit;
}

// Ensure necessary setting keys exist (Seed if missing)
$keys = [
    'hero_tag'         => '⚙️ 16+ Years of Industrial Excellence',
    'hero_title_white_1' => 'Industrial',
    'hero_title_orange'  => 'Equipment',
    'hero_title_white_2' => 'Solutions',
    'hero_desc'        => "Pakistan's leading supplier of API standard flanges, pipes, valves, and all industrial equipment. Complete workshop services including precision machining, fabrication and machine overhaul.",
    'hero_cta_primary' => 'Browse Products →',
    'hero_cta_secondary'=> 'Request Repair',
    'section_categories_title' => 'Product Categories',
    'section_categories_desc'  => 'From API standard flanges to complete industrial machinery spares — we stock and source everything your operations need.',
];

foreach ($keys as $k => $v) {
    if (Database::getSetting($k) === null) {
        Database::execute("INSERT INTO settings (setting_key, setting_value) VALUES (?,?)", [$k, $v]);
    }
}
?>

<div class="max-w-4xl">
    <form method="POST">
        <input type="hidden" name="save_content" value="1">
        
        <!-- Hero Section -->
        <div class="card-dark p-6 mb-8">
            <h2 class="text-lg font-bold mb-4 border-b border-navy-500 pb-2 flex items-center gap-2">
                <span>🚀</span> Hero Section (Landing Page)
            </h2>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <div class="md:col-span-2">
                    <label class="text-xs text-gray-400 block mb-1.5 font-bold uppercase tracking-wider">Top Tag Line</label>
                    <input type="text" name="s[hero_tag]" value="<?= e(setting('hero_tag')) ?>" placeholder="e.g. ⚙️ 16+ Years Excellence">
                </div>
                <div>
                    <label class="text-xs text-gray-400 block mb-1.5 font-bold uppercase tracking-wider">Title Line 1 (White)</label>
                    <input type="text" name="s[hero_title_white_1]" value="<?= e(setting('hero_title_white_1')) ?>">
                </div>
                <div>
                    <label class="text-xs text-gray-400 block mb-1.5 font-bold uppercase tracking-wider">Title Line 2 (Orange)</label>
                    <input type="text" name="s[hero_title_orange]" value="<?= e(setting('hero_title_orange')) ?>">
                </div>
                <div>
                    <label class="text-xs text-gray-400 block mb-1.5 font-bold uppercase tracking-wider">Title Line 3 (White)</label>
                    <input type="text" name="s[hero_title_white_2]" value="<?= e(setting('hero_title_white_2')) ?>">
                </div>
                <div class="md:col-span-2">
                    <label class="text-xs text-gray-400 block mb-1.5 font-bold uppercase tracking-wider">Hero Description</label>
                    <textarea name="s[hero_desc]" rows="3"><?= e(setting('hero_desc')) ?></textarea>
                </div>
                <div>
                    <label class="text-xs text-gray-400 block mb-1.5 font-bold uppercase tracking-wider">Primary Button Text</label>
                    <input type="text" name="s[hero_cta_primary]" value="<?= e(setting('hero_cta_primary')) ?>">
                </div>
                <div>
                    <label class="text-xs text-gray-400 block mb-1.5 font-bold uppercase tracking-wider">Secondary Button Text</label>
                    <input type="text" name="s[hero_cta_secondary]" value="<?= e(setting('hero_cta_secondary')) ?>">
                </div>
            </div>
        </div>

        <!-- Sections -->
        <div class="card-dark p-6 mb-8">
            <h2 class="text-lg font-bold mb-4 border-b border-navy-500 pb-2 flex items-center gap-2">
                <span>🗂️</span> Landing Page Sections
            </h2>
            <div class="space-y-6">
                <div>
                    <label class="text-xs text-gray-400 block mb-1.5 font-bold uppercase tracking-wider">Categories Section Title</label>
                    <input type="text" name="s[section_categories_title]" value="<?= e(setting('section_categories_title')) ?>">
                </div>
                <div>
                    <label class="text-xs text-gray-400 block mb-1.5 font-bold uppercase tracking-wider">Categories Section Description</label>
                    <textarea name="s[section_categories_desc]" rows="2"><?= e(setting('section_categories_desc')) ?></textarea>
                </div>
            </div>
        </div>

        <div class="sticky bottom-6 flex justify-end">
            <button type="submit" class="btn-primary shadow-2xl px-10 py-3 text-sm font-bold">💾 Save Changes</button>
        </div>
    </form>
</div>

<?php include __DIR__ . '/includes/footer.php'; ?>
