<?php
include APPLICATION_PATH . '/DAO/UtilisateurDAO.php';
include APPLICATION_PATH . '/DAO/PersonneDAO.php';
include APPLICATION_PATH . '/DAO/ProfilDAO.php';
include APPLICATION_PATH . '/DAO/LogDAO.php';

class LogController extends Zend_Controller_Action
{

    private $utilisateur = null;
    public function init()
    {
        session_start();
        if ( isset($_SESSION["idUtilisateur"])){
            $utilisateurDao = new UtilisateurDAO();
            $this->utilisateur = $utilisateurDao->getUtilisateurById((int)$_SESSION["idUtilisateur"]);
            $_SESSION["derniereAction"] = new \DateTime();
        }else{
            $this->_helper->redirector('index' , 'Index');
        }
    }

    public function indexAction()
    {
        $error = 0;
        $errormsg = "";
        $listeAll = array();
        //$pointDeVente = "";
        $user = "";
        $dated = "";
        $datef = "";
        
        if (!empty($_POST)) {
//            if (!empty($_GET["pointDeVente"])) {
                //$pointDeVente = $_GET["pointDeVente"];
                $user = !empty($_POST["user"]) ? $_POST["user"] : null;
            
                $dateDeb = !empty($_POST["dated"]) ? new DateTime($_POST["dated"]) : null;
                $dateFin = !empty($_POST["datef"]) ? new DateTime($_POST["datef"]) : null;
                
                
                //$pvDAO = new PointDeVenteDAO();
                $utilDAO = new UtilisateurDAO();
                $persDAO = new PersonneDAO();
                $logDAO = new LogDAO();
                
                //$user=$user->ob
                 $listeAll = $logDAO->obtenirLogParTousCriteres($user, $dateDeb, $dateFin);
                     
               
                //var_dump(count($listeAll));
                $class = 'table table-striped table-bordered';
                $table = "";
              
                
                if(count($listeAll)>0){

                    foreach ($listeAll as $unLog){
                        
                        $id=$unLog->getIdLog();
                        $nom=utf8_decode(utf8_encode($unLog->getIdutilisateur()->getIdpersonne()->getNom()))." ".utf8_decode(utf8_encode($unLog->getIdutilisateur()->getIdpersonne()->getPrenom()));
                        $description=utf8_decode(utf8_encode($unLog->getAction()));
                        $date=date_format($unLog->getDatelog(), "d/m/Y à H:i:s");
                        //$nomClient =   !is_null($paiement->getAvoir()->getFacture()->getClient())? $paiement->getAvoir()->getFacture()->getClient()->getNom() . " " . $paiement->getAvoir()->getFacture()->getClient()->getPrenom() : $paiement->getAvoir()->getFacture()->getNomClient();
                 $table.="
             <tr>
                  <td>" . $id . "</td>
                  <td>" . $nom . "</td>
                  <td>" . $description . "</td>  
                  <td>" . $date . "</td> 
            </tr>";
                    }
                }
               
               
                echo json_encode(array("type"=>"success","table"=> $table));
                exit();
//            } else {
//                $error = 1;
//                $rep = array('msg' => $error, 'msg1' => "Selectionnez le point de vente");
//            }
        } else {
           $UserDAO = new UtilisateurDAO();
           $User= $this->utilisateur;
           $listeAllUser = array();
        if($User !=null)
        {
                //$this->view->pointventes = $PVDAO->lister();
                $listeAllUser = $UserDAO->lister();
               // die("cont: ".count($listeAllUser));
//                $listeuser = $UserDAO->listerUtilisateur();
//                foreach ($listecaissier as $caissier):
//                    if($caissier->getCaissier()== UTILISATEUR_CAISSIER)
//                    {
//                        array_push($listeAllUser, $caissier);
//                    }
//                endforeach;
                
                $this->view->listeUtilisateur = $listeAllUser;
                
        }
        }
    }


}

