<?php

class Comercializacao_RelacionartaxaproponenteController extends Agro_Controller_TaxaAgrupamento {
	
	public function init() {
		$this->view->title = "Gerência de Taxas » Relacionar Proponentes";

        if ($this->getRequest()->getParam('id')) {
            $Agrupamento = (new Comercializacao_Model_TaxasAgrupamentos())->find($this->getRequest()->getParam('id'))->current();
            $this->view->taxa = $Agrupamento['ds_nome_agrupamento_taxas'];
        }

        parent::init();
	}
	
	public function settaxaAction() {
		$Session = new Zend_Session_Namespace('taxaproponente');
		$Session->id_taxa = $this->getRequest()->getParam('id');

		$this->_redirect('/comercializacao/relacionartaxaproponente');
	}
	
	public function indexAction() {
		$Model = new Comercializacao_Model_TaxasAgrupamentoUsuarios();
		$Corretores = new Sistema_Model_Corretores();
		
		if ($this->getRequest()->isPost()) {
			$this->save($this->getRequest()->getParams());
		}
		
		$Session = new Zend_Session_Namespace('taxaproponente');
		$ModelTaxaProponentes = new Comercializacao_Model_RelacionarTaxasProponente();
		$arr = $this->getRequest()->getParams();
		$arr['id_taxa'] = $Session->id_taxa;
		$arr['in'] = 1;
		
		$this->view->paginator = $ModelTaxaProponentes->getData($arr);
	}
	
	public function save(array $post) {
		try {
			$Model = new Comercializacao_Model_RelacionarTaxasProponente();
			if (array_key_exists('id_proponente', $post)) {
				if (is_array($post['id_proponente'])) {
					foreach ($post['id_proponente'] as $proponente) {
						$data = array();
						$data['id_taxa_agrupamento'] = $post['id_taxa'];
						$data['id_proponente'] = $proponente;
						
						$Model->insert($data);
					}
				}
			}
			
			$this->view->showMessage(MSG_SUCCESS, 'success');
		} catch (Exception $e) {
			$this->view->showMessage(MSG_ERROR, 'error');
		}
	}
	
	public function getproponentesAction() {
		//$this->getHelper('ViewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
		
		$Model = new Comercializacao_Model_RelacionarTaxasProponente();
		$arr = $this->getRequest()->getParams();
		$arr['in'] = 0;
		$this->view->paginator = $Model->getData($arr);
	}
	
	public function deleteAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
		$Model = new Comercializacao_Model_RelacionarTaxasProponente();
		
		if ($Model->delete($this->getRequest()->getParam('id_taxa'), $this->getRequest()->getParam('id_proponente'))) {
			$this->view->message(MSG_SUCCESS, 'success', 'afterDelete()');
		} else {
			$this->view->message(MSG_ERROR, 'error');
		}
		flush();
		exit;
	}
}