<?php require ("top.php");  
include("dbconnect.php"); // Ensure DB is connected

$query = "SELECT * FROM photos"; 
$res = mysqli_query($con, $query);
?>

<!-- Content Wrapper -->
<div class="content-wrapper" style="background: linear-gradient(135deg, #f5f7fa, #c3cfe2); padding: 20px;">
    
    <!-- Page Header -->
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-8 mx-auto">
                    <div class="card shadow-lg border-0 rounded-lg">
                        <div class="card-header bg-primary text-white">
                            <h3 class="card-title">🖼 Gallery Form</h3>
                        </div>
                        <!-- Form Start -->
                        <form action="catcodee.php" method="post" enctype="multipart/form-data">
                            <div class="card-body">
                                <div class="form-group">
                                    <label for="name">📌 Photo Name</label>
                                    <input type="text" class="form-control" id="name" name="name" placeholder="Enter photo name" required>
                                </div>

                                <div class="form-group">
                                    <label for="photo">🖼 Upload Photo</label>
                                    <input type="file" class="form-control-file" id="file" name="photo" required>
                                </div>
                            </div>
                            <div class="card-footer text-center">
                                <button type="submit" class="btn btn-success px-4 py-2">Submit</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Data Table -->
    <section class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-12">
                    <div class="card shadow-lg border-0 rounded-lg">
                        <div class="card-header bg-dark text-white">
                            <h3 class="card-title">📊 Gallery Data Table</h3>
                        </div>
                        <div class="card-body">
                            <table id="example1" class="table table-hover table-striped table-bordered">
                                <thead class="bg-secondary text-white">
                                    <tr>
                                        <th>S.N</th>
                                        <th>📌 Name</th>
                                        <th>🖼 Photo</th>
                                        <th>❌ Delete</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php 
                                    $a=1;
                                    while($row = mysqli_fetch_array($res)) { ?>
                                    <tr>
                                        <td><?php echo $a; ?></td>
                                        <td><?php echo htmlspecialchars($row['name']); ?></td>
                                        <td>
                                            <img src="<?php echo htmlspecialchars($row['photo']); ?>" 
                                                 alt="<?php echo htmlspecialchars($row['name']); ?>" 
                                                 style="width: 80px; height: 80px; border-radius: 5px;">
                                        </td>
                                        <td class="text-center">
                                            <a href="deletecatt.php?id=<?php echo $row['id']; ?>" class="btn btn-danger btn-sm" 
                                               onclick="return confirm('Are you sure you want to delete this photo?');">🗑 Delete</a>
                                        </td>
                                    </tr>
                                    <?php $a++; } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>
