<?php
class Client {
    public static function getClient() {
        $query = "SELECT * FROM clients";
        $statement = Connection::getStatement($query);
        $statement->execute();
        return $statement->fetchAll();
    }

    public static function getClientByNoClient($noClient) {
        $query = "SELECT * FROM clients WHERE NoClient=:noclient";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":noclient", $noClient);
        $statement->execute();
        return $statement->fetch();
    }

    public static function getClientByFamilleVHC($famille) {
        $query = "SELECT DISTINCT c.* FROM clients AS c, vehicules AS v WHERE c.NoClient = v.Client AND v.Famille LIKE :famille";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":famille", $famille);
        $statement->execute();
        return $statement->fetchAll();
    }

    public static function getClientByMailingPaperWrong($noClient, $noClient2) {
        $query = "SELECT * FROM clients WHERE (NoClient = :noclient OR NoClient = :noclient2) AND MailingPapier = 'wrong'";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":noclient", $noClient);
        $statement->bindParam(":noclient2", $noClient2);
        $statement->execute();
        return $statement->fetchAll();
    }

    public static function getClientByNom($nom) {
        $query = "SELECT * FROM clients WHERE Nom = :nom";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":nom", $nom);
        $statement->execute();
        return $statement->fetchAll();
    }

    public static function getClientByNomPrenom($nom, $prenom) {
        $query = "SELECT * FROM clients WHERE Nom = :nom AND Prenom = :prenom ORDER BY Prenom,NoClient";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":nom", $nom);
        $statement->bindParam(":prenom", $prenom);
        $statement->execute();
        return $statement->fetchAll();
    }
  
    public static function getClientByNoms($nom1, $nom2) {
        // TODO what does this query do
        $query = "SELECT NoClient FROM clients WHERE Nom >= :nom1 AND Nom <= :nom2";
        $statement = Connection::getStatement($query);
        $statement->bindParam(":nom1", $nom1);
        $statement->bindParam(":nom2", $nom2);
        $statement->execute();
        return $statement->fetchAll();
    }

    public static function getClientByDateNaissance()  {
        $today = date("Y-m-d");
        $param = "%-".date("m-d");
        $param1 = "%-".date("m-d", strtotime($today.' + 1 day'));
        $param2 = "%-".date("m-d", strtotime($today.' + 2 days'));
        // TODO union is really useful ?
        $query = "SELECT * FROM clients WHERE DateNaissance LIKE :date0 UNION SELECT * FROM clients WHERE DateNaissance LIKE :date1 UNION SELECT * FROM clients WHERE DateNaissance LIKE :date2";
        $statement = Connection::getStatement($query);
        $statement->bindParam("date0", $param);
        $statement->bindParam("date1", $param1);
        $statement->bindParam("date2", $param2);
        $statement->execute();
        return $statement->fetchAll();
    }

    public static function getClientPromo()  {
        $query = "SELECT * FROM `clients` WHERE Email !='' AND NoClient NOT IN (SELECT P_NoClient FROM promotions_suivis WHERE P_NoClient = clients.NoClient)";
        $statement = Connection::getStatement($query);
        $statement->execute();
        return $statement->fetchAll();
    }

    public static function getClientWithEMail($type = null)  {
        $query = "SELECT * FROM `clients` WHERE Email IS NOT NULL AND Email !='' AND AccepteEmail = '1'";
        if (!is_null($type)) {
            $query .= " AND Type=:type";
        }
        $statement = Connection::getStatement($query);
        if (!is_null($type)) {
            $statement->bindParam(":type", $type);
        }
        $statement->execute();
        return $statement->fetchAll();
    }
    
}