<?php
session_start();
require 'config.php';

if (!isset($_SESSION['admin_logged_in'])) {
    header("Location: login.php");
    exit;
}

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    echo "<div class='alert alert-danger'>Invalid survey ID.</div>";
    exit;
}

$id = (int)$_GET['id'];
$pageError = '';

$stmt = $dbh->prepare('SELECT * FROM surveys WHERE id = ?');
$stmt->execute([$id]);
$survey = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$survey) {
    $pageError = 'Survey not found.';
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && !$pageError) {
    $broker_name = trim($_POST['broker_name'] ?? '');
    $broker_logo = trim($_POST['broker_logo'] ?? '');
    $survey_title = trim($_POST['survey_title'] ?? '');
    $survey_description = trim($_POST['survey_description'] ?? '');
    $badge = trim($_POST['badge'] ?? '');
    $badge_class = trim($_POST['badge_class'] ?? '');
    $image_url = trim($_POST['image_url'] ?? '');
    $country_name = trim($_POST['country_name'] ?? '');
    $country_flag_url = trim($_POST['country_flag_url'] ?? '');
    $survey_date = $_POST['survey_date'] ?? '';
    $detail_url = trim($_POST['detail_url'] ?? '');

    $stmt = $dbh->prepare('UPDATE surveys SET broker_name = ?, broker_logo = ?, survey_title = ?, survey_description = ?, badge = ?, badge_class = ?, image_url = ?, country_name = ?, country_flag_url = ?, survey_date = ?, detail_url = ? WHERE id = ?');
    
    if ($stmt->execute([
        $broker_name,
        $broker_logo,
        $survey_title,
        $survey_description,
        $badge,
        $badge_class,
        $image_url,
        $country_name,
        $country_flag_url,
        $survey_date,
        $detail_url,
        $id
    ])) {
        header('Location: view_surveys.php?updated=1');
        exit;
    } else {
        $pageError = 'Failed to update survey.';
    }
}

include 'includes/header.php';
include 'includes/sidebar.php';
?>

