<?

class Produto_CondicaopagamentoproponenteController  extends Agro_Controller_Action  {
	public function init() {
		$this->view->title = "Gerência de Produtos » Relacionar Proponentes";
		$this->view->id_produto = $this->getRequest()->getParam('id_produto');
		$this->view->id_condicao_pagamento = $this->getRequest()->getParam('id_condicao_pagamento');
		$this->view->back = "/produto/produto/form/id/{$this->view->id_produto}/tab/4";
	}
	
	public function getForm() {
		return null;
	}
	
	public function getModel(){ 
		return new Produto_Model_Condicaopagamentoproponente();
	}
	
	public function indexAction() {
		$Model = $this->getModel();
		
		if ($this->getRequest()->isPost()) {
			$this->save($this->getRequest()->getParams());
		}
		
		$arr = $this->getRequest()->getParams();
		//baacaaaaaaaaaa - filtro por id ou nao. nesse caso,  filtra
		$arr['in'] = 1;
		
		$this->view->paginator = $Model->getData($arr);
	}
	
	public function save(array $post) {
		try {
			$Model = $this->getModel();
			if (array_key_exists('id_proponente', $post)) {
				if (is_array($post['id_proponente'])) {
					foreach ($post['id_proponente'] as $proponente) {
						$data = array();
						$data['id_condicao_pagamento'] = $post['id_condicao_pagamento'];
						$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 = $this->getModel();
		$arr = $this->getRequest()->getParams();
		//baacaaaaaaaaaa - filtro por id ou nao. nesse caso, não filtra
		$arr['in'] = 0;
		$this->view->paginator = $Model->getData($arr);
	}
	
	public function deleteAction() {
		$this->getHelper('ViewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
		$Model = $this->getModel();
		
		if ($Model->delete($this->getRequest()->getParam('id_condicao_pagamento'), $this->getRequest()->getParam('id_proponente'))) {
			$this->view->message(MSG_SUCCESS, 'success', 'afterDelete()');
		} else {
			$this->view->message(MSG_ERROR, 'error');
		}
		flush();
		exit;
	}
}