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

$user_id = $_SESSION['user_id'];
$certs = $pdo->prepare("SELECT * FROM certificates WHERE user_id = ? AND status = 'active' ORDER BY created_at DESC");
$certs->execute([$user_id]);
$my_certs = $certs->fetchAll();

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

<div class="row animate-fade-in">
    <div class="col-12">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <div>
                <h2 class="fw-bold m-0"><i class="fas fa-certificate text-primary me-2"></i> My <span class="text-primary">Certificates</span></h2>
                <p class="text-muted small mb-0">List of all your successfully generated documents.</p>
            </div>
            <a href="apply.php" class="btn btn-premium rounded-pill">
                <i class="fas fa-plus me-1"></i> New Application
            </a>
        </div>

        <?php if(isset($_GET['new'])): ?>
            <div class="alert alert-success border-0 rounded-4 shadow-sm mb-4">
                <i class="fas fa-check-circle me-2"></i> New certificate generated successfully!
            </div>
        <?php endif; ?>
        
        <div class="glass-card overflow-hidden">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="px-4 py-3 border-0 small text-uppercase text-muted fw-bold">Registration Details</th>
                            <th class="px-4 py-3 border-0 small text-uppercase text-muted fw-bold">Issue Date</th>
                            <th class="px-4 py-3 border-0 small text-uppercase text-muted fw-bold text-end">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($my_certs)): ?>
                            <tr>
                                <td colspan="3" class="text-center py-5">
                                    <i class="fas fa-folder-open fa-3x text-light mb-3"></i>
                                    <p class="text-muted">No certificates found. Apply for one today!</p>
                                </td>
                            </tr>
                        <?php endif; ?>
                        <?php foreach ($my_certs as $c): ?>
                        <tr>
                            <td class="px-4 py-3">
                                <div class="d-flex align-items-center">
                                    <div class="bg-primary bg-opacity-10 p-2 rounded-3 me-3">
                                        <i class="fas fa-file-pdf text-primary"></i>
                                    </div>
                                    <div>
                                        <div class="fw-bold text-dark"><?php echo htmlspecialchars($c['name']); ?></div>
                                        <div class="small text-muted"><?php echo htmlspecialchars($c['registration_no']); ?></div>
                                    </div>
                                </div>
                            </td>
                            <td class="px-4 py-3 text-muted">
                                <?php echo date('d M, Y', strtotime($c['date_of_issue'])); ?> <br>
                                <small><?php echo date('h:i A', strtotime($c['date_of_issue'])); ?></small>
                            </td>
                            <td class="px-4 py-3 text-end">
                                <div class="btn-group">
                                    <a href="../generate_pdf.php?id=<?php echo $c['id']; ?>&action=download" class="btn btn-sm btn-outline-primary" title="Download">
                                        <i class="fas fa-download"></i>
                                    </a>
                                    <a href="../generate_pdf.php?id=<?php echo $c['id']; ?>&action=print" target="_blank" class="btn btn-sm btn-outline-secondary" title="Print">
                                        <i class="fas fa-print"></i>
                                    </a>
                                    <a href="../verify.php?token=<?php echo $c['token']; ?>" target="_blank" class="btn btn-sm btn-outline-info" title="Preview">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

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