<?php
require_once __DIR__ . "/../../app/config/auth.php";
require_login();
require_once __DIR__ . "/../../app/config/app.php";
require_once __DIR__ . "/../../app/config/session.php";
require_once __DIR__ . "/../../app/config/koneksi.php";

// Ambil data user
$nama     = getSession('pengurus_nama') ?? 'Pengurus';
$jabatan  = getSession('pengurus_jabatan') ?? '—';
$fotoUser = getSession('pengurus_foto')
    ? base_url("/assets/img/pengurus/" . getSession('pengurus_foto'))
    : base_url("/assets/img/pengurus/user_default.png");

function esc($v) { return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); }

$currentPage = "kegiatan";

$success = "";
$err = "";

// ===================== Proses Simpan =====================
if ($_SERVER["REQUEST_METHOD"] === "POST") {

    $namaKegiatan = esc($_POST['nama_kegiatan']);
    $tanggal      = esc($_POST['tanggal']);
    $deskripsi    = esc($_POST['deskripsi']);

    if (!empty($namaKegiatan) && !empty($tanggal) && !empty($deskripsi)) {

        $folder = __DIR__ . "/../../assets/img/kegiatan/";
        if (!file_exists($folder)) mkdir($folder, 0777, true);

        $fotoName = null;

        if (!empty($_FILES['foto']['name'])) {
            $ext = strtolower(pathinfo($_FILES['foto']['name'], PATHINFO_EXTENSION));
            $fotoName = time() . "-" . rand(100,999) . "." . $ext;
            move_uploaded_file($_FILES['foto']['tmp_name'], $folder . $fotoName);
        }

        $sql = $koneksi->prepare("
            INSERT INTO kegiatan (nama_kegiatan, tanggal, deskripsi, foto, status)
            VALUES (?, ?, ?, ?, 1)
        ");
        $sql->bind_param("ssss", $namaKegiatan, $tanggal, $deskripsi, $fotoName);
        
        if ($sql->execute()) {
            $success = "Agenda berhasil ditambahkan 🎉";
        } else {
            $err = "Gagal menyimpan data!";
        }

    } else {
        $err = "Semua field wajib diisi!";
    }
}
?>
<!doctype html>
<html lang="id">
<head>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width,initial-scale=1">
   <title>Tambah Agenda — Sistem Informasi Masjid</title>

   <!-- Bootstrap + FontAwesome -->
   <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

   <!-- Dashboard Styles -->
   <link rel="stylesheet" href="<?= base_url('/assets/css/dashboard/sidebar.css'); ?>">
   <link rel="stylesheet" href="<?= base_url('/assets/css/dashboard/layout.css'); ?>">
   <link rel="stylesheet" href="<?= base_url('/assets/css/dashboard/dashboard.css'); ?>">
   <link rel="stylesheet" href="<?= base_url('/assets/css/dashboard/widgets.css'); ?>">
</head>

<body class="dashboard-page">

<!-- SIDEBAR -->
<?php include __DIR__ . "/../../assets/component/dashboard_sidebar.php"; ?>

<!-- CONTENT WRAPPER -->
<div id="content-wrapper" class="content-wrapper">

    <!-- NAVBAR -->
    <?php include __DIR__ . "/../../assets/component/dashboard_navbar.php"; ?>

    <main class="container-fluid mt-4">
        <h1 class="page-title">Tambah Agenda</h1>
        <p class="subtitle small text-muted">Mengelola agenda masjid — <?= date('d M Y'); ?></p>

        <!-- FORM -->
        <div class="card shadow-sm mt-4">
            <div class="card-header bg-success text-white fw-bold">
                Form Tambah Agenda Masjid
            </div>

            <div class="card-body">

                <?php if (!empty($success)): ?>
                    <div class="alert alert-success"><?= esc($success) ?></div>
                <?php endif; ?>

                <?php if (!empty($err)): ?>
                    <div class="alert alert-danger"><?= esc($err) ?></div>
                <?php endif; ?>

                <form method="POST" enctype="multipart/form-data">

                    <div class="mb-3">
                        <label class="form-label fw-bold">Nama Kegiatan</label>
                        <input type="text" class="form-control" name="nama_kegiatan" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label fw-bold">Tanggal Kegiatan</label>
                        <input type="date" class="form-control" name="tanggal" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label fw-bold">Deskripsi</label>
                        <textarea class="form-control" name="deskripsi" rows="3" required></textarea>
                    </div>

                    <div class="mb-3">
                        <label class="form-label fw-bold">Foto Kegiatan</label>
                        <input type="file" class="form-control" name="foto" accept="image/*" required>
                        <small class="text-muted">Maks size 2MB | Format: JPG/PNG</small>
                    </div>

                    <button type="submit" class="btn btn-success px-4"><i class="fa fa-save"></i> Simpan</button>
                    <a href="semua_kegiatan.php" class="btn btn-secondary"><i class="fa fa-arrow-left"></i> Kembali</a>

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

</div>

<!-- FOOTER -->
<?php include __DIR__ . "/../../assets/component/dashboard_footer.php"; ?>

</body>
</html>
