<?php
/**
 * Agentic Square - Premium Administrative Hero Banner Configuration Control Panel
 * CodeCanyon Secure Execution Core Template Architecture
 */

// Inject Core Global Application Engine Paths
require_once __DIR__ . '/../config/config.php';

/**
 * STRICT SECURITY AUDIT GUARD (Envato Marketplace Compliant)
 */
if (!isset($_SESSION['user_id']) || !isset($_SESSION['user_role']) || $_SESSION['user_role'] !== 'admin') {
    header('Location: ../login.php');
    exit;
}

$success_message = '';
$error_message   = '';

// ------------------------------------------------------------------------
// ACTION ROUTER: Process Core Modification Submissions
// ------------------------------------------------------------------------
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Process text attributes with formatting integrity preserved
    $tagline       = filter_var(trim($_POST['tagline'] ?? ''), FILTER_SANITIZE_SPECIAL_CHARS);
    $heading       = $_POST['heading'] ?? ''; // Allowed to hold structured <br> tags intentionally
    $subheading    = filter_var(trim($_POST['subheading'] ?? ''), FILTER_SANITIZE_SPECIAL_CHARS);
    
    $btn1_text     = filter_var(trim($_POST['btn1_text'] ?? ''), FILTER_SANITIZE_SPECIAL_CHARS);
    $btn1_url      = filter_var(trim($_POST['btn1_url'] ?? ''), FILTER_SANITIZE_SPECIAL_CHARS);
    $btn2_text     = filter_var(trim($_POST['btn2_text'] ?? ''), FILTER_SANITIZE_SPECIAL_CHARS);
    $btn2_url      = filter_var(trim($_POST['btn2_url'] ?? ''), FILTER_SANITIZE_SPECIAL_CHARS);
    
    $stat1_value   = filter_var(trim($_POST['stat1_value'] ?? ''), FILTER_SANITIZE_SPECIAL_CHARS);
    $stat1_label   = filter_var(trim($_POST['stat1_label'] ?? ''), FILTER_SANITIZE_SPECIAL_CHARS);
    $stat2_value   = filter_var(trim($_POST['stat2_value'] ?? ''), FILTER_SANITIZE_SPECIAL_CHARS);
    $stat2_label   = filter_var(trim($_POST['stat2_label'] ?? ''), FILTER_SANITIZE_SPECIAL_CHARS);
    $stat3_value   = filter_var(trim($_POST['stat3_value'] ?? ''), FILTER_SANITIZE_SPECIAL_CHARS);
    $stat3_label   = filter_var(trim($_POST['stat3_label'] ?? ''), FILTER_SANITIZE_SPECIAL_CHARS);
    
    $card_tag      = filter_var(trim($_POST['card_tag'] ?? ''), FILTER_SANITIZE_SPECIAL_CHARS);
    $card_category = filter_var(trim($_POST['card_category'] ?? ''), FILTER_SANITIZE_SPECIAL_CHARS);
    $card_rating   = filter_var($_POST['card_rating'] ?? 5.0, FILTER_VALIDATE_FLOAT);
    $card_reviews  = filter_var($_POST['card_reviews'] ?? 0, FILTER_VALIDATE_INT);
    $card_title    = filter_var(trim($_POST['card_title'] ?? ''), FILTER_SANITIZE_SPECIAL_CHARS);
    $card_dev_name = filter_var(trim($_POST['card_dev_name'] ?? ''), FILTER_SANITIZE_SPECIAL_CHARS);
    $card_price    = filter_var($_POST['card_price'] ?? 0.00, FILTER_VALIDATE_FLOAT);

    // Image Upload Allocation Directory Handling
    $upload_dir = __DIR__ . '/../uploads/banner/';
    if (!is_dir($upload_dir)) {
        mkdir($upload_dir, 0755, true);
    }

    // Process File Asset A: Feature Card Product Showcase Visual Frame
    $card_banner_img = $_POST['existing_card_banner'] ?? '';
    if (isset($_FILES['card_banner_file']) && $_FILES['card_banner_file']['error'] === UPLOAD_ERR_OK) {
        $ext = strtolower(pathinfo($_FILES['card_banner_file']['name'], PATHINFO_EXTENSION));
        if (in_array($ext, ['jpg', 'jpeg', 'png', 'svg', 'webp']) && $_FILES['card_banner_file']['size'] <= 3145728) {
            $new_name = 'spotlight_' . time() . '.' . $ext;
            if (move_uploaded_file($_FILES['card_banner_file']['tmp_name'], $upload_dir . $new_name)) {
                $card_banner_img = 'uploads/banner/' . $new_name;
            }
        } else {
            $error_message = "Feature layout graphic asset violation (Max 3MB JPG/PNG/WebP).";
        }
    }

    // Process File Asset B: Merchant Micro Avatar Icon Vector
    $card_dev_img = $_POST['existing_card_dev_img'] ?? '';
    if (isset($_FILES['card_dev_file']) && $_FILES['card_dev_file']['error'] === UPLOAD_ERR_OK) {
        $ext = strtolower(pathinfo($_FILES['card_dev_file']['name'], PATHINFO_EXTENSION));
        if (in_array($ext, ['jpg', 'jpeg', 'png', 'webp']) && $_FILES['card_dev_file']['size'] <= 1048576) {
            $new_name = 'avatar_' . time() . '.' . $ext;
            if (move_uploaded_file($_FILES['card_dev_file']['tmp_name'], $upload_dir . $new_name)) {
                $card_dev_img = 'uploads/banner/' . $new_name;
            }
        } else {
            $error_message = "Developer icon visual asset violation (Max 1MB formats).";
        }
    }

    if (empty($error_message)) {
        try {
            $sql = "UPDATE banner_settings SET 
                tagline = :tagline, heading = :heading, subheading = :subheading,
                btn1_text = :btn1_text, btn1_url = :btn1_url, btn2_text = :btn2_text, btn2_url = :btn2_url,
                stat1_value = :stat1_value, stat1_label = :stat1_label, stat2_value = :stat2_value, stat2_label = :stat2_label, stat3_value = :stat3_value, stat3_label = :stat3_label,
                card_tag = :card_tag, card_category = :card_category, card_rating = :card_rating, card_reviews = :card_reviews, card_title = :card_title, card_dev_name = :card_dev_name, card_price = :card_price,
                card_banner_img = :card_banner_img, card_dev_img = :card_dev_img
                WHERE id = 1";
                
            $stmt = $pdo->prepare($sql);
            $stmt->execute([
                'tagline' => $tagline, 'heading' => $heading, 'subheading' => $subheading,
                'btn1_text' => $btn1_text, 'btn1_url' => $btn1_url, 'btn2_text' => $btn2_text, 'btn2_url' => $btn2_url,
                'stat1_value' => $stat1_value, 'stat1_label' => $stat1_label, 'stat2_value' => $stat2_value, 'stat2_label' => $stat2_label, 'stat3_value' => $stat3_value, 'stat3_label' => $stat3_label,
                'card_tag' => $card_tag, 'card_category' => $card_category, 'card_rating' => $card_rating, 'card_reviews' => $card_reviews, 'card_title' => $card_title, 'card_dev_name' => $card_dev_name, 'card_price' => $card_price,
                'card_banner_img' => $card_banner_img, 'card_dev_img' => $card_dev_img
            ]);
            $success_message = "Hero display configuration matrix saved accurately.";
        } catch (\PDOException $e) {
            $error_message = "SQL pipeline runtime restriction: " . $e->getMessage();
        }
    }
}

