<?php
session_start();
include "../config/koneksi.php";

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

// UPLOAD
if(isset($_POST['upload'])){
    $nama = $_FILES['foto']['name'];
    $tmp  = $_FILES['foto']['tmp_name'];
    $ext  = strtolower(pathinfo($nama, PATHINFO_EXTENSION));

    $allowed = ['jpg','jpeg','png'];

    if(in_array($ext, $allowed)){
        move_uploaded_file($tmp, "../assets/galeri/".$nama);
        mysqli_query($conn,"INSERT INTO galeri (nama_file,keterangan) 
        VALUES ('$nama','$_POST[keterangan]')");
    }
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Kelola Galeri</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>
<div class="container mt-5">

<h3>Kelola Galeri</h3>

<!-- FORM -->
<form method="POST" enctype="multipart/form-data" class="mb-4">
<input type="file" name="foto" class="form-control mb-2" required>
<input type="text" name="keterangan" class="form-control mb-2" placeholder="Keterangan">
<button name="upload" class="btn btn-primary">Upload</button>
</form>

<!-- DATA -->
<div class="row">

<?php
$data = mysqli_query($conn,"SELECT * FROM galeri ORDER BY id DESC");
while($d = mysqli_fetch_array($data)){
?>

<div class="col-md-3">
<div class="card shadow mb-4">
    <img src="../assets/galeri/<?= $d['nama_file'] ?>" class="card-img-top">
    <div class="card-body">
        <small><?= $d['keterangan'] ?></small><br><br>
        <a href="hapus_galeri.php?id=<?= $d['id'] ?>" 
           class="btn btn-danger btn-sm">Hapus</a>
    </div>
</div>
</div>

<?php } ?>

</div>

</div>
</body>
</html>