<?php

class Sistema_DepartamentoController extends Agro_Controller_Action {	
	public function init() {
		$this->getHelper('layout')->disableLayout();
		$this->view->title = "Gerência de Departamentos";
		$this->view->back = '/sistema/empresa';
	}
	
	public function getModel() {
		return new Sistema_Model_Departamentos();
	}
	
	public function getForm() {
		$params = $this->getRequest()->getParams();
		
		$Form = new Sistema_Form_Departamento(array('dados'=>$params));
		$Form->populate($params);
		
		return $Form;
	}
	
	public function getFilter() {
		$where = array();
		
		if ($this->getRequest()->getParam('id_empresa')) {
			$where[] = array(
				'field' => 'id_empresa',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('id_empresa')
			);
		}
		
		if ($this->getRequest()->getParam('ds_nome_departamento')) {
			$where[] = array(
				'field' => 'ds_nome_departamento',
				'operator' => 'ilike',
				'value' => '%'.$this->getRequest()->getParam('ds_nome_departamento').'%'
			);
		}
		
		if ($this->getRequest()->getParam('fl_ativo')) {
			$where[] = array(
				'field' => 'fl_ativo',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('fl_ativo')
			);
		}
		
		return $where;
	}

	public function saveAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		
		$Model = $this->getModel();
		
		if ($this->getForm()->isValid($this->getRequest()->getParams())){
			if ($Model->save($this->getRequest()->getParams())) {
				$this->getResponse()->setBody('Processo realizado com sucesso.');
			} else {
				$this->getResponse()->setBody('Não foi possível realizar o processo.');
			}
			
		}else {
			// TO-DO: Descobrir como fazer isso direito... :)  Vinicius Berni - 18/05/2010
			$_params = $this->getRequest()->getParams();
			$_message = 'Os seguintes campos devem ser preenchidos:
';
			
			if ($_params['ds_nome_departamento'] == ''){
				$_message .= ' - Nome do departamento.
';
				
			}
			
			if ($_params['ds_chave'] == ''){
				$_message .=' - Chave.
';
				
			}
			
			$this->getResponse()->setBody($_message);
					
		}
	}

	public function deleteAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$Model = $this->getModel();

		try {
			$Model->delete($this->getRequest()->getParam('id'));
			$this->getResponse()->setBody('Processo realizado com sucesso.');
			$this->getResponse()->setBody(Zend_Json::encode(array('execute' => 'jQuery.listaDepartamentos()')));
		} catch (Exception $e) {
			$this->getResponse()->setBody('Não foi possível realizar o processo. (Existem funcionário(s) vinculados a esse departamento.)');
		}
	}
	
	public function getfuncionarioscomboAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
		
		if ($this->getRequest()->getParam('id')) {
			$Funcionarios = new Sistema_Model_Funcionarios();
			$combo = $Funcionarios->getCombofuncionariosUsuarios($this->getRequest()->getParam('id'));
			
			$opt = array();
			
			if (is_array($combo)) {
				foreach ($combo as $key => $text) {
					$opt[] = array('id'=>$key,'label'=>$text);
				}
			}
			
			$this->getResponse()->setBody(Zend_Json::encode($opt));
		}
	}
	
	
	public function getdepartamentosAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
		$Model = $this->getModel();
		
		$Rs = $Model->getComboDeptEmpresa($this->getRequest()->getParam('id_empresa'));		
		
		if (is_array($Rs)) {			
			$this->getResponse()->setBody(Zend_Json::encode($Rs));
		} else {
			return false;
		}
	}

    public function listAction()
    {
        $this->view->empresaVistoria = $this->getRequest()->has('empresaVistoria') && $this->getRequest()->getParam('empresaVistoria') === '1';

        parent::listAction();
    }
}