<?php
App::uses('AppController', 'Controller');
App::uses('PortHistory', 'Model');
App::uses('Plano', 'Model');

/**
 * Saldo Controller
 *
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 */
class SaldoController extends AppController {
	
	public $components = array(
    	        'Paginator', 
    	        'Session', 
    	        'Credit.Credit', 
    	        'Cash.Cash',
	        ); 
	
	public $helpers = array(
	            'Api',
	        );
/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
	    	    
		$this->set('title_for_layout', 'Meu Saldo');
		
		$this->loadModel('PortHistory');		
	    $this->PortHistory->create();
	    $this->PortHistory->recursive = 0;
    
		/**
		 * TODO Exemplo para consultar todos os registros por usuario logado
		 * $PortHistories = $this->PortHistory->findAllByUserId($this->Auth->user('id'));
		 */	    	
	    
		/**
		 * Valida o usuario logado para mostrar os dados
		 * dele apenas, salvo quando for admin
		 */
		if ($this->Auth->user('role_id') != '1') {
			
	    	$conditions = array(
	    			'conditions' => array(
	    					'PortHistory.user_id' => $this->Auth->user('id'),
	    			),
	    			'order' => 'PortHistory.id DESC',
	    	);
	    	
		}		
		else {
			$conditions = array(				
					'order' => 'PortHistory.id DESC',
			);	    
		}
	    
    	$PortHistories = $this->PortHistory->find('all', $conditions);
		$this->set('sms_cash', $this->Cash->getSMSCash($this->Auth->User('id')));
		$this->set('portability_cash', $this->Cash->getPortabilityCash($this->Auth->User('id')));
		$this->set(compact('PortHistories'));
	}
	
/**
 * admin_addcreditos method
 *
 * @return void
 */
	public function admin_addcreditos() {
		$this->set('title_for_layout', 'Adicionar Créditos');
		
		$this->loadModel('Plano');
		$this->Plano->create();
		$this->Plano->recursive = 0;
		
		$conditions = array(
		      'order' => 'Plano.type',
		);
		
		$Planos = $this->Plano->find('all', $conditions);		
		$this->set(compact('Planos'));		
	}
	
/**
 * admin_checkout
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_checkout() {
	    
	    $this->set('title_for_layout', 'Finalização de Compra');
	
	    $_plano_id = null;
	    $_plano_name = null;
	    $_plano_price = null;
	    $_credit_type = null;
	     
	    if ($this->request->is('post') && !empty($this->request->data)) {
	        	
	        $_plano_id = $this->request->data['Credit']['plano_id'];
	        $_plano_name = $this->request->data['Credit']['plano_name'];
	        $_plano_price = $this->request->data['Credit']['plano_price'];
	        $_credit_type = $this->request->data['Credit']['credit_type'];
	
	        $this->Credit->sumCashPending(array(
	                'plano_price' => $_plano_price,
	                'plano_id' => $_plano_id,
	                'credit_type' => $_credit_type,
	                'user_id' => $this->Auth->user('id'),
	        ));
	        	
	    }
	
	    $this->Session->write('plano_id',$_plano_id);
	    $this->Session->write('plano_name',$_plano_name);
	    $this->Session->write('plano_price',$_plano_price);
	    $this->Session->write('credit_type',$_credit_type);
	}
}
