<?php
require_once __DIR__ . '/../Logic/Backend/Authentication_Handler.php';
require_once __DIR__ . '/../db/config.php';

$Auth = new Authentication_Handler();
$Auth->Check_Auth();

if ($_SESSION['Access_Level'] < 1) {
    header('Location: Voting_Screen.php');
    exit;
}

$db = db();

// Filters
$search = $_GET['search'] ?? '';
$filterTrack = $_GET['track'] ?? '';
$filterGrade = $_GET['grade'] ?? '';
$filterSection = $_GET['section'] ?? '';

// Fetch Stats
$totalVoters = $db->query("SELECT COUNT(*) FROM Voters")->fetchColumn();
$votedCount = $db->query("SELECT COUNT(*) FROM Voters WHERE Has_Voted = 1")->fetchColumn();
$notVotedCount = $totalVoters - $votedCount;

// Fetch Breakdowns
$tracks = $db->query("SELECT Track_Cluster as label, COUNT(*) as value FROM Voters GROUP BY Track_Cluster")->fetchAll();
$grades = $db->query("SELECT Grade_Level as label, COUNT(*) as value FROM Voters GROUP BY Grade_Level")->fetchAll();
$sections = $db->query("SELECT Section as label, COUNT(*) as value FROM Voters GROUP BY Section")->fetchAll();

// Build Query for List
$queryStr = "SELECT * FROM Voters WHERE 1=1";
$params = [];

if ($search) {
    $queryStr .= " AND Email LIKE ?";
    $params[] = "%$search%";
}
if ($filterTrack) {
    $queryStr .= " AND Track_Cluster = ?";
    $params[] = $filterTrack;
}
if ($filterGrade) {
    $queryStr .= " AND Grade_Level = ?";
    $params[] = $filterGrade;
}
if ($filterSection) {
    $queryStr .= " AND Section = ?";
    $params[] = $filterSection;
}

