<?php
session_start();
include("connectdb.php");

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


if (isset($_POST["btnaddproduct"]))
{
    
    $productname= $_POST["txtpname"];
    $category=$_POST["txtselect_option"];
    $purchaseprice=$_POST["txtprice"];
    $pa=$_POST["pa"];
    $description=$_POST["txtdesc"];
    $benefice=$purchaseprice-$pa;
    
    
    
    $insert=$pdo->prepare("insert into tbl_product(pname,pcategory,saleprice,prix_achat,benefice,pdescription) values(:pname,:pcategory,:saleprice, :prix_achat,:benefice,:pdescription)");
    $insert->bindParam(":pname",$productname );
    $insert->bindParam(":pcategory",$category );
    $insert->bindParam(":saleprice",$purchaseprice );
    $insert->bindParam(":prix_achat",$pa);
    $insert->bindParam(":benefice",$benefice);
    $insert->bindParam(":pdescription",$description );
    
    if($insert->execute())
    
    {
        
            echo "<script type='text/javascript'>
        
        jQuery(function validation(){
        
        Swal.fire({
  position: 'center',
  icon: 'success',
  title: 'Saved',
  text: 'Produit ajouté',
  showConfirmButton: false,
  timer: 3000
})
        
        });
        
        </script>";
        
        
        
        
        
    }
        
        else{
            "<script type='text/javascript'>
        
        jQuery(function validation(){
        
        Swal.fire({
  position: 'center',
  icon: 'error',
  title: 'Failed',
  text: 'Erreur lors de l'ajout,
  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">Ajout de produit</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-success">
              <div class="card-header">
                <h3 class="card-title"><a href="productlist.php"
                    class= "btn btn-primary" role="button" >
                    Liste des produits
                    </a></h3>
              </div>
              <!-- /.card-header -->
              <!-- form start -->
              <form  action="" method="post">
               
                <div class="card-body">
                  <div class="form-group">
                    <label>Nom du produit</label>
                    <input type="text" class="form-control" id="exampleInputEmail1" placeholder="Entrez le nom" name="txtpname" required>
                  </div>
                  
                  <div class="form-group">
                    <label>Category</label>
                    <select  class="form-control"  placeholder="Choose category to bind" name="txtselect_option" required>
                    <option value="" disabled selected>Sélectionnez la Catégorie</option>
                    
                    <?php
                        
                        
                        $select=$pdo->prepare("select *from tbl_category order by catid desc");
                            $select->execute();
                        while($row=$select->fetch(PDO::FETCH_ASSOC)){
                            
                            extract($row)
                                ?>
                                <option>
                                    
                                    <?php echo $row['category']; ?>
                                </option>
                                
                         <?php       
                        }
                        
                        
                        ?>
                    
                    
                    
                    
                    
                      </select>
                  </div>
                   
                   
                   
                  <div class="form-group">
                    <label for="exampleInputPassword1">Prix d'achat</label>
                    <input type="number" min="1" step="0.01" class="form-control"  placeholder="" name="pa" required>
                  </div>

                  <div class="form-group">
                    <label for="exampleInputPassword1">Prix de vente</label>
                    <input type="number" min="1" step="0.01" class="form-control"  placeholder="" name="txtprice" required>
                  </div>
                 
                 
                  <div class="form-group">
                    <label>Description</label>
                      <textarea type="text" class="form-control"  placeholder="Détails sur le produit" name="txtdesc" rows="4" ></textarea>
                  </div>
                 
    
                  
                  
                </div>
                <!-- /.card-body -->

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

           
                
              
            </div>
            

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


       
     
           
                    
                    
                
                
            
        
             

           
                
              
          
        
        
        <!-- /.right column -->
        
          </div>

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
















  <!-- 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";


?>