<?php
require_once 'includes/auth.php';
require_once 'includes/functions.php';
require_once 'includes/upload.php';

$page_title = 'Laporan Fasilitas - ANATL Portal';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!validate_csrf($_POST['csrf_token'] ?? '')) {
        die('Invalid request');
    }

    $location = trim($_POST['location'] ?? '');
    $description = trim($_POST['description'] ?? '');
    $email = trim($_POST['email'] ?? '');

    if (!$location || !$description || !$email) {
        set_flash('error', 'Semua field harus diisi');
        header('Location: ' . BASE_URL . '/laporan.php');
        exit;
    }

    $photo_path = handle_upload('photo', 'facility');

    $stmt = $conn->prepare('INSERT INTO facility_reports (location, description, photo_path, reporter_email) VALUES (?, ?, ?, ?)');
    $stmt->bind_param('ssss', $location, $description, $photo_path, $email);

    if ($stmt->execute()) {
        set_flash('success', 'Laporan Anda berhasil dikirim. Terima kasih!');
    } else {
        set_flash('error', 'Gagal mengirim laporan. Coba lagi.');
    }
    $stmt->close();

    header('Location: ' . BASE_URL . '/laporan.php');
    exit;
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $page_title ?></title>
    <link rel="stylesheet" href="<?= BASE_URL ?>/assets/css/style.css">
</head>
<body>
<?php require 'includes/header.php'; ?>

<div class="main">
    <div class="container" style="max-width: 600px;">
        <h1 style="color: #003A70; margin-bottom: 32px;">Laporan Fasilitas Bandara</h1>

        <?php if ($flash = get_flash()): ?>
        <div class="alert alert--<?= esc($flash['type']) ?>">
            <?= esc($flash['msg']) ?>
        </div>
        <?php endif; ?>

        <div class="card">
            <p style="color: #5A6A7A; margin-bottom: 24px;">
                Kami menghargai feedback Anda tentang fasilitas bandara. Silakan laporkan masalah atau saran untuk peningkatan layanan kami.
            </p>

            <form method="POST" class="form" enctype="multipart/form-data">
                <input type="hidden" name="csrf_token" value="<?= csrf_token() ?>">

                <div class="form-group">
                    <label for="location">Lokasi Masalah</label>
                    <input type="text" id="location" name="location" placeholder="Contoh: Terminal Utama, Level 2" required>
                </div>

                <div class="form-group">
                    <label for="description">Deskripsi Masalah</label>
                    <textarea id="description" name="description" placeholder="Jelaskan masalah yang Anda temukan..." required></textarea>
                </div>

                <div class="form-group">
                    <label for="email">Email Anda</label>
                    <input type="email" id="email" name="email" placeholder="email@example.com" required>
                </div>

                <div class="form-group">
                    <label for="photo">Foto (Opsional)</label>
                    <input type="file" id="photo" name="photo" accept="image/*">
                    <small style="color: #5A6A7A;">Maksimal 5MB, format JPG/PNG</small>
                </div>

                <button type="submit" class="btn btn--primary">Kirim Laporan</button>
            </form>
        </div>
    </div>
</div>

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