@extends('layouts.verifier')

@section('title', 'Details KYC')
@section('page-title', 'Verification KYC - ' . $seller->shop_name)

@section('content')
<div class="row">
    <div class="col-lg-8">
        <div class="card mb-4">
            <div class="card-header bg-white">
                <h5 class="mb-0">Informations du Vendeur</h5>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="text-muted small">Nom du proprietaire</label>
                        <p class="mb-0 fw-bold">{{ $seller->user->name ?? 'N/A' }}</p>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="text-muted small">Email</label>
                        <p class="mb-0">{{ $seller->user->email ?? 'N/A' }}</p>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="text-muted small">Nom de la boutique</label>
                        <p class="mb-0 fw-bold">{{ $seller->shop_name }}</p>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="text-muted small">Telephone</label>
                        <p class="mb-0">{{ $seller->phone ?? 'N/A' }}</p>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="text-muted small">Type d'entreprise</label>
                        <p class="mb-0">
                            @switch($seller->business_type)
                                @case('individual') Individuel @break
                                @case('company') Société @break
                                @case('association') Association @break
                                @default {{ $seller->business_type ?? 'N/A' }}
                            @endswitch
                        </p>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="text-muted small">Nom entreprise</label>
                        <p class="mb-0">{{ $seller->business_name ?? 'N/A' }}</p>
                    </div>
                    <div class="col-12 mb-3">
                        <label class="text-muted small">Adresse</label>
                        <p class="mb-0">{{ $seller->address ?? 'Non renseigne' }}, {{ $seller->city ?? '' }} {{ $seller->country ?? '' }}</p>
                    </div>
                </div>
            </div>
        </div>

        <div class="card mb-4">
            <div class="card-header bg-white">
                <h5 class="mb-0">Documents KYC</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(in_array(pathinfo($seller->id_document, PATHINFO_EXTENSION), ['jpg', 'jpeg', 'png']))
                                <a href="{{ asset('storage/' . $seller->id_document) }}" target="_blank">
                                    <img src="{{ asset('storage/' . $seller->id_document) }}" class="img-fluid" alt="ID">
                                </a>
                            @else
                                <a href="{{ asset('storage/' . $seller->id_document) }}" target="_blank" class="btn btn-outline-primary w-100">
                                    <i class="bi bi-file-pdf fs-1"></i><br>Voir le document
                                </a>
                            @endif
                        </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(in_array(pathinfo($seller->address_proof, PATHINFO_EXTENSION), ['jpg', 'jpeg', 'png']))
                                <a href="{{ asset('storage/' . $seller->address_proof) }}" target="_blank">
                                    <img src="{{ asset('storage/' . $seller->address_proof) }}" class="img-fluid" alt="Address">
                                </a>
                            @else
                                <a href="{{ asset('storage/' . $seller->address_proof) }}" target="_blank" class="btn btn-outline-info w-100">
                                    <i class="bi bi-file-pdf fs-1"></i><br>Voir le document
                                </a>
                            @endif
                        </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">
                            <a href="{{ asset('storage/' . $seller->business_document) }}" target="_blank" class="btn btn-outline-warning w-100">
                                <i class="bi bi-file-earmark-text fs-1"></i><br>Voir le document
                            </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">
                            <a href="{{ asset('storage/' . $seller->business_photo) }}" target="_blank">
                                <img src="{{ asset('storage/' . $seller->business_photo) }}" class="img-fluid" alt="Business">
                            </a>
                        </div>
                    </div>
                    @endif
                </div>
                
                @if(!$seller->id_document && !$seller->address_proof && !$seller->business_photo && !$seller->business_document)
                <div class="alert alert-warning mb-0">
                    <i class="bi bi-exclamation-triangle me-2"></i>Aucun document soumis
                </div>
                @endif
            </div>
        </div>

        @if(in_array($seller->kyc_status, ['pending', 'submitted']))
        <div class="card">
            <div class="card-header bg-white">
                <h5 class="mb-0">Actions</h5>
            </div>
            <div class="card-body">
                @if($seller->id_document || $seller->address_proof || $seller->business_photo)
                <div class="row">
                    <div class="col-md-6">
                        <form action="{{ route('verifier.kyc.approve', $seller) }}" method="POST">
                            @csrf
                            <button type="submit" class="btn btn-success btn-lg w-100">
                                <i class="bi bi-check-circle me-2"></i>Approuver le KYC
                            </button>
                        </form>
                    </div>
                    <div class="col-md-6">
                        <button type="button" class="btn btn-danger btn-lg w-100" data-bs-toggle="modal" data-bs-target="#rejectModal">
                            <i class="bi bi-x-circle me-2"></i>Rejeter le KYC
                        </button>
                    </div>
                </div>
                @else
                <div class="alert alert-warning mb-0">
                    <i class="bi bi-exclamation-triangle me-2"></i>
                    En attente de soumission des documents par le vendeur. Vous pouvez lui envoyer un message.
                </div>
                @endif
            </div>
        </div>
        @endif
    </div>
    
    <div class="col-lg-4">
        <div class="card mb-4">
            <div class="card-header bg-white">
                <h5 class="mb-0">Statut actuel</h5>
            </div>
            <div class="card-body text-center">
                @if($seller->kyc_status === 'pending')
                    <span class="badge bg-warning text-dark fs-5 px-4 py-2">En attente de documents</span>
                @elseif($seller->kyc_status === 'submitted')
                    <span class="badge bg-info fs-5 px-4 py-2">Documents soumis</span>
                    <p class="mt-2 mb-0 text-muted small">
                        À vérifier
                    </p>
                @elseif($seller->kyc_status === 'approved')
                    <span class="badge bg-success fs-5 px-4 py-2">Approuve</span>
                    <p class="mt-2 mb-0 text-muted small">
                        Le {{ $seller->kyc_verified_at ? $seller->kyc_verified_at->format('d/m/Y H:i') : 'N/A' }}
                    </p>
                @else
                    <span class="badge bg-danger fs-5 px-4 py-2">Rejete</span>
                    @if($seller->kyc_rejection_reason)
                    <p class="mt-2 mb-0 text-danger small">{{ $seller->kyc_rejection_reason }}</p>
                    @endif
                @endif
            </div>
        </div>

        <div class="card">
            <div class="card-header bg-white">
                <h5 class="mb-0">Messagerie</h5>
            </div>
            <div class="card-body">
                <div class="mb-3" style="max-height: 300px; overflow-y: auto;">
                    @forelse($messages as $msg)
                    <div class="mb-2 p-2 rounded {{ $msg->is_admin ? 'bg-primary text-white' : 'bg-light' }}">
                        <small class="d-block {{ $msg->is_admin ? 'opacity-75' : 'text-muted' }}">
                            {{ $msg->is_admin ? 'Vous' : $seller->user->name }} - {{ $msg->created_at->format('d/m H:i') }}
                        </small>
                        {{ $msg->message }}
                    </div>
                    @empty
                    <p class="text-muted text-center">Aucun message</p>
                    @endforelse
                </div>
                
                <form action="{{ route('verifier.kyc.message', $seller) }}" method="POST">
                    @csrf
                    <div class="input-group">
                        <input type="text" name="message" class="form-control" placeholder="Ecrire un message..." required>
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-send"></i>
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="rejectModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="{{ route('verifier.kyc.reject', $seller) }}" method="POST">
                @csrf
                <div class="modal-header">
                    <h5 class="modal-title">Rejeter le KYC</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Raison du rejet</label>
                        <textarea name="reason" class="form-control" rows="3" required placeholder="Expliquez pourquoi le KYC est rejete..."></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annuler</button>
                    <button type="submit" class="btn btn-danger">Confirmer le rejet</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection
