@extends('bo.layouts.backend')

@section('title', 'Utilisateurs BO · LineUp France')

@section('content')
    <div class="bg-body-light bo-page-top">
        <div class="content content-full bo-page-top-content">
            <div class="d-flex flex-column flex-sm-row justify-content-sm-between align-items-sm-center py-2 bo-page-head">
                <div class="flex-grow-1">
                    <h1 class="h3 fw-bold mb-1 bo-page-title">Utilisateurs BO</h1>
                    <h2 class="fs-base lh-base fw-medium text-muted mb-0 bo-page-subtitle">Gestion admin des comptes client, liner et admin.</h2>
                </div>
                <nav class="flex-shrink-0 mt-3 mt-sm-0 ms-sm-3" aria-label="breadcrumb">
                    <ol class="breadcrumb breadcrumb-alt bo-breadcrumb">
                        <li class="breadcrumb-item">LineUp France</li>
                        <li class="breadcrumb-item" aria-current="page">Utilisateurs</li>
                    </ol>
                </nav>
            </div>
            <div class="bo-hero-actions">
                <a href="{{ route('bo.dashboard') }}" class="btn btn-sm btn-alt-secondary">
                    <i class="si si-speedometer me-1"></i> Tableau de bord
                </a>
                <a href="{{ route('bo.missions.index') }}" class="btn btn-sm btn-alt-secondary">
                    <i class="si si-briefcase me-1"></i> Missions
                </a>
                <a href="{{ route('bo.kyc.index') }}" class="btn btn-sm btn-alt-secondary">
                    <i class="si si-shield me-1"></i> KYC
                </a>
                <a href="{{ route('bo.payouts.index') }}" class="btn btn-sm btn-alt-secondary">
                    <i class="si si-wallet me-1"></i> Retraits
                </a>
                <div class="ms-auto">
                    <a href="{{ route('bo.users.create') }}" class="btn btn-alt-primary">
                        <i class="fa fa-plus opacity-50 me-1"></i> Nouvel utilisateur
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="content bo-page-content">
        <div class="block block-rounded">
            <div class="block-header block-header-default">
                <h3 class="block-title"><i class="si si-equalizer me-1"></i>Filtres avances</h3>
                <div class="block-options">
                    <a href="{{ route('bo.users.index') }}" class="btn btn-sm btn-alt-secondary">
                        <i class="fa fa-rotate-left opacity-50 me-1"></i> Reinitialiser
                    </a>
                </div>
            </div>
            <div class="block-content">
                <form method="GET" action="{{ route('bo.users.index') }}" class="row g-3 align-items-end bo-filter-form">
                    <div class="col-md-4">
                        <label class="form-label" for="q">Recherche</label>
                        <div class="input-group">
                            <span class="input-group-text bg-body-light border-0"><i class="fa fa-search text-muted"></i></span>
                            <input id="q" type="text" name="q" class="form-control form-control-alt" value="{{ $search }}" placeholder="Nom, email, telephone">
                        </div>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label" for="role">Role utilisateur</label>
                        <select id="role" name="role" class="form-select">
                            <option value="">Tous</option>
                            @foreach (['admin', 'client', 'liner'] as $roleOption)
                                <option value="{{ $roleOption }}" @selected($role === $roleOption)>{{ strtoupper($roleOption) }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label" for="kyc">KYC</label>
                        <select id="kyc" name="kyc" class="form-select">
                            <option value="">Tous</option>
                            <option value="verified" @selected($kyc === 'verified')>Verifie</option>
                            <option value="pending" @selected($kyc === 'pending')>En attente</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label" for="activity">Activite</label>
                        <select id="activity" name="activity" class="form-select">
                            <option value="">Toutes</option>
                            <option value="online" @selected($activity === 'online')>Actif (24h)</option>
                            <option value="inactive" @selected($activity === 'inactive')>Inactif (30j+)</option>
                        </select>
                    </div>
                    <div class="col-md-2 d-flex gap-2 bo-filter-actions">
                        <button type="submit" class="btn btn-alt-primary w-100">Filtrer</button>
                    </div>
                </form>
            </div>
        </div>

        <div class="block block-rounded mb-0">
            <div class="block-header block-header-default">
                <h3 class="block-title"><i class="si si-users me-1"></i>Liste utilisateurs</h3>
                <div class="block-options">
                    <span class="badge bg-primary">{{ $users->total() }} resultat(s)</span>
                </div>
            </div>
            <div class="block-content p-0">
                <div class="table-responsive">
                    <table class="table table-striped table-hover table-vcenter fs-sm mb-0 bo-table">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>Identite</th>
                            <th>Role</th>
                            <th>KYC</th>
                            <th>Missions</th>
                            <th>Paiements</th>
                            <th>Preferences</th>
                            <th>Derniere activite</th>
                            <th style="width: 160px;">Actions</th>
                        </tr>
                        </thead>
                        <tbody>
                        @forelse($users as $user)
                            <tr>
                                <td>#{{ $user->id }}</td>
                                <td>
                                    <div class="fw-semibold">{{ $user->name }}</div>
                                    <div class="text-muted">{{ $user->email }}</div>
                                    @if($user->phone)
                                        <small class="text-muted">{{ $user->phone }}</small>
                                    @endif
                                </td>
                                <td>
                                    @php
                                        $roleBadge = match(strtolower((string) $user->role)) {
                                            'admin' => 'bg-danger',
                                            'liner' => 'bg-success',
                                            default => 'bg-info',
                                        };
                                    @endphp
                                    <span class="badge {{ $roleBadge }}">{{ strtoupper($user->role) }}</span>
                                </td>
                                <td>
                                    @if(strtolower((string) $user->role) === 'admin')
                                        <span class="badge bg-secondary">SANS OBJET (ADMIN)</span>
                                    @else
                                        <span class="badge {{ $user->is_kyc_verified ? 'bg-success' : 'bg-warning text-dark' }}">
                                            {{ $user->is_kyc_verified ? 'VERIFIE' : 'EN ATTENTE' }}
                                        </span>
                                    @endif
                                </td>
                                <td>
                                    <div>Client: {{ $user->client_missions_count }}</div>
                                    <div>Liner: {{ $user->liner_missions_count }}</div>
                                </td>
                                <td>
                                    <div>Moyens de paiement: {{ $user->payment_methods_count }}</div>
                                    <div>Documents KYC: {{ $user->kyc_documents_count }}</div>
                                </td>
                                <td>
                                    <div>{{ strtoupper((string) ($user->language ?? 'fr')) }} / {{ strtoupper((string) ($user->region ?? 'FR')) }}</div>
                                    <small class="text-muted d-block">
                                        Push: {{ $user->push_notifications_enabled ? 'ON' : 'OFF' }} ·
                                        SMS: {{ $user->sms_notifications_enabled ? 'ON' : 'OFF' }}
                                    </small>
                                    <small class="text-muted d-block">
                                        Email mission: {{ $user->email_notifications_enabled ? 'ON' : 'OFF' }} ·
                                        Marketing: {{ $user->email_marketing_enabled ? 'ON' : 'OFF' }}
                                    </small>
                                </td>
                                <td>{{ $user->last_seen_at?->diffForHumans() ?? '-' }}</td>
                                <td>
                                    <div class="btn-group">
                                        <a href="{{ route('bo.users.edit', $user) }}" class="btn btn-sm btn-alt-primary">
                                            <i class="fa fa-pen me-1"></i>Modifier
                                        </a>
                                        <form method="POST" action="{{ route('bo.users.destroy', $user) }}" onsubmit="return confirm('Supprimer cet utilisateur ?');">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-sm btn-alt-danger">
                                                <i class="fa fa-trash me-1"></i>Supprimer
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="9" class="text-center text-muted">Aucun utilisateur.</td>
                            </tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="block-content">
                {{ $users->links() }}
            </div>
        </div>
    </div>
@endsection
