@extends('admin.layout')
@section('title','Users')
@section('page-title','USERS')

@section('content')
<div class="card">
  <div class="card-header">
    <div class="card-title">ALL USERS ({{ $users->total() }})</div>
    <form class="search-form" method="GET">
      <input class="search-input" name="search" placeholder="Name or email…" value="{{ request('search') }}"/>
      <button class="btn btn-primary btn-sm" type="submit">Search</button>
      @if(request('search'))
        <a href="{{ route('admin.users') }}" class="btn btn-ghost btn-sm">Clear</a>
      @endif
    </form>
  </div>

  <div class="table-wrap">
    <table>
      <thead>
        <tr><th>Name</th><th>Email</th><th>Phone</th><th>Orders</th><th>Role</th><th>Joined</th><th>Action</th></tr>
      </thead>
      <tbody>
        @forelse($users as $user)
        <tr>
          <td>
            <div style="display:flex;align-items:center;gap:10px">
              <div style="width:36px;height:36px;border-radius:50%;background:linear-gradient(135deg,var(--orange),var(--gold));display:flex;align-items:center;justify-content:center;font-weight:800;font-size:.85rem;color:#0a0a14;flex-shrink:0">
                {{ strtoupper(substr($user->name,0,1)) }}
              </div>
              <span style="font-weight:600;font-size:.875rem">{{ $user->name }}</span>
            </div>
          </td>
          <td style="color:var(--muted);font-size:.82rem">{{ $user->email }}</td>
          <td style="color:var(--muted);font-size:.82rem">{{ $user->phone ?: '—' }}</td>
          <td>
            <span style="font-weight:700;color:var(--orange)">{{ $user->orders_count }}</span>
          </td>
          <td>
            <span class="badge {{ $user->is_admin ? 'badge-admin' : 'badge-user' }}">
              {{ $user->is_admin ? 'Admin' : 'Customer' }}
            </span>
          </td>
          <td style="color:var(--muted);font-size:.78rem">{{ $user->created_at->format('d M Y') }}</td>
          <td>
            @if($user->id !== auth()->id())
            <form method="POST" action="{{ route('admin.users.toggle-admin', $user) }}">
              @csrf @method('PATCH')
              <button class="btn btn-ghost btn-sm" type="submit">
                {{ $user->is_admin ? 'Remove Admin' : 'Make Admin' }}
              </button>
            </form>
            @else
            <span style="color:var(--muted);font-size:.78rem">You</span>
            @endif
          </td>
        </tr>
        @empty
        <tr><td colspan="7" style="text-align:center;color:var(--muted);padding:40px">No users found</td></tr>
        @endforelse
      </tbody>
    </table>
  </div>

  <div class="pagination">
    {{ $users->links() }}
  </div>
</div>
@endsection
