<?
/**
 * Controller 
 * 
 * Laudo final Tipo LF016 Item Segurado | Safra14 - Soja - Granizo
 * 
 * @author Samir Ortiz
 * @date 15/03/2012
 *
 */
class Safra14_Lf016itemseguradoController 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 Safra14_Model_Lf016ItensSeguradosAmostras();
	}

	public function getModelItemSegurado() {
	    return new Safra14_Model_Lf016ItensSegurados();
	    
	}

	public function getForm($dados = array()) {
	    return new Sinistro_Form_LaudoFinalItemSegurado(array('dados'=>$dados));
	}	
    
    public function formAction() {        
    	$this->view->back = '/'.$this->_request->module."/lf016/form/id_proposta/".$this->__params["id_proposta"]."/id_processo/".$this->__params["id_processo"]."/id_cobertura/".$this->__params["id_cobertura"]."/id_vistoria/".$this->__params["id_vistoria"]."/id_laudo_final/".$this->__params["id_laudo_final"];
		$this->view->jQuery()->addStylesheet('/resources/css/sinistro/laudo.css');
        $this->view->jQuery()->addJavascriptFile('/resources/javascript/sinistro/laudofinalitemsegurado.js');

        $ProcessosEmpresas = new Sinistro_Model_ProcessosEmpresas();

		if($this->getRequest()->isPost()){

			$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);

        	if($Form->isValid($Form->getValues())) {
        		$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"));

        	} else {
        		$this->view->showMessage($this->getAllFormErros(array($Form),$_POST), '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->prepararDados($this->__params);
		}
		
		//$this->__params = $Model->avaliacao($this->__params);

    	$this->view->dados = $this->__params;   
    }
}
