<?php
declare(strict_types=1);
require_once __DIR__ . '/auth_helper.php';
require_login();
require_role(['Admin', 'Adviser', 'Officer']);

$position_id = $_GET['position_id'] ?? '';
if (!$position_id) die("Position ID required");

$pdo = db();
$pStmt = $pdo->prepare("SELECT p.*, e.title as election_title, e.id as election_id FROM positions p JOIN elections e ON p.election_id = e.id WHERE p.id = ?");
$pStmt->execute([$position_id]);
$position = $pStmt->fetch();

if (!$position) die("Position not found");

$candidates = $pdo->prepare("SELECT c.*, u.name, u.student_id FROM candidates c JOIN users u ON c.user_id = u.id WHERE c.position_id = ?");
$candidates->execute([$position_id]);
$candidates = $candidates->fetchAll();

// Get all users who could be candidates (could be improved with search)
$users = $pdo->query("SELECT id, name, student_id FROM users WHERE role = 'Voter' LIMIT 100")->fetchAll();
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Manage Candidates | <?= htmlspecialchars($position['name']) ?></title>
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="assets/css/style.css?v=<?= time() ?>">
</head>
<body>
  <nav class="navbar">
    <a href="index.php" class="brand">E-Vote Pro</a>
    <div>
      <a href="view_election.php?id=<?= $position['election_id'] ?>" class="btn btn-outline">Back to Election</a>
    </div>
  </nav>

  <div class="container">
    <div class="header-actions">
        <div>
            <h1>Candidates for <?= htmlspecialchars($position['name']) ?></h1>
            <p><?= htmlspecialchars($position['election_title']) ?></p>
        </div>
    </div>

    <div style="display: grid; grid-template-columns: 1fr 2fr; gap: 1.5rem;">
        <div class="card">
            <h3>Add Candidate</h3>
            <form action="api/add_candidate.php" method="POST">
                <input type="hidden" name="position_id" value="<?= $position_id ?>">
                <input type="hidden" name="election_id" value="<?= $position['election_id'] ?>">
                <div class="form-group">
                    <label class="form-label">Select User (Student)</label>
                    <select name="user_id" class="form-control" required>
                        <option value="">-- Choose Student --</option>
                        <?php foreach ($users as $u): ?>
                            <option value="<?= $u['id'] ?>"><?= htmlspecialchars($u['name']) ?> (<?= $u['student_id'] ?>)</option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">Party Name</label>
                    <input type="text" name="party_name" class="form-control" placeholder="e.g. Independent">
                </div>
                <div class="form-group">
                    <label class="form-label">Manifesto</label>
                    <textarea name="manifesto" class="form-control" rows="3"></textarea>
                </div>
                <button type="submit" class="btn btn-primary" style="width: 100%;">Add Candidate</button>
            </form>
        </div>

        <div class="card">
            <h3>Current Candidates</h3>
            <?php if (empty($candidates)): ?>
                <p class="text-muted">No candidates added yet.</p>
            <?php else: ?>
                <table class="table">
                    <thead>
                        <tr>
                            <th>Student</th>
                            <th>Party</th>
                            <th>Approved</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($candidates as $c): ?>
                            <tr>
                                <td>
                                    <strong><?= htmlspecialchars($c['name']) ?></strong><br>
                                    <small><?= $c['student_id'] ?></small>
                                </td>
                                <td><?= htmlspecialchars($c['party_name'] ?: 'None') ?></td>
                                <td>
                                    <span class="badge" style="background: <?= $c['approved'] ? '#22c55e' : '#94a3b8' ?>">
                                        <?= $c['approved'] ? 'Yes' : 'Pending' ?>
                                    </span>
                                </td>
                                <td>
                                    <form action="api/toggle_candidate_approval.php" method="POST" style="display:inline;">
                                        <input type="hidden" name="id" value="<?= $c['id'] ?>">
                                        <button type="submit" class="btn btn-outline" style="padding: 0.25rem 0.5rem; font-size: 0.75rem;">
                                            <?= $c['approved'] ? 'Revoke' : 'Approve' ?>
                                        </button>
                                    </form>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>
        </div>
    </div>
  </div>
</body>
</html>
