<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width,initial-scale=1" />
  <title>Admin — Users by Category</title>

  <!-- Bootstrap 5 CSS -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

  <style>
    /* Small visual tweaks */
    .category-badge { font-weight: 600; color: #fff; padding: 0.25rem 0.5rem; border-radius: 0.25rem; font-size: .8rem; }
    .badge-district { background:#f6a623; }   /* amber */
    .badge-regional { background:#17a2b8; }   /* teal */
    .badge-national { background:#1f6feb; }   /* blue */
    .user-row:hover .row-actions { visibility: visible; opacity: 1; }
    .row-actions { visibility: hidden; opacity: 0; transition: .12s ease-in-out; }
    .table-avatar { width:36px; height:36px; object-fit:cover; border-radius:50%; }
    .toolbar { gap:.5rem; display:flex; flex-wrap:wrap; align-items:center; }
    .select-all { margin-right:.5rem; }
    @media (max-width: 767px) {
      .toolbar { flex-direction:column; align-items:stretch; }
    }
  </style>
</head>
<body class="bg-light">

<div class="container py-4">
  <div class="d-flex align-items-center mb-3">
    <h3 class="me-auto">Manage Users</h3>
    <div class="text-muted">June 15, 2026</div>
  </div>

  <!-- Tabs -->
  <ul class="nav nav-tabs mb-3" id="userTabs" role="tablist">
    <li class="nav-item" role="presentation">
      <button class="nav-link active" id="district-tab" data-bs-toggle="tab" data-bs-target="#district" type="button" role="tab" aria-controls="district" aria-selected="true">District <span class="badge bg-secondary ms-1" id="count-district">3</span></button>
    </li>
    <li class="nav-item" role="presentation">
      <button class="nav-link" id="regional-tab" data-bs-toggle="tab" data-bs-target="#regional" type="button" role="tab" aria-controls="regional" aria-selected="false">Regional <span class="badge bg-secondary ms-1" id="count-regional">2</span></button>
    </li>
    <li class="nav-item" role="presentation">
      <button class="nav-link" id="national-tab" data-bs-toggle="tab" data-bs-target="#national" type="button" role="tab" aria-controls="national" aria-selected="false">National <span class="badge bg-secondary ms-1" id="count-national">1</span></button>
    </li>
  </ul>

  <!-- Shared toolbar -->
  <div class="card mb-3">
    <div class="card-body">
      <div class="toolbar">
        <div class="d-flex align-items-center">
          <input type="checkbox" id="globalSelect" class="form-check-input select-all" aria-label="Select all visible users">
          <div class="me-2">Select</div>
        </div>

        <div class="input-group" style="max-width:420px;">
          <span class="input-group-text">Search</span>
          <input id="globalSearch" class="form-control" type="search" placeholder="Search name, email or ID (press /)" aria-label="Search users">
        </div>

        <div class="ms-auto d-flex gap-2">
          <div class="dropdown">
            <button class="btn btn-outline-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">Filter</button>
            <ul class="dropdown-menu p-3" style="min-width:220px;">
              <div class="mb-2"><strong>Status</strong></div>
              <div class="form-check"><input class="form-check-input filter-status" type="checkbox" value="Active" id="fActive"><label class="form-check-label" for="fActive">Active</label></div>
              <div class="form-check"><input class="form-check-input filter-status" type="checkbox" value="Pending" id="fPending"><label class="form-check-label" for="fPending">Pending</label></div>
              <div class="form-check"><input class="form-check-input filter-status" type="checkbox" value="Suspended" id="fSuspended"><label class="form-check-label" for="fSuspended">Suspended</label></div>
            </ul>
          </div>

          <button id="bulkMoveBtn" class="btn btn-primary" disabled data-bs-toggle="modal" data-bs-target="#moveModal">Move</button>
          <button id="bulkDeactivate" class="btn btn-outline-danger" disabled>Deactivate</button>
        </div>
      </div>
    </div>
  </div>

  <!-- Tab content -->
  <div class="tab-content" id="userTabsContent">
    <!-- District -->
    <div class="tab-pane fade show active" id="district" role="tabpanel" aria-labelledby="district-tab">
      <div class="card mb-4">
        <div class="card-body p-0">
          <div class="table-responsive">
            <table class="table table-hover mb-0">
              <thead class="table-light">
                <tr>
                  <th style="width:42px;"></th>
                  <th>Person</th>
                  <th>Email</th>
                  <th>Role</th>
                  <th>Last activity</th>
                  <th style="width:130px;">Actions</th>
                </tr>
              </thead>
              <tbody id="districtBody">
                <!-- Example rows -->
                <tr class="user-row" data-category="District" data-status="Active" data-name="Alice Doe" data-email="alice@example.com" data-id="D001">
                  <td><input class="row-select form-check-input" type="checkbox" aria-label="Select Alice Doe"></td>
                  <td class="align-middle">
                    <div class="d-flex align-items-center">
                      <img src="https://i.pravatar.cc/40?img=5" alt="avatar" class="table-avatar me-2">
                      <div>
                        <div class="fw-semibold">Alice Doe</div>
                        <div class="text-muted small">ID: D001</div>
                      </div>
                    </div>
                  </td>
                  <td class="align-middle">alice@example.com</td>
                  <td class="align-middle"><span class="category-badge badge-district">District</span> &middot; Manager</td>
                  <td class="align-middle">2026-06-10</td>
                  <td class="align-middle">
                    <div class="row-actions d-flex gap-2">
                      <button class="btn btn-sm btn-outline-secondary viewBtn" data-id="D001">View</button>
                      <button class="btn btn-sm btn-primary editBtn" data-id="D001" data-bs-toggle="modal" data-bs-target="#editModal">Edit</button>
                    </div>
                  </td>
                </tr>

                <tr class="user-row" data-category="District" data-status="Pending" data-name="Ben Kyle" data-email="ben@example.com" data-id="D002">
                  <td><input class="row-select form-check-input" type="checkbox" aria-label="Select Ben Kyle"></td>
                  <td class="align-middle">
                    <div class="d-flex align-items-center">
                      <img src="https://i.pravatar.cc/40?img=6" alt="avatar" class="table-avatar me-2">
                      <div>
                        <div class="fw-semibold">Ben Kyle</div>
                        <div class="text-muted small">ID: D002</div>
                      </div>
                    </div>
                  </td>
                  <td class="align-middle">ben@example.com</td>
                  <td class="align-middle"><span class="category-badge badge-district">District</span> &middot; Staff</td>
                  <td class="align-middle">2026-06-12</td>
                  <td class="align-middle">
                    <div class="row-actions d-flex gap-2">
                      <button class="btn btn-sm btn-outline-secondary viewBtn" data-id="D002">View</button>
                      <button class="btn btn-sm btn-primary editBtn" data-id="D002" data-bs-toggle="modal" data-bs-target="#editModal">Edit</button>
                    </div>
                  </td>
                </tr>

                <tr class="user-row" data-category="District" data-status="Suspended" data-name="Clara Park" data-email="clara@example.com" data-id="D003">
                  <td><input class="row-select form-check-input" type="checkbox" aria-label="Select Clara Park"></td>
                  <td class="align-middle">
                    <div class="d-flex align-items-center">
                      <img src="https://i.pravatar.cc/40?img=7" alt="avatar" class="table-avatar me-2">
                      <div>
                        <div class="fw-semibold">Clara Park</div>
                        <div class="text-muted small">ID: D003</div>
                      </div>
                    </div>
                  </td>
                  <td class="align-middle">clara@example.com</td>
                  <td class="align-middle"><span class="category-badge badge-district">District</span> &middot; Supervisor</td>
                  <td class="align-middle">2026-05-30</td>
                  <td class="align-middle">
                    <div class="row-actions d-flex gap-2">
                      <button class="btn btn-sm btn-outline-secondary viewBtn" data-id="D003">View</button>
                      <button class="btn btn-sm btn-primary editBtn" data-id="D003" data-bs-toggle="modal" data-bs-target="#editModal">Edit</button>
                    </div>
                  </td>
                </tr>

              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>

    <!-- Regional -->
    <div class="tab-pane fade" id="regional" role="tabpanel" aria-labelledby="regional-tab">
      <div class="card mb-4">
        <div class="card-body p-0">
          <div class="table-responsive">
            <table class="table table-hover mb-0">
              <thead class="table-light">
                <tr>
                  <th style="width:42px;"></th>
                  <th>Person</th>
                  <th>Email</th>
                  <th>Role</th>
                  <th>Last activity</th>
                  <th style="width:130px;">Actions</th>
                </tr>
              </thead>
              <tbody id="regionalBody">
                <tr class="user-row" data-category="Regional" data-status="Active" data-name="Derek Holt" data-email="derek@example.com" data-id="R001">
                  <td><input class="row-select form-check-input" type="checkbox" aria-label="Select Derek Holt"></td>
                  <td class="align-middle">
                    <div class="d-flex align-items-center">
                      <img src="https://i.pravatar.cc/40?img=8" alt="avatar" class="table-avatar me-2">
                      <div>
                        <div class="fw-semibold">Derek Holt</div>
                        <div class="text-muted small">ID: R001</div>
                      </div>
                    </div>
                  </td>
                  <td class="align-middle">derek@example.com</td>
                  <td class="align-middle"><span class="category-badge badge-regional">Regional</span> &middot; Lead</td>
                  <td class="align-middle">2026-06-13</td>
                  <td class="align-middle">
                    <div class="row-actions d-flex gap-2">
                      <button class="btn btn-sm btn-outline-secondary viewBtn" data-id="R001">View</button>
                      <button class="btn btn-sm btn-primary editBtn" data-id="R001" data-bs-toggle="modal" data-bs-target="#editModal">Edit</button>
                    </div>
                  </td>
                </tr>

                <tr class="user-row" data-category="Regional" data-status="Active" data-name="Eva Stone" data-email="eva@example.com" data-id="R002">
                  <td><input class="row-select form-check-input" type="checkbox" aria-label="Select Eva Stone"></td>
                  <td class="align-middle">
                    <div class="d-flex align-items-center">
                      <img src="https://i.pravatar.cc/40?img=9" alt="avatar" class="table-avatar me-2">
                      <div>
                        <div class="fw-semibold">Eva Stone</div>
                        <div class="text-muted small">ID: R002</div>
                      </div>
                    </div>
                  </td>
                  <td class="align-middle">eva@example.com</td>
                  <td class="align-middle"><span class="category-badge badge-regional">Regional</span> &middot; Coordinator</td>
                  <td class="align-middle">2026-06-11</td>
                  <td class="align-middle">
                    <div class="row-actions d-flex gap-2">
                      <button class="btn btn-sm btn-outline-secondary viewBtn" data-id="R002">View</button>
                      <button class="btn btn-sm btn-primary editBtn" data-id="R002" data-bs-toggle="modal" data-bs-target="#editModal">Edit</button>
                    </div>
                  </td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>

    <!-- National -->
    <div class="tab-pane fade" id="national" role="tabpanel" aria-labelledby="national-tab">
      <div class="card mb-4">
        <div class="card-body p-0">
          <div class="table-responsive">
            <table class="table table-hover mb-0">
              <thead class="table-light">
                <tr>
                  <th style="width:42px;"></th>
                  <th>Person</th>
                  <th>Email</th>
                  <th>Role</th>
                  <th>Last activity</th>
                  <th style="width:130px;">Actions</th>
                </tr>
              </thead>
              <tbody id="nationalBody">
                <tr class="user-row" data-category="National" data-status="Active" data-name="Frank West" data-email="frank@example.com" data-id="N001">
                  <td><input class="row-select form-check-input" type="checkbox" aria-label="Select Frank West"></td>
                  <td class="align-middle">
                    <div class="d-flex align-items-center">
                      <img src="https://i.pravatar.cc/40?img=10" alt="avatar" class="table-avatar me-2">
                      <div>
                        <div class="fw-semibold">Frank West</div>
                        <div class="text-muted small">ID: N001</div>
                      </div>
                    </div>
                  </td>
                  <td class="align-middle">frank@example.com</td>
                  <td class="align-middle"><span class="category-badge badge-national">National</span> &middot; Director</td>
                  <td class="align-middle">2026-06-05</td>
                  <td class="align-middle">
                    <div class="row-actions d-flex gap-2">
                      <button class="btn btn-sm btn-outline-secondary viewBtn" data-id="N001">View</button>
                      <button class="btn btn-sm btn-primary editBtn" data-id="N001" data-bs-toggle="modal" data-bs-target="#editModal">Edit</button>
                    </div>
                  </td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- Pagination stub -->
  <nav aria-label="User list pages" class="d-flex justify-content-end">
    <ul class="pagination pagination-sm">
      <li class="page-item disabled"><a class="page-link" href="#" tabindex="-1">Prev</a></li>
      <li class="page-item active"><a class="page-link" href="#">1</a></li>
      <li class="page-item"><a class="page-link" href="#">2</a></li>
      <li class="page-item"><a class="page-link" href="#">Next</a></li>
    </ul>
  </nav>
</div>

<!-- Edit modal -->
<div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-sm modal-dialog-centered">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="editModalLabel">Edit user</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <form id="editForm">
        <div class="modal-body">
          <input type="hidden" id="editId">
          <div class="mb-3"><label class="form-label">Name</label><input id="editName" class="form-control"></div>
          <div class="mb-3"><label class="form-label">Email</label><input id="editEmail" class="form-control" type="email"></div>
          <div class="mb-3">
            <label class="form-label">Category</label>
            <select id="editCategory" class="form-select">
              <option>District</option>
              <option>Regional</option>
              <option>National</option>
            </select>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
          <button type="submit" class="btn btn-primary">Save</button>
        </div>
      </form>
    </div>
  </div>
</div>

<!-- Move modal (bulk) -->
<div class="modal fade" id="moveModal" tabindex="-1" aria-labelledby="moveModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-sm modal-dialog-centered">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="moveModalLabel">Move selected users</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <form id="moveForm">
        <div class="modal-body">
          <p id="moveCountText">Move <span id="moveCount">0</span> users to:</p>
          <div class="mb-3">
            <select id="moveTarget" class="form-select" required>
              <option value="">Choose category</option>
              <option value="District">District</option>
              <option value="Regional">Regional</option>
              <option value="National">National</option>
            </select>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
          <button type="submit" class="btn btn-primary">Move</button>
        </div>
      </form>
    </div>
  </div>
</div>

<!-- Bootstrap 5 JS + Popper + jQuery (for DOM convenience) -->
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>

<script>
  (function(){
    // keyboard shortcut to focus search
    $(document).on('keydown', function(e){
      if (e.key === '/' && !$(e.target).is('input, textarea')) {
        e.preventDefault();
        $('#globalSearch').focus().select();
      }
    });

    // quick helpers
    function updateCounts(){
      $('#count-district').text($('#districtBody .user-row:visible').length);
      $('#count-regional').text($('#regionalBody .user-row:visible').length);
      $('#count-national').text($('#nationalBody .user-row:visible').length);
    }

    function getVisibleRowsInActiveTab(){
      const activePane = $('.tab-pane.active');
      return activePane.find('.user-row:visible');
    }

    // Select all visible in active tab
    $('#globalSelect').on('change', function(){
      const checked = $(this).is(':checked');
      getVisibleRowsInActiveTab().find('.row-select').prop('checked', checked).trigger('change');
    });

    // Row checkbox toggles bulk buttons
    $(document).on('change', '.row-select', function(){
      const anyChecked = $('.tab-pane.active .row-select:checked').length > 0;
      $('#bulkMoveBtn, #bulkDeactivate').prop('disabled', !anyChecked);
      // sync header select state
      const total = getVisibleRowsInActiveTab().find('.row-select').length;
      const checked = getVisibleRowsInActiveTab().find('.row-select:checked').length;
      $('#globalSelect').prop('checked', total>0 && total===checked);
    });

    // Search + filter (client-side demo)
    function applyFilters(){
      const q = $('#globalSearch').val().trim().toLowerCase();
      const statuses = $('.filter-status:checked').map(function(){ return this.value; }).get();

      $('.user-row').each(function(){
        const $r = $(this);
        const name = $r.data('name').toLowerCase();
        const email = $r.data('email').toLowerCase();
        const status = $r.data('status');
        const matchesQuery = !q || name.includes(q) || email.includes(q) || $r.data('id').toLowerCase().includes(q);
        const matchesStatus = statuses.length === 0 || statuses.indexOf(status) !== -1;
        const show = matchesQuery && matchesStatus;
        $r.toggle(show);
      });

      updateCounts();
      // reset bulk buttons if nothing selected
      $('.row-select').trigger('change');
    }

    $('#globalSearch').on('input', applyFilters);
    $('.filter-status').on('change', applyFilters);

    // Activate tab event -> clear globalSelect and bulk buttons
    $('button[data-bs-toggle="tab"]').on('shown.bs.tab', function(){
      $('#globalSelect').prop('checked', false);
      $('#bulkMoveBtn, #bulkDeactivate').prop('disabled', true);
    });

    // Edit button opens modal and populates fields
    let lastTrigger = null;
    $(document).on('click', '.editBtn', function(){
      lastTrigger = this;
      const row = $(this).closest('.user-row');
      $('#editId').val(row.data('id'));
      $('#editName').val(row.data('name'));
      $('#editEmail').val(row.data('email'));
      $('#editCategory').val(row.data('category'));
    });

    // When edit modal closes, return focus to trigger
    $('#editModal').on('hidden.bs.modal', function(){
      if (lastTrigger && document.contains(lastTrigger)) lastTrigger.focus();
      lastTrigger = null;
    });

    // Submit edit (demo: updates row inline)
    $('#editForm').on('submit', function(e){
      e.preventDefault();
      const id = $('#editId').val();
      const name = $('#editName').val();
      const email = $('#editEmail').val();
      const category = $('#editCategory').val();

      const row = $(`.user-row[data-id="${id}"]`);
      // Update data attrs and visible cells
      row.data('name', name).data('email', email).data('category', category);
      row.find('.fw-semibold').text(name);
      row.find('td').eq(2).text(email);
      row.find('.category-badge').removeClass('badge-district badge-regional badge-national');
      if (category === 'District') row.find('.category-badge').addClass('badge-district').text('District');
      if (category === 'Regional') row.find('.category-badge').addClass('badge-regional').text('Regional');
      if (category === 'National') row.find('.category-badge').addClass('badge-national').text('National');

      // Move row to correct table if category changed
      const targetBody = category === 'District' ? '#districtBody' : (category === 'Regional' ? '#regionalBody' : '#nationalBody');
      if (!row.parent().is(targetBody)) {
        row.appendTo($(targetBody));
      }

      updateCounts();
      $('#editModal').modal('hide');
    });

    // Bulk move: gather selected rows and show count
    $('#bulkMoveBtn').on('click', function(){
      const selected = $('.tab-pane.active .row-select:checked');
      $('#moveCount').text(selected.length);
      // focus first control in modal for accessibility
      $('#moveModal').on('shown.bs.modal', function(){ $('#moveTarget').focus(); });
    });

    // Remember trigger to restore focus after move modal
    $('#moveModal').on('hidden.bs.modal', function(){ $('.tab-pane.active').find('.row-select:checked').first().closest('tr').find('.row-select').focus(); });

    // Execute move
    $('#moveForm').on('submit', function(e){
      e.preventDefault();
      const target = $('#moveTarget').val();
      const selected = $('.tab-pane.active .row-select:checked').closest('.user-row');
      selected.each(function(){
        const $r = $(this);
        $r.data('category', target);
        $r.find('.category-badge').removeClass('badge-district badge-regional badge-national');
        if (target === 'District') $r.find('.category-badge').addClass('badge-district').text('District');
        if (target === 'Regional') $r.find('.category-badge').addClass('badge-regional').text('Regional');
        if (target === 'National') $r.find('.category-badge').addClass('badge-national').text('National');
        const targetBody = target === 'District' ? '#districtBody' : (target === 'Regional' ? '#regionalBody' : '#nationalBody');
        $r.appendTo($(targetBody));
        $r.find('.row-select').prop('checked', false);
      });
      $('#moveModal').modal('hide');
      $('#globalSelect').prop('checked', false);
      $('#bulkMoveBtn, #bulkDeactivate').prop('disabled', true);
      updateCounts();
    });

    // Bulk deactivate demo (toggles status)
    $('#bulkDeactivate').on('click', function(){
      const selected = $('.tab-pane.active .row-select:checked').closest('.user-row');
      selected.each(function(){
        const $r = $(this);
        $r.data('status', 'Suspended');
        // optionally add visual cue
        $r.addClass('table-danger');
        setTimeout(()=> $r.removeClass('table-danger'), 1200);
        $r.find('.row-select').prop('checked', false);
      });
      $('#bulkMoveBtn, #bulkDeactivate').prop('disabled', true);
    });

    // Accessibility: avoid aria-hidden on focused elements (demo check)
    // If you manually toggle aria-hidden elsewhere, ensure focus is moved first.
    // Update counts initially
    updateCounts();

    // Demo: focus first edit button when pressing 'e' (example of keyboard shortcut)
    $(document).on('keydown', function(e){
      if (e.key === 'e' && !$(e.target).is('input, textarea')) {
        e.preventDefault();
        $('.editBtn').first().focus();
      }
    });
  })();
</script>