<?php

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

$pls = ''; 
if( isset( $_GET['pls'])) {
    $pls = $_GET['pls']; 
} 

$select=$pdo->prepare("select * from tbl_product where pid='$pls'");
$select->execute();
$row=$select->fetch(PDO::FETCH_ASSOC);
$id_db=$row["pid"];
$productname_db=$row["pname"];
$category_db=$row["pcategory"];
$purchaseprice_db=$row["saleprice"];
       // $stock_db=$row["pstock"];
    $description_db=$row["pdescription"];

//print_r($row);

if (isset($_POST["btnupdateproduct"]))
{
    
    $qte= $_POST["qte"];
   

 $update=$pdo->prepare("update tbl_product SET pstock=pstock+? where pid=? ");
    
     $update->execute(array($qte,$pls));

    $ver=$pdo->prepare("insert into stock(user, produit, qte) values(?,?,?)");
    $ver->execute(array($_SESSION["userid"], $productname_db, $qte));
    
    if ($update && $ver)
    {
        
        
            echo "<script type='text/javascript'>
        
        jQuery(function validation(){
        
        Swal.fire({
  position: 'center',
  icon: 'success',
  title: 'Succès',
  text: 'Quantité ajoutée',
  showConfirmButton: false,
  timer: 3000
})
        
        });
        
        </script>";
        
        
        
        
        
    }
        
        else{
            "<script type='text/javascript'>
        
        jQuery(function validation(){
        
        Swal.fire({
  position: 'center',
  icon: 'error',
  title: 'Echec',
  text: 'Erreur lors de l'opération,
  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">Approvisionnement d'un 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-warning">
              <div class="card-header">
                <h3 class="card-title">
                    
                    Gestion du stock
                    </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"  name="txtpname" value= "<?php echo $productname_db; ?>" readonly>
                  </div>
                  
                  <div class="form-group">
                    <label>Quantité à ajouter</label>
                    <input type="number" min="1" class="form-control" id="exampleInputEmail1"  name="qte" value= "1" required>
                  </div>
                 
    
                  
                  
                </div>
                <!-- /.card-body -->

                <div class="card-footer">
                  <button type="submit" class="btn btn-warning" name="btnupdateproduct">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";


?>