<?php
class TaskController extends Controller
{
    private TeamTask $tasks;public function __construct(){$this->tasks=new TeamTask();}
    public function index():void{Auth::requireLogin();if(!can('tasks.manage')){http_response_code(403);require app_path('Views/errors/403.php');exit;}$user=Auth::user();$tasks=in_array($user['role'],['owner','admin','dispatcher'],true)?$this->tasks->all():$this->tasks->all((int)$user['id']);$users=Database::connection()->query("SELECT id,name,role FROM users WHERE status='active' ORDER BY name")->fetchAll();$this->view('tasks/index',compact('tasks','users'));}
    public function store():void{Auth::requireLogin();if(!can('tasks.manage')){http_response_code(403);exit;}Csrf::verify();$this->tasks->create($_POST);$this->flash('success','Task created.');$this->redirect('/tasks');}
    public function status():void{Auth::requireLogin();if(!can('tasks.manage')){http_response_code(403);exit;}Csrf::verify();$this->tasks->status((int)($_POST['id']??0),$_POST['status']??'open');$this->flash('success','Task updated.');$this->redirect('/tasks');}
}