$queryStr .= " ORDER BY Email ASC";
$stmt = $db->prepare($queryStr);
$stmt->execute($params);
$votersList = $stmt->fetchAll();

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Voter Management | Online School Election System</title>
    <link rel="stylesheet" href="../Design/Style.css?v=<?php echo time(); ?>">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body>
    <div class="Dashboard_Container">
        <!-- Sidebar -->
        <aside class="Sidebar">
            <div class="Sidebar_Header">
                <div class="Logo_Text">
                    <div class="Logo_Title" style="font-size: 1.1rem; color: var(--Primary_Color);">ElectionSystem</div>
                    <div class="Logo_Subtitle">Administrator Portal</div>
                </div>
            </div>
            <nav class="Sidebar_Nav">
                <a href="Election_Dashboard.php" class="Nav_Item">
                    <i class="fas fa-th-large"></i> Election Dashboard
                </a>
                <a href="Election_History.php" class="Nav_Item">
                    <i class="fas fa-vote-yea"></i> Election History
                </a>
                <a href="Voters.php" class="Nav_Item Active">
                    <i class="fas fa-users"></i> Voter Management
                </a>
                <a href="Candidates.html" class="Nav_Item">
                    <i class="fas fa-user-tie"></i> Candidate Management
                </a>
                <a href="Officers.html" class="Nav_Item">
                    <i class="fas fa-user-shield"></i> Officers Management
                </a>
                <a href="Audit_Trail.html" class="Nav_Item">
                    <i class="fas fa-file-alt"></i> Reports & Audit
                </a>
            </nav>
            <div style="padding: 24px; border-top: 1px solid var(--Border_Color);">
                <a href="Logout.php" class="Nav_Item" style="padding: 0; color: var(--Error_Color);">
                    <i class="fas fa-sign-out-alt"></i> Logout
                </a>
            </div>
        </aside>

        <!-- Main Content -->
        <main class="Main_Content">
            <!-- Top Bar -->
            <header class="Top_Bar">
                <div class="Search_Box">
                    <i class="fas fa-search" style="color: var(--Secondary_Color);"></i>
                    <input type="text" placeholder="Quick search...">
                </div>
                <div class="Top_Bar_Actions">
                    <div style="display: flex; align-items: center; gap: 12px;">
                        <div style="text-align: right;">
                            <div style="font-weight: 700; font-size: 0.9rem;"><?php echo htmlspecialchars($_SESSION['User_Name']); ?></div>
                            <div style="font-size: 0.75rem; color: var(--Text_Secondary);"><?php echo htmlspecialchars($_SESSION['User_Role']); ?></div>
                        </div>
                        <div style="width: 40px; height: 40px; background: var(--Primary_Light); color: var(--Primary_Color); border-radius: 50%; display: flex; justify-content: center; align-items: center; font-weight: 700;">
                            <?php echo strtoupper(substr($_SESSION['User_Name'] ?? 'A', 0, 1)); ?>
                        </div>
                    </div>
                </div>
            </header>

            <!-- Content Body -->
            <div class="Content_Body">
                <div class="Page_Title_Section">
                    <div style="display: flex; align-items: center; gap: 15px;">
                        <div style="width: 48px; height: 48px; background: var(--Primary_Light); color: var(--Primary_Color); border-radius: 12px; display: flex; justify-content: center; align-items: center; font-size: 1.5rem;">
                            <i class="fas fa-user-check"></i>
                        </div>
                        <div>
                            <h1 class="Page_Title">Voters List</h1>
                            <p class="Page_Subtitle">View and manage registered voters</p>
                        </div>
                    </div>
                </div>

                <!-- Primary Stats -->
                <div class="Stat_Cards_Grid" style="margin-bottom: 20px;">
                    <div class="Stat_Card">
                        <div class="Stat_Label" style="text-transform: uppercase; font-size: 0.7rem; font-weight: 700;">Total Voters</div>
                        <div class="Stat_Value" style="color: var(--Primary_Color); font-size: 2.5rem; margin-top: 10px;"><?php echo $totalVoters; ?></div>
                    </div>
                    <div class="Stat_Card">
                        <div class="Stat_Label" style="text-transform: uppercase; font-size: 0.7rem; font-weight: 700; color: var(--Secondary_Color);">Voters Who Voted</div>
                        <div class="Stat_Value" style="color: var(--Secondary_Color); font-size: 2.5rem; margin-top: 10px;"><?php echo $votedCount; ?></div>
                    </div>
                    <div class="Stat_Card">
                        <div class="Stat_Label" style="text-transform: uppercase; font-size: 0.7rem; font-weight: 700; color: var(--Error_Color);">Voters Who Haven't Voted</div>
                        <div class="Stat_Value" style="color: var(--Error_Color); font-size: 2.5rem; margin-top: 10px;"><?php echo $notVotedCount; ?></div>
                    </div>
                </div>

                <!-- Breakdown Stats -->
                <div class="Summary_Breakdowns">
                    <div class="Breakdown_Card">
                        <h3 class="Breakdown_Title">Total by Track</h3>
                        <?php foreach ($tracks as $track): ?>
                        <div class="Breakdown_Item">
                            <span><?php echo htmlspecialchars($track['label'] ?: 'N/A'); ?></span>
                            <span style="font-weight: 700;"><?php echo $track['value']; ?></span>
                        </div>
                        <?php endforeach; ?>
                    </div>
                    <div class="Breakdown_Card">
                        <h3 class="Breakdown_Title">Total by Grade</h3>
                        <?php foreach ($grades as $grade): ?>
                        <div class="Breakdown_Item">
                            <span>Grade <?php echo htmlspecialchars($grade['label'] ?: 'N/A'); ?></span>
                            <span style="font-weight: 700;"><?php echo $grade['value']; ?></span>
                        </div>
                        <?php endforeach; ?>
                    </div>
                    <div class="Breakdown_Card">
                        <h3 class="Breakdown_Title">Total by Section</h3>
                        <?php foreach (array_slice($sections, 0, 5) as $section): ?>
                        <div class="Breakdown_Item">
                            <span><?php echo htmlspecialchars($section['label'] ?: 'N/A'); ?></span>
                            <span style="font-weight: 700;"><?php echo $section['value']; ?></span>
                        </div>
                        <?php endforeach; ?>
                        <?php if (count($sections) > 5): ?>
                            <div style="font-size: 0.75rem; color: var(--Text_Secondary); text-align: center; margin-top: 10px;">+ <?php echo count($sections) - 5; ?> more sections</div>
                        <?php endif; ?>
                    </div>
                </div>

                <!-- Actions -->
                <div style="display: flex; justify-content: flex-end; gap: 12px; margin-bottom: 20px;">
                    <button class="Button_Primary" style="width: auto; padding: 10px 20px;">
                        <i class="fas fa-plus"></i> Add Voter
                    </button>
                    <button class="Button_Secondary" style="width: auto; padding: 10px 20px;">
                        <i class="fas fa-file-import"></i> Import CSV
                    </button>
                </div>

                <!-- Filters -->
                <form method="GET" class="Filter_Bar">
                    <div class="Filter_Group">
                        <label>Search</label>
                        <div class="Search_Input_Wrapper">
                            <i class="fas fa-search"></i>
                            <input type="text" name="search" class="Input" placeholder="Search by email" value="<?php echo htmlspecialchars($search); ?>">
                        </div>
                    </div>
                    <div class="Filter_Group">
                        <label>Track</label>
                        <select name="track" class="Select" onchange="this.form.submit()">
                            <option value="">All Tracks</option>
                            <?php 
                            $allTracks = $db->query("SELECT DISTINCT Track_Cluster FROM Voters WHERE Track_Cluster IS NOT NULL")->fetchAll(PDO::FETCH_COLUMN);
                            foreach ($allTracks as $t): ?>
                                <option value="<?php echo htmlspecialchars($t); ?>" <?php echo $filterTrack == $t ? 'selected' : ''; ?>><?php echo htmlspecialchars($t); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="Filter_Group">
                        <label>Grade</label>
                        <select name="grade" class="Select" onchange="this.form.submit()">
                            <option value="">All Grades</option>
                            <?php 
                            $allGrades = $db->query("SELECT DISTINCT Grade_Level FROM Voters WHERE Grade_Level IS NOT NULL ORDER BY Grade_Level")->fetchAll(PDO::FETCH_COLUMN);
                            foreach ($allGrades as $g): ?>
                                <option value="<?php echo htmlspecialchars($g); ?>" <?php echo $filterGrade == $g ? 'selected' : ''; ?>>Grade <?php echo htmlspecialchars($g); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="Filter_Group">
                        <label>Section</label>
                        <select name="section" class="Select" onchange="this.form.submit()">
                            <option value="">All Sections</option>
                            <?php 
                            $allSections = $db->query("SELECT DISTINCT Section FROM Voters WHERE Section IS NOT NULL ORDER BY Section")->fetchAll(PDO::FETCH_COLUMN);
                            foreach ($allSections as $s): ?>
                                <option value="<?php echo htmlspecialchars($s); ?>" <?php echo $filterSection == $s ? 'selected' : ''; ?>><?php echo htmlspecialchars($s); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </form>

                <!-- Table -->
                <div class="Card" style="padding: 0;">
                    <div class="Table_Wrapper">
                        <table>
                            <thead>
                                <tr>
                                    <th>Email</th>
                                    <th>Track</th>
                                    <th>Grade</th>
                                    <th>Section</th>
                                    <th>Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if (empty($votersList)): ?>
                                <tr>
                                    <td colspan="5" style="text-align: center; padding: 40px; color: var(--Text_Secondary);">
                                        No voters found matching your criteria.
                                    </td>
                                </tr>
                                <?php else: ?>
                                    <?php foreach ($votersList as $voter): ?>
                                    <tr>
                                        <td style="font-weight: 600;"><?php echo htmlspecialchars($voter['Email']); ?></td>
                                        <td><?php echo htmlspecialchars($voter['Track_Cluster'] ?: 'N/A'); ?></td>
                                        <td>Grade <?php echo htmlspecialchars($voter['Grade_Level'] ?: 'N/A'); ?></td>
                                        <td><?php echo htmlspecialchars($voter['Section'] ?: 'N/A'); ?></td>
                                        <td>
                                            <?php if ($voter['Has_Voted']): ?>
                                                <span class="Badge Badge_Success">Voted</span>
                                            <?php else: ?>
                                                <span class="Badge" style="background: var(--Primary_Light); color: var(--Primary_Color);">Pending</span>
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                    <div style="padding: 15px 24px; font-size: 0.85rem; color: var(--Text_Secondary); border-top: 1px solid var(--Border_Color);">
                        Showing <?php echo count($votersList); ?> of <?php echo $totalVoters; ?> voters
                    </div>
                </div>
            </div>
        </main>
    </div>
</body>
</html>