<?php

class ShopController extends Zend_Controller_Action {

	public function init() {	
		$this->view->menuSel = 5;		
	}
	
	public function resetAction() {
		$this->_helper->viewRenderer->setNoRender();
		
		Zend_Session::namespaceUnset('order');
		Zend_Session::namespaceUnset('basket');
		
		$this->_helper->redirector('index','index');
	}
	
	public function shopinfoAction() {
		$order = new Zend_Session_Namespace('order');
		$basket = new Zend_Session_Namespace('basket');
		$information = new Zend_Session_Namespace('information');	
		$client = new Zend_Session_Namespace('client');	

	//	echo rand(0,10000);
		
		if( $client->client_auth == true ){

			$Client1 = new Client();

			$data = array(					
				'client_basket' => json_encode($basket->product),
				'client_order' => json_encode($order->product)
			);
			$Client1->update( $data , 'client_id='.$client->client_id);
		
		}
		
		$v = $this->_request->getParams();	
		$idProduct = $v['idProduct'];			

		if( $v['operation'] == 'delproduct' ){		
			unset($order->product[$v['idDel']]);		
		}
				
		if( $v['operation'] == 'addproduct' ){
			$order->lastId = $order->lastId + 1;
			
			$order->product[$order->lastId]['idProduct'] = $v['idProduct'];
			$order->product[$order->lastId]['amount'] = $v['amount'];
			foreach($basket->product[$idProduct] as $key => $value){
			/*
				if( $key == 'price' ){
					if( $client->client_auth == true ){
						$Client = new Client();
						$select = $Client->select();		
						$select->where('client_id = ?', $client->client_id);				
						$rowClient = $Client->fetchRow($select);	
						
						$rabat_5 = $rowClient->client_rabat_5;
					}else{
						$rabat_5 = 1;		
					}
		
					$value = $value * $rabat_5;
				}
			*/
				$order->product[$order->lastId][$key] = $value;
			}
                        Zend_Session::rememberMe(60 * 60 * 24);
		
		}
				
		if( $v['operation'] == 'editproduct' ){				
			$ids = $basket->product[$idProduct]['basketid'];
			
			$order->product[$ids]['idProduct'] = $v['idProduct'];
			$order->product[$ids]['amount'] = $v['amount'];
			$basket->product[$idProduct]['amount'] = $v['amount'];
			foreach($basket->product[$idProduct] as $key => $value){
				$order->product[$ids][$key] = $value;
				//echo$key.'='.$value.', ';
			}			
			//var_dump($order->product[$ids]);die();
		}
				
		$ilosc = count($order->product);
		$this->view->countOrder = $ilosc;	
		
		$cena = 0;
		foreach($order->product as $key => $value){
			$cena = $cena + $value['price']*$value['amount'];
		}
		$this->view->priceOrder = $cena;

		$this->view->orderData = $order->product;		
		
		
	}
	
	
	public function indexAction() {			
		$v = $this->_request->getParams();
		
		if( !isset($v['id']) ){
			$idCategory = 0;
		}else{
			$idCategory = $v['id'];
		}		
		
		$this->view->idCategory = $idCategory;
		
		$ctype = 3;
		$Category = new Category();		
		$data = $Category->fetchAll('category_type = '.$ctype.' AND category_parent_id = '.$idCategory , 'category_name ASC');
		if( count($data) > 0 ){
			$this->view->dataTable = $data;
		}else{
			$this->_helper->redirector('product','shop',null,array('id' => $idCategory));			
		} 
	}
	
	
	public function productAction() {			
		$v = $this->_request->getParams();
		
		if( !isset($v['id']) ){
			$idCategory = 0;
		}else{
			$idCategory = $v['id'];
		}		
		
		$this->view->idCategory = $idCategory;
		
		$Product = new Product();		
		$data = $Product->fetchAll('category_id = '.$idCategory , 'product_name ASC');
		$this->view->dataTable = $data;
	}
	
	
}
