<?php
ob_start(); // prevent header issues

require '../includes/db.php';
include 'includes/header.php'; 
include 'includes/sidebar.php';

// DELETE
if (isset($_GET['delete'])) {

    $id = intval($_GET['delete']);

    $res = mysqli_query($conn, "SELECT file FROM payment_proofs WHERE id=$id");
    $row = mysqli_fetch_assoc($res);

    if ($row) {
        @unlink("../uploads/" . $row['file']);
        mysqli_query($conn, "DELETE FROM payment_proofs WHERE id=$id");
    }

    header("Location: proofs.php");
    exit;
}

// FETCH DATA
$result = mysqli_query($conn, "SELECT * FROM payment_proofs ORDER BY id DESC");
?>

<div class="container mt-4" style="max-width: 1050px;">

    <div class="card shadow-sm border-0">
        <div class="card-header bg-dark text-white d-flex justify-content-between align-items-center">
            <h5 class="mb-0">📁 Payment Proofs</h5>
            <span class="badge bg-light text-dark">
                <?php echo mysqli_num_rows($result); ?> Records
            </span>
        </div>

        <div class="card-body">

            <div class="table-responsive">

                <table class="table table-hover align-middle">

                    <thead class="table-dark">
                        <tr>
                            <th>#</th>
                            <th>Email</th>
                            <th>Proof</th>
                            <th>Date</th>
                            <th class="text-center">Action</th>
                        </tr>
                    </thead>

                    <tbody>

                        <?php if(mysqli_num_rows($result) > 0): ?>
                            <?php while($row = mysqli_fetch_assoc($result)): ?>

                                <tr>
                                    <td><strong>#<?php echo $row['id']; ?></strong></td>

                                    <td>
                                        <span class="badge bg-primary">
                                            <?php echo htmlspecialchars($row['email']); ?>
                                        </span>
                                    </td>

                                    <td>
                                        <a href="../uploads/<?php echo $row['file']; ?>" 
                                           target="_blank" 
                                           class="btn btn-sm btn-outline-success">
                                            👁 View
                                        </a>
                                    </td>

                                    <td>
                                        <small class="text-muted">
                                            <?php echo date("d M Y, H:i", strtotime($row['created_at'])); ?>
                                        </small>
                                    </td>

                                    <td class="text-center">
                                        <a href="?delete=<?php echo $row['id']; ?>" 
                                           class="btn btn-sm btn-danger"
                                           onclick="return confirm('Delete this proof?')">
                                            🗑 Delete
                                        </a>
                                    </td>
                                </tr>

                            <?php endwhile; ?>
                        <?php else: ?>

                            <tr>
                                <td colspan="5" class="text-center text-muted">
                                    No payment proofs uploaded yet.
                                </td>
                            </tr>

                        <?php endif; ?>

                    </tbody>

                </table>

            </div>

        </div>
    </div>

</div>