<?php
class Atendimento_ProponenteController extends Agro_Controller_Action{
	
	public function getModel() {
		return new Atendimento_Model_Proponente();
	}
	
	public function getForm() {
		return new Atendimento_Form_Proponente();
	}
	
	public function getFilter() {
		$where = array();
		
		if ($this->getRequest()->getParam('ds_nome_proponente')) {
			$where[] = array (
				'field' => 'ds_nome_proponente',
				'operator' => 'ilike',
				'value' => '%'.$this->getRequest()->getParam('ds_nome_proponente').'%'
			);
		}
		
		if ($this->getRequest()->getParam('cpf_cnpj')) {
			$where[] = array (
				'field' => 'cpf_cnpj',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('cpf_cnpj')
			);
		}
		
		if ($this->getRequest()->getParam('fl_ativo')) {
			$where[] = array (
				'field' => 'p.fl_ativo',
				'operator' => '=',
				'value' => $this->getRequest()->getParam('fl_ativo')
			);
		}
		
		return $where;
	}

	public function formsociosAction(){
		$this->isAjax(true);

		$Model = new Seguro_Model_PropostasProponentesSocios();
		$socio = $Model->getSocio($this->getRequest()->getParam('id'));

		$this->view->id = $socio['id'];	
		$this->view->ds_nome_socio = $socio['ds_nome_socio'];
		$this->view->cpf_cnpj = $socio['cpf_cnpj'];
		$this->view->tp_pessoa = $socio['tp_pessoa'];	
		$this->view->id_parent = $socio['id_parent'];
		$this->view->cnpj_segurado = $socio['cnpj_segurado'];

	}	
	
	public function sociosAction(){
		$this->isAjax(true);
		
		if($this->getRequest()->getParam('id_parent')){
			$this->view->id_parent = $this->getRequest()->getParam('id_parent');	
		}

		$this->view->cnpj_segurado = $this->getRequest()->getParam('cnpj_segurado');
	}
	
	public function getsociosAction(){
		$this->isAjax();

		$Model = new Seguro_Model_PropostasProponentesSocios();
		$socios = $Model->getSociosByCnpjSegurado($this->getRequest()->getParam('cnpj'),$this->getRequest()->getParam('id_parent'));
		foreach($socios as $key => $value){
			$dados[$key] = $value;
		}
		$this->getResponse()->setBody(Zend_Json::encode($dados));

	}	
	

	public function savesocioAction(){
		$this->isAjax();
		$Model = new Seguro_Model_PropostasProponentesSocios();

		$dados = array();
		
		$dados['cnpj_segurado'] = $this->getRequest()->getParam('cnpj_segurado');
		$dados['id_parent'] = $this->getRequest()->getParam('id_parent');

		if($Model->save($this->getRequest()->getParams())){
			$return = array();
			$return['cnpj_segurado'] = $dados['cnpj_segurado'];
			$return['id_parent'] = $dados['id_parent'];
		}

		$this->getResponse()->setBody(Zend_Json::encode($return));
		
	}
	
	public function removesocioAction(){
		$this->isAjax();
		$Model = new Seguro_Model_PropostasProponentesSocios();
		
		if($Model->removeSocio($this->getRequest()->getParam('id'))){
			$return = true;
		}

		$this->getResponse()->setBody(Zend_Json::encode($return)); 
	}
	
	
	
}