<?php
session_start(); // Start session for login/logout

require_once 'dbconnect.php'; // $con available

// Pagination settings
$limit = 10; // 10 records per page
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $limit;

// Search
$search = isset($_GET['search']) ? $con->real_escape_string($_GET['search']) : "";

// Total records count
$total_sql = "SELECT COUNT(*) as total FROM images";
if($search != ""){
    $total_sql .= " WHERE name LIKE '%$search%'";
}
$total_result = $con->query($total_sql);
$total_row = $total_result->fetch_assoc();
$total_records = $total_row['total'];

// Calculate total pages
$total_pages = ceil($total_records / $limit);

// Fetch records with limit and search
$sql = "SELECT * FROM images";
if($search != ""){
    $sql .= " WHERE name LIKE '%$search%'";
}
$sql .= " ORDER BY id DESC LIMIT $start, $limit";
$result = $con->query($sql);
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Demo Upload Display</title>
<style>
body {
    font-family: 'Poppins', sans-serif;
    background: linear-gradient(135deg, #1e3c72, #2a5298);
    margin: 0;
    padding: 0;
    color: white;
}
.container {
    width: 92%;
    max-width: 900px;
    margin: 40px auto;
    background: rgba(255,255,255,0.12);
    padding: 20px;
    border-radius: 18px;
    backdrop-filter: blur(10px);
    box-shadow: 0 8px 25px rgba(0,0,0,0.35);
}
h2 { text-align: center; margin-bottom: 20px; }

/* Logout Button */
.logout-btn {
    display: inline-block;
    float: right;
    margin-bottom: 15px;
    padding: 7px 15px;
    background: #dc3545;
    color: white;
    text-decoration: none;
    border-radius: 6px;
    font-weight: bold;
    transition: 0.3s;
}
.logout-btn:hover {
    background: #c82333;
}

/* Search Form */
form.search-form { text-align: center; margin-bottom: 15px; }
input[type="text"] {
    padding: 7px 10px;
    border-radius: 6px;
    border: none;
    width: 250px;
    margin-right: 5px;
}
button { padding: 7px 12px; border-radius: 6px; border: none; background: #28a745; color: white; cursor: pointer; }

table { width: 100%; border-collapse: collapse; margin-top: 10px; }
th, td { padding: 14px; text-align: center; border-bottom: 1px solid rgba(255,255,255,0.3); }
th { background: rgba(0,0,0,0.35); font-weight: bold; }

img {
    width: 70px; height: 70px;
    border-radius: 10px;
    object-fit: cover;
    border: 2px solid rgba(255,255,255,0.4);
}

.btn {
    padding: 8px 12px; border-radius: 8px;
    font-size: 13px; margin: 3px;
    text-decoration: none; font-weight: bold; color: white;
    transition: 0.3s;
}
.edit-btn { background: #28a745; }
.delete-btn { background: #dc3545; }
.btn:hover { opacity: 0.8; transform: translateY(-2px); }

/* Pagination */
.pagination {
    text-align: center;
    margin-top: 15px;
}
.pagination a {
    padding: 6px 12px;
    margin: 2px;
    background: rgba(0,0,0,0.3);
    color: white;
    border-radius: 6px;
    text-decoration: none;
}
.pagination a.active { background: #28a745; }

/* Responsive */
@media (max-width: 600px) {
    table, thead, tbody, th, td, tr { display: block; }
    tr { margin-bottom: 15px; background: rgba(255,255,255,0.1);
         padding: 10px; border-radius: 12px; }
    th { display: none; }
    td { text-align: left; padding: 8px 5px; }
    img { margin-top: 6px; }
}
</style>
</head>
<body>

<div class="container">

    <!-- Logout Button -->
    <a href="dashboard.php" class="logout-btn">Logout</a>

    <h2>Uploaded Image List</h2>

    <!-- Search Form -->
    <form method="get" class="search-form">
        <input type="text" name="search" placeholder="Search by Name" value="<?= htmlspecialchars($search) ?>">
        <button type="submit">Search</button>
    </form>

    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Image</th>
                <th>Uploaded</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
        <?php while($row = $result->fetch_assoc()) { 
            $id = $row['id'];
            $name = $row['name'];
            $file = $row['filename'];
            $date = $row['uploaded_at'];
        ?>
            <tr>
                <td><?= $id ?></td>
                <td><?= htmlspecialchars($name) ?></td>
                <td><img src="uploads/<?= $file ?>" alt="<?= htmlspecialchars($name) ?>"></td>
                <td><?= $date ?></td>
                <td>
                    <a href="demouploadedit.php?id=<?= $id ?>" class="btn edit-btn">Edit</a>
                    <a href="demouploaddelete.php?id=<?= $id ?>" class="btn delete-btn" onclick="return confirm('Delete this image?')">Delete</a>
                </td>
            </tr>
        <?php } ?>
        </tbody>
    </table>

    <!-- Pagination -->
    <div class="pagination">
    <?php if($total_pages > 1){
        for($i=1; $i<=$total_pages; $i++){
            $active = ($i==$page) ? "active" : "";
            $link = "?page=$i";
            if($search != "") $link .= "&search=".urlencode($search);
            echo "<a class='$active' href='$link'>$i</a>";
        }
    } ?>
    </div>

</div>

</body>
</html>