<div class="admin-main-content">
    <div class="survey-container">
        <div class="survey-hero">
            <div class="hero-icon">
                <i class="fas fa-poll"></i>
            </div>

            <div class="hero-copy">
                <span class="hero-kicker">Survey Management</span>
                <h1>Edit Survey</h1>
                <p>Update survey details, broker information, and publication settings.</p>
            </div>

            <div class="hero-meta">
                <span class="meta-pill">Survey ID: #<?= (int)$survey['id'] ?></span>
                <span class="meta-pill meta-pill-soft"><?= htmlentities($survey['country_name'] ?? 'Global') ?></span>
            </div>
        </div>

        <div class="survey-body">
            <?php if (!empty($pageError)): ?>
                <div class="page-alert page-alert-danger">
                    <i class="fas fa-exclamation-triangle"></i> <?= htmlentities($pageError) ?>
                </div>
            <?php endif; ?>

            <form method="POST" action="" class="survey-form">
                <div class="section-head">
                    <h2>Broker Information</h2>
                    <p>Primary broker details and visual identity.</p>
                </div>

                <div class="form-grid two-col">
                    <div class="field">
                        <label for="broker_name">Broker Name</label>
                        <input type="text" id="broker_name" name="broker_name" value="<?= htmlspecialchars($survey['broker_name']) ?>" class="input" required>
                    </div>

                    <div class="field">
                        <label for="broker_logo">Broker Logo URL</label>
                        <input type="url" id="broker_logo" name="broker_logo" value="<?= htmlspecialchars($survey['broker_logo']) ?>" class="input" placeholder="https://..." required>
                    </div>
                </div>

                <div class="section-head">
                    <h2>Survey Content</h2>
                    <p>Title, description, and publication details.</p>
                </div>

                <div class="field">
                    <label for="survey_title">Survey Title</label>
                    <input type="text" id="survey_title" name="survey_title" value="<?= htmlspecialchars($survey['survey_title']) ?>" class="input" required>
                </div>

                <div class="field">
                    <label for="survey_description">Survey Description</label>
                    <textarea id="survey_description" name="survey_description" class="input textarea" required><?= htmlspecialchars($survey['survey_description']) ?></textarea>
                </div>

                <div class="form-grid two-col">
                    <div class="field">
                        <label for="survey_date">Survey Date</label>
                        <input type="date" id="survey_date" name="survey_date" value="<?= htmlspecialchars($survey['survey_date']) ?>" class="input" required>
                    </div>

                    <div class="field">
                        <label for="detail_url">Detail URL</label>
                        <input type="url" id="detail_url" name="detail_url" value="<?= htmlspecialchars($survey['detail_url']) ?>" class="input" placeholder="https://..." required>
                    </div>
                </div>

                <div class="section-head">
                    <h2>Media Assets</h2>
                    <p>Survey image and flag settings.</p>
                </div>

                <div class="form-grid two-col">
                    <div class="field">
                        <label for="image_url">Image URL</label>
                        <input type="url" id="image_url" name="image_url" value="<?= htmlspecialchars($survey['image_url']) ?>" class="input" placeholder="https://">
                    </div>

                    <div class="field">
                        <label for="country_flag_url">Country Flag URL</label>
                        <input type="url" id="country_flag_url" name="country_flag_url" value="<?= htmlspecialchars($survey['country_flag_url']) ?>" class="input" placeholder="https://">
                    </div>
                </div>

                <div class="section-head">
                    <h2>Categorization</h2>
                    <p>Badge and display settings.</p>
                </div>

                <div class="form-grid two-col">
                    <div class="field">
                        <label for="badge">Badge Label</label>
                        <input type="text" id="badge" name="badge" value="<?= htmlspecialchars($survey['badge']) ?>" class="input" placeholder="e.g. Featured, Popular">
                    </div>

                    <div class="field">
                        <label for="badge_class">Badge CSS Class</label>
                        <input type="text" id="badge_class" name="badge_class" value="<?= htmlspecialchars($survey['badge_class']) ?>" class="input" placeholder="e.g. badge-success">
                    </div>
                </div>

                <div class="field">
                    <label for="country_name">Country Name</label>
                    <input type="text" id="country_name" name="country_name" value="<?= htmlspecialchars($survey['country_name']) ?>" class="input" placeholder="e.g. United States" required>
                </div>

                <div class="actions">
                    <button type="submit" class="btn-save">Update Survey</button>
                    <a href="view_surveys.php" class="btn-cancel">Back to List</a>
                </div>
            </form>
        </div>
    </div>
</div>

<style>
@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@700&family=Plus+Jakarta+Sans:wght@400;500;600;700&display=swap');

.admin-main-content {
    padding: 100px 40px 32px;
    transition: all 0.3s ease;
    position: relative;
    z-index: 1;
}

