
<?php
session_start();
include("header.php");?>
       <?php
       if(isset($_SESSION["productediterror"])){
        echo "<div class='alert m-2 alert-danger alert-dismissible fade show' role='alert'>
  <strong>Empty fields are not allowed for update!!</strong>
  <button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button>
</div>";
session_destroy();
}
if(isset($_SESSION["producteditalert"])){
  echo"<div class='alert m-2 alert-success alert-dismissible fade show show-cat-update' role='alert'>
  <strong>Product Updated Successfully!! !</strong></div>";
  session_destroy();
}
       if(isset($_SESSION["product_error"])){
        echo "<div class='alert m-2 alert-danger alert-dismissible fade show' role='alert'>
  <strong>All fields are required!</strong>
  <button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button>
</div>";
session_destroy();
}
if(isset($_SESSION["product_alert"])){
  echo"<div class='alert m-2 alert-success alert-dismissible fade show show-cat-update' role='alert'>
  <strong>Product Added Successfully!! !</strong></div>";
  session_destroy();
}
       ?>
<?php
 $connection = new mysqli($dbhost,$dbusername,$dbpassword,$dbname); 
if($connection->connect_error){
  die("error while connecting database".$connection->connect_error);
}
?>

<h3 class="p-2">All Products</h1> 
<div class="container-fluid">
  <div class="d-flex py-3 d-flex justify-content-between">
  <div class="search-bar mx-2">
 <i class="fa-solid fa-magnifying-glass"></i>
 <input type="text" placeholder="Search">
     </div>
     <div>
      <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addProductModal"><i class="fa-solid fa-plus" ></i> Add Product</button>
      <button class="btn btn-outline-dark"><i class="fa-solid fa-download"></i> Export</button>
     </div>
     <div class="modal fade modal-form " id="addProductModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
  <div class="modal-dialog col-12">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title fs-5" id="staticBackdropLabel">Add New Product</h1>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <form class="form" method="POST" action="./submissions/productsSubmission.php">
            <div class="row">
              
                <div class="col-6">
                    <div class="mb-3">
                         <label for="" class="form-label">Name:</label>
                        <input type="text" class="form-control" placeholder="Enter Name" name="add_product_name">
                    </div>
                </div>
                <div class="col-6">
                    <div class="mb-3">
                         <label for="" class="form-label">Price:</label>
                        <input type="number" class="form-control" placeholder="Enter Price" name="add_product_price">
                    </div>
                </div>
                <div class="col-6">
                    <div class="mb-3">
                         <label for="" class="form-label">Category:</label>
                         <select name="add_product_category" class="form-control" id="">
                            <?php
                          $query = "Select * from Category";
                          $data = mysqli_query($connection,$query);
                          while($row = mysqli_fetch_array($data)){
                            ?>

                              <option  value="<?php echo $row["ID"];  ?>"><?php echo $row["Name"]; ?></option>

                <?php
                          }

                        
                        ?>
                         </select>
                    </div>
                </div>

               
            </div>
            <div class="modal-footer">
               <button type="submit" class="btn btn-primary" name="add_product">Add</button>
            </div>
        </form>
      </div>
    </div>
  </div>
</div>
</div> 
</div>
<table class='table table-hover rounded-3'>
  <thead>
    <tr class='table-secondary'>
      <th>ID</th>
      <th>Name</th>
      <th>Price</th>
      <th>Category</th>
      <th>Action</th>
    </th>
  </thead>
<tbody>
    <?php


$sql="Select product.ID, product.Name,product.price,product.Image,Category.Name as Category, Category.ID as catid From product  Inner Join Category ON product.Category=Category.ID ";
$result=$connection->query($sql);
if(!$result){die("Invalid Query!!".$connection->error);}

while($row=mysqli_fetch_array($result)){
  ?>
  <tr class='table-light'>
  <td><?php echo $row["ID"]?> </td>
  <td><?php echo $row["Name"]; ?></td>
  <td><?php echo $row["price"]; ?></td>
  <td><?php echo $row["Category"]; ?></td>
 
  <td>
  <button class='btn btn-primary mx-2 editProductbtn'
  data-id="<?php echo $row["ID"]; ?>"
  data-name="<?php echo $row["Name"];?>"
  data-price="<?php echo $row["price"]?>"
  data-category="<?php echo $row["catid"]?>"
  data-bs-toggle='modal' data-bs-target='#editProductModal'><i class='fa-solid fa-pen-to-square'></i> Edit Category</button>
  <button class='btn btn-danger mx-2 delete-product-btn' data-id="<?php echo $row["ID"] ?>"> <i class='fa-solid fa-trash'></i> Delete Category</button>
  </td>
  </tr>
  <div class='modal fade modal-form ' id='editProductModal' data-bs-backdrop='static' data-bs-keyboard='false' tabindex='-1' aria-labelledby='staticBackdropLabel' aria-hidden='true'>
  <div class='modal-dialog col-12'>
    <div class='modal-content'>
      <div class='modal-header'>
        <h1 class='modal-title fs-5' id='staticBackdropLabel'>Edit Product</h1>
        <button type='button' class='btn-close' data-bs-dismiss='modal' aria-label='Close'></button>
      </div>
      <div class='modal-body'>
        <form class='form' method="POST" action="./submissions/productsSubmission.php" >
            <div class='row'>
                <input type='number' class='form-control' placeholder='Enter User Id'
                        id='editProductid' name="edit_product_id" hidden 
                        >
                <div class='col-6'>
                    <div class='mb-3'>
                         <label for='' class='form-label'>Name:</label>
                        <input type='text' class='form-control' placeholder='Enter Name' name="edit_product_name"
                        id='editProductname'>
                    </div>
                </div>
                <div class='col-6'>
                    <div class='mb-3'>
                         <label for='' class='form-label'>Price:</label>
                        <input type='number' class='form-control' placeholder='Enter Price' 
                        id='editProductprice' name="edit_product_price">
                    </div>
                </div>
                <div class='col-6'>
                    <div class='mb-3'>
                         <label for='' class='form-label'>Category:</label>
                        <select class='form-control' id='editProductcategory' name="edit_product_category">

                        <?php
                          $query = "Select * from Category";
                          $data = mysqli_query($connection,$query);
                          while($row = mysqli_fetch_array($data)){
                            ?>

                              <option value="<?php echo $row["ID"];  ?>"><?php echo $row["Name"]; ?></option>

                <?php
                          }

                        
                        ?>

                        </select>
                    </div>
                </div>
               
              </div>
              <div class='modal-footer'>
                 <button type='submit' class='btn btn-primary' name="edit_submit_product">Add</button>
              </div>
        </form>
      </div>
    </div>
  </div>
</div>

  <?php
};
?>
</tbody>  
</table>
<?php include("footer.php");?>