<?php
require_once 'auth.php';
require_once 'db/config.php';

if (!is_logged_in()) {
    header('Location: login.php');
    exit;
}

if (!hasPermission('manage_candidates')) {
    header('Location: app.php');
    exit;
}

$pdo = db();
$candidate_id = $_GET['id'] ?? null;

if (!$candidate_id) {
    header('Location: app.php');
    exit;
}

// Fetch candidate data
$stmt = $pdo->prepare("SELECT * FROM candidates WHERE id = ?");
$stmt->execute([$candidate_id]);
$candidate = $stmt->fetch();

if (!$candidate) {
    header('Location: app.php');
    exit;
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_candidate'])) {
    $name = $_POST['name'] ?? '';
    $email = $_POST['email'] ?? '';
    $phone = $_POST['phone'] ?? '';
    $status = $_POST['status'] ?? 'Applied';
    $notes = $_POST['notes'] ?? '';

    if (!empty($name) && !empty($email)) {
        try {
            $stmt = $pdo->prepare("UPDATE candidates SET name = ?, email = ?, phone = ?, status = ?, notes = ? WHERE id = ?");
            $stmt->execute([$name, $email, $phone, $status, $notes, $candidate_id]);
            header('Location: app.php');
            exit;
        } catch (PDOException $e) {
            error_log("Error updating candidate: " . $e->getMessage());
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Candidate</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="assets/css/custom.css">
</head>
<body>
    <header class="header d-flex justify-content-between align-items-center">
        <div class="logo">
            <a href="app.php">
                <img src="assets/pasted-20251120-051320-b2b0cdfa.png" alt="FinMox Logo" style="height: 32px;">
            </a>
        </div>
        <nav class="d-flex align-items-center">
            <a href="app.php" class="btn btn-outline-primary me-2">Home</a>
            <a href="chat.php" class="btn btn-outline-primary me-2">Chat</a>
            <a href="dashboard.php" class="btn btn-outline-primary me-2">Dashboard</a>
            <a href="workflows.php" class="btn btn-outline-primary me-2">Workflows</a>
            <a href="settings.php" class="btn btn-outline-primary me-3">Settings</a>
            <div class="dropdown">
                <button class="btn btn-outline-secondary dropdown-toggle" type="button" id="userDropdown" data-bs-toggle="dropdown" aria-expanded="false">
                    <?php echo htmlspecialchars($_SESSION['username']); ?>
                </button>
                <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="userDropdown">
                    <?php if (hasPermission('manage_roles')): ?>
                        <li><a class="dropdown-item" href="roles.php">Manage Roles</a></li>
                        <li><hr class="dropdown-divider"></li>
                    <?php endif; ?>
                    <li><a class="dropdown-item" href="logout.php">Logout</a></li>
                </ul>
            </div>
        </nav>
    </header>

    <main class="container-fluid">
        <h2 class="mb-4">Edit Candidate</h2>
        <div class="card">
            <div class="card-body">
                <form method="POST" action="edit_candidate.php?id=<?php echo $candidate_id; ?>">
                    <input type="hidden" name="update_candidate" value="1">
                    <div class="mb-3">
                        <label for="name" class="form-label">Name</label>
                        <input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($candidate['name']); ?>" required>
                    </div>
                    <div class="mb-3">
                        <label for="email" class="form-label">Email</label>
                        <input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($candidate['email']); ?>" required>
                    </div>
                    <div class="mb-3">
                        <label for="phone" class="form-label">Phone</label>
                        <input type="text" class="form-control" id="phone" name="phone" value="<?php echo htmlspecialchars($candidate['phone']); ?>">
                    </div>
                    <div class="mb-3">
                        <label for="status" class="form-label">Status</label>
                        <select class="form-select" id="status" name="status">
                            <option value="Applied" <?php if ($candidate['status'] === 'Applied') echo 'selected'; ?>>Applied</option>
                            <option value="Interviewing" <?php if ($candidate['status'] === 'Interviewing') echo 'selected'; ?>>Interviewing</option>
                            <option value="Offered" <?php if ($candidate['status'] === 'Offered') echo 'selected'; ?>>Offered</option>
                            <option value="Hired" <?php if ($candidate['status'] === 'Hired') echo 'selected'; ?>>Hired</option>
                            <option value="Rejected" <?php if ($candidate['status'] === 'Rejected') echo 'selected'; ?>>Rejected</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="notes" class="form-label">Notes</label>
                        <textarea class="form-control" id="notes" name="notes" rows="3"><?php echo htmlspecialchars($candidate['notes']); ?></textarea>
                    </div>
                    <button type="submit" class="btn btn-primary">Update Candidate</button>
                    <a href="app.php" class="btn btn-secondary">Cancel</a>
                </form>
            </div>
        </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>