<?php

class Sistema_MunicipioController extends Agro_Controller_Action {
	public $_columnList = array(
		'ds_nome_municipio' => 'Nome',
		'nr_ibge' => array('label'=>'Nº IBGE', 'align'=>'center', 'width'=>'10%'),
		'ds_sigla' => array('label'=>'Estado', 'align'=>'center', 'width'=>'10%'),
        'ds_nome_regiao' => array('label'=>'Região', 'align'=>'center', 'width'=>'10%')
	);
	
	public function init() {
		$this->view->title = "Sistema » CEP's » Municípios";
	}
	
	public function setBack() {
		$this->view->back = '/sistema/municipio';
	}
	
	public function getModel() {
		return new Sistema_Model_Municipios();
	}
	
	public function getForm() {
		return new Sistema_Form_Municipio();
	}
	
	public function getFilter() {
		$where = array();
		
		if ($this->getRequest()->getParam('ds_nome_municipio')) {
			$where[] = array(
				'field' => 'ds_nome_municipio',
				'operator' => 'ilike',
				'value' => '%'.$this->getRequest()->getParam('ds_nome_municipio').'%'
			);
		}
		
		if ($this->getRequest()->getParam('id_estado')) {
			$where[] = array(
				'field' => 'id_estado',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('id_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();
		$Model = $this->getModel();
		
		$Rs = false;
		
		if ($this->getRequest()->getParam('id_estado')) {
			$arEstados = explode(',', $this->getRequest()->getParam('id_estado'));
			if (count($arEstados) > 1) {
				$Rs = $Model->getCombo($arEstados);
			} else {
				$Rs = $Model->getCombo($this->getRequest()->getParam('id_estado'));
			}
		// dump($Rs);
		} else if ($this->getRequest()->getParam('ds_sigla')) {
			$Rs = $Model->getComboSigla($this->getRequest()->getParam('ds_sigla'));
		}
		
		if ($Rs) {
			$this->getResponse()->setBody(Zend_Json::encode($Rs));
		} else {
			return false;
		}
	}

	public function getmunicipiosmultiselectAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
		$Model = $this->getModel();
		
		$Rs = false;
		
		if ($this->getRequest()->getParam('id_estado')) {
			$arEstados = explode(',', $this->getRequest()->getParam('id_estado'));
			if (count($arEstados) > 1) {
				$Rs = $Model->getCombo($arEstados, false);
			} else {
				$Rs = $Model->getCombo($this->getRequest()->getParam('id_estado'), false);
			}
		// dump($Rs);
		} else if ($this->getRequest()->getParam('ds_sigla')) {
			$Rs = $Model->getComboSigla($this->getRequest()->getParam('ds_sigla'), false);
		}

		$result = array();
		foreach ($Rs as $key => $value) {
			$result[$key] = array(
				'value' => $value['id'],
				'text' => $value['nome']
			);
		}
		
		if ($result) {
			$this->getResponse()->setBody(Zend_Json::encode($result));
		} else {
			return false;
		}
	}
}
