<?php

class Sinistro_Model_MotivosQuadraNaoVistoriada extends Agro_Db_Table_Abstract {
	protected $_schema = 'sinistro';
	protected $_name = 'motivos_quadra_nao_vistoriada';
	protected $_primary = 'id';
	
	public function save(array $data) {
		$Row = ($data[id] ? $this->find($data[id])->current() : $this->createRow());
		
		$Row->ds_motivo = $data[ds_motivo];
		
		return $Row->save();
	}
	
	public function getComboLaudo(array $ItensSegurados) {
	    $select = $this->select()->setIntegrityCheck(false);
	    $select->from(array('m'=>$this->_schema.'.'.$this->_name), array('id', 'ds_motivo'));
	    
	    $MotivosQuadraNaoVistoriada = $this->_db->fetchAll($select);
	    
	    $Combos = array();
	    
	    $htmlCombo  = '<select name="id_motivo_quadra_nao_vistoriada[{ID_ITEM_SEGURADO}]" id="id_motivo_quadra_nao_vistoriada_{ID_ITEM_SEGURADO}">';
	    $htmlCombo .= '   <option value="0">Selecione um motivo</option>';
	    foreach($MotivosQuadraNaoVistoriada as $Motivo) {
	        $htmlCombo .= '   <option value="'.$Motivo['id'].'"{SELECTED_'.$Motivo['id'].'}>'.$Motivo['ds_motivo'].'</option>';
	    }
	    $htmlCombo .= '</select>';
	    
	    foreach ($ItensSegurados as $ItemSegurado) {
	        $htmlComboItemSegurado = str_replace('{ID_ITEM_SEGURADO}', $ItemSegurado['id_item_segurado'], $htmlCombo);
	        if($ItemSegurado['fl_nao_vistoriada']) {
	            $htmlComboItemSegurado = str_replace('{SELECTED_'.$ItemSegurado['id_motivo_quadra_nao_vistoriada'].'}', ' selected', $htmlComboItemSegurado);
	        }
	        $Combos[$ItemSegurado['id_item_segurado']] = $htmlComboItemSegurado;
	    }
	    
	    return $Combos;
	}

    public function getMotivosNaoVistoriadas (array $ItensSegurados) {
        
        $arMotivosQuadraNaoVistoriada = array();                        
            
        $select = $this->select()->setIntegrityCheck(false);
        $select->from(array('m'=>$this->_schema.'.'.$this->_name), array('id', 'ds_motivo'));
        
        $MotivosQuadraNaoVistoriada = $this->_db->fetchAll($select);
        
        foreach ($MotivosQuadraNaoVistoriada as $Motivo) {
            $arMotivosQuadraNaoVistoriada[$Motivo['id']] = $Motivo['ds_motivo'];
        }
        
        $arQuadrasMotivos = array();
        
        foreach($ItensSegurados as $ItemSegurado) {
            if ($ItemSegurado['fl_nao_vistoriada']) {
                $arQuadrasMotivos[$ItemSegurado['id_item_segurado']] = $arMotivosQuadraNaoVistoriada[$ItemSegurado['id_motivo_quadra_nao_vistoriada']];
            } else {
                $arQuadrasMotivos[$ItemSegurado['id_item_segurado']] = '-';
            }
        }
        
        return $arQuadrasMotivos;
    }

    public function getOptions() {
        
        $arMotivos = array();
        
        $arMotivos[0] = 'Selecione o motivo';
        
        $select = $this->select()->setIntegrityCheck(false);
        $select->from(array('m'=>$this->_schema.'.'.$this->_name), array('id', 'ds_motivo'));
        
        $MotivosQuadraNaoVistoriada = $this->_db->fetchAll($select);
        foreach($MotivosQuadraNaoVistoriada as $Motivo) {
            $arMotivos[$Motivo['id']] = $Motivo['ds_motivo'];
        }
        
        return $arMotivos;
    }
}