<?
/**
 * @name Safra16_Lf003itemseguradocfController
 * @desc Item segurado laudo final Tipo LF003 / Safra16 
 * @author Vinicius Marconi Vasconcelos Berni
 * @since 20/03/2011 10:02
 * @see Agro_Controller_Action
 */
class Safra16_Lf003itemseguradoController extends Agro_Controller_Action{
	private $__ModelIS;
	private $__params;
	private $__IS;
	
	public function init() {
		$ModelLfIS = new Sinistro_Model_LaudosFinaisItensSegurados();
		
		$this->__params = $this->getRequest()->getParams();
		$this->__ModelIS = new Seguro_Model_ItensSegurados();
		$this->__IS = $this->__ModelIS->find($this->__params["id_item_segurado"])->current();
		
		$this->view->title = "Sinistro » Processos » Laudo Final » Item Segurado {$this->__IS->ds_item_segurado}";
		
		$LFIS = $ModelLfIS->find($this->__params["id_laudo_final"], $this->__params["id_item_segurado"]);
		$this->__params["id_laudo_final_item_segurado"] = (count($LFIS) ? $LFIS["id"] : false);	
	}
	
	public function getModel() {
		return new Safra16_Model_Lf003ItensSeguradosAmostras();
	}
	
	public function getModelItemSegurado() {
	    return new Safra16_Model_Lf003ItensSegurados();
	}

	public function getForm($dados = array()) {
	    return new Sinistro_Form_LaudoFinalItemSegurado(array('dados'=>$dados));
	}	
    
    public function formAction() {        
    	$this->view->back = '/'.$this->_request->module.'/lf003/form/id_laudo_final/'.$this->__params['id_laudo_final'].'/id_processo/'.$this->__params['id_processo'];
		$this->view->jQuery()->addStylesheet('/resources/css/sinistro/laudo.css');
        $this->view->jQuery()->addJavascriptFile('/resources/javascript/sinistro/laudofinalitemsegurado.js');
		
		$idLaudoFinalItemSegurado = false;
		
        if($this->getRequest()->isPost()){
        	$Model = $this->getModel();
			$this->__params["id_laudo_final_item_segurado"] = $Model->save($this->__params);
			$this->view->showMessage($Model->getMessages(), ($this->__params["id_laudo_final_item_segurado"] ? "success" : "error"));
        }
        
        $this->populate();
    }
    
    public function populate(){
    	$ProcessosEmpresas = new Sinistro_Model_ProcessosEmpresas();
    	
    	$Model = $this->getModel();
    	
    	if ($this->__params["id_laudo_final_item_segurado"]){
    		$this->__params = array_merge($this->__params, $Model->get($this->__params["id_laudo_final_item_segurado"]));
    	}
    	
    	$dadosForm = array(
			"id_laudo_final" => $this->__params["id_laudo_final"],
			"id_item_segurado" => $this->__params["id_item_segurado"],
			"id_empresa_vistoriadora" => $ProcessosEmpresas->getEmpresaByProcesso($this->__params['id_processo']),
		);
    	
    	$Form = $this->getForm($dadosForm);
    	
        $Form->populate($this->__params);

		$this->view->form = $Form;

		if(!array_key_exists("amostras", $this->__params)){
			$this->__params = $Model->montaArray($this->__params);
		}
		
    	$this->view->dados = $this->__params;   
    }
}
