<?php include "includes/header.php";?>

<?php include "includes/navbar.php";?>

  <div id="wrapper">

    <!-- Sidebar -->
   <?php include "includes/sidebar.php";?>

    <div id="content-wrapper">

      <div class="container-fluid">

        <!-- Breadcrumbs-->
        <ol class="breadcrumb">
          <li class="breadcrumb-item">
            <a href="#">Ola</a>
          </li>
          <li class="breadcrumb-item active"><?php echo $_SESSION['nome'];?></li>
        </ol>

        <!-- Icon Cards-->
        <div class="row">
       
	   <table class="table">
	    <thead>
		 <tr>
		  <th>Nome</th>
		  <th>Cell1</th>
		  <th>Cell2</th>
		  <th>Email</th>
		  <th>Editar</th>
		  <th>Bloquear</th>
		  <th>Apagar</th>
		  <th>Numero</th>
		  <th>Ficha</th>
		  
		 </tr>		
		</thead>
		
		<tbody>
		
		<?php 
		
		
		if($_SESSION['nivel'] ==1 || $_SESSION['nivel'] ==2){
			
			$leva_cliente = "SELECT * FROM clientes";
		$query = mysqli_query($con,$leva_cliente);
		while($row = mysqli_fetch_assoc($query)){
			
			$id_cliente = $row['id_cliente'];
			$nome_cliente = $row['nome_cliente'];
			$codigo = $row['codigo_cliente'];
			$vendedor = $row['vendedor'];
			$cell1 = $row['cell1'];
			$cell2 = $row['cell2'];
			$cell3 = $row['cell3'];	
            $estado = $row['status'];			
		
		
		?>
		
		 <tr>
		  <td><?php echo $nome_cliente;?></td>
		
		  <td><?php echo $cell1;?></td>
		  <td><?php echo $cell2;?></td>
		  <td><?php echo $cell3;?></td>
		  
		  
		  <?php 
		  
		  if($_SESSION['nivel'] ==1){
			  
		  ?>
		  
		  <td><button type="button" class="btn btn-primary btn-sm">
		  <a href="editar_cliente.php?editar=<?php echo $id_cliente;?>" style="text-decoration:none; color:white;"><span>Editar</span></a></button></td>
		 
		<?php 
		if($estado == "Activo"){
			?>
			
			<td><button type="button" class="btn btn-warning btn-sm">
		  <a href="lista_clientes.php?bloquear=<?php echo $id_cliente;?>" style="text-decoration:none; color:white;"><span>Bloquear</span></a></button></td>
			
			
		<?php }else{
			
			?>
			
			<td><button type="button" class="btn btn-success btn-sm">
		  <a href="lista_clientes.php?activar=<?php echo $id_cliente;?>" style="text-decoration:none; color:white;"><span>Activar</span></a></button></td>
				

<!--FIM DOS BPTOES-->

				
		<?php }
		
		
		
		?> 
				 
		  <td><button type="button" class="btn btn-danger btn-sm">
		  <a href="lista_clientes.php?apagar=<?php echo $id_cliente;?>" style="text-decoration:none; color:white;"><span>Apagar</span></a></button></td>
		  <td><?php echo $codigo;?></td>
		  <td><button type="button" class="btn btn-info btn-sm">
		  <a href="ficha_cliente.php?id=<?php echo $id_cliente;?>" style="text-decoration:none; color:white;"><span>Ficha</span></a></button></td>
		  
		  <?php }else{
			?>
			<td><button type="button" class="btn btn-primary btn-sm">
		  <a href="editar_cliente.php?editar=<?php echo $id_cliente;?>" style="text-decoration:none; color:white;"><span>Editar</span></a></button></td>
		       
			   <td><font color='red'>N/A</font></td>
			   <td><font color='red'>N/A</font></td>
			   <td><button type="button" class="btn btn-info btn-sm">
		  <a href="ficha_cliente.php?id=<?php echo $id_cliente;?>" style="text-decoration:none; color:white;"><span>Ficha</span></a></button></td>
			
		<?php }?> 
		  
		 </tr>
		 
		
		<?php }
			
			
			
			
			
			
			//TERMINA AQUI-->
		}else{
			
			$gerente = $_SESSION['nome'];
		
		$leva_cliente = "SELECT * FROM clientes WHERE vendedor='$gerente'";
		$query = mysqli_query($con,$leva_cliente);
		while($row = mysqli_fetch_assoc($query)){
			
			$id_cliente = $row['id_cliente'];
			$nome_cliente = $row['nome_cliente'];
			$codigo = $row['codigo_cliente'];
			$vendedor = $row['vendedor'];
			$cell1 = $row['cell1'];
			$cell2 = $row['cell2'];
			$cell3 = $row['cell3'];	
            $estado = $row['status'];			
		
		
		?>
		
		 <tr>
		  <td><?php echo $nome_cliente;?></td>
		  <td><?php echo $codigo;?></td>
		  <td><?php echo $vendedor?></td>
		  <td><?php echo $cell1;?></td>
		  <td><?php echo $cell2;?></td>
		  <td><?php echo $cell3;?></td>
		  
		  
		  <?php 
		  
		  if($_SESSION['nivel'] ==1){
			  
		  ?>
		  
		  <td><button type="button" class="btn btn-primary btn-sm">
		  <a href="editar_cliente.php?editar=<?php echo $id_cliente;?>" style="text-decoration:none; color:white;"><span>Editar</span></a></button></td>
		 
		<?php 
		if($estado == "Activo"){
			?>
			
			<td><button type="button" class="btn btn-warning btn-sm">
		  <a href="lista_clientes.php?bloquear=<?php echo $id_cliente;?>" style="text-decoration:none; color:white;"><span>Bloquear</span></a></button></td>
			
			
		<?php }else{
			
			?>
			
			<td><button type="button" class="btn btn-success btn-sm">
		  <a href="lista_clientes.php?activar=<?php echo $id_cliente;?>" style="text-decoration:none; color:white;"><span>Activar</span></a></button></td>
				

<!--FIM DOS BPTOES-->

				
		<?php }
		
		
		
		?> 
				 
		  <td><button type="button" class="btn btn-danger btn-sm">
		  <a href="lista_clientes.php?apagar=<?php echo $id_cliente;?>" style="text-decoration:none; color:white;"><span>Apagar</span></a></button></td>
		  
		  <?php }else{
			
			echo "<td><font color='red'>N/A</font></td>";
			echo "<td><font color='red'>N/A</font></td>";
			echo "<td><font color='red'>N/A</font></td>";
			
		}?> 
		  
		 </tr>
		 
		
		<?php }
		
		}
		
		?>
		
		</tbody>
	   
	   
	   
	   </table>
	   
	   
	   
	   <!--ACOOES DE CLIENTE-->
	   
	   
	  <?php
    if(isset($_GET['bloquear']) && !empty($_GET['bloquear'])){
		
		$id_bloquear = $_GET['bloquear'];
		
		$actualiza_status = "UPDATE clientes SET status='Inactivo' WHERE id_cliente='$id_bloquear'";
		$legal = mysqli_query($con,$actualiza_status);
		
		if($legal){
			
			echo "<script>window.open('lista_clientes.php','_self')</script>";
		}else{
			
			echo "ERRRRRRRRRRRRRROOOOOOORRRRRRRRRRR";
		}
		
		
	}
	
	
	
	if(isset($_GET['activar']) && !empty($_GET['activar'])){
		
		$id_activar = $_GET['activar'];
		
		$actualiza_status1 = "UPDATE clientes SET status='Activo' WHERE id_cliente='$id_activar'";
		$legal1 = mysqli_query($con,$actualiza_status1);
		
		if($legal1){
			
			echo "<script>window.open('lista_clientes.php','_self')</script>";
		}else{
			
			echo "ERRRRRRRRRRRRRROOOOOOORRRRRRRRRRR";
		}
		
		
	}
	
	
	
	if(isset($_GET['apagar']) && !empty($_GET['apagar'])){
		
		$id_apagar = $_GET['apagar'];
		
		$actualiza_status2 = "DELETE FROM clientes WHERE id_cliente='$id_apagar'";
		$legal2 = mysqli_query($con,$actualiza_status2);
		
		if($legal2){
			
			echo "<script>window.open('lista_clientes.php','_self')</script>";
		}else{
			
			echo "ERRRRRRRRRRRRRROOOOOOORRRRRRRRRRR";
		}
		
		
	}
	

	  ?>
	   
	   
        
        
        </div>

     
        

      </div>
      <!-- /.container-fluid -->

      <!-- Sticky Footer -->
      <?php include "includes/footer.php";?>

    </div>
    <!-- /.content-wrapper -->

  </div>
  <!-- /#wrapper -->

  <!-- Scroll to Top Button-->
  <a class="scroll-to-top rounded" href="#page-top">
    <i class="fas fa-angle-up"></i>
  </a>

  <!-- Logout Modal-->
 <?php include "includes/logout_modal.php";?>

  <!-- Bootstrap core JavaScript-->
  <script src="vendor/jquery/jquery.min.js"></script>
  <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

  <!-- Core plugin JavaScript-->
  <script src="vendor/jquery-easing/jquery.easing.min.js"></script>

  <!-- Page level plugin JavaScript-->
  <script src="vendor/chart.js/Chart.min.js"></script>
  <script src="vendor/datatables/jquery.dataTables.js"></script>
  <script src="vendor/datatables/dataTables.bootstrap4.js"></script>

  <!-- Custom scripts for all pages-->
  <script src="js/sb-admin.min.js"></script>

  <!-- Demo scripts for this page-->
  <script src="js/demo/datatables-demo.js"></script>
  <script src="js/demo/chart-area-demo.js"></script>

</body>

</html>
