@extends('layouts.admin')
@section('title','User Management')
@section('subtitle','Manage and monitor all platform participants.')
@section('content')
<form class="filter-panel" method="get">
    <div class="tabs">
        <a class="{{!request('type')?'active':''}}" href="{{route('users',array_filter(['q'=>request('q'),'status'=>request('status')]))}}">All Users</a>
        <a class="{{request('type')==='professional'?'active':''}}" href="{{route('users',array_filter(['type'=>'professional','q'=>request('q'),'status'=>request('status')]))}}">Experts Only</a>
        <a class="{{request('type')==='client'?'active':''}}" href="{{route('users',array_filter(['type'=>'client','q'=>request('q'),'status'=>request('status')]))}}">Clients Only</a>
    </div>
    <input type="hidden" name="type" value="{{request('type')}}">
    <div class="filter-controls">
        <input name="q" value="{{request('q')}}" placeholder="Name, email, or ID">
        <select name="status">
            <option value="">Status: All</option>@foreach(['active','inactive','deactivated'] as $status) <option value="{{$status}}" @selected(request('status')===$status)>{{ucfirst($status)}}</option>@endforeach
        </select>
        <button>Apply Filters</button>
    </div>
</form>
<div class="user-stats">
    <article>
        <div>
            <p>Total Users</p>
            <strong>{{number_format($stats['total'])}}</strong>
            <span>Live database count</span>
        </div>
        <i data-lucide="users"></i>
    </article>
    <article>
        <div>
            <p>Unverified Users</p>
            <strong>{{number_format($stats['unverified'])}}</strong>
            <span>Accounts not yet verified</span>
        </div>
        <i class="warning" data-lucide="user-round-x"></i>
    </article>
    <article>
        <div>
            <p>Active Experts</p>
            <strong>{{number_format($stats['experts'])}}</strong>
            <span>Approved and active professionals</span>
        </div>
        <i data-lucide="sprout"></i>
    </article>
</div>
<div class="user-table">
    <div class="table-scroll">
        <table>
            <thead>
                <tr>
                    <th>User Details</th>
                    <th>Type / Specialty</th>
                    <th>Status</th>
                    <th>Join Date</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>@forelse($users as $user)@php($image=trim((string)($user->profile?->image??'')))@php($imageUrl=$image!==''?(Illuminate\Support\Str::startsWith($image,['http://','https://'])?$image:rtrim(config('admin.api_url'),'/').'/storage/users/'.$user->id.'/'.ltrim($image,'/')):null)@php($borderColor=$user->professional?->profession?->color_hex?:'#d8ddd4') <tr>
                    <td>
                        <div class="user-cell">
                            <div class="avatar user-avatar" style="--avatar-border:{{$borderColor}}">@if($imageUrl) <img src="{{$imageUrl}}" alt="{{$user->profile?->full_name??'User'}}" loading="lazy" onerror="this.hidden=true;this.nextElementSibling.hidden=false">
                                <span hidden>{{strtoupper(substr($user->profile?->full_name??$user->email,0,1))}}</span>@else <span>{{strtoupper(substr($user->profile?->full_name??$user->email,0,1))}}</span>@endif
                            </div>
                            <div>
                                <b>{{$user->profile?->full_name??$user->display_name??'No profile'}}</b>
                                <span>{{$user->email}}</span>
                            </div>
                        </div>
                    </td>
                    <td>
                        <span class="role-badge {{strtolower($user->role?->name??'user')}}">{{strtoupper($user->role?->name??'USER')}}</span>
                        <small>{{$user->professional?->profession?->name??'—'}}</small>
                    </td>
                    <td>
                        <span class="account-status {{$user->verified?'verified':'pending'}}">
                            <i></i>{{$user->verified?'Verified':'Pending Review'}}
                        </span>
                    </td>
                    <td>{{$user->created_at?->format('M d, Y')}}</td>
                    <td>
                        <form method="post" action="{{route('users.update',$user)}}" class="user-action">@csrf @method('patch') <select name="status">@foreach(['active','inactive','deactivated'] as $status) <option value="{{$status}}" @selected($user->status===$status)>{{ucfirst($status)}}</option>@endforeach </select>
                            <select name="verified">
                                <option value="1" @selected($user->verified)>Verified</option>
                                <option value="0" @selected(!$user->verified)>Unverified</option>
                            </select>
                            <button>Save</button>
                        </form>
                    </td>
                </tr>@empty <tr>
                    <td colspan="5" class="empty">No users match these filters.</td>
                </tr>@endforelse </tbody>
        </table>
    </div>
    <div class="table-footer">
        <span>Showing {{$users->firstItem()??0}} to {{$users->lastItem()??0}} of {{number_format($users->total())}} entries</span>{{$users->onEachSide(1)->links('vendor.pagination.default')}}
    </div>
</div>
@endsection
