<?php

include_once("connectdb.php");
session_start();
if ($_SESSION["useremail"]=="" ){
    
    header("location:index.php");
    
}
include_once"header.php";


if(isset($_POST["btnSave"])){
    
    $category=$_POST["txtcategory"];
    
    
   if(empty($category)){
       
       $eror="<script type='text/javascript'>
        
        jQuery(function validation(){
        
        Swal.fire({
  position: 'center',
  icon: 'error',
  title: 'Echec',
  text: 'Le champ est vide',
  showConfirmButton: false,
  timer: 3000
})
        
        });
        
        </script>";
            
  
   
   echo $eror;
   
   
   
   }
    
    if(!isset($eror)){
        
        $insert=$pdo->prepare("insert into tbl_category(category) values(:category)");
        $insert->bindParam(":category",$category);
        if($insert->execute()){
            
            echo 
            
                "<script type='text/javascript'>
        
        jQuery(function validation(){
        
        Swal.fire({
  position: 'center',
  icon: 'success',
  title: 'Succès',
  text: 'Catégorie ajoutée',
  showConfirmButton: false,
  timer: 3000
})
        
        });
        
        </script>";
                
                
                
            
        }
        
        
    }
    
    
}




if(isset($_POST["btndelete"])){
    
    $dltid=$_POST["btndelete"];
    
   $delete=$pdo->prepare("delete from tbl_category where catid='$dltid'");
    
    if ($delete->execute()){
        
         echo 
            
                "<script type='text/javascript'>
        
        jQuery(function validation(){
        
        Swal.fire({
  position: 'center',
  icon: 'success',
  title: 'Succès',
  text: 'Catégorie supprimée',
  showConfirmButton: false,
  timer: 3000
})
        
        });
        
        </script>";
        
        
        
    }else{
        echo 
            
                "<script type='text/javascript'>
        
        jQuery(function validation(){
        
        Swal.fire({
  position: 'center',
  icon: 'error',
  title: 'Echec',
  text: 'Catégorie non supprimée',
  showConfirmButton: false,
  timer: 3000
})
        
        });
        
        </script>";
    }
    
    
}



?>

 <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0">Catégories</h1>
        
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="logout.php">Déconnexion</a></li>
              <li class="breadcrumb-item active"><a href="changepassword.php">Modifier mon mot de passe</a></li>
            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </section>

   
   
   
   
   
   
    <!-- Main content -->
    <section class="content container-fluid">
              <div class="row">
              
               <!-- left column -->
          <div class="col-md-12">
            <!-- general form elements -->
            <div class="card card-primary">
              <div class="card-header">
                <h3 class="card-title">Gestion des catégories</h3>
              </div>
              <!-- /.card-header -->
              <!-- form start -->
              <form role="form" action="" method="post">
               
                <div class="card-body">
                  <div class="form-group">
                    <label for="exampleInputEmail1">Nom de la catégorie</label>
                    <input type="text" class="form-control" id="exampleInputEmail1" placeholder="Entrez le nom" name="txtcategory">
                  </div>
                  
                </div>
                <!-- /.card-body -->

                <div class="card-footer">
                  <button type="submit" class="btn btn-primary" name="btnSave">Ajouter</button>
                </div>
              </form>
            </div>
            <!-- /.card -->

           
                
              
            </div>
            

          <!--/.col (left) -->
          <!-- right column -->


       <div class="col-md-12">
            
            <div class="card card-success">
              <div class="card-header">
                <h3 class="card-title">Liste des catégories</h3>
              </div>
              <div class="card-body">
        
        <form  action="" method="POST">
        <table id="example1" class="table table-bordered table-hover">
            
          <thead>
             <tr>
                 
                 <td>No.</td>
                 <td>CATEGORIE</td>
                <!-- <td>EDIT</td> 
                
                 also add this in td below
                 
                 <td><button input type=\"submit\" value=\".$row->catid.\" class=\"btn btn-success\" name=\"btnedit\">EDIT</button></td>
                  
                   
                     
                 <td>Supprimer</td>-->
                 
             </tr>  
      
           </thead>
       
            <tbody>
     
           <?php
                
                $select=$pdo->prepare("select * from tbl_category order by catid desc");
                $select->execute();
                
                while($row=$select->fetch(PDO::FETCH_OBJ)){
                    
                  echo "
                    
                    <tr>
                    <td>$row->catid</td>
                    <td>$row->category</td>
                    
                   
                    <!--<td><button input type=\"submit\" value=" .$row->catid."\"  class=\"btn btn-danger\" name=\"btndelete\"><span class=\"fas fa-trash\"   style=\"color:#ffffff\" data-toggle=\"tooltip\" title=\"DELETE\"></span></button></td>-->
                    
                    </tr>";
                    
                }
                
                
                
                
                ?>
                    
                    
                
                
            </tbody>
            
            
        </table>
                  </form>
        
             </div>
               </div>
           
                
              
            </div>
        
        
        <!-- /.right column -->
        
          </div> 

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












<script>
  $(function () {
    $("#example1").DataTable({
      "responsive": true, "lengthChange": false, "autoWidth": false,
      "buttons": ["copy", "csv", "excel", "pdf", "print", "colvis"]
    }).buttons().container().appendTo('#example1_wrapper .col-md-6:eq(0)');
    $('#example2').DataTable({
      "paging": true,
      "lengthChange": false,
      "searching": false,
      "ordering": true,
      "info": true,
      "autoWidth": false,
      "responsive": true,
    });
  });
</script>






  <!-- Control Sidebar -->
  <aside class="control-sidebar control-sidebar-dark">
    <!-- Control sidebar content goes here -->
    <div class="p-3">
      <h5>Title</h5>
      <p>Sidebar content</p>
    </div>
  </aside>
  <!-- /.control-sidebar -->

 <?php
include_once"footer.php";


?>