<?php
/**
 * TypeVehicule Model
 * Represents vehicle types from type_vehicule table
 */

class TypeVehicule {
    private $conn;
    private $table_name = "type_vehicule";

    public $id;
    public $libelle;
    public $creer_par;
    public $date_creer;

    public function __construct($db) {
        $this->conn = $db;
    }

    public function getAll() {
        $query = "SELECT * FROM " . $this->table_name . " ORDER BY libelle ASC";
        
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        
        return $stmt;
    }

    public function getById($id) {
        $query = "SELECT * FROM " . $this->table_name . " WHERE id = ? LIMIT 0,1";
        
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $id);
        $stmt->execute();
        
        return $stmt;
    }

    public function getByLibelle($libelle) {
        $query = "SELECT * FROM " . $this->table_name . " WHERE libelle = ? LIMIT 0,1";
        
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $libelle);
        $stmt->execute();
        
        return $stmt;
    }

    public function create() {
        $query = "INSERT INTO " . $this->table_name . " SET libelle=:libelle, creer_par=:creer_par, date_creer=:date_creer";
        
        $stmt = $this->conn->prepare($query);
        
        $this->libelle = htmlspecialchars(strip_tags($this->libelle));
        $this->creer_par = htmlspecialchars(strip_tags($this->creer_par));
        $this->date_creer = htmlspecialchars(strip_tags($this->date_creer));
        
        $stmt->bindParam(":libelle", $this->libelle);
        $stmt->bindParam(":creer_par", $this->creer_par);
        $stmt->bindParam(":date_creer", $this->date_creer);
        
        if ($stmt->execute()) {
            return true;
        }
        
        return false;
    }
}
?>