<?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>

        <h2><font color='green'>Gestao de arquivos</font></h2>
		
		<div class="container">
		 <div class="col-sm-4">
		 
		  <form action="" method="post" enctype="multipart/form-data">
		    <?php arquivos();?>
		   <div class="form-group">
		    <label for="assunto">Assunto</label>
			 <input type="text" name="assunto" class="form-control" autocomplete="off" required/>
		   </div>
		   
		   <div class="form-group">
		    <label for="ficheiro">Ficheiro</label>
			 <input type="file" name="ficheiro" class="form-control" required/>
		   </div>
		   
		   <div class="form-group">
		    <label for="cliente">Nome do cliente</label>
			 <select required name="cliente" class="form-control">
			  <option></option>
			  <?php buscaCliente();?>
			 </select>
		   </div>
		   
		   
		   
		   <div class="form-group">
		    <input type="submit" name="submit" class="btn btn-success btn-block" value="Enviar"/>
		   </div>
		  
		  </form>
		 
		 </div>
		
		</div>
      
	  
	  <?php 
	  //FUNCAO PARA EDITAR
	  
	  if(isset($_GET['edita']) && !empty($_GET['edita'])){
		  
		  $id_edit = $_GET['edita'];
		  
		  $arq = "SELECT * FROM arquivos WHERE id='$id_edit'";
		  $link = mysqli_query($con,$arq);
		  while($row = mysqli_fetch_assoc($link)){
			  
			  $assunto = $row['assunto'];
			  ?>
			  
			  <div class="container">
		        <div class="col-sm-4">		 
		    <form action="" method="post" enctype="multipart/form-data">
		   
		   <div class="form-group">
		    <label for="assunto">Assunto</label>
			 <input type="text" name="assunto" class="form-control" value="<?php echo $assunto;?>" autocomplete="off" required/>
		   </div>
		   
		   <div class="form-group">
		    <label for="ficheiro">Ficheiro</label>
			 <input type="file" name="ficheiro" class="form-control" required/>
		   </div>
		   
		   <div class="form-group">
		    <label for="cliente">Nome do cliente</label>
			 <select required name="cliente" class="form-control">
			  <option></option>
			  <?php buscaCliente();?>
			 </select>
		   </div>
		   
		   <div class="form-group">
		    <input type="submit" name="submit_up" class="btn btn-success btn-block" value="Actualizar"/>
		   </div>
		  
		  </form>
		 
		 </div>
		
		</div>
			  
		 <?php }
		 
		 if(isset($_POST['submit_up'])){
			 
			 $assunto = $_POST['assunto'];
			 $ficheiro = $_FILES['ficheiro']['name'];
		     $ficheiro_tmp = $_FILES['ficheiro']['tmp_name'];
		     move_uploaded_file($ficheiro_tmp,"arquivos/$ficheiro");
			 
			 $cliente = $_POST['cliente'];
		 
		   $actualiza = "UPDATE arquivos SET assunto='$assunto',ficheiro='$ficheiro',cliente='$cliente' WHERE id='$id_edit'";
		   $query_link = mysqli_query($con,$actualiza);
		   
		   if($query_link){
			   
			   echo "<script>alert('Actualizado com sucesso')</script>";
			   echo "<script>window.open('arquivos.php','_self')</script>";
		   
		   }else{
			   
			   echo "<h1>BUGGGGGGGGGGG</h1>";
		   }
		 
		 
		 }
		  
	  }
	  
	  
	  ?>

      <?php 
	  //FUNCAO Delete
	  if(isset($_GET['deleta']) && !empty($_GET['deleta'])){
		  
	  $id_delete = $_GET['deleta'];
	  
	  $apagar = "DELETE FROM arquivos WHERE id='$id_delete'";
	  $isso = mysqli_query($con,$apagar);
	  
	  if($isso){
		  
		  echo "<script>alert('Deletado com sucesso')</script>";
		  echo "<script>window.open('arquivos.php','_self')</script>";
	  }else{
		  
		  echo "<h1>BUGSGSGGSGSGSGGSGSG</h1>";
	  }
	  
	  }
	  
	  
	  ?> 
		<table class="table">
		 <thead>
		  <tr>
		   <th>Item</th>
		   <th>Assunto</th>
		   <th>Cliente</th>
		   <th>Ficheiro</th>
		   <th>Delete</th>
		   <th>Update</th>
		 
		  </tr>
		 </thead>
		 
		 <tbody>
		 
		<?php 
		$item=1;	
		
		$buscar_ficheiros = "SELECT * FROM arquivos";
		$tirar = mysqli_query($con,$buscar_ficheiros);
		while($row = mysqli_fetch_assoc($tirar)){
			
			$id = $row['id'];
			$assunto = $row['assunto'];
			$file = $row['ficheiro'];
			$cliente = $row['cliente'];
			
		
		?>
		  <tr>
		     <td><?php echo $item++;?></td>
		   <td><?php echo $assunto;?></td>
		   <td><?php echo $cliente;?></td>
           <td><a href="downlod_file.php?file=<?php echo $file;?>">Download</a></td>		   
		
		  
		   <td><button type="button" class="btn btn-danger btn-sm"><a href="arquivos.php?deleta=<?php echo $id;?>"
		   style="text-decoration:none; color:white;"><span>Delete</span></a></button></td>
		   
		   <td><button type="button" class="btn btn-info btn-sm"><a href="arquivos.php?edita=<?php echo $id;?>"
		   style="text-decoration:none; color:white;"><span>Update</span></a></button></td>
		  </tr>
		  <?php }?>
		
		  
		 </tbody>
		 
		 
		</table>	
		

      </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>
