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

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

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

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

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

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

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

// Fetch candidates for the dropdown
$stmt = $pdo->query("SELECT id, name FROM candidates");
$candidates = $stmt->fetchAll();

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_task'])) {
    $task_name = $_POST['task_name'] ?? '';
    $candidate_id = $_POST['candidate_id'] ?? null;
    $due_date = $_POST['due_date'] ?? null;
    $status = $_POST['status'] ?? 'To Do';
    $description = $_POST['description'] ?? '';

    if (!empty($task_name) && !empty($candidate_id)) {
        try {
            $stmt = $pdo->prepare("UPDATE tasks SET task_name = ?, candidate_id = ?, due_date = ?, status = ?, description = ? WHERE id = ?");
            $stmt->execute([$task_name, $candidate_id, $due_date, $status, $description, $task_id]);
            header('Location: app.php');
            exit;
        } catch (PDOException $e) {
            error_log("Error updating task: " . $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 Task</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 Task</h2>
        <div class="card">
            <div class="card-body">
                <form method="POST" action="edit_task.php?id=<?php echo $task_id; ?>">
                    <input type="hidden" name="update_task" value="1">
                    <div class="mb-3">
                        <label for="task_name" class="form-label">Task Name</label>
                        <input type="text" class="form-control" id="task_name" name="task_name" value="<?php echo htmlspecialchars($task['task_name']); ?>" required>
                    </div>
                    <div class="mb-3">
                        <label for="candidate_id" class="form-label">Assign to Candidate</label>
                        <select class="form-select" id="candidate_id" name="candidate_id" required>
                            <option value="" disabled>Select a candidate</option>
                            <?php foreach ($candidates as $candidate): ?>
                                <option value="<?php echo $candidate['id']; ?>" <?php if ($task['candidate_id'] == $candidate['id']) echo 'selected'; ?>><?php echo htmlspecialchars($candidate['name']); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="due_date" class="form-label">Due Date</label>
                        <input type="date" class="form-control" id="due_date" name="due_date" value="<?php echo htmlspecialchars($task['due_date']); ?>">
                    </div>
                    <div class="mb-3">
                        <label for="task_status" class="form-label">Status</label>
                        <select class="form-select" id="task_status" name="status">
                            <option value="To Do" <?php if ($task['status'] === 'To Do') echo 'selected'; ?>>To Do</option>
                            <option value="In Progress" <?php if ($task['status'] === 'In Progress') echo 'selected'; ?>>In Progress</option>
                            <option value="Done" <?php if ($task['status'] === 'Done') echo 'selected'; ?>>Done</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="task_description" class="form-label">Description</label>
                        <textarea class="form-control" id="task_description" name="description" rows="3"><?php echo htmlspecialchars($task['description']); ?></textarea>
                    </div>
                    <button type="submit" class="btn btn-primary">Update Task</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>