@extends('layout')

@section('title', __('store.clientarea_nav_tickets'))

@section('content')
<div class="container py-5">
    <div class="row">
        <div class="col-md-12">
            <x-clientarea.page-header
                icon="fas fa-headset"
                :title="__('store.clientarea_nav_tickets')"
                :subtitle="$tickets->total() . ' ' . __('store.total_tickets')"
            >
                @if($canCreateTickets)
                    <a href="{{ route('store.auth.tickets.create') }}" class="btn btn-primary">
                        <i class="fas fa-plus me-2"></i>{{ __('store.new_ticket') }}
                    </a>
                @endif
            </x-clientarea.page-header>

            @include('partials.support-banners', ['banners' => $supportBanners ?? []])

            @if($tickets->count() > 0)
                <!-- Search and Filter Bar -->
                <div class="card border-0 shadow-sm mb-3">
                    <div class="card-body py-3">
                        <div class="row g-3">
                            <div class="col-md-4">
                                <div class="input-group">
                                    <span class="input-group-text"><i class="fas fa-search"></i></span>
                                    <input type="text" id="ticketSearch" class="form-control" placeholder="{{ __('store.search_tickets') }}">
                                </div>
                            </div>
                            <div class="col-md-3">
                                <select id="statusFilter" class="form-select">
                                    <option value="">{{ __('store.all_statuses') }}</option>
                                    <option value="open">{{ __('store.ticket_status_open') }}</option>
                                    <option value="pending">{{ __('store.ticket_status_pending') }}</option>
                                    <option value="answered">{{ __('store.ticket_status_answered') }}</option>
                                    <option value="closed">{{ __('store.ticket_status_closed') }}</option>
                                </select>
                            </div>
                            <div class="col-md-3">
                                <select id="priorityFilter" class="form-select">
                                    <option value="">{{ __('store.ticket_filter_priority_all') }}</option>
                                    <option value="low">{{ __('store.ticket_priority_low') }}</option>
                                    <option value="medium">{{ __('store.ticket_priority_medium') }}</option>
                                    <option value="high">{{ __('store.ticket_priority_high') }}</option>
                                    <option value="urgent">{{ __('store.ticket_priority_urgent') }}</option>
                                </select>
                            </div>
                            <div class="col-md-2">
                                <button type="button" id="clearFilters" class="btn btn-outline-secondary w-100">
                                    <i class="fas fa-times me-1"></i>{{ __('store.clear') }}
                                </button>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="card border-0 shadow-sm">
                    <div class="card-body p-0">
                        <div class="table-responsive">
                            <table class="table table-hover align-middle mb-0">
                                <thead>
                                    <tr>
                                        <th>{{ __('store.ticket_id_column') }}</th>
                                        <th>{{ __('store.ticket_title') }}</th>
                                        <th class="d-none d-md-table-cell">{{ __('store.ticket_department') }}</th>
                                        <th class="d-none d-lg-table-cell">{{ __('store.ticket_created_date') }}</th>
                                        <th class="d-none d-lg-table-cell">{{ __('store.ticket_last_reply') }}</th>
                                        <th>{{ __('store.ticket_priority_text') }}</th>
                                        <th>{{ __('store.common_status') }}</th>
                                        <th class="text-end">{{ __('store.common_actions') }}</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($tickets as $ticket)
                                        @php
                                            $priorityKey = strtolower($ticket->priority ?? '');
                                            $priorityIcon = match($priorityKey) {
                                                'urgent' => 'fa-exclamation-circle text-danger',
                                                'high' => 'fa-arrow-up text-warning',
                                                'medium' => 'fa-equals text-info',
                                                'low' => 'fa-arrow-down text-success',
                                                default => 'fa-minus text-secondary',
                                            };
                                            $statusColor = 'secondary';
                                            if ($ticket->status && $ticket->status->is_closed) {
                                                $statusColor = 'success';
                                            } elseif ($ticket->status && $ticket->status->slug === 'open') {
                                                $statusColor = 'primary';
                                            } elseif ($ticket->status && $ticket->status->slug === 'answered') {
                                                $statusColor = 'info';
                                            } elseif ($ticket->status && $ticket->status->slug === 'pending') {
                                                $statusColor = 'warning';
                                            }
                                        @endphp
                                        <tr data-priority="{{ $priorityKey }}" data-status="{{ $ticket->status ? $ticket->status->slug : '' }}">
                                            <td class="fw-semibold text-muted">#{{ $ticket->id }}</td>
                                            <td>
                                                <strong>{{ $ticket->title }}</strong>
                                            </td>
                                            <td class="d-none d-md-table-cell text-muted">
                                                {{ $ticket->department->name ?? __('store.ticket_general') }}
                                            </td>
                                            <td class="d-none d-lg-table-cell text-nowrap">
                                                {{ $ticket->created_at->format($dateFormat) }} <span class="text-muted small">{{ $ticket->created_at->format('H:i') }}</span>
                                            </td>
                                            <td class="d-none d-lg-table-cell text-nowrap">
                                                @if($ticket->lastReply)
                                                    @if($ticket->lastReply->created_at->gt(now()->subHours(6)))
                                                        <span class="text-info">{{ $ticket->lastReply->created_at->diffForHumans() }}</span>
                                                    @else
                                                        {{ $ticket->lastReply->created_at->format($dateFormat) }} <span class="text-muted small">{{ $ticket->lastReply->created_at->format('H:i') }}</span>
                                                    @endif
                                                @else
                                                    <span class="text-muted">{{ __('store.ticket_no_replies') }}</span>
                                                @endif
                                            </td>
                                            <td>
                                                <i class="fas {{ $priorityIcon }} me-1"></i>{{ $ticket->priority_label }}
                                            </td>
                                            <td>
                                                <span class="badge bg-{{ $statusColor }}">
                                                    {{ $ticket->status->name ?? __('store.ticket_status_unknown') }}
                                                </span>
                                                @if($ticket->status && $ticket->status->is_closed && $ticket->rating)
                                                    <div class="mt-1">
                                                        @for($i = 1; $i <= 5; $i++)
                                                            <i class="fas fa-star {{ $i <= $ticket->rating->rating ? 'text-warning' : 'text-muted' }} small"></i>
                                                        @endfor
                                                    </div>
                                                @endif
                                            </td>
                                            <td class="text-end">
                                                @if($canManageTickets)
                                                    <a href="{{ route('store.auth.tickets.details', $ticket->id) }}" class="btn btn-sm btn-outline-info" title="{{ __('store.view_details') }}">
                                                        <i class="fas fa-eye"></i>
                                                    </a>
                                                @else
                                                    <span class="text-muted small"><i class="fas fa-lock me-1"></i>{{ __('store.no_permission') }}</span>
                                                @endif
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

                <div class="d-flex justify-content-center mt-4">
                    {{ $tickets->links() }}
                </div>
            @else
                <div class="card border-0 shadow-sm">
                    <div class="card-body text-center py-5">
                        <i class="fas fa-ticket-alt fa-3x text-muted mb-3"></i>
                        <h5>{{ __('store.no_tickets') }}</h5>
                        <p class="text-muted">{{ __('store.no_tickets_description') }}</p>
                        @if($canCreateTickets)
                        <a href="{{ route('store.auth.tickets.create') }}" class="btn btn-primary">
                            {{ __('store.new_ticket') }}
                        </a>
                        @endif
                    </div>
                </div>
            @endif
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
window.ticketListConfig = {
    translations: {
        noTicketsFound: '{{ __("store.no_tickets_found") }}'
    }
};
</script>
@vite('resources/js/store/ticket-list.js')
@endpush
