@extends('layouts.admin')

@section('title', 'Litige - ' . $dispute->ticket_number)

@section('content')
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3 mb-0">Litige #{{ $dispute->ticket_number }}</h1>
        <a href="{{ route('admin.disputes.index') }}" class="btn btn-secondary">Retour</a>
    </div>

    @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
    @endif

    <div class="row">
        <div class="col-md-4">
            <div class="card mb-4">
                <div class="card-header"><h5 class="mb-0">Informations</h5></div>
                <div class="card-body">
                    <p><strong>Client:</strong> {{ $dispute->user->name ?? 'N/A' }}</p>
                    <p><strong>Commande:</strong> #{{ $dispute->order->order_number ?? 'N/A' }}</p>
                    <p><strong>Vendeur:</strong> {{ $dispute->seller->shop_name ?? 'N/A' }}</p>
                    <p><strong>Type:</strong> {{ ucfirst(str_replace('_', ' ', $dispute->type)) }}</p>
                    <p><strong>Priorité:</strong> 
                        <span class="badge bg-{{ $dispute->priority === 'urgent' ? 'danger' : ($dispute->priority === 'high' ? 'warning' : 'info') }}">
                            {{ ucfirst($dispute->priority) }}
                        </span>
                    </p>
                    <p><strong>Assigné à:</strong> {{ $dispute->assignedTo->name ?? 'Non assigné' }}</p>
                    <p><strong>Créé le:</strong> {{ $dispute->created_at->format('d/m/Y H:i') }}</p>
                </div>
            </div>

            <div class="card mb-4">
                <div class="card-header"><h5 class="mb-0">Changer Statut</h5></div>
                <div class="card-body">
                    <form action="{{ route('admin.disputes.update-status', $dispute) }}" method="POST">
                        @csrf
                        @method('PUT')
                        <div class="mb-3">
                            <select name="status" class="form-select">
                                <option value="open" {{ $dispute->status == 'open' ? 'selected' : '' }}>Ouvert</option>
                                <option value="in_progress" {{ $dispute->status == 'in_progress' ? 'selected' : '' }}>En cours</option>
                                <option value="waiting_response" {{ $dispute->status == 'waiting_response' ? 'selected' : '' }}>Attente réponse</option>
                                <option value="resolved" {{ $dispute->status == 'resolved' ? 'selected' : '' }}>Résolu</option>
                                <option value="rejected" {{ $dispute->status == 'rejected' ? 'selected' : '' }}>Rejeté</option>
                                <option value="closed" {{ $dispute->status == 'closed' ? 'selected' : '' }}>Fermé</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <textarea name="resolution_notes" class="form-control" placeholder="Notes de résolution"></textarea>
                        </div>
                        <button type="submit" class="btn btn-primary w-100">Mettre à jour</button>
                    </form>
                </div>
            </div>

            @if($dispute->order && in_array($dispute->status, ['open', 'in_progress']))
            <div class="card">
                <div class="card-header"><h5 class="mb-0">Remboursement</h5></div>
                <div class="card-body">
                    <form action="{{ route('admin.disputes.refund', $dispute) }}" method="POST">
                        @csrf
                        <div class="mb-3">
                            <label class="form-label">Montant (max: {{ number_format($dispute->order->total_amount, 0, ',', ' ') }} XOF)</label>
                            <input type="number" name="refund_amount" class="form-control" 
                                   min="1" max="{{ $dispute->order->total_amount }}" required>
                        </div>
                        <button type="submit" class="btn btn-warning w-100">Rembourser</button>
                    </form>
                </div>
            </div>
            @endif
        </div>

        <div class="col-md-8">
            <div class="card mb-4">
                <div class="card-header"><h5 class="mb-0">{{ $dispute->subject }}</h5></div>
                <div class="card-body">
                    <p>{{ $dispute->description }}</p>
                    @if($dispute->resolution_notes)
                        <hr>
                        <p><strong>Résolution:</strong> {{ $dispute->resolution_notes }}</p>
                    @endif
                </div>
            </div>

            <div class="card">
                <div class="card-header"><h5 class="mb-0">Messages</h5></div>
                <div class="card-body" style="max-height: 400px; overflow-y: auto;">
                    @forelse($dispute->messages as $message)
                    <div class="mb-3 p-3 rounded {{ $message->is_from_admin ? 'bg-light' : 'bg-white border' }}">
                        <div class="d-flex justify-content-between">
                            <strong>{{ $message->user->name ?? 'N/A' }}</strong>
                            @if($message->is_from_admin)
                                <span class="badge bg-primary">Admin</span>
                            @endif
                        </div>
                        <p class="mb-1">{{ $message->message }}</p>
                        <small class="text-muted">{{ $message->created_at->format('d/m/Y H:i') }}</small>
                    </div>
                    @empty
                    <p class="text-muted">Aucun message</p>
                    @endforelse
                </div>
                <div class="card-footer">
                    <form action="{{ route('admin.disputes.add-message', $dispute) }}" method="POST">
                        @csrf
                        <div class="input-group">
                            <textarea name="message" class="form-control" placeholder="Votre réponse..." rows="2" required></textarea>
                            <button type="submit" class="btn btn-primary">Envoyer</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
