<?php

class Sistema_EstadoController extends Agro_Controller_Action{
	public $_columnList = array(
		'ds_nome_estado' => 'Nome',
		'ds_sigla' => array('label'=>'Sigla', 'align'=>'center', 'width'=>'10%'),
		'nr_ibge' => array('label'=>'Nº IBGE', 'align'=>'center', 'width'=>'10%')
	);
	
	public function init() {
		$this->view->title = "Sistema » CEP's » Estados";
	}
	
	public function setBack() {
		$this->view->back = '/sistema/estado';
	}
	
	public function getModel() {
		return new Sistema_Model_Estados();
	}
	
	public function getForm() {
		return new Sistema_Form_Estado();
	}
	
	public function getFilter() {
		$where = array();
		
		if ($this->getRequest()->getParam('ds_nome_estado')) {
			$where[] = array(
				'field' => 'ds_nome_estado',
				'operator' => 'ilike',
				'value' => '%'.$this->getRequest()->getParam('ds_nome_estado').'%'
			);
		}
		
		if ($this->getRequest()->getParam('fl_ativo')) {
			$where[] = array(
				'field' => 'fl_ativo',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('fl_ativo')
			);
		}
		
		return $where;
	}
	
	public function getmunicipiosAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
		
		if ($this->getRequest()->getParam('id')) {
			$Municipio = new Sistema_Model_Municipios();
			$municipios = $Municipio->getCombo($this->getRequest()->getParam('id'));
			
			$opt = array();
			if (is_array($municipios)) {
				foreach ($municipios as $key => $text) {
					$opt[] = array('id'=>$key,'label'=>$text);
				}
			}
			
			$Json = new Zend_Json();
			$this->getResponse()->setBody($Json->encode($opt));
		}
	}
}
