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

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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $stmt = $dbh->prepare('INSERT INTO surveys (broker_name, broker_logo, survey_title, survey_description, badge, badge_class, image_url, country_name, country_flag_url, survey_date, detail_url) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
    $stmt->execute([
        $_POST['broker_name'],
        $_POST['broker_logo'],
        $_POST['survey_title'],
        $_POST['survey_description'],
        $_POST['badge'],
        $_POST['badge_class'],
        $_POST['image_url'],
        $_POST['country_name'],
        $_POST['country_flag_url'],
        $_POST['survey_date'],
        $_POST['detail_url']
    ]);
    header('Location: view_surveys.php');
    exit;
}

// Include Premium Admin Header & Sidebar
include "includes/header.php";
include "includes/sidebar.php";
?>

<style>
  /* ── Layout & Page Header ── */
  .dash { max-width: 1000px; margin: 0 auto; padding: 30px 26px; z-index: 1; position: relative; }
  
  .page-header { display: flex; align-items: flex-end; justify-content: space-between; margin-bottom: 28px; flex-wrap: wrap; gap: 14px; }
  .ph-eyebrow { font-family: 'JetBrains Mono', monospace; font-size: 0.6rem; letter-spacing: 3.5px; text-transform: uppercase; color: var(--yellow-dim); margin-bottom: 6px; display: flex; align-items: center; gap: 7px; }
  .ph-eyebrow::before { content: ''; display: inline-block; width: 20px; height: 2px; background: var(--yellow); border-radius: 2px; }
  .ph-title { font-family: 'Syne', sans-serif; font-size: 2rem; font-weight: 800; color: var(--text-1); letter-spacing: -0.5px; line-height: 1.1; }

  /* ── Form Panel ── */
  .form-panel { background: var(--dark-700); border: 1px solid rgba(255,255,255,0.05); border-radius: 14px; padding: 30px; }
  
  .section-title { font-family: 'Syne', sans-serif; font-size: 1.1rem; font-weight: 700; color: var(--text-1); border-bottom: 1px solid rgba(255,255,255,0.05); padding-bottom: 10px; margin-bottom: 20px; margin-top: 30px; }
  .section-title:first-child { margin-top: 0; }
  .section-title i { color: var(--yellow); margin-right: 8px; font-size: 1rem; }

  .grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
  .grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 20px; }

  .form-group { margin-bottom: 18px; }
  .form-label { display: block; font-family: 'JetBrains Mono', monospace; font-size: 0.7rem; text-transform: uppercase; letter-spacing: 1px; color: var(--text-2); margin-bottom: 8px; font-weight: 600; }
  
  .form-control { width: 100%; padding: 12px 16px; background: var(--dark-800); border: 1px solid rgba(255,255,255,0.08); border-radius: 8px; color: var(--text-1); font-size: 0.9rem; transition: 0.3s; font-family: 'Manrope', sans-serif; }
  .form-control:focus { outline: none; border-color: var(--yellow); box-shadow: 0 0 0 3px var(--yellow-glass); }
  .form-control::placeholder { color: rgba(255,255,255,0.2); }
  
  /* Date picker icon visibility trick for dark mode */
  input[type="date"]::-webkit-calendar-picker-indicator { filter: invert(1); opacity: 0.6; cursor: pointer; }

  textarea.form-control { min-height: 100px; resize: vertical; }

  /* ── Action Buttons ── */
  .form-actions { display: flex; gap: 15px; margin-top: 30px; padding-top: 20px; border-top: 1px solid rgba(255,255,255,0.05); }
  .btn-submit { background: var(--yellow); color: var(--dark-900); padding: 12px 28px; border: none; border-radius: 8px; font-weight: 700; font-size: 0.95rem; cursor: pointer; transition: 0.2s; display: flex; align-items: center; gap: 8px; }
  .btn-submit:hover { background: var(--yellow-bright); transform: translateY(-2px); box-shadow: 0 4px 12px rgba(245,197,24,0.3); }
  
  .btn-cancel { background: rgba(255,255,255,0.05); color: var(--text-1); padding: 12px 28px; border: 1px solid rgba(255,255,255,0.1); border-radius: 8px; font-weight: 600; font-size: 0.95rem; cursor: pointer; text-decoration: none; transition: 0.2s; display: flex; align-items: center; gap: 8px; }
  .btn-cancel:hover { background: rgba(255,255,255,0.1); color: #fff; }

  @media (max-width: 768px) {
      .grid-2, .grid-3 { grid-template-columns: 1fr; gap: 10px; }
      .form-actions { flex-direction: column; }
      .btn-submit, .btn-cancel { width: 100%; justify-content: center; }
  }
</style>

<main class="dash">

    <!-- Page Header -->
    <div class="page-header">
        <div>
            <div class="ph-eyebrow">User Feedback</div>
            <div class="ph-title">Add New <strong>Survey</strong></div>
        </div>
    </div>

    <!-- Form Panel -->
    <div class="form-panel">
        <form method="post" action="">

            <!-- Section 1: Basic Information -->
            <h3 class="section-title"><i class="fas fa-info-circle"></i> General Information</h3>
            
            <div class="grid-3">
                <div class="form-group">
                    <label class="form-label" for="survey_title">Survey Title</label>
                    <input type="text" class="form-control" id="survey_title" name="survey_title" placeholder="Enter survey title..." required>
                </div>
                <div class="form-group">
                    <label class="form-label" for="broker_name">Broker Name</label>
                    <input type="text" class="form-control" id="broker_name" name="broker_name" placeholder="e.g. FOREX.com" required>
                </div>
                <div class="form-group">
                    <label class="form-label" for="survey_date">Survey Date</label>
                    <input type="date" class="form-control" id="survey_date" name="survey_date" required>
                </div>
            </div>

            <div class="form-group">
                <label class="form-label" for="survey_description">Survey Description</label>
                <textarea class="form-control" id="survey_description" name="survey_description" placeholder="Write a brief description of the survey..."></textarea>
            </div>

            <!-- Section 2: Classification -->
            <h3 class="section-title"><i class="fas fa-tags"></i> Classification & Badges</h3>
            
            <div class="grid-3">
                <div class="form-group">
                    <label class="form-label" for="country_name">Country Name</label>
                    <input type="text" class="form-control" id="country_name" name="country_name" placeholder="e.g. United Kingdom">
                </div>
                <div class="form-group">
                    <label class="form-label" for="badge">Badge Text</label>
                    <input type="text" class="form-control" id="badge" name="badge" placeholder="e.g. Top Rated">
                </div>
                <div class="form-group">
                    <label class="form-label" for="badge_class">Badge CSS Class</label>
                    <input type="text" class="form-control" id="badge_class" name="badge_class" placeholder="e.g. badge-success">
                </div>
            </div>

            <!-- Section 3: Media & URLs -->
            <h3 class="section-title"><i class="fas fa-link"></i> Media & Links</h3>
            
            <div class="grid-2">
                <div class="form-group">
                    <label class="form-label" for="broker_logo">Broker Logo URL</label>
                    <input type="text" class="form-control" id="broker_logo" name="broker_logo" placeholder="https://example.com/logo.png">
                </div>
                <div class="form-group">
                    <label class="form-label" for="country_flag_url">Country Flag URL</label>
                    <input type="text" class="form-control" id="country_flag_url" name="country_flag_url" placeholder="https://example.com/flag.png">
                </div>
            </div>

            <div class="grid-2">
                <div class="form-group">
                    <label class="form-label" for="image_url">Main Image URL</label>
                    <input type="text" class="form-control" id="image_url" name="image_url" placeholder="https://example.com/main-image.jpg">
                </div>
                <div class="form-group">
                    <label class="form-label" for="detail_url">Detail Page URL</label>
                    <input type="text" class="form-control" id="detail_url" name="detail_url" placeholder="https://example.com/survey-details">
                </div>
            </div>

            <!-- Action Buttons -->
            <div class="form-actions">
                <button type="submit" class="btn-submit">
                    <i class="fas fa-save"></i> Add Survey
                </button>
                <a href="view_surveys.php" class="btn-cancel">
                    <i class="fas fa-times"></i> Cancel
                </a>
            </div>

        </form>
    </div>
</main>

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