<script type="text/javascript" src="/resources/javascript/helpers/email.js"></script>
<? if ($this->msgEncerramento === true): ?>
	<script type="text/javascript">
		$(document).ready(function(){
			$("#formDefault :input").prop("disabled", true);
		});
	</script>
<?php
endif;

$temCoberturaAdicionalReplantio = $temCoberturaQuedaParreiral = false;

foreach (Produto_Model_Coberturas::getCoberturasReplantioChave() as $coberturaChave) {
  if (in_array($coberturaChave, $this->listChaveCoberturas)) {
      $temCoberturaAdicionalReplantio = true;
  }
}

foreach (Produto_Model_Coberturas::getCoberturasQuedaParreiralChave() as $coberturaChave) {
    if (in_array($coberturaChave, $this->listChaveCoberturas)) {
        $temCoberturaQuedaParreiral = true;
    }
}
?>

<form class="formDefault" id="formDefault" name="formAvisoSinistro" method="post">
	<div class="geral">
		<input type="hidden" name="id_proposta" value="<?=$this->id_proposta?>" />
		<input type="hidden" name="id_proposta_mae" value="<?=$this->id_proposta_mae?>" />
		<input type="hidden" id="temCoberturaAdicionalReplantio" value="<?=$temCoberturaAdicionalReplantio?>" />
		<input type="hidden" id="temCoberturaQuedaParreiral" value="<?=$temCoberturaQuedaParreiral?>" />

		<?=$this->form->fl_verify_alerts?>
		<?=$this->getHeaderProposta($this->id_proposta, null)?>

		<script>
			lista = new Array();
			<?
			if (is_array($this->listDtAvisos)) {
				$count = 0;
				foreach($this->listDtAvisos as $item) {
					?>
					lista[<?=$count?>] = '<?=$item['dt_sinistro']?>';
					<?
					$count++;
				}
			}
			?>
			setListDtAvisos(lista);
		</script>

		<? if ($this->msgEncerramento === true): ?>
			<div style="background-color: red; color: #FFF">
				O Aviso de Encerramento de Colheita determina automaticamente o final do período de cobertura sendo vedado o início de um novo processo de regulação baseado em Aviso de Sinistro com data posterior ao do encerramento de colheita.
			</div>
		<? endif; ?>

		<fieldset>
			<legend>Informações do Sinistro</legend>

			<div class="row">
				<?=$this->form->dt_sinistro?>
				<?=$this->form->hr_sinistro?>
				<?=$this->form->min_sinistro?>
			</div>

      <?
      if ($temCoberturaQuedaParreiral) : ?>
        <div class="row">
          <table>
            <tr>
              <td colspan="2">
                <label class="required">Ocorreu Queda de Parreiral?</label>
              </td>
            </tr>
            <tr>
              <td width="1%">
                <input type='radio' name='fl_ocorrencia_queda_parreiral' id='fl_ocorrencia_queda_parreiral-S' class="ocorrencia-queda-parreiral" value='S' <?=$this->values['fl_ocorrencia_queda_parreiral'] == 'S' ? 'checked' : ''?> />
              </td>
              <td>
                <label for="fl_ocorrencia_queda_parreiral-S">Sim</label>
              </td>
            </tr>
            <tr>
              <td>
                <input type='radio' name='fl_ocorrencia_queda_parreiral' id='fl_ocorrencia_queda_parreiral-N' class="ocorrencia-queda-parreiral" value='N' <?=$this->values['fl_ocorrencia_queda_parreiral'] == 'N' ? 'checked' : ''?> />
              </td>
              <td>
                <label for="fl_ocorrencia_queda_parreiral-N">Não</label>
              </td>
            </tr>
          </table>
        </div>
      <?
      endif;

      if ($temCoberturaAdicionalReplantio):?>
        <div class="row">
          <table>
            <tr>
              <td colspan="2">
                <label class="required">Aviso de sinistro referente a cobertura adicional de replantio?</label>
              </td>
            </tr>
            <tr>
              <td width="3%">
                <input type='radio' name='fl_ocorrencia_replantio' id='fl_ocorrencia_replantio-S' class="ocorrencia-replantio" value='S' <?=$this->values['fl_ocorrencia_replantio'] == 'S' ? 'checked' : ''?> />
              </td>
              <td>
                <label for="fl_ocorrencia_replantio-S">Sim</label>
              </td>
            </tr>
            <tr>
              <td>
                <input type='radio' name='fl_ocorrencia_replantio' id='fl_ocorrencia_replantio-N' class="ocorrencia-replantio" value='N' <?=$this->values['fl_ocorrencia_replantio'] == 'N' ? 'checked' : ''?> />
              </td>
              <td>
                <label for="fl_ocorrencia_replantio-N">Não</label>
              </td>
            </tr>
          </table>
        </div>
      <? endif; ?>

			<div id="divEventos" class="row hide" style="padding-left: 4px;">
				<br />
				<label class="required" style="padding-bottom: 3px;">Eventos:</label>
				<table>
					<?
					foreach($this->eventos as $evento):
              $evento['id'] = $evento['id_evento'] ?? $evento['id'];

              if (is_array($this->values['eventos'])) {
                  $checked = (array_key_exists($evento['id'], $this->values['eventos'])) ? "checked=\"checked\"" : "";
              }
              ?>
              <tr>
                <td>
                  <input type="radio" name="id_evento" value="<?=$evento['id']?>" id="evento<?=$evento['id']?>" class="rd-evento" <?=$checked?> />
                  <input type="hidden" name="ds_nome_evento" class="eventos_nome" value="<?=$evento['ds_nome_evento']?>" />
                  <input type="hidden" name="ds_chave_evento" value="<?=$evento['ds_chave_evento']?>" />
                </td>
                <td>
                  <label for="evento<?=$evento['id']?>">
                    <?=$evento['ds_nome_evento']?>
                  </label>
                </td>
              </tr>
          <?
					endforeach;
          ?>
				</table>
			</div>

      <div id="divEventosReplantioParreiral" class="row hide" style="padding-left: 4px;">
        <br />
        <label class="required" style="padding-bottom: 3px;">Eventos:</label>
        <table>
            <?
            $eventos = $temCoberturaAdicionalReplantio ? $this->eventosReplantio : ($temCoberturaQuedaParreiral ? $this->eventosQueda: $this->eventos);
            foreach($eventos as $evento):
                $evento['id'] = $evento['id_evento'] ?? $evento['id'];

                if (is_array($this->values['eventos'])) {
                    $checked = (array_key_exists($evento['id'], $this->values['eventos'])) ? "checked=\"checked\"" : "";
                }

                if ($temCoberturaAdicionalReplantio && $evento['ds_chave_evento'] === Produto_Model_Eventos::DS_CHAVE_ESTIAGEM) {
                    continue;
                }
                ?>
              <tr>
                <td>
                  <input type="radio" name="id_evento" value="<?=$evento['id']?>" id="eventos_replantio_parreiral_<?=$evento['id']?>" class="rd-eventos-replantio-parreiral" <?=$checked?> />
                  <input type="hidden" name="ds_nome_evento" class="eventos_replantio_parreiral_nome" value="<?=$evento['ds_nome_evento']?>" />
                  <input type="hidden" name="ds_chave_evento" value="<?=$evento['ds_chave_evento']?>" />
                </td>
                <td>
                  <label for="eventos_replantio_parreiral_<?=$evento['id']?>">
                      <?=$evento['ds_nome_evento']?>
                  </label>
                </td>
              </tr>
            <?
            endforeach;
            ?>
        </table>
      </div>

        <?php
        if ($temCoberturaQuedaParreiral) {
          ?>
            <div id="observacaoQuedaParreiral" class="hide padding-10">
              <span>
                <b>
                  * Observação: serão avaliados também danos por vento
                </b>
              </span>
            </div>
          <?php
        }
        ?>

      <div class="row">
          <?=$this->form->ds_email_segurado?>
      </div>
			
		</fieldset>
		
		<?if(count($this->pessoa_autorizada) > 0):?>
			<fieldset>
			<legend>Representante(s) do proponente autorizado(s) a acompanhar a(s) vistoria(s): </legend>
			<?
			foreach($this->pessoa_autorizada as $pessoa){
				$cpf = ($pessoa['cpf_cnpj'] ? '/ <b>CPF:</b> '.$pessoa['cpf_cnpj'] : '');
				echo ($pessoa['ds_pessoa_autorizada'] ? '<b>Nome:</b> '.$pessoa['ds_pessoa_autorizada'].' '.$cpf  : '&nbsp;');
				echo ($pessoa['ds_telefone'] ? ' / <b>Telefone:</b> '.$pessoa['ds_telefone'] : '&nbsp;');
				echo ($pessoa['ds_grau_parentesco'] ? ' / <b>Parentesco e/ou Grau de Relacionamento:</b> '.$pessoa['ds_grau_parentesco'] : '&nbsp;');
				echo '<br><br>';
			}
			?>
			</fieldset>
		<?endif;?>
		
		<fieldset>
		<legend>Números adicionais de contato com o segurado <b>(somente para agendamento, não válidos como pessoas autorizadas)</b>:  </legend>
			<table>
				<tr class="<?=$class?>" id="div_outra_pessoa">
					<td>
					<table id="pessoa_contato">
					<?
					$i = 0;
					foreach($this->values['pessoa_contato'] as $pessoa_contato){
						$i++;
						?>
						<tr id="tr_pessoa_contato" class="tr_pessoa_contato">
							<td>
								<label for="ds_nome" class="required">Nome: </label>
								<input type="text" name="pessoa_contato[<?=$i ?>][ds_nome]" id="ds_nome" value="<?=$pessoa_contato['ds_nome'] ?>" maxlength="100" class="upper" size="50">
							</td>
							<td>
								<label for="ds_telefone" class="required">Telefone: </label>
								<input type="text" class="telefone" name="pessoa_contato[<?=$i ?>][ds_telefone]" id="ds_telefone_'<?=$i ?>'" value="<?=$pessoa_contato['ds_telefone'] ?>" maxlength="20" size="14">
							</td>

							<td>
								<a href="#"><img src="/resources/imagens/icon/delete2.png" id="delAutorizado" /></a><span id="cliqueAqui"></span>
							</td>
						</tr>  
						<?
					}
					?>
					</table>
					</td>
				</tr>
				<tr>
					<td>
					<a href="#"><img src="/resources/imagens/mini/add.png" id="addAutorizado" /></a><span id="cliqueAqui"> Adicionar número adicional</span>
					</td>
				</tr>
			</table>
		</fieldset>						
						
		<fieldset id="fieldset-buttons">
		<div class="row">
			<button id="salvar" name="salvar" type="button" style="float: left;">Enviar</button>	
		</div>
		</fieldset>
	</div>
	
</form>