<?php
class Category {
    private $pdo;
    public function __construct(){ $this->pdo = $GLOBALS['pdo']; }
    public function all(){ return $this->pdo->query("SELECT * FROM categories ORDER BY sort_order ASC, name ASC")->fetchAll(); }
    public function find($id){ $st=$this->pdo->prepare("SELECT * FROM categories WHERE id=?"); $st->execute([$id]); return $st->fetch(); }
    public function create($data){ $st=$this->pdo->prepare("INSERT INTO categories (name, description, sort_order) VALUES (?,?,?)"); return $st->execute([$data['name'], $data['description']??'', (int)($data['sort_order']??0)]); }
    public function update($id,$data){ $st=$this->pdo->prepare("UPDATE categories SET name=?, description=?, sort_order=? WHERE id=?"); return $st->execute([$data['name'],$data['description']??'',(int)($data['sort_order']??0),$id]); }
    public function delete($id){ $st=$this->pdo->prepare("DELETE FROM categories WHERE id=?"); return $st->execute([$id]); }
}
