<?php
namespace App\Controllers;
use App\Core\Auth;use App\Core\Controller;use App\Core\Csrf;use App\Core\Permission;use App\Models\PricingCatalog;
final class PricingCatalogController extends Controller
{
 private function org():int{return (int)(Auth::user()['organisation_id']??1);}
 public function index():void{Auth::requireLogin();Permission::require('tarifs.view');$org=$this->org();$this->view('pricing_catalog/index',['pageTitle'=>'Catalogue des prestations','items'=>PricingCatalog::all($org),'poles'=>PricingCatalog::poles($org),'disciplines'=>PricingCatalog::disciplines($org),'courses'=>PricingCatalog::courses($org),'publics'=>PricingCatalog::publics($org),'years'=>PricingCatalog::years($org)]);}
 public function save():void{Auth::requireLogin();Permission::require('tarifs.manage');Csrf::verify($_POST['_csrf']??null);$mode=in_array($_POST['mode_tarif']??'', ['fixe','libre','modifiable'],true)?$_POST['mode_tarif']:'fixe';$v=$_POST;$v['mode_tarif']=$mode;$v['code']=strtoupper(trim((string)($v['code']??'')));$v['libelle']=trim((string)($v['libelle']??''));$labelRequired=($v['mode_libelle']??'propose')!=='libre';if($v['code']===''||($labelRequired&&$v['libelle']==='')||(int)($v['pole_id']??0)<1||($mode!=='libre'&&($v['montant']??'')==='')||(($v['categorie']??'')==='adhesion'&&(int)($v['annee_scolaire_id']??0)<1)){$_SESSION['error']='Vérifiez le pôle, l’année, le libellé et le montant requis.';header('Location: /public/administration/axiom/catalogue-prestations');exit;}try{PricingCatalog::save($this->org(),$v);$_SESSION['success']='La prestation a été enregistrée.';}catch(\Throwable $e){$_SESSION['error']=$e->getMessage();}header('Location: /public/administration/axiom/catalogue-prestations');exit;}
}
