<div id="page-wrapper">
  <div id="page-inner">
           <?php
if (session_status() == PHP_SESSION_NONE) session_start();
if (!empty($_SESSION['message'])): ?>
    <div class="custom-alert" id="customAlert">
        <?= $_SESSION['message'] ?>
        <span class="close-btn" onclick="closeAlert()">&times;</span>
    </div>
<?php 
    unset($_SESSION['message']); // Clear message after displaying
endif;
?>

    <div class="row mb-3">
        <div class="col-6"><h2>All Users</h2></div>
        <div class="col-6 text-right">
            <button class="btn btn-warning" data-toggle="modal" data-target="#addUserModal">Add New User</button>
        </div>
    </div>

    <table class="table table-bordered table-striped">
        <thead class="thead-dark">
            <tr>
                <th>ID</th><th>Username</th><th>Phone</th><th>Email</th>
                <th>User Type</th><th>Registered At</th><th>Action</th>
            </tr>
        </thead>
        <tbody>
        <?php if(!empty($users)): foreach($users as $user): ?>
            <tr>
                <td><?= $user['id'] ?></td>
                <td><?= htmlspecialchars($user['username']) ?></td>
                <td><?= htmlspecialchars($user['phone']) ?></td>
                <td><?= htmlspecialchars($user['email']) ?></td>
                <td><?= htmlspecialchars($user['user_type']) ?></td>
                <td><?= $user['created_at'] ?? '' ?></td>
                <td>
                    <button class="btn btn-sm btn-warning" data-toggle="modal" data-target="#editUserModal<?= $user['id'] ?>">
                        <i class="fas fa-edit"></i>
                    </button>
                    <a href="<?= BASE_URL ?>/user?delete=<?= $user['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Delete this user?')">
                        <i class="fas fa-trash-alt"></i>
                    </a>
                </td>
            </tr>

            <!-- Edit User Modal -->
            <div class="modal fade" id="editUserModal<?= $user['id'] ?>" tabindex="-1" role="dialog">
              <div class="modal-dialog" role="document">
                <div class="modal-content">
                  <form method="post">
                    <input type="hidden" name="id" value="<?= $user['id'] ?>">

                    <div class="modal-header">
                      <h5 class="modal-title">Edit User: <?= htmlspecialchars($user['username']) ?></h5>
                      <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>

                    <div class="modal-body">
                      <div class="form-group">
                          <label>Username</label>
                          <input type="text" name="username" class="form-control" value="<?= htmlspecialchars($user['username']) ?>" required>
                      </div>
                      <div class="form-group">
                          <label>Phone</label>
                          <input type="text" name="phone" class="form-control" value="<?= htmlspecialchars($user['phone']) ?>" required>
                      </div>
                      <div class="form-group">
                          <label>Email</label>
                          <input type="email" name="email" class="form-control" value="<?= htmlspecialchars($user['email']) ?>" required>
                      </div>
                      <div class="form-group">
                          <label>Password (leave blank to keep current)</label>
                          <input type="password" name="password" class="form-control">
                      </div>
                      <div class="form-group">
                          <label>User Type</label>
                          <select name="user_type" class="form-control" required>
                              <option value="buyer" <?= $user['user_type']=='buyer'?'selected':'' ?>>Buyer</option>
                              <option value="seller" <?= $user['user_type']=='seller'?'selected':'' ?>>Seller</option>
                          </select>
                      </div>
                    </div>

                    <div class="modal-footer">
                      <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                      <button type="submit" name="edit" class="btn btn-success">Save Changes</button>
                    </div>
                  </form>
                </div>
              </div>
            </div>
        <?php endforeach; else: ?>
            <tr><td colspan="7" class="text-center">No users found</td></tr>
        <?php endif; ?>
        </tbody>
    </table>
  </div>
</div>

<!-- Add User Modal -->
<div class="modal fade" id="addUserModal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
       <form method="post">
        <div class="modal-header">
          <h5 class="modal-title">Add New User</h5>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>

        <div class="modal-body">
          <div class="form-group">
              <label>Username</label>
              <input type="text" name="username" class="form-control" required>
          </div>
          <div class="form-group">
              <label>Phone</label>
              <input type="text" name="phone" class="form-control" required>
          </div>
          <div class="form-group">
              <label>Email</label>
              <input type="email" name="email" class="form-control" required>
          </div>
          <div class="form-group">
              <label>Password</label>
              <input type="password" name="password" class="form-control" required>
          </div>
          <div class="form-group">
              <label>User Type</label>
              <select name="user_type" class="form-control" required>
                  <option value="buyer">Buyer</option>
                  <option value="seller">Seller</option>
              </select>
          </div>
        </div>

        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          <button type="submit" name="add" class="btn btn-warning">Add User</button>
        </div>
      </form>
    </div>
  </div>
</div>
