<?php
$page_title = "All Certificates";
include "includes/header.php";

// Check if admin is logged in
if(!isset($_SESSION['user_id']) || $_SESSION['user_role'] != 'admin'){
    header("Location: login.php");
    exit();
}

include "db.php";

$sql = "SELECT * FROM birth_certificate ORDER BY id DESC";
$result = mysqli_query($conn, $sql);
$total = mysqli_num_rows($result);
?>

<?php include "includes/admin_sidebar.php"; ?>

<!-- Main Content -->
<div class="main-content">
    <?php include "includes/topbar.php"; ?>

    <div class="page-content">
        <div class="card">
            <div class="card-header">
                <h3><i class="fas fa-certificate"></i> All Birth Certificates (<?php echo $total; ?>)</h3>
                <a href="add_certificate.php" class="btn btn-admin"><i class="fas fa-plus"></i> Add New</a>
            </div>
            <div class="card-body">
                <?php if($total > 0): ?>
                <div style="overflow-x: auto;">
                    <table class="data-table">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Registration No</th>
                                <th>Name</th>
                                <th>Father Name</th>
                                <th>DOB</th>
                                <th>Place</th>
                                <th>Issue Date</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $i=1; while($row = mysqli_fetch_assoc($result)):
                                $cert_name = isset($row['name']) ? $row['name'] : (isset($row['child_name']) ? $row['child_name'] : '');
                                $cert_dob = isset($row['date_of_birth']) ? $row['date_of_birth'] : (isset($row['dob']) ? $row['dob'] : '');
                                $cert_issue = isset($row['date_of_issue']) ? $row['date_of_issue'] : (isset($row['issue_date']) ? $row['issue_date'] : '');
                            ?>
                            <tr>
                                <td><?php echo $i++; ?></td>
                                <td><strong><?php echo $row['registration_no']; ?></strong></td>
                                <td><?php echo $cert_name; ?></td>
                                <td><?php echo $row['father_name']; ?></td>
                                <td><?php echo date('d-m-Y', strtotime($cert_dob)); ?></td>
                                <td><?php echo $row['place_of_birth']; ?></td>
                                <td><?php echo date('d-m-Y', strtotime($cert_issue)); ?></td>
                                <td>
                                    <a href="view_certificate.php?reg_no=<?php echo $row['registration_no']; ?>" class="btn btn-primary" style="padding: 6px 14px; font-size: 12px; margin-right: 5px;" title="View Certificate" target="_blank"><i class="fas fa-eye"></i> View</a>
                                    <a href="generate_certificate.php?reg_no=<?php echo $row['registration_no']; ?>" class="btn btn-danger" style="padding: 6px 14px; font-size: 12px; margin-right: 5px;" title="Download PDF" target="_blank"><i class="fas fa-file-pdf"></i> PDF</a>
                                    <a href="edit_certificate.php?id=<?php echo $row['id']; ?>" class="btn btn-admin" style="padding: 6px 14px; font-size: 12px;" title="Edit Certificate"><i class="fas fa-edit"></i> Edit</a>
                                </td>
                            </tr>
                            <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
                <?php else: ?>
                <div style="text-align: center; padding: 50px; color: #94a3b8;">
                    <i class="fas fa-folder-open" style="font-size: 60px; margin-bottom: 20px; color: #cbd5e1;"></i>
                    <p>No certificates found</p>
                    <a href="add_certificate.php" class="btn btn-admin" style="margin-top: 15px;"><i class="fas fa-plus"></i> Add First Certificate</a>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

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