@extends('layouts.seller')

@section('title', 'Verification KYC')

@section('content')
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="mb-0">Verification KYC</h2>
    </div>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    @if($seller->kyc_status === 'approved')
    <div class="alert alert-success text-center py-5">
        <i class="bi bi-check-circle fs-1 mb-3 d-block"></i>
        <h4 class="alert-heading">Compte verifie</h4>
        <p class="mb-0">Votre identite a ete verifiee. Vous pouvez vendre sur notre plateforme.</p>
        @if($seller->kyc_verified_at)
        <hr>
        <small class="text-muted">Verifie le {{ $seller->kyc_verified_at->format('d/m/Y a H:i') }}</small>
        @endif
    </div>
    @elseif($seller->kyc_status === 'submitted')
    <div class="alert alert-info text-center py-5">
        <i class="bi bi-hourglass-split fs-1 mb-3 d-block"></i>
        <h4 class="alert-heading">Documents soumis - En attente de vérification</h4>
        <p class="mb-0">Vos documents ont été soumis et sont en cours de vérification. Cela peut prendre 24 à 48 heures.</p>
        @if($seller->kyc_submitted_at)
        <hr>
        <small class="text-muted">Soumis le {{ $seller->kyc_submitted_at->format('d/m/Y à H:i') }}</small>
        @endif
    </div>
    @elseif($seller->kyc_status === 'pending')
    <div class="alert alert-warning text-center py-5">
        <i class="bi bi-file-earmark-arrow-up fs-1 mb-3 d-block"></i>
        <h4 class="alert-heading">Documents requis</h4>
        <p class="mb-0">Veuillez soumettre vos documents pour activer votre compte vendeur.</p>
    </div>
    @elseif($seller->kyc_status === 'rejected')
    <div class="alert alert-danger mb-4">
        <h4 class="alert-heading"><i class="bi bi-x-circle me-2"></i>Verification refusee</h4>
        <p class="mb-0">{{ $seller->kyc_rejection_reason ?? 'Veuillez soumettre de nouveaux documents.' }}</p>
    </div>
    @endif

    <div class="row">
        <div class="col-lg-8">
            @if($seller->kyc_status !== 'approved' && $seller->kyc_status !== 'submitted')
            <div class="card mb-4">
                <div class="card-header bg-white">
                    <h5 class="mb-0"><i class="bi bi-file-earmark-text me-2"></i>Soumettre vos documents</h5>
                </div>
                <div class="card-body">
                    <form action="{{ route('seller.kyc.store') }}" method="POST" enctype="multipart/form-data">
                        @csrf
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label fw-bold">Nom de l'entreprise / Nom complet *</label>
                                <input type="text" name="business_name" class="form-control" value="{{ old('business_name', $seller->business_name ?? '') }}" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label fw-bold">Type d'entreprise *</label>
                                <select name="business_type" class="form-select" required>
                                    <option value="individual" {{ ($seller->business_type ?? '') === 'individual' ? 'selected' : '' }}>Individuel / Personne Physique</option>
                                    <option value="company" {{ ($seller->business_type ?? '') === 'company' ? 'selected' : '' }}>Societe / Entreprise</option>
                                    <option value="association" {{ ($seller->business_type ?? '') === 'association' ? 'selected' : '' }}>Association / ONG</option>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Numero registre commerce (optionnel)</label>
                                <input type="text" name="business_registration" class="form-control" value="{{ old('business_registration', $seller->business_registration ?? '') }}" placeholder="RC-XXX-XXXX">
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Numero fiscal (optionnel)</label>
                                <input type="text" name="tax_id" class="form-control" value="{{ old('tax_id', $seller->tax_id ?? '') }}" placeholder="IFU / NIF">
                            </div>
                        </div>
                        <hr class="my-4">
                        <h6 class="mb-3"><i class="bi bi-upload me-2"></i>Documents a fournir</h6>
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label fw-bold">Piece d'identite (CNI, Passeport) *</label>
                                <input type="file" name="id_document" class="form-control" accept=".jpg,.jpeg,.png,.pdf" required>
                                <small class="text-muted">Formats: JPG, PNG, PDF. Max 5MB</small>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Justificatif d'adresse (optionnel)</label>
                                <input type="file" name="address_proof" class="form-control" accept=".jpg,.jpeg,.png,.pdf">
                                <small class="text-muted">Facture eau/electricite, quittance loyer</small>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Document d'entreprise (optionnel)</label>
                                <input type="file" name="business_document" class="form-control" accept=".jpg,.jpeg,.png,.pdf">
                                <small class="text-muted">Registre de commerce, statuts, RCCM</small>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Photo de votre boutique/atelier (optionnel)</label>
                                <input type="file" name="business_photo" class="form-control" accept=".jpg,.jpeg,.png">
                                <small class="text-muted">Photo de votre lieu de travail</small>
                            </div>
                        </div>
                        <div class="alert alert-info mt-3">
                            <i class="bi bi-info-circle me-2"></i>
                            Les documents seront traites dans les 24 a 48 heures. Vous serez notifie par email.
                        </div>
                        <button type="submit" class="btn btn-primary btn-lg w-100">
                            <i class="bi bi-send me-2"></i>Soumettre pour verification
                        </button>
                    </form>
                </div>
            </div>
            @endif

            @if($seller->kyc_status === 'approved' || $seller->kyc_status === 'pending')
            <div class="card">
                <div class="card-header bg-white">
                    <h5 class="mb-0"><i class="bi bi-file-earmark-check me-2"></i>Documents soumis</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        @if($seller->id_document)
                        <div class="col-md-4 mb-3">
                            <label class="text-muted small">Piece d'identite</label>
                            <div class="border rounded p-2">
                                @if(Str::endsWith($seller->id_document, ['.jpg', '.jpeg', '.png', '.gif']))
                                    <img src="{{ Storage::url($seller->id_document) }}" alt="Piece d'identite" class="img-fluid rounded mb-2" style="max-height: 150px;">
                                @endif
                                <a href="{{ Storage::url($seller->id_document) }}" target="_blank" class="btn btn-outline-primary btn-sm w-100">
                                    <i class="bi bi-eye me-1"></i> Voir
                                </a>
                            </div>
                        </div>
                        @endif
                        @if($seller->address_proof)
                        <div class="col-md-4 mb-3">
                            <label class="text-muted small">Justificatif d'adresse</label>
                            <div class="border rounded p-2">
                                @if(Str::endsWith($seller->address_proof, ['.jpg', '.jpeg', '.png', '.gif']))
                                    <img src="{{ Storage::url($seller->address_proof) }}" alt="Justificatif d'adresse" class="img-fluid rounded mb-2" style="max-height: 150px;">
                                @endif
                                <a href="{{ Storage::url($seller->address_proof) }}" target="_blank" class="btn btn-outline-primary btn-sm w-100">
                                    <i class="bi bi-eye me-1"></i> Voir
                                </a>
                            </div>
                        </div>
                        @endif
                        @if($seller->business_document)
                        <div class="col-md-4 mb-3">
                            <label class="text-muted small">Document entreprise</label>
                            <div class="border rounded p-2">
                                @if(Str::endsWith($seller->business_document, ['.jpg', '.jpeg', '.png', '.gif']))
                                    <img src="{{ Storage::url($seller->business_document) }}" alt="Document entreprise" class="img-fluid rounded mb-2" style="max-height: 150px;">
                                @endif
                                <a href="{{ Storage::url($seller->business_document) }}" target="_blank" class="btn btn-outline-primary btn-sm w-100">
                                    <i class="bi bi-eye me-1"></i> Voir
                                </a>
                            </div>
                        </div>
                        @endif
                        @if($seller->business_photo)
                        <div class="col-md-4 mb-3">
                            <label class="text-muted small">Photo boutique</label>
                            <div class="border rounded p-2">
                                <img src="{{ Storage::url($seller->business_photo) }}" alt="Photo boutique" class="img-fluid rounded mb-2" style="max-height: 150px;">
                                <a href="{{ Storage::url($seller->business_photo) }}" target="_blank" class="btn btn-outline-primary btn-sm w-100">
                                    <i class="bi bi-eye me-1"></i> Voir
                                </a>
                            </div>
                        </div>
                        @endif
                    </div>
                </div>
            </div>
            @endif
        </div>

        <div class="col-lg-4">
            <div class="card">
                <div class="card-header bg-white">
                    <h5 class="mb-0"><i class="bi bi-chat-dots me-2"></i>Messagerie Verificateur</h5>
                </div>
                <div class="card-body">
                    <div class="messages-container" style="max-height: 300px; overflow-y: auto;">
                        @forelse($messages ?? [] as $message)
                            <div class="mb-3 {{ $message->is_admin ? 'text-start' : 'text-end' }}">
                                <div class="d-inline-block p-2 rounded {{ $message->is_admin ? 'bg-light' : 'bg-primary text-white' }}" style="max-width: 90%;">
                                    <small class="d-block {{ $message->is_admin ? 'text-muted' : 'text-white-50' }}">
                                        {{ $message->is_admin ? 'Verificateur' : 'Vous' }} - {{ $message->created_at->format('d/m H:i') }}
                                    </small>
                                    {{ $message->message }}
                                </div>
                            </div>
                        @empty
                            <p class="text-muted text-center">Aucun message</p>
                        @endforelse
                    </div>
                    <hr>
                    <form action="{{ route('seller.kyc.message') }}" method="POST">
                        @csrf
                        <div class="mb-2">
                            <textarea name="message" class="form-control" rows="2" placeholder="Ecrire un message au verificateur..." required></textarea>
                        </div>
                        <button type="submit" class="btn btn-primary btn-sm w-100">
                            <i class="bi bi-send me-1"></i> Envoyer
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
