<?php

class Produto_CiclosController extends Agro_Controller_Action {
	public $_columnList = array(
		'ds_nome_ciclo' => 'Nome',
		'cd_ciclo_seguradora' => 'Cód. da Seguradora',
		'ds_chave_ciclo' => 'Chave'
	);
	
	public function init() {
		$this->view->title = 'Produto » Cadastro » Ciclos';
	}
	
	public function getModel() {
		return new Produto_Model_Ciclos();
	}
	
	public function getForm() {
		return new Produto_Form_Ciclos();
	}
	
	public function getFilter() {
		$where = array();
		
		if ($this->getRequest()->getParam('ds_nome_ciclo')) {
			$where[] = array(
				'field' => 'ds_nome_ciclo',
				'operator' => 'ilike',
				'value' => '%'.$this->getRequest()->getParam('ds_nome_ciclo').'%'
			);
		}
		
		if ($this->getRequest()->getParam('cd_ciclo_seguradora')) {
			$where[] = array(
				'field' => 'cd_ciclo_seguradora',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('cd_ciclo_seguradora')
			);
		}
		if ($this->getRequest()->getParam('fl_ativo')) {
			$where[] = array (
				'field' => 'fl_ativo',
				'operator' => ' = ',
				'value' => $this->getRequest()->getParam('fl_ativo')
			);
		}
		if ($this->getRequest()->getParam('ds_chave_ciclo')) {
			$where[] = array (
				'field' => 'ds_chave_ciclo',
				'operator' => 'ilike',
				'value' => '%' .$this->getRequest()->getParam('ds_chave_ciclo') . '%'
			);
		}
		return $where;
	}
	
	public function geraxlsAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
		$dados = $this->getRequest()->getParams();
		$Model = $this->getModel();
		
		$ret = $Model->fetchAll();
		require_once(APPLICATION_PATH . "/../library/Agro/xls/PhpSimpleXlsGen.php");
		
		$myxls = new PhpSimpleXlsGen();
		$myxls->totalcol = 5;
		//ID | NOME | COD SEGURADORA | CHAVE | ATIVO
		$myxls->InsertText(utf8_decode('Código'));
		$myxls->InsertText('Nome');

		$myxls->InsertText(utf8_decode('Código Seguradora'));
		$myxls->InsertText(utf8_decode('Chave'));
		$myxls->InsertText('Ativo');
		foreach($ret as $linha) {
			$myxls->InsertText( utf8_decode($linha['id']) . ' ');
			$myxls->InsertText( utf8_decode($linha['ds_nome_ciclo']) . ' ');
			$myxls->InsertText( utf8_decode($linha['cd_ciclo_seguradora']) . ' ');
			$myxls->InsertText( utf8_decode($linha['ds_chave_ciclo']) . ' ');
			
			$myxls->InsertText( $linha['fl_ativo'] ? 'Ativo' : 'Inativo' );
		}
		
		$myxls->SendFile();
	}
}