// Hydrate Data Structure Context Array Fields
$banner = $pdo->query("SELECT * FROM banner_settings WHERE id = 1 LIMIT 1")->fetch();

// Inject dynamic UI Header Layout Wrapper
require_once __DIR__ . '/../sections/header.php';
?>

<div class="min-h-screen bg-nftDark text-white antialiased">
    <?php require_once __DIR__ . '/admin-sidebar.php'; ?>

    <main class="lg:ml-64 min-h-screen pt-24 lg:pt-10 pb-12 px-4 sm:px-6 lg:px-8">
        <div class="max-w-5xl mx-auto space-y-8">
            
            <div class="flex items-center space-x-2 text-xs text-nftTextGray uppercase tracking-widest font-bold">
                <a href="settings.php" class="hover:text-nftAccentCyan transition-colors">Settings Hub</a>
                <span>/</span>
                <span class="text-white">// Hero Banner Interface</span>
            </div>

            <div class="border-b border-slate-900 pb-6">
                <h1 class="text-3xl font-extrabold tracking-tight bg-clip-text text-transparent bg-gradient-to-r from-white via-white to-nftAccentCyan">Hero Banner Customization Matrix</h1>
                <p class="text-sm text-nftTextGray mt-1">Configure global display slogans, high-impact call-to-actions, statistical metric metrics, and featured layout visual assets.</p>
            </div>

            <?php if (!empty($success_message)): ?>
                <div class="bg-emerald-500/10 border border-emerald-500/20 text-emerald-400 p-4 rounded-xl text-sm flex items-center space-x-2"><i class="fa-solid fa-circle-check"></i><span><?php echo $success_message; ?></span></div>
            <?php endif; ?>
            <?php if (!empty($error_message)): ?>
                <div class="bg-rose-500/10 border border-rose-500/20 text-rose-400 p-4 rounded-xl text-sm flex items-center space-x-2"><i class="fa-solid fa-triangle-exclamation"></i><span><?php echo $error_message; ?></span></div>
            <?php endif; ?>

            <form action="banner-setting.php" method="POST" enctype="multipart/form-data" class="space-y-6">
                <input type="hidden" name="existing_card_banner" value="<?php echo htmlspecialchars($banner['card_banner_img'] ?? ''); ?>">
                <input type="hidden" name="existing_card_dev_img" value="<?php echo htmlspecialchars($banner['card_dev_img'] ?? ''); ?>">

                <div class="glass-card p-6 rounded-2xl border border-slate-800/80 space-y-4">
                    <h2 class="text-base font-bold text-nftAccentCyan flex items-center space-x-2"><i class="fa-solid fa-paragraph"></i><span>Typography and Narrative Elements</span></h2>
                    
                    <div class="grid grid-cols-1 gap-4">
                        <div class="space-y-1">
                            <label class="text-xs font-bold uppercase tracking-wider text-nftTextGray">Top Accent Tag Slogan</label>
                            <input type="text" name="tagline" value="<?php echo htmlspecialchars($banner['tagline'] ?? ''); ?>" class="w-full bg-slate-950 border border-slate-800 focus:border-nftAccentCyan rounded-xl px-4 py-3 text-sm text-white focus:outline-none">
                        </div>
                        <div class="space-y-1">
                            <label class="text-xs font-bold uppercase tracking-wider text-nftTextGray">Primary Dynamic Heading (HTML &lt;br&gt; Tags Permitted)</label>
                            <textarea name="heading" rows="3" class="w-full bg-slate-950 border border-slate-800 focus:border-nftAccentCyan rounded-xl px-4 py-3 text-sm font-mono text-white focus:outline-none"><?php echo htmlspecialchars($banner['heading'] ?? ''); ?></textarea>
                        </div>
                        <div class="space-y-1">
                            <label class="text-xs font-bold uppercase tracking-wider text-nftTextGray">Subheading Paragraph Description Context</label>
                            <textarea name="subheading" rows="3" class="w-full bg-slate-950 border border-slate-800 focus:border-nftAccentCyan rounded-xl px-4 py-3 text-sm text-white focus:outline-none"><?php echo htmlspecialchars($banner['subheading'] ?? ''); ?></textarea>
                        </div>
                    </div>
                </div>

                <div class="glass-card p-6 rounded-2xl border border-slate-800/80 space-y-4">
                    <h2 class="text-base font-bold text-nftAccentPurple flex items-center space-x-2"><i class="fa-solid fa-link"></i><span>Action Routing Links</span></h2>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div class="space-y-1">
                            <label class="text-xs font-bold uppercase tracking-wider text-nftTextGray">Button 1 Title (Gradient Filled)</label>
                            <input type="text" name="btn1_text" value="<?php echo htmlspecialchars($banner['btn1_text'] ?? ''); ?>" class="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-3 text-sm text-white focus:outline-none">
                        </div>
                        <div class="space-y-1">
                            <label class="text-xs font-bold uppercase tracking-wider text-nftTextGray">Button 1 Target URL / Anchor Link</label>
                            <input type="text" name="btn1_url" value="<?php echo htmlspecialchars($banner['btn1_url'] ?? ''); ?>" class="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-3 text-sm text-white focus:outline-none">
                        </div>
                        <div class="space-y-1">
                            <label class="text-xs font-bold uppercase tracking-wider text-nftTextGray">Button 2 Title (Border Outlined)</label>
                            <input type="text" name="btn2_text" value="<?php echo htmlspecialchars($banner['btn2_text'] ?? ''); ?>" class="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-3 text-sm text-white focus:outline-none">
                        </div>
                        <div class="space-y-1">
                            <label class="text-xs font-bold uppercase tracking-wider text-nftTextGray">Button 2 Target URL / Anchor Link</label>
                            <input type="text" name="btn2_url" value="<?php echo htmlspecialchars($banner['btn2_url'] ?? ''); ?>" class="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-3 text-sm text-white focus:outline-none">
                        </div>
                    </div>
                </div>

                <div class="glass-card p-6 rounded-2xl border border-slate-800/80 space-y-4">
                    <h2 class="text-base font-bold text-nftAccentCyan flex items-center space-x-2"><i class="fa-solid fa-chart-simple"></i><span>Live Stat Metric Matrix Counters</span></h2>
                    <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                        <div class="p-4 bg-slate-950 rounded-xl border border-slate-900 space-y-2">
                            <label class="text-[10px] font-bold uppercase tracking-widest text-nftTextGray">Metric Tracker A (Value & Label)</label>
                            <input type="text" name="stat1_value" value="<?php echo htmlspecialchars($banner['stat1_value'] ?? ''); ?>" placeholder="e.g. <?php echo htmlspecialchars(currency_symbol(), ENT_QUOTES, 'UTF-8'); ?>2.4M+" class="w-full bg-slate-900 border border-slate-800 rounded-lg px-3 py-2 text-xs mb-1">
                            <input type="text" name="stat1_label" value="<?php echo htmlspecialchars($banner['stat1_label'] ?? ''); ?>" placeholder="e.g. Total Revenue" class="w-full bg-slate-900 border border-slate-800 rounded-lg px-3 py-2 text-xs">
                        </div>
                        <div class="p-4 bg-slate-950 rounded-xl border border-slate-900 space-y-2">
                            <label class="text-[10px] font-bold uppercase tracking-widest text-nftTextGray">Metric Tracker B (Value & Label)</label>
                            <input type="text" name="stat2_value" value="<?php echo htmlspecialchars($banner['stat2_value'] ?? ''); ?>" placeholder="e.g. 45k+" class="w-full bg-slate-900 border border-slate-800 rounded-lg px-3 py-2 text-xs mb-1">
                            <input type="text" name="stat2_label" value="<?php echo htmlspecialchars($banner['stat2_label'] ?? ''); ?>" placeholder="e.g. Scripts" class="w-full bg-slate-900 border border-slate-800 rounded-lg px-3 py-2 text-xs">
                        </div>
                        <div class="p-4 bg-slate-950 rounded-xl border border-slate-900 space-y-2">
                            <label class="text-[10px] font-bold uppercase tracking-widest text-nftTextGray">Metric Tracker C (Value & Label)</label>
                            <input type="text" name="stat3_value" value="<?php echo htmlspecialchars($banner['stat3_value'] ?? ''); ?>" placeholder="e.g. 99%" class="w-full bg-slate-900 border border-slate-800 rounded-lg px-3 py-2 text-xs mb-1">
                            <input type="text" name="stat3_label" value="<?php echo htmlspecialchars($banner['stat3_label'] ?? ''); ?>" placeholder="e.g. Fulfillment" class="w-full bg-slate-900 border border-slate-800 rounded-lg px-3 py-2 text-xs">
                        </div>
                    </div>
                </div>

                <div class="glass-card p-6 rounded-2xl border border-slate-800/80 space-y-4">
                    <h2 class="text-base font-bold text-nftAccentPurple flex items-center space-x-2"><i class="fa-solid fa-rocket"></i><span>Spotlight Asset Display Properties</span></h2>
                    
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div class="space-y-1">
                            <label class="text-xs font-bold uppercase tracking-wider text-nftTextGray">Product Grid Spotlight Image (4:3 Ratio Target)</label>
                            <input type="file" name="card_banner_file" class="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-2.5 text-xs text-slate-400">
                        </div>
                        <div class="space-y-1">
                            <label class="text-xs font-bold uppercase tracking-wider text-nftTextGray">Corner Accent Badging Tag</label>
                            <input type="text" name="card_tag" value="<?php echo htmlspecialchars($banner['card_tag'] ?? ''); ?>" class="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-3 text-sm text-white">
                        </div>
                        <div class="space-y-1">
                            <label class="text-xs font-bold uppercase tracking-wider text-nftTextGray">Sub-Header Category Classification</label>
                            <input type="text" name="card_category" value="<?php echo htmlspecialchars($banner['card_category'] ?? ''); ?>" class="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-3 text-sm text-white">
                        </div>
                        <div class="space-y-1">
                            <label class="text-xs font-bold uppercase tracking-wider text-nftTextGray">Product Meta Heading Title</label>
                            <input type="text" name="card_title" value="<?php echo htmlspecialchars($banner['card_title'] ?? ''); ?>" class="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-3 text-sm text-white">
                        </div>
                        <div class="grid grid-cols-2 gap-3">
                            <div class="space-y-1">
                                <label class="text-xs font-bold uppercase tracking-wider text-nftTextGray">Rating (e.g. 4.9)</label>
                                <input type="number" step="0.1" name="card_rating" value="<?php echo htmlspecialchars($banner['card_rating'] ?? ''); ?>" class="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-3 text-sm text-white">
                            </div>
                            <div class="space-y-1">
                                <label class="text-xs font-bold uppercase tracking-wider text-nftTextGray">Review Frequency Total</label>
                                <input type="number" name="card_reviews" value="<?php echo htmlspecialchars($banner['card_reviews'] ?? ''); ?>" class="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-3 text-sm text-white">
                            </div>
                        </div>
                        <div class="grid grid-cols-2 gap-3">
                            <div class="space-y-1">
                                <label class="text-xs font-bold uppercase tracking-wider text-nftTextGray">Merchant Handler Name</label>
                                <input type="text" name="card_dev_name" value="<?php echo htmlspecialchars($banner['card_dev_name'] ?? ''); ?>" class="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-3 text-sm text-white">
                            </div>
                            <div class="space-y-1">
                                <label class="text-xs font-bold uppercase tracking-wider text-nftTextGray">Commercial Yield Valuation Price (<?php echo htmlspecialchars(get_site_currency(), ENT_QUOTES, 'UTF-8'); ?>)</label>
                                <input type="number" step="0.01" name="card_price" value="<?php echo htmlspecialchars($banner['card_price'] ?? ''); ?>" class="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-3 text-sm text-white">
                            </div>
                        </div>
                        <div class="space-y-1 md:col-span-2">
                            <label class="text-xs font-bold uppercase tracking-wider text-nftTextGray">Merchant Mini-Avatar Photo Asset File</label>
                            <input type="file" name="card_dev_file" class="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-2.5 text-xs text-slate-400">
                        </div>
                    </div>
                </div>

                <div class="flex items-center justify-end space-x-4 pt-4 border-t border-slate-900">
                    <a href="settings.php" class="px-6 py-3 rounded-xl border border-slate-800 text-xs font-bold tracking-widest text-nftTextGray uppercase hover:bg-slate-900 hover:text-white transition-all">Cancel</a>
                    <button type="submit" class="px-6 py-3 rounded-xl bg-gradient-to-r from-nftAccentCyan to-nftAccentPurple font-bold text-xs tracking-widest text-white uppercase shadow-lg shadow-cyan-500/10 hover:shadow-cyan-500/20 transition-all">Apply Parameters</button>
                </div>
            </form>

        </div>
    </main>
</div>
</body>
</html>
