<?php
session_start();
include("header.php");
if(isset($_SESSION["voucher_add_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["voucher_add_alert"])){
  echo "<div class='alert m-2 alert-success alert-dismissible fade show show-cat-update' role='alert'>
  <strong>Voucher Added Successfully!! !</strong></div>";
session_destroy();
}
if(isset($_SESSION["voucher_edit_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["voucher_edit_alert"])){
  echo "<div class='alert m-2 alert-success alert-dismissible fade show show-cat-update' role='alert'>
  <strong>Voucher Updated Successfully!! !</strong></div>";
session_destroy();
}

?>
  <div class="tab-content" id="pills-tabContent">
  <div class="tab-pane fade show active" id="pills-dashboard" role="tabpanel" aria-labelledby="pills-dashboard-tab" tabindex="0">
<h3 class="p-2">All Vouchers</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="#addTableModal"><i class="fa-solid fa-plus" ></i> Add Voucher</button>
      <button class="btn btn-outline-dark"><i class="fa-solid fa-download"></i> Export</button>
     </div>
     <div class="modal fade modal-form " id="addTableModal" 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 Voucher</h1>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <form method="POST" action="./submissions/vouchersSubmission.php" class='form'>
            <div class='row'>
               
                <div class='col-6'>
                    <div class='mb-3'>
                         <label for='' class='form-label'>Voucher Name:</label>
                        <input type='text' class='form-control' placeholder='Enter Name' name="add_voucher_name">
                    </div>
                </div>
                
                <div class='col-6'>
                    <div class='mb-3'>
                         <label for='' class='form-label'>Discount (%):</label>
                        <input type='number' class='form-control' placeholder='Enter Name'  name="add_voucher_discount">
                    </div>
                </div>
                <div class='col-6'>
                    <div class='mb-3'>
                         <label for='' class='form-label'>Status:</label>
                        <select class='form-control'name="add_voucher_status">
                         <option value="Active">Active</option>
                          <option value="Inactive">Inactive</option>
                        </select>
                    </div>
                </div>
                <div class='col-6'>
                    <div class='mb-3'>
                         <label for='' class='form-label'>Discount Type:</label>
                        <input type='text' class='form-control' placeholder='Enter Name' name="add_voucher_discountType">
                    </div>
                </div>
                
            </div>
            <div class='modal-footer'>
               <button type='submit' name="add_voucher_btn" class='btn btn-primary'>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>Voucher Name</th>
      <th>Discount %</th>
      <th>Active / Inactive</th>
      <th>Discount Type</th>
      <th>Action</th>
    </th>
  </thead>
<tbody>
    <?php

 $connection = new mysqli($dbhost,$dbusername,$dbpassword,$dbname); 
if($connection->connect_error){
  die("error while connecting database".$connection->connect_error);
}
$sql="SELECT * FROM 	voucher";
$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["VoucherName"] ?></td>
  <td><?php echo $row["Discount"]?></td>
  <td><?php echo $row["IsActive"]?></td>
  <td><?php echo $row["Discount_Type"]?></td>
 
  <td>
  <button class='btn btn-primary mx-2 editVoucherbtn'
  data-id="<?php echo $row["ID"] ?>"
  data-name="<?php echo $row["VoucherName"] ?>"
  data-discount="<?php echo $row["Discount"] ?>"
  data-active="<?php echo $row["IsActive"] ?>"
  data-discountType="<?php echo $row["Discount_Type"] ?>"
  data-bs-toggle='modal' data-bs-target='#editTableModal'><i class='fa-solid fa-pen-to-square'></i> Edit Voucher</button>
  <button class='btn btn-danger mx-2 voucher_dlt_btn' data-id="<?php echo $row['ID'] ?>"> <i class='fa-solid fa-trash'></i> Delete Voucher</button>
  </td>
  </tr>
  <div class='modal fade modal-form ' id='editTableModal' 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 Voucher</h1>
        <button type='button' class='btn-close' data-bs-dismiss='modal' aria-label='Close'></button>
      </div>
      <div class='modal-body'>
        <form method="POST" action="./submissions/vouchersSubmission.php" class='form'>
            <div class='row'>
               <input type="number" name="edit_voucher_id" id="editVoucherid" hidden>
                <div class='col-6'>
                    <div class='mb-3'>
                         <label for='' class='form-label'>Voucher Name:</label>
                        <input type='text' class='form-control' placeholder='Enter Name' id="editVouchername" name="edit_voucher_name">
                    </div>
                </div>
                
                <div class='col-6'>
                    <div class='mb-3'>
                         <label for='' class='form-label'>Discount (%):</label>
                        <input type='number' class='form-control' placeholder='Enter Name' id="editVoucherdiscount" name="edit_voucher_discount">
                    </div>
                </div>
                <div class='col-6'>
                    <div class='mb-3'>
                         <label for='' class='form-label'>Status:</label>
                        <select class='form-control' name="edit_voucher_status" id="editVoucherStatus">
                          <option value=""></option>
                          <option value="Active">Active</option>
                          <option value="Inactive">Inactive</option>
                        </select>
                    </div>
                </div>
                <div class='col-6'>
                    <div class='mb-3'>
                         <label for='' class='form-label'>Discount Type:</label>
                        <input type='text' class='form-control' placeholder='Enter Name' id="editVoucherdiscounttype" name="edit_voucher_discountType">
                    </div>
                </div>
                
            </div>
            <div class='modal-footer'>
               <button type='submit' name="edit_voucher_btn" class='btn btn-primary'>Add</button>
            </div>
        </form>
      </div>
    </div>
  </div>
</div>

  <?php
}
?>
</tbody>  
</table>
  
</div>
</div>


    
  <?php include("footer.php") ?>