<?php

class Produto_CoberturaController extends Agro_Controller_Action {
	public $_columnList = array(
		'ds_nome_cobertura' => 'Nome',
		'ds_chave_cobertura' => 'Chave'
	);

	public function init() {
		$this->view->title = 'Produto » Cadastro » Coberturas';
	}

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

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

	public function viewAction() {
		parent::viewAction();

		$Model = $this->getModel();
		$this->view->data['eventos'] = $Model->getEventos($this->getRequest()->getParam('id'));
	}

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

	public function getFilter() {
		$where = array();

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

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

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

		return $where;
	}

	/*
	public function save(array $post) {
		$Model = $this->getModel();

		if ($Model->save($post)){
			$this->_redirect('/produto/cobertura');

		} else {
			$this->view->showMessage("Não foi possível realizar a operação", 'error');
		}


		try {
			$Model = $this->getModel();

			$data = array();
			$data['ds_nome_cobertura'] = $post['ds_nome_cobertura'];
			$data['ds_chave_cobertura'] = $post['ds_chave_cobertura'];
			$data['ds_descricao_cobertura'] = $post['ds_descricao_cobertura'];
			$data['cd_cobertura_seguradora'] = $post['cd_cobertura_seguradora'];

			if ($post['id']) {
				$Model->update($data, "id = {$post['id']}");
			} else {
				$Model->insert($data);
			}

			$this->_redirect('/produto/cobertura');
		} catch (Exception $e) {
			$this->view->showMessage($e->getMessage(), 'error');
		}

	}
	*/

	public function deleteAction() {
		$Model = new Produto_Model_CoberturasEventos();
		$id = $this->getRequest()->getParam('id');
		$Model->delete($id, 'id_cobertura');

		parent::deleteAction();
	}

	public function geraxlsAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
		$dados = $this->getRequest()->getParams();
		$Model = $this->getModel();

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

		$myxls = new PhpSimpleXlsGen();
		$myxls->totalcol = 5;
		//ID | NOME | CHAVE | COD SEGURADORA | ATIVO
		$myxls->InsertText(utf8_decode('Código'));
		$myxls->InsertText('Nome');
		$myxls->InsertText('Chave');
		$myxls->InsertText(utf8_decode('Código Seguradora'));
		$myxls->InsertText('Ativo');

		foreach($ret as $linha) {
			$myxls->InsertText( utf8_decode($linha['id']) . ' ');
			$myxls->InsertText( utf8_decode($linha['ds_nome_cobertura']) . ' ');
			$myxls->InsertText( utf8_decode($linha['ds_chave_cobertura']) . ' ');
			$myxls->InsertText( utf8_decode($linha['cd_cobertura_seguradora']) . ' ');

			$myxls->InsertText( $linha['fl_ativo'] ? 'Ativo' : 'Inativo' );
		}

		$myxls->SendFile();
	}

}
