<?php
App::uses('AppController', 'Controller');
/**
 * TmCampaigns Controller
 *
 * @property TmCampaign $TmCampaign
 */
class TmCampaignsController extends AppController {

/**
 * Components
 *
 * @var array
 */
    public $components = array('Paginator');
    
/**
 * Components
 *
 * @var array
 */
    public $helpers = array('Api');    
    
/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index($status = null) {
		$this->set('title_for_layout', 'Campanhas Cadastradas');
		$this->TmCampaign->recursive = 0;
		
		if ($status != null) {
			$conditions = array(			    
			        'TmCampaign.status' => $status,
			     );
		}
		
		$this->Paginator->settings = array(
				'conditions' => $conditions,
		        'order' => array(
		        		'TmCampaign.status' => 'asc',
		                'TmCampaign.updated' => 'desc',
		        ),
		);		
		$this->set('tmCampaigns', $this->paginate());
	}
	

/**
 * admin_index method
 *
 * @return void
 */
	public function admin_show() {
		$this->set('title_for_layout', 'Campanhas');
		
		$activeCampaigns = $this->TmCampaign->find('all', array(
				'conditions' => array(
						'TmCampaign.status' => 1,
					),
				'limit' => 3
			));
		
		$inactiveCampaigns = $this->TmCampaign->find('all', array(
				'conditions' => array(
						'TmCampaign.status' => 0,
				),
				'order' => 'TmCampaign.updated DESC',
				'limit' => 3
		));
		
		$closedCampaigns = $this->TmCampaign->find('all', array(
				'conditions' => array(
						'TmCampaign.status' => 2,
				),
				'order' => 'TmCampaign.updated DESC',
				'limit' => 3
		));
		
		$this->loadModel('TmItem');
		$this->TmItem->create();
			
		$lastSents = $this->TmItem->find('all', array(
				'conditions' => array(
						'TmItem.status' => 1, 
				),
				'order' => 'TmItem.updated DESC',
				'limit' => 10,
					
		));
		
		$this->set('activeCampaigns', $activeCampaigns);
		$this->set('inactiveCampaigns', $inactiveCampaigns);
		$this->set('closedCampaigns', $closedCampaigns);		
		$this->set(compact('lastSents'));
	}

/**
 * admin_view method
 *
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		$this->set('title_for_layout', 'Campanha');
		
		$this->TmCampaign->id = $id;
		if (!$this->TmCampaign->exists()) {
			throw new NotFoundException(__('Invalid tm campaign'));
		}
		$this->set('tmCampaign', $this->TmCampaign->read(null, $id));
		$this->set('currentkey', $id);
	}

/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add($id) {
		$this->set('title_for_layout', 'Adicionar Campanha');
		
		if ($this->request->is('post')) {
			$this->TmCampaign->create();
			if ($this->TmCampaign->save($this->request->data)) {
				$this->Session->setFlash(__('The tm campaign has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The tm campaign could not be saved. Please, try again.'));
			}
		}
		$this->set('mailing_id',$id);
	}

/**
 * admin_edit method
 *
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		$this->set('title_for_layout', 'Editar Campanha');
		$this->TmCampaign->id = $id;
		if (!$this->TmCampaign->exists()) {
			throw new NotFoundException(__('Invalid tm campaign'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->TmCampaign->save($this->request->data)) {
				$this->Session->setFlash(__('The tm campaign has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The tm campaign could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->TmCampaign->read(null, $id);
		}
		$mailings = $this->TmCampaign->Mailing->find('list');
		$this->set(compact('mailings'));
		$this->set('tm_campaign_id', $id);
	}
	
/**
 * admin_resume method
 *
 * @param string $id da campanha
 * @return void
 */
	public function admin_resume($id = null) {
		
		$this->set('title_for_layout', 'Resumo de Campanha');
		$this->TmCampaign->id = $id;
		if (!$this->TmCampaign->exists()) {
			throw new NotFoundException(__('Invalid tm campaign'));
		}
		else {			
			$this->loadModel('TmItem');
			$this->TmItem->create();
			
			$pending = $this->TmItem->find('count', array(
					'conditions' => array(
							'TmItem.status' => 0,
							'TmItem.tm_campaign_id' => $id,
						)
			));
			
			$closed = $this->TmItem->find('count', array(
					'conditions' => array(
							'TmItem.status' => 1,
							'TmItem.tm_campaign_id' => $id,
						)
			));
			
			$Items = $this->TmItem->find('all', array(
					'conditions' => array(
							'TmItem.status' => 1,
							'TmItem.tm_campaign_id' => $id,
						),
					'order' => 'TmItem.updated DESC',
					'limit' => 30,
					
			));

			$this->set('campaign', $id);
			$this->set('pending', $pending);
			$this->set('closed', $closed); 
			$this->set(compact('Items'));

			$Campaign = $this->TmCampaign->read();
			$this->set('status', $Campaign['TmCampaign']['status']);
		}
	}
	
/**
 * Ativa uma campanha
 */	
	public function admin_ativar($campaign_id, $action) {
	    $this->TmCampaign->id = $campaign_id;
	    if (!$this->TmCampaign->exists()) {
	        throw new NotFoundException(__('Invalid tm campaign'));
	        $this->redirect(array('action' => $action, $campaign_id));
        }
        else {
            $this->TmCampaign->saveField('status', 1);
            $this->redirect(array('action' => $action, $campaign_id));
	    }	    
	}
	
/**
 * Desativa uma campanha
 */	
	public function admin_desativar($campaign_id, $action) {
	    $this->TmCampaign->id = $campaign_id;
	    if (!$this->TmCampaign->exists()) {
	        throw new NotFoundException(__('Invalid tm campaign'));
	        $this->redirect(array('action' => $action, $campaign_id));
	    }
	    else {
	        $this->TmCampaign->saveField('status', 0);
	        $this->redirect(array('action' => $action, $campaign_id));
	    }
	}
}
