@extends('bo.layouts.backend')

@section('title', 'KYC 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">KYC BO</h1>
                    <h2 class="fs-base lh-base fw-medium text-muted mb-0 bo-page-subtitle">Moderation centralisee des dossiers KYC liners.</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">KYC</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.users.index') }}" class="btn btn-sm btn-alt-secondary">
                    <i class="si si-users me-1"></i> Utilisateurs
                </a>
                <a href="{{ route('bo.missions.index') }}" class="btn btn-sm btn-alt-secondary">
                    <i class="si si-briefcase me-1"></i> Missions
                </a>
            </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.kyc.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.kyc.index') }}" class="row g-3 align-items-end bo-filter-form">
                    <div class="col-md-4">
                        <label class="form-label" for="q">Recherche user</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 ou email">
                        </div>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label" for="status">Statut</label>
                        <select id="status" name="status" class="form-select">
                            <option value="">Tous</option>
                            @foreach(['pending', 'approved', 'rejected'] as $statusOption)
                                @php
                                    $statusLabel = match ($statusOption) {
                                        'pending' => 'En attente',
                                        'approved' => 'Approuve',
                                        'rejected' => 'Rejete',
                                        default => ucfirst(str_replace('_', ' ', $statusOption)),
                                    };
                                @endphp
                                <option value="{{ $statusOption }}" @selected($status === $statusOption)>{{ $statusLabel }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label" for="document_type">Type doc</label>
                        <select id="document_type" name="document_type" class="form-select">
                            <option value="">Tous</option>
                            @foreach($documentTypes as $type)
                                <option value="{{ $type }}" @selected($documentType === $type)>{{ strtoupper($type) }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label" for="date_from">Date de creation</label>
                        <div class="input-group">
                            <input id="date_from" type="date" name="date_from" class="form-control form-control-alt" value="{{ $dateFrom }}">
                            <input id="date_to" type="date" name="date_to" class="form-control form-control-alt" value="{{ $dateTo }}">
                        </div>
                    </div>
                    <div class="col-md-1 d-flex gap-2 bo-filter-actions">
                        <button type="submit" class="btn btn-alt-primary w-100">
                            <i class="fa fa-filter opacity-50"></i>
                        </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-shield me-1"></i>Dossiers KYC</h3>
                <div class="block-options">
                    <span class="badge bg-primary">{{ $kycDocuments->total() }} resultat(s)</span>
                </div>
            </div>
            <div class="block-content p-0">
                <div class="table-responsive">
                    <table class="table table-vcenter table-striped table-hover fs-sm mb-0 bo-table">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>Liner</th>
                            <th>Document</th>
                            <th>Statut</th>
                            <th>Validation</th>
                            <th style="width: 300px;">Actions</th>
                        </tr>
                        </thead>
                        <tbody>
                        @forelse($kycDocuments as $doc)
                            <tr>
                                <td>#{{ $doc->id }}</td>
                                <td>
                                    <div class="fw-semibold">{{ $doc->user?->name ?? '-' }}</div>
                                    <div class="text-muted">{{ $doc->user?->email ?? '-' }}</div>
                                </td>
                                <td>
                                    @php
                                        $documentUrl = (string) ($doc->document_url ?? '');
                                        $documentPath = strtolower(parse_url($documentUrl, PHP_URL_PATH) ?? '');
                                        $isImageDocument = str_ends_with($documentPath, '.jpg')
                                            || str_ends_with($documentPath, '.jpeg')
                                            || str_ends_with($documentPath, '.png')
                                            || str_ends_with($documentPath, '.webp');
                                        $isPdfDocument = str_ends_with($documentPath, '.pdf');
                                    @endphp
                                    <div class="fw-semibold">{{ strtoupper($doc->document_type) }}</div>
                                    <div class="text-muted">{{ $doc->document_number ?: '-' }}</div>
                                    <div class="small text-muted">Depose le {{ $doc->created_at?->format('d/m/Y H:i') ?? '-' }}</div>
                                    @if($documentUrl !== '')
                                        <div class="mt-2">
                                            @if($isImageDocument)
                                                <a href="{{ $documentUrl }}" target="_blank" rel="noopener">
                                                    <img
                                                        src="{{ $documentUrl }}"
                                                        alt="Document KYC #{{ $doc->id }}"
                                                        style="width: 90px; height: 90px; object-fit: cover; border-radius: 12px; border: 1px solid #E5E7EB;"
                                                    >
                                                </a>
                                            @else
                                                <div class="d-inline-flex align-items-center gap-2 px-3 py-2 rounded border bg-body-light">
                                                    <i class="fa {{ $isPdfDocument ? 'fa-file-pdf text-danger' : 'fa-file-lines text-primary' }}"></i>
                                                    <span class="small">{{ $isPdfDocument ? 'Document PDF' : 'Document joint' }}</span>
                                                </div>
                                            @endif
                                        </div>
                                        <div class="d-flex gap-2 mt-2">
                                            <a href="{{ $documentUrl }}" target="_blank" rel="noopener" class="btn btn-sm btn-alt-primary">
                                                <i class="fa fa-up-right-from-square me-1"></i>Ouvrir
                                            </a>
                                            <a href="{{ $documentUrl }}" target="_blank" rel="noopener" download class="btn btn-sm btn-alt-secondary">
                                                <i class="fa fa-download me-1"></i>Telecharger
                                            </a>
                                        </div>
                                    @else
                                        <div class="small text-danger mt-2">Aucun document exploitable sur ce dossier.</div>
                                    @endif
                                </td>
                                <td>
                                    @php
                                        $badgeClass = match($doc->status) {
                                            'approved' => 'bg-success',
                                            'rejected' => 'bg-danger',
                                            default => 'bg-warning text-dark',
                                        };
                                    @endphp
                                    @php
                                        $docStatusLabel = match ($doc->status) {
                                            'pending' => 'En attente',
                                            'approved' => 'Approuve',
                                            'rejected' => 'Rejete',
                                            default => ucfirst(str_replace('_', ' ', (string) $doc->status)),
                                        };
                                    @endphp
                                    <span class="badge {{ $badgeClass }}">{{ $docStatusLabel }}</span>
                                    @if($doc->notes)
                                        <small class="d-block text-muted mt-1">{{ $doc->notes }}</small>
                                    @endif
                                </td>
                                <td>
                                    <div>{{ $doc->validator?->name ?? '-' }}</div>
                                    <small class="text-muted d-block">{{ $doc->validated_at?->format('d/m/Y H:i') ?? '-' }}</small>
                                    @if($doc->status === 'pending')
                                        <small class="text-muted d-block mt-1">En attente de moderation</small>
                                    @elseif($doc->notes)
                                        <small class="text-muted d-block mt-1">Motif / note: {{ $doc->notes }}</small>
                                    @endif
                                </td>
                                <td>
                                    <div class="bo-table-actions">
                                        <form method="POST" action="{{ route('bo.kyc.approve', $doc) }}">
                                            @csrf
                                            <button class="btn btn-sm btn-success" type="submit">
                                                <i class="fa fa-check me-1"></i>Approuver
                                            </button>
                                        </form>
                                        <form method="POST" action="{{ route('bo.kyc.reject', $doc) }}" class="d-flex gap-2">
                                            @csrf
                                            <input class="form-control form-control-sm" type="text" name="reason" placeholder="Motif rejet">
                                            <button class="btn btn-sm btn-danger" type="submit">
                                                <i class="fa fa-xmark me-1"></i>Rejeter
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="text-center text-muted">Aucun dossier KYC.</td>
                            </tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="block-content">
                {{ $kycDocuments->links() }}
            </div>
        </div>
    </div>
@endsection
