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

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

if (!isset($_GET['id'])) {
    header('Location: admin_reports.php');
    exit;
}

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

// Fetch existing data
$stmt = $dbh->prepare("SELECT * FROM protection_reports WHERE id = ?");
$stmt->execute([$id]);
$report = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$report) {
    $pageError = 'Report not found.';
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && !$pageError) {
    $broker_name = trim($_POST['broker_name'] ?? '');
    $broker_image_url = trim($_POST['broker_image_url'] ?? '');
    $title = trim($_POST['title'] ?? '');
    $description = trim($_POST['description'] ?? '');
    $report_date = $_POST['report_date'] ?? '';
    $report_url = trim($_POST['report_url'] ?? '');
    $type = trim($_POST['type'] ?? '');
    $size_type = trim($_POST['size_type'] ?? '');

    $stmt = $dbh->prepare("UPDATE protection_reports SET
        broker_name = ?, broker_image_url = ?, title = ?, description = ?, report_date = ?, report_url = ?, type = ?, size_type = ?
        WHERE id = ?");
    
    if ($stmt->execute([$broker_name, $broker_image_url, $title, $description, $report_date, $report_url, $type, $size_type, $id])) {
        header('Location: admin_reports.php?updated=1');
        exit;
    } else {
        $pageError = 'Failed to update report.';
    }
}

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

<div class="admin-main-content">
    <div class="report-container">
        <div class="report-hero">
            <div class="hero-icon">
                <i class="fas fa-shield-alt"></i>
            </div>

            <div class="hero-copy">
                <span class="hero-kicker">Protection Reports</span>
                <h1>Edit Report</h1>
                <p>Update report details, broker information, and publication settings.</p>
            </div>

            <div class="hero-meta">
                <span class="meta-pill">Report ID: #<?= (int)$report['id'] ?></span>
                <span class="meta-pill meta-pill-soft"><?= htmlentities($report['type'] ?? 'Uncategorized') ?></span>
            </div>
        </div>

        <div class="report-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="report-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($report['broker_name']) ?>" class="input" required>
                    </div>

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

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

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

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

                <div class="form-grid two-col">
                    <div class="field">
                        <label for="report_date">Report Date</label>
                        <input type="datetime-local" id="report_date" name="report_date" value="<?= date('Y-m-d\TH:i', strtotime($report['report_date'])) ?>" class="input" required>
                    </div>

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

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

                <div class="form-grid two-col">
                    <div class="field">
                        <label for="type">Type</label>
                        <input type="text" id="type" name="type" value="<?= htmlspecialchars($report['type']) ?>" class="input" placeholder="e.g. Warning, Update" required>
                    </div>

                    <div class="field">
                        <label for="size_type">Display Size</label>
                        <select id="size_type" name="size_type" class="input" required>
                            <option value="">Select size</option>
                            <option value="small" <?= $report['size_type'] === 'small' ? 'selected' : '' ?>>Small</option>
                            <option value="long" <?= $report['size_type'] === 'long' ? 'selected' : '' ?>>Long</option>
                        </select>
                    </div>
                </div>

                <div class="actions">
                    <button type="submit" class="btn-save">Update Report</button>
                    <a href="admin_reports.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;
}

.report-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);
}

.report-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(62, 203, 119, 0.1);
    border: 1px solid rgba(62, 203, 119, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 36px;
    color: var(--green);
}

.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: var(--green);
    background: rgba(62, 203, 119, 0.1);
    border: 1px solid rgba(62, 203, 119, 0.2);
}

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

.report-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(62, 203, 119, 0.34);
    box-shadow: 0 0 0 4px rgba(62, 203, 119, 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, var(--green), #4fd890);
    color: var(--dark-900);
    box-shadow: 0 12px 22px rgba(62, 203, 119, 0.2);
}

.btn-save:hover {
    transform: translateY(-1px);
    box-shadow: 0 16px 26px rgba(62, 203, 119, 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(62, 203, 119, 0.2);
}

@media (max-width: 1080px) {
    .report-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;
    }

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

    .report-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 .report-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(62, 203, 119, 0.5);
    box-shadow: 0 0 0 4px rgba(62, 203, 119, 0.15);
}
</style>

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