@extends('layouts.admin.app')

@section('title', translate('Courier API Logs'))

@section('content')
    <div class="content container-fluid">
        <div class="page-header">
            <h1 class="page-header-title">
                <span class="page-header-icon"><i class="tio-code"></i></span>
                <span>{{ translate('Courier API Logs') }}</span>
            </h1>
        </div>

        @include('admin-views.courier.partials._nav')

        <div class="card mb-3">
            <div class="card-body">
                <form method="get">
                    <div class="row g-3">
                        <div class="col-md-3">
                            <select name="provider" class="form-control">
                                <option value="">{{ translate('All Providers') }}</option>
                                @foreach($providers as $provider)
                                    <option value="{{ $provider }}" {{ request('provider') === $provider ? 'selected' : '' }}>{{ ucfirst($provider) }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-3">
                            <select name="action" class="form-control">
                                <option value="">{{ translate('All Actions') }}</option>
                                @foreach($actions as $action)
                                    <option value="{{ $action }}" {{ request('action') === $action ? 'selected' : '' }}>{{ translate(str_replace('_', ' ', $action)) }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-2">
                            <select name="result" class="form-control">
                                <option value="">{{ translate('All Result') }}</option>
                                <option value="success" {{ request('result') === 'success' ? 'selected' : '' }}>{{ translate('Success') }}</option>
                                <option value="failed" {{ request('result') === 'failed' ? 'selected' : '' }}>{{ translate('Failed') }}</option>
                            </select>
                        </div>
                        <div class="col-md-3"><div class="input-group"><input type="date" name="from" value="{{ request('from') }}" class="form-control"><input type="date" name="to" value="{{ request('to') }}" class="form-control"></div></div>
                        <div class="col-md-1"><button class="btn btn--primary btn-block">{{ translate('Go') }}</button></div>
                    </div>
                </form>
            </div>
        </div>

        <div class="card">
            <div class="table-responsive">
                <table class="table table-borderless table-thead-bordered table-nowrap card-table">
                    <thead class="thead-light">
                    <tr>
                        <th>{{ translate('Time') }}</th>
                        <th>{{ translate('Order / Parcel') }}</th>
                        <th>{{ translate('Provider') }}</th>
                        <th>{{ translate('Action') }}</th>
                        <th>{{ translate('Result') }}</th>
                        <th>{{ translate('HTTP') }}</th>
                        <th>{{ translate('Error') }}</th>
                    </tr>
                    </thead>
                    <tbody>
                    @forelse($logs as $log)
                        <tr>
                            <td>{{ $log->created_at?->format('d M Y h:i A') }}</td>
                            <td>
                                @if($log->order_id)
                                    <a href="{{ route('admin.order.details', $log->order_id) }}">#{{ $log->order_id }}</a>
                                @endif
                                <div class="fs-12 text-muted text-break">{{ $log->shipment?->courier_consignment_id ?: $log->shipment?->courier_parcel_id ?: $log->shipment?->courier_tracking_id }}</div>
                            </td>
                            <td>{{ ucfirst($log->provider) }}</td>
                            <td>{{ translate(str_replace('_', ' ', $log->action)) }}</td>
                            <td><span class="badge {{ $log->success ? 'badge-soft-success' : 'badge-soft-danger' }}">{{ $log->success ? translate('Processed') : translate('Failed') }}</span></td>
                            <td>{{ $log->http_status ?? '-' }}</td>
                            <td class="text-break">{{ $log->error_message ?? translate('N/A') }}</td>
                        </tr>
                    @empty
                        <tr><td colspan="7" class="text-center py-4">{{ translate('No API log found') }}</td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
            @if($logs->hasPages())
                <div class="card-footer">{!! $logs->links() !!}</div>
            @endif
        </div>
    </div>
@endsection
