<?php require_once('../topo.php');?>
<?php require_once('../lateral.php');?>
<br><br><br>
<?php
//GERADOR DE CAMPOS POST

//echo GerarCamposPost("logistica", "linha_logistica");

$idlogistica		= $_GET['idlogistica'];

if($idlogistica == ""){
	$acao		 	= "Salvar";
}else{
	$acao 			= "Alterar";	
}

//Recebe Ação
$acao_bd		    = $_GET['acao'];
$acao_bd		   .= $_POST['acao'];

//Recebendo Dados do Formulário

$idlogistica .= $_POST['idlogistica'];
$gasolina = $_POST['gasolina'];
$pedagio = $_POST['pedagio'];
$kmrodado = $_POST['kmrodado'];
$viagem = $_POST['viagem'];
$refeicao = $_POST['refeicao'];
$responsavel = $_POST['responsavel'];
$responsavel_mercadoria = $_POST['responsavel_mercadoria'];
$dataida = InverteDataNovoSeparador($_POST['dataida'], "/", "-");
$dataretorno = InverteDataNovoSeparador($_POST['dataretorno'], "/", "-");
$datacadastro = date('Y-m-d');
$idusuario = $_POST['idusuario'];
$status = $_POST['status'];
$idobra = $_POST['idobra'];


//Dados da Tabela
$tabela                 ="logistica";
$campos_tabela = "gasolina, pedagio, kmrodado, viagem, refeicao, responsavel, responsavel_mercadoria, dataida, dataretorno, datacadastro, idusuario, status, idobra, idfilial";

$campos_value = "'$gasolina', '$pedagio', '$kmrodado', '$viagem', '$refeicao', '$responsavel', '$responsavel_mercadoria', '$dataida', '$dataretorno', '$datacadastro', '$idusuario', '$status', '$idobra', '".$_SESSION['idfilial_usuario']."'";
//INSERIR REGISTROS
if($acao_bd == "Salvar"){
	$salvar = Salvar($tabela,  $campos_tabela, $campos_value);

// RETORNA ULTIMO ID INSERIDO
	$id_insert = mysql_insert_id();


	$redireciona = VerificaSql($salvar, "manutencao_logistica.php&idlogistica=".$id_insert."", "manutencao_logistica.php");
}

//ATUALIZANDO REGISTRO
if($acao_bd == "Alterar"){

$campos_alterar = "gasolina='$gasolina', pedagio='$pedagio', kmrodado='$kmrodado', viagem='$viagem', refeicao='$refeicao', responsavel='$responsavel', responsavel_mercadoria='$responsavel_mercadoria', dataida='$dataida', dataretorno='$dataretorno', datacadastro='$datacadastro', idusuario='$idusuario', status='$status', idobra='$idobra', idfilial='".$_SESSION['idfilial_usuario']."'";


    $atualizar = Alterar($tabela, $campos_alterar, "idlogistica", $idlogistica);
	$redireciona = VerificaSql($atualizar, "manutencao_logistica.php&idlogistica=".$idlogistica."", "manutencao_logistica.php&idlogistica=".$idlogistica."");
}

//EXCLUINDO REGISTRO
if($_GET['excluir'] == "sim"){
    $deletar = Excluir($tabela, "idlogistica", $idlogistica);	
	$redireciona = VerificaSql($deletar, "listar_logistica.php", "manutencao_logistica.php&idlogistica=".$idlogistica."");
} 


