<?php
include("connection.php");
include("header.php");

 
// Get category ID (optional)
$category_id = isset($_GET['cat']) ? (int)$_GET['cat'] : 0;

// Fetch category (only if cat is passed)
$category = null;
if ($category_id > 0) {
    $category = mysqli_fetch_assoc(
        mysqli_query($con, "SELECT * FROM gallery_categories WHERE id=$category_id")
    );
    if (!$category) {
        die("Gallery category not found.");
    }
}

// Delete item
if (isset($_GET['delete'])) {
    $delete_id = (int)$_GET['delete'];

    $item = mysqli_fetch_assoc(
        mysqli_query($con, "SELECT * FROM gallery_items WHERE id=$delete_id")
    );

    if ($item) {
        if ($item['type'] === 'image' && $item['file']) {
            $path = "./assets/images/gallery/" . $item['file'];
            if (file_exists($path)) unlink($path);
        }
        mysqli_query($con, "DELETE FROM gallery_items WHERE id=$delete_id");
    }

    // Redirect correctly
    if ($category_id > 0) {
        header("Location: gallery_items.php?cat=$category_id");
    } else {
        header("Location: gallery_items.php");
    }
    exit;
}

// Fetch gallery items
if ($category_id > 0) {
    // Filtered
    $items = mysqli_query(
        $con,
        "SELECT gi.*, gc.title AS category_title
         FROM gallery_items gi
         JOIN gallery_categories gc ON gc.id = gi.category_id
         WHERE gi.category_id = $category_id
         ORDER BY gi.id DESC"
    );
} else {
    // All items
    $items = mysqli_query(
        $con,
        "SELECT gi.*, gc.title AS category_title
         FROM gallery_items gi
         JOIN gallery_categories gc ON gc.id = gi.category_id
         ORDER BY gi.id DESC"
    );
}
?>

<div class="main-content">
<div class="page-content">
<div class="container-fluid">

<h4>
    Gallery Items
    <?php if ($category): ?>
        – <?php echo htmlspecialchars($category['title']); ?>
    <?php endif; ?>
</h4>

<div class="mb-3">

   
         <a href="add_gallery_item.php"
           class="btn btn-success">
            Add Gallery Item
        </a>

        <a href="gallery_categories.php" class="btn btn-secondary">
            View Categories
        </a> 

</div>

<table class="table table-bordered table-striped">
<thead>
<tr>
    <th>#</th>
    <th>Preview</th>
    <th>Title</th>
    <th>Category</th>
    <th>Type</th>
    <th>Created</th>
    <th>Action</th>
</tr>
</thead>

<tbody>
<?php if (mysqli_num_rows($items) > 0): ?>
<?php while ($row = mysqli_fetch_assoc($items)): ?>
<tr>

    <td><?php echo $row['id']; ?></td>

    <td>
        <?php if ($row['type'] === 'image' && $row['file']): ?>
            <img src="assets/images/gallery/<?php echo htmlspecialchars($row['file']); ?>"
                 style="height:60px; border-radius:6px;">
        <?php else: ?>
            <span class="badge bg-info">Video</span>
        <?php endif; ?>
    </td>

    <td><?php echo htmlspecialchars($row['title']); ?></td>

    <td><?php echo htmlspecialchars($row['category_title']); ?></td>

    <td>
        <span class="badge bg-<?php echo $row['type'] === 'image' ? 'primary' : 'warning'; ?>">
            <?php echo ucfirst($row['type']); ?>
        </span>
    </td>


    <td><?php echo date('d M Y', strtotime($row['created_at'])); ?></td>

    <td>
        <a href="edit_gallery_item.php?id=<?php echo $row['id']; ?>&cat=<?php echo $category_id; ?>"
           class="btn btn-sm btn-primary">
            Edit
        </a>

        <a href="gallery_items.php?<?php
            echo $category_id > 0
                ? 'cat='.$category_id.'&'
                : '';
        ?>delete=<?php echo $row['id']; ?>"
           class="btn btn-sm btn-danger"
           onclick="return confirm('Delete this item?');">
            Delete
        </a>
    </td>

</tr>
<?php endwhile; ?>
<?php else: ?>
<tr>
    <td colspan="8" class="text-center">No gallery items found.</td>
</tr>
<?php endif; ?>
</tbody>
</table>

</div>
</div>
</div>

<?php include("footer.php"); ?>
