<?php
    session_start();
	ob_start();
	//resgata os valores das session em variaveis
	$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";	
	//varificamos e a var logado contém o valos (S) OU (N), se conter N quer dizer que a pessoa não fez o login corretamente
	//que no caso satisfará nossa condição no if e a pessoa sera redirecionada para a tela de login novamente
	if ($logado == "N" && $id_users == ""){	    
		echo  "<script type='text/javascript'>
					location.href='login.php'
				</script>";	
		exit();
	}
	require 'conexion.php';
	require_once('functions.php');
    
	$where = "WHERE ativo = '1'";
	
	if(!empty($_POST))
	{
		$valor = $_POST['campo'];
		if(!empty($valor)){
			$where = "WHERE popular_name LIKE '%$valor' and ativo = '1'";
		}
	}
	$sql = "SELECT * FROM companies $where";
	$resultado = $mysqli->query($sql);
    include(HEADER_TEMPLATE);
   //phpinfo();
?>

				<table class="table table-hover" id="tabelaCompanies" >
					<thead>
						<tr>
							<th>ID</th>
							<th>TP</th>
							<th>Nome Fantasia</th>
							<th>CNPJ</th>
							<th>Endereço</th>
							<th>Artigo</th>
							<th></th>
							<th></th>
						</tr>
					</thead>
					
					<tbody>
						<?php $resultado = $mysqli->query($sql);
                                while($row = $resultado->fetch_array(MYSQLI_ASSOC)) { ?>
							<tr>
								<td><?php echo $row['id']; ?></td>
								<td><?php echo $row['type']; ?></td>
                                <td><?php echo substr($row['popular_name'],0,30); ?></td>                                
								<td><?php echo $row['cpf_cnpj']; ?></td>
                                <td><?php echo substr($row['address'],0,20); ?></td>                                
								<td><?php echo $row['material']; ?></td>
								<td><a href="companies_view.php?id=<?php echo $row['id']; ?>"><span class="glyphicon glyphicon-pencil"></span></a></td>
                                <td><a href="#" data-href="eliminar.php?id=<?php echo $row['id']; ?>&x=1" data-toggle="modal" data-target="#confirm-delete"><span class="glyphicon glyphicon-trash"></span></a></td>
							</tr>
						<?php } ?>
					</tbody>
				</table>

                <div class="row">
                    <a href="companies_novo.php" class="btn btn-primary">Adicionar</a>
                </div>      
                <!-- Modal -->
                <div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                <h4 class="modal-title" id="myModalLabel">Eliminar Registro</h4>
                            </div>
                            
                            <div class="modal-body">
                                Deseja eliminar este registro?
                            </div>
                            
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">Cancela</button>
                                <a class="btn btn-danger btn-ok">Elimina</a>
                            </div>
                        </div>
                    </div>
                </div>                
    
<script>
    $('#confirm-delete').on('show.bs.modal', function(e) {
        $(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
        
        $('.debug-url').html('Delete URL: <strong>' + $(this).find('.btn-ok').attr('href') + '</strong>');
    });
</script>	
        
<?php include(FOOTER_TEMPLATE); ?>      
	<script type="text/javascript">		
		$(document).ready(function() {
			$('#tabelaCompanies').DataTable( {
				"order": [[1, "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
				}
				]
			} );
		} );
		</script>   