if($acao == "Alterar"){

	$selecionar_logistica = Seleciona("logistica", "WHERE idlogistica='".$idlogistica."'", "") or die(mysql_error());
	while($linha_logistica=mysql_fetch_array($selecionar_logistica)){
		
$idlogistica = $linha_logistica['idlogistica']; 
$gasolina = $linha_logistica['gasolina']; 
$pedagio = $linha_logistica['pedagio']; 
$kmrodado = $linha_logistica['kmrodado']; 
$viagem = $linha_logistica['viagem']; 
$refeicao = $linha_logistica['refeicao']; 
$responsavel = $linha_logistica['responsavel']; 
$responsavel_mercadoria = $linha_logistica['responsavel_mercadoria']; 
$dataida = InverteDataNovoSeparador($linha_logistica['dataida'], "-", "/");
$dataretorno = InverteDataNovoSeparador($linha_logistica['dataretorno'], "-", "/");
$datacadastro = $linha_logistica['datacadastro']; 
$idusuario = $linha_logistica['idusuario']; 
$status = $linha_logistica['status']; 
$idobra = $linha_logistica['idobra']; 


	}
}
?>
<script type="text/javascript">
function EnviarFormulario()
{
    document.forms["cadastro"].submit();
}
</script>
<table width="100%" border="0" cellspacing="0" cellpadding="2">
  <tbody>
    <tr>
      <td valign="top"><table width="100%" border="0" align="center" cellpadding="5" cellspacing="0">
        <tr>
          <td colspan="2"><table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="289" height="40" class="TituloPagina" style="padding-left: 5px;">TRANSPORTE</td>
              <td width="670" align="left" class="TituloPagina" style="padding-left: 5px;"><table width="100" border="0" align="right" cellpadding="2" cellspacing="0">
                <tr>
                  <td width="167" align="left"><table width="160" border="0" cellspacing="0" cellpadding="2" class="BoxMenuAzul" onclick="location.href='listar_logistica.php';">
                    <tr>
                      <td height="25" align="center" class="TextoMenuCinza"><img src="img/icone_novo.gif" width="29" height="21" /></td>
                      <td align="left"><span class="TextoMenuBranco" style="font-weight:normal; color:#FFF;">Todos Transportes</span></td>
                      </tr>
                    </table></td>
                  <td width="150" align="left"><table width="150" border="0" cellspacing="0" cellpadding="2" class="BoxMenuAzul" onclick="location.href='manutencao_logistica.php';">
                    <tr>
                      <td height="25" align="center" class="TextoMenuCinza"><img src="img/icone_novo.gif" width="29" height="21" /></td>
                      <td align="left"><span class="TextoMenuBranco" style="font-weight:normal; color:#FFF;">Novo Transporte</span></td>
                      </tr>
                    </table></td>
                  <td width="125" align="left">&nbsp;</td>
                  <td width="370" align="right"><table width="180" border="0" align="right" cellpadding="0" cellspacing="0" class="BoxMenuBranco">
                    <form method="post" action="listar_logistica.php" enctype="multipart/form-data">
                      <tr>
                        <td width="148" align="left" bgcolor="#FFFFFF"><input type="text" name="busca" id="busca" style="width: 100%; height: 20px;" class="InputBranco" /></td>
                        <td width="28" align="center" bgcolor="#FFFFFF"><input type="image" src="img/icone_buscar.gif" /></td>
                        </tr>
                      </form>
                    </table></td>
                  </tr>
                </table></td>
              <td width="80" align="right" style="padding-right: 8px;"><table width="70" border="0" cellspacing="0" cellpadding="2" class="BoxMenuCinza" onclick="history.go(-1);">
                <tr>
                  <td height="25" align="center" class="TextoMenuBranco">Voltar</td>
                  </tr>
                </table></td>
              </tr>
            </table></td>
        </tr>
        <!-- INICIO FORM -->
        <form name="cadastro" id="cadastro" method="post" action="manutencao_logistica.php" enctype="multipart/form-data">
          <input type="hidden" name="acao" value="<?php echo $acao;?>">
          <input type="hidden" name="idlogistica" value="<?php echo $idlogistica;?>">
          <tr>
            <td width="50%" valign="top" bgcolor="#D9D9D9"><table width="100%" border="0" cellspacing="0" cellpadding="4">
              <tr>
                <td valign="top"><strong>INFORMAÇÕES DA VIAGEM</strong></td>
              </tr>
              <tr>
                <td valign="top"><!-- INICIO DIV -->
                  <table width="100%" border="0" cellspacing="0" cellpadding="7">
                    <tr>
                      <td style="color: #676767;  font-size: 12px;"><strong style="font-family: 'Trebuchet MS'">Obra</strong></td>
                      <td style="color: #676767;  font-size: 12px;">&nbsp;</td>
                    </tr>
                    <tr>
                      <td style="color: #676767;  font-size: 12px;">
                      
                      <select name="idobra" size="1" id="idatendimento" class="form-campos" style="width:100%; ">
                        <option value="" <?php if($idobra == ""){ echo "Selected";}?>>Selecione a Obra</option>
                        <?php
						$SelecionaAtendimento = Seleciona("obras", "", "ORDER BY idobra DESC") or die(mysql_error());
							while($linha_dados_atendimento=mysql_fetch_array($SelecionaAtendimento)){				  
					  ?>
                        <option value="<?php echo $linha_dados_atendimento['idobra'];?>" <?php if($linha_dados_atendimento['idobra'] == $idobra){ echo "Selected";}?>><?php echo $linha_dados_atendimento['idobra'] . " - " . $linha_dados_atendimento['titulo'];?></option>
                        <?php
                      }
					  ?>
                      </select></td>
                      <td style="color: #676767;  font-size: 12px;">&nbsp;</td>
                    </tr>
                    <tr>
                      <td style="color: #676767;  font-size: 12px;"><strong style="font-family: 'Trebuchet MS'">Data Ida</strong></td>
                      <td style="color: #676767;  font-size: 12px;"><strong style="font-family: 'Trebuchet MS'">Data Volta</strong></td>
                    </tr>
                    <tr>
                      <td style="color: #676767;  font-size: 12px;"><span style="color: #676767; font-size: 12px;">
                        <input type="text" name="dataida" id="dataida" class="form-campos datepicker"  style="width:100%; height: 30px; font-weight: bold"  value="<?php echo $dataida;?>" />
                        </span></td>
                      <td style="color: #676767;  font-size: 12px;"><input type="text" name="dataretorno" id="dataretorno" class="form-campos datepicker"  style="width:100%; height: 30px; font-weight: bold"  value="<?php echo $dataretorno;?>" /></td>
                    </tr>
                    <tr>
                      <td align="left"><strong style="color: #676767;  font-size: 12px;">Gasolina</strong></td>
                      <td style="color: #676767;  font-size: 12px;"><strong style="color: #676767;  font-size: 12px;">Responsável Caminhão</strong></td>
                    </tr>
                    <tr>
                      <td style="color: #676767; font-size: 12px;"><input type="text" name="gasolina" id="gasolina" class="form-campos"  style="width:100%; height: 30px; font-weight: bold"  value="<?php echo $gasolina;?>" /></td>
                      <td style="color: #676767;  font-size: 12px;">
                        <select name="responsavel" size="1" required class="form-campos" id="responsavel" style="width:100%; height: 30px;">
                          <option value="">Selecione o Responsável Caminhão</option>
                          <?php


						$SelecionaClientes = Seleciona("usuarios", "", "ORDER BY nome ASC") or die(mysql_error());
							while($linha_dadosclientes=mysql_fetch_array($SelecionaClientes)){
								$id_usuario = $linha_dadosclientes['idusuario'];					  
					  ?>
                          <option value="<?php echo $linha_dadosclientes['idusuario'];?>" <?php if($id_usuario == $responsavel){ echo "Selected";}?>><?php echo $linha_dadosclientes['nome'] ;?></option>
                          <?php
                      }
					  ?>
                        </select>
                      </td>
                    </tr>
                    <tr>
                      <td width="50%" style="color: #676767;  font-size: 12px;"><strong style="color: #676767;  font-size: 12px;">Pedágio</strong></td>
                      <td width="1600" style="color: #676767;  font-size: 12px;"><strong style="font-family: 'Trebuchet MS'">KM Rodado</strong></td>
                    </tr>
                    <tr>
                      <td style="color: #676767;  font-size: 12px;"><span style="color: #676767; font-size: 12px;">
                        <input type="text" name="pedagio" id="pedagio" class="form-campos"  style="width:100%; height: 30px; font-weight: bold"  value="<?php echo $pedagio;?>" />
                      </span></td>
                      <td style="color: #676767;  font-size: 12px;"><input name="kmrodado" type="text" class="form-campos" id="kmrodado"  style="width:100%; height: 30px;"  value="<?php echo $kmrodado;?>" /></td>
                    </tr>
                    <tr>
                      <td width="50%" style="color: #676767;  font-size: 12px;"><strong style="font-family: 'Trebuchet MS'">Viagem</strong></td>
                      <td style="color: #676767;  font-size: 12px;"><strong style="font-family: 'Trebuchet MS'">Responsável Mercadoria</strong></td>
                    </tr>
                    <tr>
                      <td style="color: #676767;  font-size: 12px;"><input name="viagem" type="text" class="form-campos" id="viagem"  style="width:100%; height: 30px;"  value="<?php echo $viagem;?>"></td>
                      <td style="color: #676767;  font-size: 12px;"><select name="responsavel_mercadoria" size="1" required class="form-campos" id="responsavel_mercadoria" style="width:100%; height: 30px;">
                        <option value="">Selecione o Responsável Mercadoria</option>
                        <?php


						$SelecionaClientes = Seleciona("usuarios", "", "ORDER BY nome ASC") or die(mysql_error());
							while($linha_dadosclientes=mysql_fetch_array($SelecionaClientes)){
								$id_usuario = $linha_dadosclientes['idusuario'];					  
					  ?>
                        <option value="<?php echo $linha_dadosclientes['idusuario'];?>" <?php if($id_usuario == $responsavel_mercadoria){ echo "Selected";}?>><?php echo $linha_dadosclientes['nome'] ;?></option>
                        <?php
                      }
					  ?>
                      </select></td>
                    </tr>
                    <tr>
                      <td style="color: #676767;  font-size: 12px;"><strong style="color: #676767;  font-size: 12px;">Refeição</strong></td>
                      <td style="color: #676767;  font-size: 12px;"><strong style="font-family: 'Trebuchet MS'">Status</strong></td>
                      </tr>
                    <tr>
                      <td style="color: #676767;  font-size: 12px;"><span style="color: #676767; font-size: 12px;">
                        <input type="text" name="refeicao" id="refeicao" class="form-campos"  style="width:100%; height: 30px; font-weight: bold"  value="<?php echo $refeicao;?>" />
                      </span></td>
                      <td style="color: #676767;  font-size: 12px;">
                        <select name="status" size="1" id="status" class="form-campos" style="width:100%; height: 30px;">
                          <option value="">Selecione o Status</option>
                          <option value="Entregue" <?php if($status == "Entregue"){ echo "selected";}?>>Entregue</option>
                          <option value="Rota" <?php if($status == "Rota"){ echo "selected";}?>>Em Rota</option>
                          <option value="Cancelado" <?php if($status == "Cancelado"){ echo "selected";}?>>Cancelado</option>
                        </select>
                      </td>
                      </tr>
                    </table>
                  <!-- FIM DIV --></td>
              </tr>
            </table>
              <table width="100%" border="0" cellspacing="0" cellpadding="7">
                <tr>
                  <td align="right"><input name="button" type="submit" class="BoxVerde" id="button" value="Salvar" style="width: 100px; height: 35px;" onclick="EnviarFormulario()"></td>
                  <?php if($acao == "Alterar"){ ?>
                  <td width="100" align="right"><table width="70" border="0" cellspacing="0" cellpadding="2" class="BoxMenuVermelho" onclick="location.href='manutencao_logistica.php?idlogistica=<?php echo $idlogistica;?>&amp;excluir=sim';" style="width: 100px; height: 35px; color:#FFFFFF;">
                    <tr>
                      <td height="25" align="center" class="TextoMenuBranco">Excluir</td>
                    </tr>
                  </table></td>
                  <?php } ?>
                </tr>
              </table></td>
            <td width="50%" valign="top" bgcolor="#D9D9D9">&nbsp;</td>
          </tr>
        </form>
        <!-- FIM FORM -->
      </table></td>
      <td width="250" valign="top"><?php require_once('lateral_geral.php');?></td>
    </tr>
  </tbody>
</table>
<?php require_once('../rodape.php');?>