<?php

class Produto_HabilitacoesestadosController extends Agro_Controller_Action {

	public function init() {
		ini_set('memory_limit','1024M');
	}

	public function loadprepostosAction() {
		$this->getHelper('layout')->disableLayout();

		$Prepostos = new Sistema_Model_Prepostos();
		$this->view->rs = $Prepostos->getCombo($this->getRequest()->getParam('corretor'));
		$this->view->produto = $this->getRequest()->getParam('produto');
	}

	public function indexAction() {
		$this->view->title = 'Relacionar Habilitações';
		$this->view->form = new Produto_Form_Habilitacao();
		$this->view->isPost = $this->getRequest()->isPost();
		$params = $this->getRequest()->getParams();
		
		if ($this->view->isPost) {
			$modelHabilitacoes = new Produto_Model_Habilitacoes();

			if ($params['save']) {
				try{
					if ($params['fl_habilitar_prepostos']) {
						$modelHabilitacoes->adicionarHabilitacoesFila($params);
						$msg = "A configuração será finalizada em instantes. Você receberá um e-mail assim que o processo for concluído!";
					} else {
						$modelHabilitacoes->saveEstados($params);
						$msg = "Salvo com sucesso!";
					}
					
					$this->view->showMessage($msg, 'success');
				}catch(Exception $e){
					$msg = "Não foi possível processar as habilitações.";
					$this->view->showMessage($msg, 'error');
				}
			}

			$this->view->estados = $modelHabilitacoes->getHabilitacoesEstados($params);
		}

		$this->view->form->populate($params);
		
	}

	public function checkedAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();

		$Habilitacoes = new Produto_Model_Habilitacoes();
		$this->getResponse()->setBody(Zend_Json::encode($Habilitacoes->fetchCheckeds($this->getRequest()->getParams())));
	}

	public function viewAction() {
		$Model = new Produto_Model_Habilitacoes();

		$habilitacoes = array();
		$Rs = $Model->fetchUsuario($this->getRequest()->getParam('id_usuario'));
		foreach ($Rs as $item) {
			$habilitacoes[$item['id_produto']]['NOME'] = $item['ds_nome_produto'];
			$habilitacoes[$item['id_produto']]['ESTADOS'][$item['ds_sigla']][] = $item['ds_nome_municipio'];
		}

		$this->view->data = $habilitacoes;
	}

	public function geraxlsAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();

		$dados = $this->getRequest()->getParams();
		$Model = new Produto_Model_Habilitacoes();

		$ret = $Model->getHabilitacoesXls($dados);
		require_once(APPLICATION_PATH . "/../library/Agro/xls/PhpSimpleXlsGen.php");

		$myxls = new PhpSimpleXlsGen();
		$myxls->totalcol = 4;
		$myxls->InsertText('Corretor');
		$myxls->InsertText('Produto');
		$myxls->InsertText('Safra');
		$myxls->InsertText('UF');
		//$myxls->InsertText('Municipio');

		foreach($ret as $linha) {
			$myxls->InsertText( utf8_decode($linha['ds_nome_corretor']) . ' ');
			$myxls->InsertText( utf8_decode($linha['ds_nome_produto']) . ' ' );
			$myxls->InsertText( utf8_decode($linha['ds_nome_safra']) . ' ' );
			$myxls->InsertText( $linha['ds_sigla'] . ' ' );
			//$myxls->InsertText( utf8_decode($linha['ds_nome_municipio']) . ' ' );
		}

		$myxls->SendFile();

		//$this->forward($this->_forwardSettings);
	}

	public function comercialAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();

		$Corretores = new Sistema_Model_Corretores();

		if ($this->getRequest()->getParam('id_comercial')>0) {
			$combo = $Corretores->getComboUsuarioComercialVinculado(false, $this->getRequest()->getParam('id_comercial'));
		} else {
			$combo = $Corretores->getComboUsuario(false);
		}

		$opt = array();
		$opt[] = array('id' => '', 'label' => '');
		if (count($combo)>0) {
			foreach ($combo as $id => $label) {
				$opt[] = array('id' => $id, 'label' => $label);
			}
		}

		$Json = new Zend_Json();
		$this->getResponse()->setBody($Json->encode($opt));
	}

}

