<?php

class Solicitacao_TarefaajaxController extends Agro_Controller_Action {
	
	/*
	 * Inicializa a classe
	 */
	public function init() {
		$this->getHelper('layout')->disableLayout();
	}
	
	/*
	 * Marcar uma solicitacao como excluida
	 */
	public function excluirAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$Tarefas = new Solicitacao_Model_Tarefa();
		
		$data = array(
			"id" => $this->getRequest()->getParam('id'),
			"fl_del" => 1
		);
		
		if ($Tarefas->save($data)) {
			$this->getResponse()->setBody(Zend_Json::encode(array('status' => true)));
		} else {
			$this->getResponse()->setBody(Zend_Json::encode(array('status' => false)));
		}
	}
	
	/*
	 * Monta a listagem de tarefas que o usuario logado e o executor
	 */
	public function executorAction() {
		$Tarefas = new Solicitacao_Model_Tarefa();
		$this->view->paginator = $Tarefas->executor($this->getRequest()->getParams());
		
		$Departamentos = new Solicitacao_Model_Departamentos();
		$this->view->departamento = $Departamentos->getCombo();
		$this->view->status = $Tarefas->getComboStatus('SOLICITACAO');
	}
	
	/*
	 * Monta a listagem de tarefas que o usuario logado e o criador
	 */
	public function criadorAction() {
		$Tarefas = new Solicitacao_Model_Tarefa();
		$this->view->paginator = $Tarefas->criador($this->getRequest()->getParams());
		
		$Departamentos = new Solicitacao_Model_Departamentos();
		$this->view->departamento = $Departamentos->getCombo();
		$this->view->status = $Tarefas->getComboStatus('SOLICITACAO');		
	}
	
	/*
	 * Monta a listagem de tarefas do usuario logado que estao finalizadas
	 */
	public function finalizadasAction() {
		$Tarefas = new Solicitacao_Model_Tarefa();
		$this->view->paginator = $Tarefas->finalizadas($this->getRequest()->getParams());
		$this->view->status = $Tarefas->getComboStatus('SOLICITACAO');
	}
	
	/*
	 * Assumir uma tarefa
	 */
	public function assumirAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		
		if ($this->getRequest()->getParam('id')) {
			$Tarefas = new Solicitacao_Model_Tarefa();
			$User = Zend_Auth::getInstance()->getIdentity();

			// envia tarefa para trello, apenas para o usuario of IF (por enquanto)
			// deligado migramos de trello para planner (MS)
			if ($User->ds_nome_usuario == 'DESATIVADO') {
				$Task = $Tarefas->getRow($this->getRequest()->getParam('id'));
				$Trello = new Agro_Utils_Trello($User);
				$Trello->setCardName($this->getRequest()->getParam('id').' - '.$Task['ds_titulo']);
				$Trello->setCardDescription($Task['ds_descricao']);
				$Trello->newCard();

				if ($Trello->getCardId() != null) {
					$TarefasArquivos = new Solicitacao_Model_TarefaArquivos();
					$Files = $TarefasArquivos->getArquivos($this->getRequest()->getParam('id'));

					if (count($Files)>0) {
						foreach($Files as $f) {
							if (file_exists(APPLICATION_PATH.'/upload_files/solicitacoes/'.$f['ds_nome_arquivo'])) {
								$Trello->addAttachment(
									$Trello->getCardId(), 
									$f['ds_nome_arquivo'], 
									APPLICATION_PATH.DIRECTORY_SEPARATOR.'upload_files'.DIRECTORY_SEPARATOR.'solicitacoes'.DIRECTORY_SEPARATOR);
							}
						}
					}
				}
			}

			if ($Tarefas->assumir($this->getRequest()->getParams())) {
				$this->getResponse()->setBody(Zend_Json::encode(array('status' => true)));
			} else {
				$this->getResponse()->setBody(Zend_Json::encode(array('status' => false)));
			}
		}
	}
	
	/*
	 * Altera o status de uma solicitacao
	 */
	public function statusAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		
		if ($this->getRequest()->getParam('id') && $this->getRequest()->getParam('id_status')) {
			$Tarefas = new Solicitacao_Model_Tarefa();
			$Row = $Tarefas->find($this->getRequest()->getParam('id'))->current();
			$Row->id_status = $this->getRequest()->getParam('id_status');
			$Row->fl_del = 0;
			
			if ($Tarefas->save($Row->toArray())) {
				$this->getResponse()->setBody(Zend_Json::encode(array('status' => true)));
			} else {
				$this->getResponse()->setBody(Zend_Json::encode(array('status' => false)));
			}
		}
	}
	
	/*
	 * Informar que esta executando uma tarefa
	 */
	public function executarAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$Tarefas = new Solicitacao_Model_Tarefa();
		
		if ($this->getRequest()->getParam('id')) {
			$Row = $Tarefas->find($this->getRequest()->getParam('id'))->current();
			$Row->dados();
			
			switch ($Row->status) {
				case 'EXECUTANDO':
					$image = '/resources/imagens/mini/start.png';
					$Row->id_status = $Tarefas->getIdStatus('ANDAMENTO', 'SOLICITACAO');
					$Row->fl_del = 0;
					break;
				default:
					$image = '/resources/imagens/mini/stop.png';
					$Row->id_status = $Tarefas->getIdStatus('EXECUTANDO', 'SOLICITACAO');
					$Row->fl_del = 0;
					break;
			}
			
			$Tarefas->save($Row->toArray());
			
			$Row = $Tarefas->find($this->getRequest()->getParam('id'))->current();
			
			$Row->status();
			$return = array(
				'status' => $Row->label_status,
				'image' => $image,
				'id' => $Row->id
			);
			
			$this->getResponse()->setBody(Zend_Json::encode($return));
		}
	}
	
	/*
	 * Informar que finalizou uma tarefa
	 */
	public function finalizarAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		
		if ($this->getRequest()->getParam('id')) {
			$Tarefas = new Solicitacao_Model_Tarefa();
			$Row = $Tarefas->find($this->getRequest()->getParam('id'))->current();
			$Row->id_status = $Tarefas->getIdStatus('FINALIZADO', 'SOLICITACAO');
			$Tarefas->save($Row->toArray());
			$this->getResponse()->setBody(Zend_Json::encode(array('status' => true)));
		}
	}
	
	/*
	 * Busca os tipos do departamento
	 */
	public function tiposAction() {
		$Tipos = new Solicitacao_Model_TarefaTipo();
		$this->isAjax();
		
		if ($this->getRequest()->getParam('id')) {
			$Rs = $Tipos->getCombo($this->getRequest()->getParam('id'));
			
			$options = array('');
			if (is_array($Rs)) {
				foreach ($Rs as  $id => $tipo) {
					$options[] = array('id' => $id, 'label' => $tipo);
				}
			}
			
			$this->getResponse()->setBody(Zend_Json::encode($options));		
		}
	}
	
	public function tipotarefaAction() {
		$Tipos = new Solicitacao_Model_TarefaTipo();
		$this->isAjax();
		
		if ($this->getRequest()->getParam('id')) {
			$Rs = $Tipos->getCombo($this->getRequest()->getParam('id'));

			$this->getResponse()->setBody(Zend_Json::encode($Rs));		
		}
	}	
	
}