@extends('layouts.admin')

@section('title', 'Gestion des Litiges')

@section('content')
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3 mb-0">Gestion des Litiges</h1>
    </div>

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

    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card bg-warning text-dark">
                <div class="card-body">
                    <h6>Ouverts</h6>
                    <h3>{{ $stats['open'] }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-info text-white">
                <div class="card-body">
                    <h6>En cours</h6>
                    <h3>{{ $stats['in_progress'] }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-danger text-white">
                <div class="card-body">
                    <h6>Urgents</h6>
                    <h3>{{ $stats['urgent'] }}</h3>
                </div>
            </div>
        </div>
    </div>

    <div class="card mb-4">
        <div class="card-body">
            <form action="{{ route('admin.disputes.index') }}" method="GET" class="row g-3">
                <div class="col-md-3">
                    <select name="status" class="form-select">
                        <option value="">Tous les statuts</option>
                        <option value="open" {{ request('status') == 'open' ? 'selected' : '' }}>Ouvert</option>
                        <option value="in_progress" {{ request('status') == 'in_progress' ? 'selected' : '' }}>En cours</option>
                        <option value="waiting_response" {{ request('status') == 'waiting_response' ? 'selected' : '' }}>Attente réponse</option>
                        <option value="resolved" {{ request('status') == 'resolved' ? 'selected' : '' }}>Résolu</option>
                        <option value="refunded" {{ request('status') == 'refunded' ? 'selected' : '' }}>Remboursé</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <select name="priority" class="form-select">
                        <option value="">Toutes priorités</option>
                        <option value="low" {{ request('priority') == 'low' ? 'selected' : '' }}>Basse</option>
                        <option value="medium" {{ request('priority') == 'medium' ? 'selected' : '' }}>Moyenne</option>
                        <option value="high" {{ request('priority') == 'high' ? 'selected' : '' }}>Haute</option>
                        <option value="urgent" {{ request('priority') == 'urgent' ? 'selected' : '' }}>Urgente</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100">Filtrer</button>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>Ticket</th>
                            <th>Sujet</th>
                            <th>Client</th>
                            <th>Type</th>
                            <th>Priorité</th>
                            <th>Statut</th>
                            <th>Date</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($disputes as $dispute)
                        <tr>
                            <td><code>{{ $dispute->ticket_number }}</code></td>
                            <td>{{ Str::limit($dispute->subject, 40) }}</td>
                            <td>{{ $dispute->user->name ?? 'N/A' }}</td>
                            <td>{{ ucfirst(str_replace('_', ' ', $dispute->type)) }}</td>
                            <td>
                                @switch($dispute->priority)
                                    @case('low')
                                        <span class="badge bg-secondary">Basse</span>
                                        @break
                                    @case('medium')
                                        <span class="badge bg-info">Moyenne</span>
                                        @break
                                    @case('high')
                                        <span class="badge bg-warning text-dark">Haute</span>
                                        @break
                                    @case('urgent')
                                        <span class="badge bg-danger">Urgente</span>
                                        @break
                                @endswitch
                            </td>
                            <td>
                                @switch($dispute->status)
                                    @case('open')
                                        <span class="badge bg-warning text-dark">Ouvert</span>
                                        @break
                                    @case('in_progress')
                                        <span class="badge bg-info">En cours</span>
                                        @break
                                    @case('resolved')
                                        <span class="badge bg-success">Résolu</span>
                                        @break
                                    @case('refunded')
                                        <span class="badge bg-primary">Remboursé</span>
                                        @break
                                    @default
                                        <span class="badge bg-secondary">{{ $dispute->status }}</span>
                                @endswitch
                            </td>
                            <td>{{ $dispute->created_at->format('d/m/Y') }}</td>
                            <td>
                                <a href="{{ route('admin.disputes.show', $dispute) }}" class="btn btn-sm btn-primary">Voir</a>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="8" class="text-center">Aucun litige</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            {{ $disputes->links() }}
        </div>
    </div>
</div>
@endsection
