<?php
require_once 'db_admin.php';
checkLogin();

// Fetch Centers with Voter Count
// We add a subquery to count voters for each center efficiently
$stmt = $conn->query("
    SELECT vc.*, 
    (SELECT COUNT(id) FROM voters WHERE Center_Id = vc.id) as total_voters 
    FROM vote_centers vc 
    ORDER BY ward ASC, area_name ASC
");
$centers = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
    <title>Center List</title>
    <style>
        body { font-family: 'Arial', sans-serif; padding: 20px; }
        table { width: 100%; border-collapse: collapse; background: white; }
        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
        th { background-color: #006a4e; color: white; }
        tr:nth-child(even) { background-color: #f2f2f2; }
        .btn { padding: 5px 10px; background: #d9534f; color: white; text-decoration: none; border-radius: 4px; }
        .header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
    </style>
</head>
<body>
    <div class="header">
        <h2>Vote Centers List</h2>
        <a href="logout.php" style="color:red">Logout</a>
    </div>

    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Ward & Area</th>
                <th>Center Name</th>
                <th>PDFs</th>
            </tr>
        </thead>
        <tbody>
        <tbody>
            <?php foreach($centers as $c): ?>
            <tr>
                <td><?php echo $c['id']; ?></td>
                <td><?php echo $c['ward']; ?><br><?php echo $c['area_name']; ?></td>
                <td>
                    <?php echo $c['center_name']; ?> (<?php echo $c['gender']; ?>)
                    <br>
                    <small style="color:gray;">(Total: <?php echo $c['total_voters']; ?>)</small>
                </td>
                <td>
                    <?php 
                    $limit = 2000; // Voters per PDF
                    
                    if ($c['total_voters'] > 0) {
                        // Calculate how many parts are needed
                        $parts = ceil($c['total_voters'] / $limit);
                        
                        for ($i = 1; $i <= $parts; $i++) {
                            $offset = ($i - 1) * $limit;
                            
                            // Style logic for buttons
                            $btnStyle = "display:inline-block; padding:3px 8px; margin:2px; background:#006a4e; color:white; text-decoration:none; border-radius:3px; font-size:12px;";
                            
                            echo "<a href='generate_pdf.php?center_id={$c['id']}&offset={$offset}&limit={$limit}' target='_blank' style='{$btnStyle}'>Part {$i}</a> ";
                        }
                    } else {
                        echo "<span style='color:red; font-size:12px;'>No Voters Found</span>";
                    }
                    ?>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</body>
</html>