<?php
namespace App\Controllers;
use App\Core\Auth;use App\Core\Controller;use App\Core\Csrf;use App\Core\Permission;use App\Models\Enrollment;
final class EnrollmentController extends Controller {
 public function create(): void { Auth::requireLogin();Permission::require('inscriptions.create');$id=(int)($_GET['membre_id']??0);$m=Enrollment::member($id);if(!$m){http_response_code(404);exit('Membre introuvable.');}$this->view('enrollments/form',['pageTitle'=>'Inscrire à un atelier','member'=>$m,'workshops'=>Enrollment::workshops()]); }
 public function groups(): void { Auth::requireLogin();Permission::require('inscriptions.create');header('Content-Type: application/json; charset=utf-8');echo json_encode(Enrollment::groups((int)($_GET['atelier_id']??0)),JSON_UNESCAPED_UNICODE);exit; }
 public function store(): void { Auth::requireLogin();Permission::require('inscriptions.create');Csrf::verify($_POST['_csrf']??null);$id=(int)($_POST['membre_id']??0);try{Enrollment::create($id,(int)($_POST['atelier_id']??0),(int)($_POST['groupe_id']??0)?:null,['organisation_id'=>(int)(Auth::user()['organisation_id']??1),'date_debut'=>$_POST['date_debut']??date('Y-m-d'),'date_fin'=>$_POST['date_fin']??'','eligibility_ce1'=>$_POST['eligibility_ce1']??'','heure_debut'=>$_POST['heure_debut']??'','heure_fin'=>$_POST['heure_fin']??'','observations'=>$_POST['observations']??''],(int)(Auth::user()['id']??0));$_SESSION['success']='Inscription enregistrée.';}catch(\Throwable $e){$_SESSION['error']=$e->getMessage();}$this->redirect('/inscriptions/nouvelle?membre_id='.$id); }
}
