<?php

class Sinistro2_GruposvistoriaController extends Agro_Controller_Action {
	public $_columnList = array(
		'ds_nome_grupo_vistoria' => 'Nome',
	    'dt_inicio_grupo' => array('label'=>'Data Início', 'align'=>'center', 'type' => 'data'),
		'dt_expira_grupo' => array('label'=>'Data Expiração', 'align'=>'center', 'type' => 'data')
	);

	public function setBack() {
		$this->view->back = '/sinistro2/gruposvistoria';
	}

	public function init() {
		$this->view->title = 'Sinistro » Cobrança » Configurações » Grupos de Vistoria';
	}

	public function getModel() {
		return new Sinistro2_Model_Gruposvistoria();
	}

	public function getForm() {
		return new Sinistro2_Form_Grupovistoria();
	}

	public function culturasAction() {
		$Grupos = new Sinistro2_Model_Gruposvistoria();
		$this->isAjax(true);

		if ($this->getRequest()->getParam('id')) {
			$Culturas = new Produto_Model_Culturas();
			$this->view->culturas = $Culturas->fetchAll('fl_ativo is true', 'ds_nome_cultura');
			$this->view->grupo = $Grupos->find($this->getRequest()->getParam('id'))->current();
		}
	}

	public function relacionarculturasAction() {
		$Grupos = $this->getModel();
		$this->isAjax();

		if ($this->getRequest()->isPost() && $this->getRequest()->getParam('id')) {
			if ($Grupos->relacionarCulturas($this->getRequest()->getParams())) {
				$this->getResponse()->setBody(Zend_Json::encode(
					array('message' => 'Culturas Relacionadas com Sucesso!!!', 'closeDialog' => '#divAjaxDialog', 'status' => true)
				));
			} else {
				$this->getResponse()->setBody(Zend_Json::encode(
					array('message' => 'Não foi possível relacionar as culturas.', 'status' => false)
				));
			}
		}
	}

	
	public function estadosAction() {
		$Grupos = $this->getModel();
		$this->isAjax(true);
	
		if ($this->getRequest()->getParam('id')) {
			$Estados = new Produto_Model_Estados();
			$this->view->estados = $Estados->fetchAll('fl_ativo is true', 'ds_nome_estado');
			$this->view->grupo = $Grupos->find($this->getRequest()->getParam('id'))->current();
		}
	}
	
	public function relacionarestadosAction() {
		$Grupos = $this->getModel();
		$this->isAjax();
	
		if ($this->getRequest()->isPost() && $this->getRequest()->getParam('id')) {
			if ($Grupos->relacionarEstados($this->getRequest()->getParams())) {
				$this->getResponse()->setBody(Zend_Json::encode(
					array('message' => 'Estados relacionados com Sucesso!!!', 'closeDialog' => '#divAjaxDialog', 'status' => true)
				));
			} else {
				$this->getResponse()->setBody(Zend_Json::encode(
					array('message' => 'Não foi possível relacionar os estados.', 'status' => false)
				));
			}
		}
	}	
	
	public function getFilter() {
		$where = array();

		if ($this->getRequest()->getParam('ds_nome_grupo_vistoria')) {
			$where[] = array (
				'field' => 'ds_nome_grupo_vistoria',
				'operator' => 'ilike',
				'value' => '%'.$this->getRequest()->getParam('ds_nome_grupo_vistoria').'%'
			);
		}

		if ($this->getRequest()->getParam('fl_ativo')) {
			$where[] = array (
				'field' => 'fl_ativo',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('fl_ativo')
			);
		}

		return $where;
	}
}