.survey-container {
    max-width: 1000px;
    margin: 0 auto;
    background: var(--dark-700, #161a20);
    border: 1px solid rgba(255,255,255,0.04);
    border-radius: 18px;
    overflow: hidden;
    box-shadow: 0 22px 45px rgba(0,0,0,0.25);
}

.survey-hero {
    display: grid;
    grid-template-columns: 80px minmax(0, 1fr) auto;
    gap: 30px;
    align-items: center;
    padding: 28px 30px;
    background: linear-gradient(135deg, var(--dark-800, #101214), var(--dark-700, #161a20));
    border-bottom: 1px solid rgba(255,255,255,0.06);
}

.hero-icon {
    width: 80px;
    height: 80px;
    border-radius: 16px;
    background: rgba(76, 168, 245, 0.1);
    border: 1px solid rgba(76, 168, 245, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 36px;
    color: #4ca8f5;
}

.hero-kicker {
    display: inline-block;
    margin-bottom: 8px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.62rem;
    letter-spacing: 2.8px;
    text-transform: uppercase;
    color: var(--yellow-dim, #c9a010);
}

.hero-copy h1 {
    margin: 0 0 8px;
    font-family: 'Playfair Display', serif;
    font-size: 28px;
    color: var(--text-1);
}

.hero-copy p {
    margin: 0;
    color: var(--text-2);
    font-size: 13px;
}

.hero-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.meta-pill {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 6px 12px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 700;
    color: #4ca8f5;
    background: rgba(76, 168, 245, 0.1);
    border: 1px solid rgba(76, 168, 245, 0.2);
}

.meta-pill-soft {
    color: var(--text-2);
    background: rgba(255,255,255,0.03);
    border-color: rgba(255,255,255,0.06);
}

.survey-body {
    padding: 26px 30px 30px;
}

.page-alert {
    border-radius: 10px;
    padding: 12px 14px;
    font-size: 14px;
    margin-bottom: 16px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.page-alert-danger {
    color: #ffb4b2;
    border: 1px solid rgba(239,79,74,0.35);
    background: rgba(239,79,74,0.11);
}

.section-head {
    margin: 6px 0 14px;
}

.section-head h2 {
    margin: 0 0 5px;
    font-family: 'Playfair Display', serif;
    font-size: 20px;
    color: var(--text-1);
}

.section-head p {
    margin: 0;
    color: var(--text-2);
    font-size: 13px;
}

.form-grid {
    display: grid;
    gap: 16px;
}

.two-col {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.field {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 14px;
}

.field label {
    font-size: 13px;
    font-weight: 700;
    color: var(--text-2);
}

.input {
    width: 100%;
    min-height: 46px;
    border-radius: 12px;
    border: 1px solid rgba(255,255,255,0.07);
    background: var(--dark-600, #1c2028);
    color: var(--text-1);
    font-size: 14px;
    padding: 11px 14px;
    font-family: 'Manrope', sans-serif;
    transition: all 0.2s ease;
}

.input:focus {
    outline: none;
    border-color: rgba(76, 168, 245, 0.34);
    box-shadow: 0 0 0 4px rgba(76, 168, 245, 0.1);
}

.textarea {
    min-height: 120px;
    resize: vertical;
}

.actions {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    align-items: center;
    border-top: 1px solid rgba(255,255,255,0.06);
    padding-top: 20px;
    margin-top: 8px;
}

.btn-save,
.btn-cancel {
    min-height: 44px;
    padding: 10px 18px;
    border-radius: 12px;
    font-weight: 700;
    text-decoration: none;
    border: 1px solid transparent;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    cursor: pointer;
}

.btn-save {
    background: linear-gradient(135deg, #4ca8f5, #6eb9f7);
    color: var(--dark-900);
    box-shadow: 0 12px 22px rgba(76, 168, 245, 0.2);
}

.btn-save:hover {
    transform: translateY(-1px);
    box-shadow: 0 16px 26px rgba(76, 168, 245, 0.25);
}

.btn-cancel {
    background: var(--dark-600, #1c2028);
    color: var(--text-2);
    border-color: rgba(255,255,255,0.09);
}

.btn-cancel:hover {
    transform: translateY(-1px);
    color: var(--text-1);
    border-color: rgba(76, 168, 245, 0.2);
}

@media (max-width: 1080px) {
    .survey-hero {
        grid-template-columns: 1fr;
    }

    .hero-meta {
        grid-column: 1 / -1;
    }
}

@media (max-width: 900px) {
    .two-col {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 768px) {
    .admin-main-content {
        margin-left: 0;
        padding: 20px 14px;
    }

    .survey-hero,
    .survey-body {
        padding: 20px;
    }

    .survey-hero {
        grid-template-columns: 1fr;
        gap: 16px;
    }

    .hero-icon {
        width: 60px;
        height: 60px;
        font-size: 28px;
    }

    .hero-copy h1 {
        font-size: 24px;
    }

    .actions {
        flex-direction: column;
        align-items: stretch;
    }

    .btn-save,
    .btn-cancel {
        width: 100%;
    }
}

/* Day/Night Toggle Support */
:root.light-mode {
    --text-1: #0f1419;
    --text-2: #525f7f;
    --dark-600: #e5e7ef;
    --dark-700: #ececf1;
    --dark-800: #f3f4f7;
}

:root.light-mode .admin-main-content,
:root.light-mode .survey-container,
:root.light-mode .input {
    color: var(--text-1);
}

:root.light-mode .input {
    background: var(--dark-600);
    border-color: rgba(0,0,0,0.1);
}

:root.light-mode .input:focus {
    border-color: rgba(76, 168, 245, 0.5);
    box-shadow: 0 0 0 4px rgba(76, 168, 245, 0.15);
}
</style>

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