@extends('layouts.master')
@section('page-title') 
    Admin | {{ $page_title }}
@endsection
@section('page-css')
<?php //dd(session('current_user')); ?>
<link rel="stylesheet" href="{{ url('public/assets/libs/select2/dist/css/select2.css') }}">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.19/css/intlTelInput.css"/>

<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; }
    .status-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; }
    .status-active { background-color: #28a745; }
    .status-suspended { background-color: #dc3545; }
    @media (max-width: 767px) {
      .toolbar { flex-direction:column; align-items:stretch; }
    }
</style>
@endsection

@section('page-content')
@include('admin.partials.create-user')
@include('admin.partials.edit-user')
@include('admin.partials.move-user')
@include('admin.partials.view-user')
@include('layouts.partials.navbar')
<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">{{ \Carbon\Carbon::now()->format('F j, Y') }}</div> -->
    <div class="text-muted">Sekondi Takoradi</div>
  </div>

  <ul class="nav nav-tabs mb-3" id="userTabs" role="tablist">

    @if (in_array(session('current_user.user_type'), ['district_user', 'regional_luspa', 'national_luspa']))
    <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">{{ $districtUsers->count() }}</span>
      </button>
    </li>
    @endif
     @if (in_array(session('current_user.user_type'), ['regional_luspa', 'national_luspa']))
    <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 <?php //dump($regionalUsers->count()); ?> <span class="badge bg-secondary ms-1" id="count-regional">{{ $regionalUsers->count() }}</span>
      </button>
    </li>
    @endif
     @if (in_array(session('current_user.user_type'), ['national_luspa']))
    <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">{{ $nationalUsers->count() }}</span>
      </button>
    </li>
    @endif
  </ul>

  <div class="card mb-3">
    <div class="card-body">
      <div class="toolbar">
        <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" aria-label="Search users">
        </div>

        <div class="ms-auto d-flex gap-2">
          <!-- <button id="addUserBtn" class="btn btn-outline-success" disabled>Add User</button> -->
          <button type="button" class="btn btn-success btn-sm" data-bs-toggle="modal" data-bs-target="#addUserModal">
        <i class="bi bi-plus-circle"></i> Add User</button>
          <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="Inactive" id="fSuspended"><label class="form-check-label" for="fSuspended">Inactive</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>Disable</button>
        </div>
      </div>
    </div>
  </div>

  <div class="tab-content" id="userTabsContent">
    @if (in_array(session('current_user.user_type'), ['district_user', 'regional_luspa', 'national_luspa']))
    <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>Name</th>
                  <th>Email</th>
                  <th>Role</th>
                  <th>Status</th>
                  <th>Allowed Apps</th>
                  <th style="width:130px;">Actions</th>
                </tr>
              </thead>
              <tbody id="districtBody">
                @forelse($districtUsers as $user)
                    @php $status = ($user['is_disabled'] === 'true' || $user['is_disabled'] === true) ? 'Inactive' : 'Active'; @endphp
                    <tr class="user-row" data-category="District" data-status="{{ $status }}" data-name="{{ $user['full_name'] }}" data-email="{{ $user['email'] }}" data-id="{{ $user['user_id'] }}">
                      <td><input class="row-select form-check-input" type="checkbox" aria-label="Select {{ $user['full_name'] }}"></td>
                      <td class="align-middle">
                        <div class="d-flex align-items-center">
                          <img src="https://ui-avatars.com/api/?name={{ urlencode($user['full_name']) }}&background=random" alt="avatar" class="table-avatar me-2">
                          <div>
                            <div class="fw-semibold">{{ $user['title'] }} {{ $user['full_name'] }}</div>
                            <div class="text-muted small">Username: {{ $user['username'] }}</div>
                          </div>
                        </div>
                      </td>
                      <td class="align-middle">{{ $user['email'] }}</td>
                      <td class="align-middle"><span class="category-badge badge-district">{{ strtoupper(str_replace('-', ' ', $user['ua_position'])) }}</span></td>
                      <td class="align-middle">
                          <span class="status-dot {{ $status === 'Active' ? 'status-active' : 'status-suspended' }}"></span> {{ $status }}
                      </td>
                      <td class="align-middle">{{ $user['allowed_apps'] }}</td>
                      <td class="align-middle">
                        <div class="row-actions d-flex gap-2">
                          <!-- <button class="btn btn-sm btn-outline-secondary viewBtn" data-id="{{ $user['user_id'] }}"><i class="bi bi-eye"></i> View</button> -->
                          <button class="btn btn-sm btn-primary editUserBtn" data-id="{{ $user['user_id'] }}" data-bs-toggle="modal" data-bs-target="#editUserModal"><i class="bi bi-pen"></i> Edit</button>
                        </div>
                      </td>
                    </tr>
                @empty
                    <tr><td colspan="7" class="text-center text-muted py-4">No district users found.</td></tr>
                @endforelse
              </tbody>
            </table>
            <div id="district-pagination" class="mt-3 px-3 pb-3">
                {{ $districtUsers->links() }}
            </div>
          </div>
        </div>
      </div>
    </div>
    @endif
     @if (in_array(session('current_user.user_type'), ['regional_luspa', 'national_luspa']))
    <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>Status</th>
                  <th>Allowed Apps</th>
                  <th style="width:130px;">Actions</th>
                </tr>
              </thead>
              <tbody id="regionalBody">
                @forelse($regionalUsers as $user)
                  <?php  ?>
                    @php $status = ($user['is_disabled'] === 'true' || $user['is_disabled'] === true) ? 'Inactive' : 'Active'; @endphp
                    <tr class="user-row" data-category="Regional" data-status="{{ $status }}" data-name="{{ $user['full_name'] }}" data-email="{{ $user['email'] }}" data-id="{{ $user['user_id'] }}">
                      <td><input class="row-select form-check-input" type="checkbox" aria-label="Select {{ $user['full_name'] }}"></td>
                      <td class="align-middle">
                        <div class="d-flex align-items-center">
                          <img src="https://ui-avatars.com/api/?name={{ urlencode($user['full_name']) }}&background=random" alt="avatar" class="table-avatar me-2">
                          <div>
                            <div class="fw-semibold">{{ $user['title'] }} {{ $user['full_name'] }}</div>
                            <div class="text-muted small">Username: {{ $user['username'] }}</div>
                          </div>
                        </div>
                      </td>
                      <td class="align-middle">{{ $user['email'] }}</td>
                      <td class="align-middle"><span class="category-badge badge-regional">{{ strtoupper(str_replace('-', ' ', $user['ua_position'])) }}</span></td>
                      <td class="align-middle">
                          <span class="status-dot {{ $status === 'Active' ? 'status-active' : 'status-suspended' }}"></span> {{ $status }}
                      </td>
                      <td class="align-middle">{{ $user['allowed_apps'] }}</td>
                      <td class="align-middle">
                        <div class="row-actions d-flex gap-2">
                          <!-- <button class="btn btn-sm btn-outline-secondary viewBtn" data-id="{{ $user['user_id'] }}"><i class="bi-eye"></i> View</button> -->
                          <button class="btn btn-sm btn-primary editUserBtn" data-id="{{ $user['user_id'] }}" data-bs-toggle="modal" data-bs-target="#editUserModal"><i class="bi-pen"></i> Edit</button>
                        </div>
                      </td>
                    </tr>
                @empty
                    <tr><td colspan="7" class="text-center text-muted py-4">No regional users found.</td></tr>
                @endforelse
              </tbody>
            </table>
            <div id="regional-pagination" class="mt-3 px-3 pb-3">
                {{ $regionalUsers->links() }}
            </div>
          </div>
        </div>
      </div>
    </div>
    @endif
     @if (in_array(session('current_user.user_type'), ['district_user', 'regional_luspa', 'national_luspa']))
    <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>Status</th>
                  <th>Allowed Apps</th>
                  <th style="width:130px;">Actions</th>
                </tr>
              </thead>
              <tbody id="nationalBody">
                @forelse($nationalUsers as $user)
                    @php $status = ($user['is_disabled'] === 'true' || $user['is_disabled'] === true) ? 'Inactive' : 'Active'; @endphp
                    <tr class="user-row" data-category="National" data-status="{{ $status }}" data-name="{{ $user['full_name'] }}" data-email="{{ $user['email'] }}" data-id="{{ $user['user_id'] }}">
                      <td><input class="row-select form-check-input" type="checkbox" aria-label="Select {{ $user['full_name'] }}"></td>
                      <td class="align-middle">
                        <div class="d-flex align-items-center">
                          <img src="https://ui-avatars.com/api/?name={{ urlencode($user['full_name']) }}&background=random" alt="avatar" class="table-avatar me-2">
                          <div>
                            <div class="fw-semibold">{{ $user['title'] }} {{ $user['full_name'] }}</div>
                            <div class="text-muted small">Username: {{ $user['username'] }}</div>
                          </div>
                        </div>
                      </td>
                      <td class="align-middle">{{ $user['email'] }}</td>
                      <td class="align-middle"><span class="category-badge badge-national">{{ strtoupper(str_replace('-', ' ', $user['ua_position'])) }}</span></td>
                      <td class="align-middle">
                          <span class="status-dot {{ $status === 'Active' ? 'status-active' : 'status-suspended' }}"></span> {{ $status }}
                      </td>
                      <td class="align-middle">{{ $user['allowed_apps'] }}</td>
                      <td class="align-middle">
                        <div class="row-actions d-flex gap-2">
                          <!-- <button class="btn btn-sm btn-outline-secondary viewBtn" data-id="{{ $user['user_id'] }}"><i class="bi bi-eye"></i> View</button> -->
                          <button class="btn btn-sm btn-primary editUserBtn" data-id="{{ $user['user_id'] }}" data-bs-toggle="modal" data-bs-target="#editUserModal"><i class="bi bi-eye"></i> Edit</button>
                        </div>
                      </td>
                    </tr>
                @empty
                    <tr><td colspan="7" class="text-center text-muted py-4">No national users found.</td></tr>
                @endforelse
              </tbody>
            </table>
            <div id="national-pagination" class="mt-3 px-3 pb-3">
                {{ $nationalUsers->links() }}
            </div>
          </div>
        </div>
      </div>
    </div>
    @endif
    
  </div>
</div>
@endsection
@section('page-js')
  <script src="{{ url('public/assets/libs/select2/dist/js/select2.full.min.js') }}" type="text/javascript"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.19/js/intlTelInput.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.19/js/utils.js"></script>

  <script src="{{ url('public/assets/js/user_mgt_new.js') }}" type="text/javascript"></script>
  <script src="{{ url('public/assets/js/add_edit_user.js') }}" type="text/javascript"></script>
@endsection
