<?php
class Location {

    public static function getByNoClient($noClient) {
        $query = "SELECT * FROM location".ID_GARAGE." WHERE NoClient=:client";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":client", $noClient);
        $statement->execute();
        return $statement->fetchAll();
    }

    
    public static function getByNoChassisAndDate($noChassis, $start, $end) {
        $query = "SELECT * FROM location".ID_GARAGE." WHERE NoChasis=:nochassis AND((Debut>=:start' AND Debut<=:end') OR (Fin>=:start AND Fin<=:end) OR (Debut<:start AND Fin>:end))";;
        $statement = Connection::getStatement($query);
        $statement->bindParam(":nochassis", $noChassis);
        $statement->bindParam(":start", $start);
        $statement->bindParam(":end", $end);
        $statement->execute();
        return $statement->fetchAll();
    }
    public static function getNbPretByNoChassisAndDate($noChassis, $start, $end) : int {
        $result = self::getByNoChassisAndDate($noChassis, $start, $end);
        if(is_array($result)) {
            return count($result);
        }
        return 0;
    }

    public static function getByNoChassisAndDateDebutExact($noChassis, $date) {
        $query = "SELECT * FROM location".ID_GARAGE." WHERE NoChasis=:nochassis AND Debut =:date";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":nochassis", $noChassis);
        $statement->bindParam(":date", $date);
        $statement->execute();
        return $statement->fetchAll();
    }

    public static function getByNoChassis($noChassis) {
        $query = "SELECT * FROM location".ID_GARAGE." WHERE NoChasis=:nochassis ORDER BY Debut DESC";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":nochassis", $noChassis);
        $statement->execute();
        return $statement->fetchAll();
    }

    public static function getByNoChassisAndDateDebut($noChassis, $date) {
        $date .= ' 23:59:59';
        $query = "SELECT * FROM location".ID_GARAGE." WHERE NoChasis=:nochassis AND Debut>:date ORDER BY Debut LIMIT 0, 1";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":nochassis", $noChassis);
        $statement->bindParam(":date", $date);
        $statement->execute();
        return $statement->fetchAll();
    }

      
}