<?php
require_once '../includes/functions.php';
requireLogin();
requireAdmin();

$search = $_GET['search'] ?? '';
$where = "WHERE 1=1";
$params = [];

if ($search) {
    $where .= " AND (c.registration_no LIKE ? OR u.name LIKE ?)";
    $params[] = "%$search%";
    $params[] = "%$search%";
}

// Fetch certificates with user info
$sql = "SELECT c.*, u.name as username FROM certificates c JOIN users u ON c.user_id = u.id $where ORDER BY c.created_at DESC";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$certificates = $stmt->fetchAll();

include '../includes/header.php';
?>

<div class="row">
    <div class="col-12">
        <h4 class="mb-3">Manage Certificates</h4>
        
        <form class="row g-3 mb-4" method="GET">
            <div class="col-auto">
                <input type="text" name="search" class="form-control" placeholder="Search Reg No or User" value="<?php echo htmlspecialchars($search); ?>">
            </div>
            <div class="col-auto">
                <button type="submit" class="btn btn-primary">Search</button>
            </div>
        </form>

        <div class="table-responsive">
            <table class="table table-bordered table-striped">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Registration No</th>
                        <th>User</th>
                        <th>Beneficiary Name</th>
                        <th>Issued Date</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (count($certificates) > 0): ?>
                        <?php foreach ($certificates as $c): ?>
                        <tr>
                            <td><?php echo $c['id']; ?></td>
                            <td><?php echo htmlspecialchars($c['registration_no']); ?></td>
                            <td><?php echo htmlspecialchars($c['username']); ?></td>
                            <td><?php echo htmlspecialchars($c['name']); ?></td>
                            <td><?php echo date('d-m-Y', strtotime($c['date_of_issue'])); ?></td>
                            <td>
                                <span class="badge <?php echo $c['status'] === 'active' ? 'bg-success' : 'bg-danger'; ?>">
                                    <?php echo ucfirst($c['status']); ?>
                                </span>
                            </td>
                            <td>
                                <div class="btn-group" role="group">
                                    <a href="edit_certificate.php?id=<?php echo $c['id']; ?>" class="btn btn-sm btn-warning">Edit</a>
                                    <a href="../generate_pdf.php?id=<?php echo $c['id']; ?>&action=view" target="_blank" class="btn btn-sm btn-info text-white">Print</a>
                                    <a href="../verify.php?token=<?php echo $c['token']; ?>" target="_blank" class="btn btn-sm btn-success">Verify</a>
                                    <a href="delete_certificate.php?id=<?php echo $c['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this certificate?');">Delete</a>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="7" class="text-center">No certificates found.</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include '../includes/footer.php'; ?>
