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

  <h3 class="p-2">All Users</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="#addUserModal"><i class="fa-solid fa-plus" ></i> Add User</button>
      <button class="btn btn-outline-dark"><i class="fa-solid fa-download"></i> Export</button>
     </div>
     <div class="modal fade modal-form " id="addUserModal" 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 User</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/userSubmission.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_user_name">
                    </div>
                </div>
                <div class="col-6">
                    <div class="mb-3">
                         <label for="" class="form-label">Username:</label>
                        <input type="text" class="form-control" placeholder="Enter Username" name="add_user_username">
                    </div>
                </div>
                <div class="col-6">
                    <div class="mb-3">
                         <label for="" class="form-label">Password:</label>
                        <input type="password" class="form-control" placeholder="Enter User Password" name="add_user_password">
                    </div>
                </div>
                <div class="col-6">
                    <div class="mb-3">
                         <label for="" class="form-label">Choose Role:</label>
                        <select  id="" class="form-control" name="add_user_role">
                            <option value=""></option>
                            <option value="Admin">Admin</option>
                            <option value="Cashier">Cashier</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
               <button type="submit" class="btn btn-primary" 
               name="add_user_btn"
               >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>Username</th>
      <th>Password</th>
      <th>Role</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 user";
$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["Username"]; ?></td>
  <td><?php echo $row["Password"];?></td>
  <td><?php echo $row["Role"];?></td>
  <td>
  <button class='btn btn-primary mx-2  edituserbtn' 
  data-id=<?php echo $row["ID"];?>
  data-name=<?php echo $row["Name"];?>
 data-username=<?php echo $row["Username"];?>
 data-password=<?php echo $row["Password"];?>
 data-role=<?php echo $row["Role"];?>
  data-bs-toggle='modal' data-bs-target='#editUserModal'><i class='fa-solid fa-pen-to-square'
  ></i> Edit User</button>
  <button class='btn btn-danger mx-2 dlt_user_btn' data-id="<?php echo $row['ID'] ?>"> <i class='fa-solid fa-trash'></i> Delete User</button>
  </td>
  </tr>
  

  

  <?php
}
?>
<div class='modal fade modal-form' id='editUserModal'  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 User</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/userSubmission.php">
            <div class="row">
              <input type="hidden" id="edituserId"  name="edit_user_id"> 
                <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_user_name" id="edituserName">
                    </div>
                </div>
                <div class="col-6">
                    <div class="mb-3">
                         <label for="" class="form-label">Username:</label>
                        <input type="text" class="form-control" placeholder="Enter Username" name="edit_user_username" id="edituserUName">
                    </div>
                </div>
                <div class="col-6">
                    <div class="mb-3">
                         <label for="" class="form-label">Password:</label>
                        <input type="password" class="form-control" placeholder="Enter User Password" name="edit_user_password" id="edituserPassword">
                    </div>
                </div>
                <div class="col-6">
                    <div class="mb-3">
                         <label for="" class="form-label">Choose Role:</label>
                        <select  id="" class="form-control" name="edit_user_role" id="editUserRole">
                            <option value="Admin">Admin</option>
                            <option value="Cashier">Cashier</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
               <button type="submit" class="btn btn-primary" 
               name="edit_user_btn"
               >Add</button>
            </div>
        </form>
      </div>
     
    </div>
  </div>
</div>
</tbody>  
</table>

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