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

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

// Handle Form Submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $broker_name = $_POST['broker_name'];
    $broker_image_url = $_POST['broker_image_url'];
    $title = $_POST['title'];
    $description = $_POST['description'];
    $report_date = $_POST['report_date'];
    $report_url = $_POST['report_url'];
    $type = $_POST['type'];
    $size_type = $_POST['size_type'];

    $stmt = $dbh->prepare("INSERT INTO protection_reports 
        (broker_name, broker_image_url, title, description, report_date, report_url, type, size_type)
        VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
    $stmt->execute([$broker_name, $broker_image_url, $title, $description, $report_date, $report_url, $type, $size_type]);

    header('Location: admin_reports.php');
    exit;
}

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

<style>
  /* ── Layout & Page Header ── */
  .dash { max-width: 900px; 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; }

  .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); }
  
  /* Datetime picker icon visibility trick for dark mode */
  input[type="datetime-local"]::-webkit-calendar-picker-indicator { filter: invert(1); opacity: 0.6; cursor: pointer; }

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

  /* Custom Select Dropdown */
  .form-select { appearance: none; background-image: url('data:image/svg+xml;charset=US-ASCII,<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="%238a90a6" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg>'); background-repeat: no-repeat; background-position: right 15px center; background-size: 14px; cursor: pointer; }
  .form-select option { background: var(--dark-800); color: var(--text-1); }

  /* ── 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-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">Reports Management</div>
            <div class="ph-title">Add Protection <strong>Report</strong></div>
        </div>
    </div>

    <!-- Form Panel -->
    <div class="form-panel">
        <form method="POST" action="">
            
            <h3 class="section-title"><i class="fas fa-info-circle"></i> General Details</h3>
            
            <div class="grid-2">
                <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. TrustFX" required />
                </div>
                <div class="form-group">
                    <label class="form-label" for="title">Report Title</label>
                    <input type="text" class="form-control" id="title" name="title" placeholder="e.g. User Fund Protection Review" required />
                </div>
            </div>

            <div class="grid-2">
                <div class="form-group">
                    <label class="form-label" for="report_date">Report Date & Time</label>
                    <input type="datetime-local" class="form-control" id="report_date" name="report_date" required />
                </div>
                <div class="form-group">
                    <label class="form-label" for="type">Report Type</label>
                    <input type="text" class="form-control" id="type" name="type" placeholder="e.g. Security Audit, Fund Safety" required />
                </div>
            </div>

            <div class="form-group">
                <label class="form-label" for="size_type">Size Type</label>
                <select class="form-control form-select" id="size_type" name="size_type" required>
                    <option value="" disabled selected>Select size type...</option>
                    <option value="small">Small</option>
                    <option value="long">Long</option>
                </select>
            </div>

            <h3 class="section-title"><i class="fas fa-link"></i> Media & URLs</h3>

            <div class="grid-2">
                <div class="form-group">
                    <label class="form-label" for="broker_image_url">Broker Image URL</label>
                    <input type="url" class="form-control" id="broker_image_url" name="broker_image_url" placeholder="https://example.com/logo.png" required />
                </div>
                <div class="form-group">
                    <label class="form-label" for="report_url">Report Link URL</label>
                    <input type="url" class="form-control" id="report_url" name="report_url" placeholder="https://example.com/full-report" required />
                </div>
            </div>

            <h3 class="section-title"><i class="fas fa-align-left"></i> Content</h3>

            <div class="form-group">
                <label class="form-label" for="description">Report Description</label>
                <textarea class="form-control" id="description" name="description" placeholder="Write a detailed description of the protection report..." required></textarea>
            </div>

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

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

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