<!DOCTYPE html>
<html>
<head>
    <title>Task Management</title>
</head>
<body>
    <h1>Task Management</h1>

    <?php if (session()->getFlashdata('success')): ?>
        <p style="color: green;"><?= session()->getFlashdata('success') ?></p>
    <?php endif; ?>
    <?php if (session()->getFlashdata('error')): ?>
        <p style="color: red;"><?= session()->getFlashdata('error') ?></p>
    <?php endif; ?>

    <!-- ฟอร์มเพิ่มงาน -->
    <h2>Add Task</h2>
    <form action="/tasks/add" method="post">
        <?= csrf_field() ?>
        <label for="task_description">Task Description:</label>
        <input type="text" id="task_description" name="task_description" required>

        <label for="employee_id">Assign to Employee:</label>
        <select id="employee_id" name="employee_id" required>
            <option value="">-- Select Employee --</option>
            <?php foreach ($employees as $employee): ?>
                <option value="<?= $employee['id'] ?>"><?= $employee['name'] ?></option>
            <?php endforeach; ?>
        </select>

        <button type="submit">Add Task</button>
    </form>

    <!-- ฟอร์มแบ่งงาน -->
    <h2>Assign Tasks Evenly</h2>
    <form action="/tasks/assign" method="post">
        <?= csrf_field() ?>
        <button type="submit">Assign Tasks Evenly</button>
    </form>

    <!-- แสดงรายการงาน -->
    <h2>All Tasks</h2>
    <table border="1">
        <thead>
            <tr>
                <th>#</th>
                <th>Description</th>
                <th>Assigned To</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($tasks as $task): ?>
                <tr>
                    <td><?= $task['id'] ?></td>
                    <td><?= $task['task_description'] ?></td>
                    <td>
                        <?= $task['employee_id'] 
                            ? $employees[array_search($task['employee_id'], array_column($employees, 'id'))]['name'] 
                            : 'Unassigned' ?>
                    </td>
                    <td><?= $task['is_assigned'] ? 'Assigned' : 'Unassigned' ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</body>
</html>
