@extends('admin.clients.tabs._layout')

@section('tab-content')
<div class="card border-0 shadow-sm">
    <div class="card-header border-bottom d-flex justify-content-between align-items-center">
        <h6 class="mb-0">
            <i class="fas fa-ticket-alt text-primary me-2"></i>{{ __('admin/clients.tabs_tickets') }}
        </h6>
        @can('edit clients')
        <a href="{{ route('admin.tickets.create', ['client' => $client->id]) }}" class="btn btn-primary btn-sm">
            <i class="fas fa-plus me-1"></i>{{ __('admin/clients.tickets_new_ticket') }}
        </a>
        @endcan
    </div>
    <div class="card-body p-0">
        @if($tickets->count() > 0)
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th class="small">{{ __('admin/clients.tickets_ticket_id') }}</th>
                            <th class="small">{{ __('admin/clients.tickets_title') }}</th>
                            <th class="small">{{ __('admin/clients.tickets_department') }}</th>
                            <th class="small">{{ __('admin/clients.tickets_status') }}</th>
                            <th class="small">{{ __('admin/clients.tickets_priority') }}</th>
                            <th class="small">{{ __('admin/clients.tickets_last_reply') }}</th>
                            <th class="small">{{ __('admin/clients.tickets_created_at') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($tickets as $ticket)
                            <tr class="cursor-pointer" data-href="{{ route('admin.tickets.show', $ticket) }}">
                                <td class="small">#{{ $ticket->id }}</td>
                                <td class="small">
                                    <strong>{{ $ticket->title }}</strong>
                                </td>
                                <td class="small">{{ $ticket->department?->getTranslation('name') ?? '-' }}</td>
                                <td class="small">
                                    @if($ticket->status)
                                        <span class="badge" style="background-color: {{ $ticket->status->color }}">
                                            {{ $ticket->status->getTranslation('name') ?: $ticket->status->name }}
                                        </span>
                                    @else
                                        <span class="badge bg-secondary">-</span>
                                    @endif
                                </td>
                                <td class="small">
                                    @if($ticket->priority === 'low')
                                        <span class="badge bg-secondary">{{ __('admin/clients.tickets_priority_low') }}</span>
                                    @elseif($ticket->priority === 'medium')
                                        <span class="badge bg-info">{{ __('admin/clients.tickets_priority_medium') }}</span>
                                    @elseif($ticket->priority === 'high')
                                        <span class="badge bg-warning">{{ __('admin/clients.tickets_priority_high') }}</span>
                                    @elseif($ticket->priority === 'urgent')
                                        <span class="badge bg-danger">{{ __('admin/clients.tickets_priority_urgent') }}</span>
                                    @else
                                        <span class="badge bg-secondary">-</span>
                                    @endif
                                </td>
                                <td class="small">
                                    @if($ticket->lastReply)
                                        <span class="text-muted">{{ $ticket->lastReply->created_at->diffForHumans() }}</span>
                                    @else
                                        <span class="text-muted">-</span>
                                    @endif
                                </td>
                                <td class="small text-muted">{{ $ticket->created_at->format($dateTimeFormat) }}</td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @else
            <div class="text-center py-5">
                <i class="fas fa-ticket-alt fa-3x text-muted mb-3"></i>
                <p class="text-muted">{{ __('admin/clients.tickets_no_tickets') }}</p>
                <a href="{{ route('admin.tickets.create', ['client' => $client->id]) }}" class="btn btn-primary btn-sm">
                    <i class="fas fa-plus me-1"></i>{{ __('admin/clients.tickets_new_ticket') }}
                </a>
            </div>
        @endif
    </div>
</div>

@push('styles')
<style>
.cursor-pointer {
    cursor: pointer;
}
.cursor-pointer:hover {
    background-color: rgba(0, 0, 0, 0.025);
}
</style>
@endpush

@push('scripts')
@vite('resources/js/admin/clients/tickets-row-navigation.js')
@endpush
@endsection
