<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Gestioncommandes extends Controller_Applications {
	public $template = 'templates';
	

/*Controlleur du LOGIN*/
public function action_ajoutercommande() 
 {
		
		$commande =  Model::factory( 'commande');
			$commande->add(session::instance()->get('id_client'),
			             date("Y-m-d"),session::instance()->get('total'));
				       $commande=$commande->get_max_id();
					   $id_commande = max($commande);
					  /* foreach ($commande as $commandes) : 
							$id_commande=$commandes['id_commande'];
						endforeach;*/
						
				$nbArticles=session::instance()->get('panier'.'id');
	   $nbArticles=count($nbArticles);
	   $lignecommande =  Model::factory( 'lignecommande');
	   for ($i=0;$i < $nbArticles ; $i++)
	      {
		      $id_produit=session::instance()->get('panier'.'id');
			  $libelle=session::instance()->get('panier'.'libelleProduit');
			  $quantite=session::instance()->get('panier'.'qteProduit');
			  $prix=session::instance()->get('panier'.'prixProduit');
			
			$lignecommande->add($id_commande,$i+1,$id_produit[$i], $libelle[$i],$quantite[$i],$prix[$i]);
	      }
 
		//$content = View::factory('pages/session');
		// $this->template->content = $content;
		
		  session::instance()->set('panier',array());
						   session::instance()->set('panier'.'id', array());
						  session::instance()->set('panier'.'libelleProduit', array());
						   session::instance()->set('panier'.'prixProduit' , array());
						  session::instance()->set('panier'.'qteProduit',array());
						 
						  session::instance()->set('panier'.'verrou' ,false);
						  session::instance()->set('total' ,0);
		$this->action_monespace();
 }
 
 public function action_detailscommande() 
 {
		
$content = View::factory('pages/detailscommande')
->bind('lignescommande', $lignescommande)
->bind('date_commande', $date_commande)
->bind('montant_commande', $montant_commande)
->bind('id_commande', $id_commande);

$lignescommande = Model::factory( 'lignecommande');


$id_commande= (string) $this->request->param('id');
$date_commande= (string) $this->request->param('date');
$montant_commande= (string) $this->request->param('montant');


$lignescommande = $lignescommande->get_all($id_commande);

$this->template->content = $content;
 }
 
 public function action_supprimercommande() 
 {
		$id_commande= (string) $this->request->param('id');
		$commande =  Model::factory( 'commande');
			$commande->delete($id_commande);
			
		$this->action_monespace();
 }
 /////////////////////////////////////////////////////////////////////
 /////////////////// GEStion des commande //////////////////////////////:
 ///////////////////////////////////////////////////////////////////::
public function action_listecommandes()
{

$content = View::factory('pages/listecommandes')
->bind('commandes', $commandes)
->bind('pager_links', $pager_links)
->set('items_per_page', 50);

$commandes = Model::factory( 'Commande');


$message_count = $commandes->count_all();
$pagination = Pagination::factory(array(
'total_items' => $message_count,
'items_per_page' => 50,
));

$pager_links = $pagination->render();

$commandes = $commandes->get_all(session::instance()->get('id_client'),$pagination->items_per_page,$pagination->offset);

$this->template->content = $content;
}


}
//end page