<?php
$page_title = 'Task Board';
require_once __DIR__ . '/header.php';

// Update Task Status
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_task'])) {
    $tid = (int)$_POST['task_id'];
    $status = $_POST['status'];
    $db->prepare("UPDATE emp_tasks SET status=? WHERE id=? AND employee_id=?")->execute([$status, $tid, $emp_id]);
    redirect(SITE_URL . '/employee/tasks.php');
}

$tasks = $db->query("SELECT * FROM emp_tasks WHERE employee_id={$emp_id} ORDER BY target_date ASC, created_at DESC")->fetchAll();
?>

<div class="card">
  <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px;">
    <div>
      <h3 style="margin:0;color:#0f172a;">My Tasks</h3>
      <p style="margin:4px 0 0;font-size:.85rem;color:#64748b;">Manage your daily assignments</p>
    </div>
  </div>
  
  <div class="table-wrap" style="margin-top:0;">
    <table>
      <thead>
        <tr>
          <th>Task Title</th>
          <th>Type</th>
          <th>Deadline</th>
          <th>Status</th>
          <th>Action</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($tasks as $t): ?>
        <tr>
          <td>
            <strong style="color:#0f172a;display:block;margin-bottom:4px;"><?= h($t['title']) ?></strong>
            <span style="font-size:.85rem;color:#64748b;"><?= h($t['description']) ?></span>
          </td>
          <td><span class="badge badge-info" style="border-radius:4px;padding:4px 8px;"><?= h($t['task_type']) ?></span></td>
          <td style="color:#dc2626;font-weight:600;font-size:.9rem;"><?= $t['target_date'] ? date('d M Y', strtotime($t['target_date'])) : 'No Deadline' ?></td>
          <td>
            <?php 
              $c = 'info';
              if($t['status']==='Completed' || $t['status']==='Verified') $c='success';
              if($t['status']==='Pending') $c='warning';
            ?>
            <span class="badge badge-<?= $c ?>"><?= h($t['status']) ?></span>
          </td>
          <td>
            <?php if($t['status'] !== 'Verified'): ?>
            <form method="POST" style="display:flex;gap:8px;">
              <input type="hidden" name="update_task" value="1">
              <input type="hidden" name="task_id" value="<?= $t['id'] ?>">
              <select name="status" style="padding:6px;border-radius:4px;border:1px solid #cbd5e1;font-size:.85rem;outline:none;">
                <option value="Pending" <?= $t['status']==='Pending'?'selected':'' ?>>Pending</option>
                <option value="In Progress" <?= $t['status']==='In Progress'?'selected':'' ?>>In Progress</option>
                <option value="Completed" <?= $t['status']==='Completed'?'selected':'' ?>>Mark Completed</option>
              </select>
              <button type="submit" style="background:#2557d6;color:#fff;border:none;padding:6px 12px;border-radius:4px;cursor:pointer;font-weight:600;font-size:.85rem;">Save</button>
            </form>
            <?php else: ?>
            <span style="color:#16a34a;font-weight:600;font-size:.85rem;"><i class="fas fa-check-circle"></i> Verified</span>
            <?php endif; ?>
          </td>
        </tr>
        <?php endforeach; ?>
        <?php if(!$tasks): ?>
        <tr><td colspan="5" style="text-align:center;padding:30px;color:#64748b;">You have no assigned tasks. Great job!</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

<?php require_once __DIR__ . '/footer.php'; ?>
