<?php
    session_start();
	ob_start();

	$id_users = isset($_SESSION['id_users']) ? $_SESSION['id_users']: "";	
	$nome_user = isset($_SESSION['nome']) ? $_SESSION['nome']: "";	
	$email_users = isset($_SESSION['email']) ? $_SESSION['email']: "";	
	$pass_users = isset($_SESSION['pass']) ? $_SESSION['pass']: "";	
	$logado = isset($_SESSION['logado']) ? $_SESSION['logado']: "N";	

	if ($logado == "N" && $id_users == ""){	    
		echo  "<script type='text/javascript'>
					location.href='login.php'
				</script>";	
		exit();
	}

	require 'conexion.php';
	require_once('functions.php');
    
	$where = "ON companies.id = pilotos.nr_company WHERE companies.ativo = '1';";
	
    $sql = "SELECT companies.popular_name, pilotos.*, date_format(dlvdate, '%d/%m/%Y') as dlvdate, date_format(dtapproved, '%d/%m/%Y') as dtapproved FROM `companies` INNER JOIN pilotos $where";
	$resultado = $mysqli->query($sql);
    
    //echo '<pre>', $sql, '<br />'; bloco de parada
    include(HEADER_TEMPLATE);
	
?>

			<div class="container-fluid">
            
				<table class="table table-hover table-bordered" id="tabelaPiloto" >
					<thead>

							<th>ID Pedido</th>
							<th>Cliente</th>							
							<th>Lacre</th>
							<th>Modelagem</th>
							<th>Tecido</th>
							<th>Linha</th>	
							<th>Cintura</th>	
							<th>Tamanho</th>	
							<th>Obs</th>			
							<th></th>
							<th></th>					
						</tr>
					</thead>
					
					<tbody>
						<?php while($row = $resultado->fetch_array(MYSQLI_ASSOC)) { ?>
							<tr>
								<td><?php echo $row['id_pedido']; ?></td>
								<td><?php echo $row['popular_name']; ?></td>								
								<td><?php echo $row['lacre']; ?></td>								
								<td><?php echo $row['modelagem']; ?></td>
								<td><?php echo $row['tecido']; ?></td>
								<td><?php echo $row['linha']; ?></td>	
								<td><?php echo $row['cintura']; ?></td>	
								<td><?php echo $row['tamanhoPiloto']; ?></td>									
								<td><?php echo $row['obs']; ?></td>											
								<td><a href="piloto_view.php?id=<?php echo $row['id']; ?>" data-toggle="tooltip" data-placement="top" title="Visualiza dados do PILOTO"><span class="glyphicon glyphicon-eye-open"></span></a></td>
                                <?php if ($row['id_pedido'] > 0): ?>
                                    <td><a href="pedido_view.php?id=<?php echo $row['id_pedido']; ?>" data-toggle="tooltip" data-placement="top" title="Visualiza dados do PEDIDO. Altera se não estiver na OC"><span class="glyphicon glyphicon-ok"></span></a></td>
                                <?php else : ?>          
                                    <td><a href="pedido_novo.php?id=<?php echo $row['id']?>&nr_company=<?php echo $row['nr_company']?>&modelagem=<?php echo $row['modelagem']?>&model=<?php echo $row['model']?>&lacre=<?php echo $row['lacre']?>&tecido=<?php echo $row['tecido']?>" data-toggle="tooltip" data-placement="top" title="Lançar Pedido" <span class="glyphicon glyphicon-plus-sign"></span></a></td?
                                <?php endif ?>          
                                
							</tr>
						<?php } ?>
					</tbody>
				</table>
                
			</div>

			<div class="container-fluid">
				<a href="piloto_novo.php" class="btn btn-primary">Adicionar Piloto</a>
			</div>

        </div>
		
    <?php include(FOOTER_TEMPLATE); ?>  
		
	<script type="text/javascript">		
		$(document).ready(function() {
			$('#tabelaPiloto').DataTable( {
				"stateSave": true,
				"order": [[0, "desc"]],
						"language":{
							"lengthMenu": "Mostrar _MENU_ registros por página",
							"info": "Mostrando pagina _PAGE_ de _PAGES_",
							"infoEmpty": "Sem registros para serem mostrados",
							"infoFiltered": "(filtrada de _MAX_ registros)",
							"loadingRecords": "Lendo...",
							"processing":     "Procesando...",
							"search": "Buscar:",
							"zeroRecords":    "Não se encontra registros com esta caracterísica",
							"paginate": {
								"next":       ">>",
								"previous":   "<<"
						}},                  
			"columnDefs": [
				{
					"targets": [ 0 ],
					"visible": true,
					"searchable": true
				},
				{
					"targets": [ 1 ],
					"visible": true,
					"searchable": true
				}		

				]
			} );
		} );
		</script>	