@extends('admin.layouts.app')

@section('content')
    <section class="section">
        <div class="section-header">
            <h1>{{ $pageTitle }}</h1>
            <div class="section-header-breadcrumb">
                <div class="breadcrumb-item active"><a href="/admin">{{ trans('admin/main.dashboard') }}</a></div>
                <div class="breadcrumb-item">{{ $pageTitle }}</div>
            </div>
        </div>

        <div class="section-body">
            @if($failedOrders->count())
                <div class="alert alert-warning">
                    <strong>{{ trans('admin/main.recent_failed_orders') }}</strong>
                    <ul class="mb-0 mt-2">
                        @foreach($failedOrders as $order)
                            <li>
                                #{{ $order->id }} — {{ $order->user->full_name ?? '-' }}
                                — {{ $order->total_amount }} {{ $order->currency ?? 'EGP' }}
                                @if(!empty($order->payment_failure_reason))
                                    — {{ $order->payment_failure_reason }}
                                @endif
                            </li>
                        @endforeach
                    </ul>
                </div>
            @endif

            <div class="card">
                <div class="card-body">
                    <form method="get" action="/admin/financial/payment-transactions" class="row mb-3">
                        <div class="col-md-2">
                            <input type="number" name="order_id" class="form-control" placeholder="{{ trans('admin/main.order_id') }}" value="{{ $filters['order_id'] ?? '' }}">
                        </div>
                        <div class="col-md-2">
                            <input type="number" name="user_id" class="form-control" placeholder="{{ trans('admin/main.user_id') }}" value="{{ $filters['user_id'] ?? '' }}">
                        </div>
                        <div class="col-md-2">
                            <select name="status" class="form-control">
                                <option value="">{{ trans('admin/main.all_status') }}</option>
                                <option value="failed" @if(($filters['status'] ?? '') === 'failed') selected @endif>{{ trans('admin/main.failed') }}</option>
                                <option value="success" @if(($filters['status'] ?? '') === 'success') selected @endif>{{ trans('admin/main.success') }}</option>
                                <option value="info" @if(($filters['status'] ?? '') === 'info') selected @endif>info</option>
                            </select>
                        </div>
                        <div class="col-md-2">
                            <select name="gateway" class="form-control">
                                <option value="">{{ trans('admin/main.all') }}</option>
                                <option value="easykash" @if(($filters['gateway'] ?? '') === 'easykash') selected @endif>EasyKash</option>
                            </select>
                        </div>
                        <div class="col-md-2">
                            <button type="submit" class="btn btn-primary btn-block">{{ trans('admin/main.show_results') }}</button>
                        </div>
                    </form>

                    <div class="table-responsive">
                        <table class="table table-striped font-14">
                            <thead>
                            <tr>
                                <th>#</th>
                                <th>{{ trans('admin/main.date') }}</th>
                                <th>{{ trans('admin/main.order_id') }}</th>
                                <th>{{ trans('admin/main.user') }}</th>
                                <th>{{ trans('admin/main.event') }}</th>
                                <th>{{ trans('admin/main.status') }}</th>
                                <th>{{ trans('admin/main.amount') }}</th>
                                <th>{{ trans('admin/main.failure_reason') }}</th>
                                <th></th>
                            </tr>
                            </thead>
                            <tbody>
                            @forelse($logs as $log)
                                <tr>
                                    <td>{{ $log->id }}</td>
                                    <td>{{ dateTimeFormat($log->created_at, 'j M Y H:i') }}</td>
                                    <td>{{ $log->order_id ?? '-' }}</td>
                                    <td>
                                        @if($log->user)
                                            {{ $log->user->full_name }}<br><small>{{ $log->user->email }}</small>
                                        @else
                                            -
                                        @endif
                                    </td>
                                    <td>{{ $log->event }}</td>
                                    <td>
                                        <span class="badge badge-{{ $log->status === 'failed' ? 'danger' : ($log->status === 'success' ? 'success' : 'secondary') }}">
                                            {{ $log->status }}
                                        </span>
                                    </td>
                                    <td>{{ $log->amount }} {{ $log->currency }}</td>
                                    <td class="text-wrap" style="max-width:280px">{{ \Illuminate\Support\Str::limit($log->failure_reason, 120) }}</td>
                                    <td>
                                        <a href="/admin/financial/payment-transactions/{{ $log->id }}" class="btn btn-sm btn-primary">{{ trans('admin/main.view') }}</a>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="9" class="text-center text-muted">{{ trans('admin/main.no_result') }}</td>
                                </tr>
                            @endforelse
                            </tbody>
                        </table>
                    </div>

                    <div class="mt-3">{{ $logs->appends($filters)->links() }}</div>
                </div>
            </div>
        </div>
    </section>
@endsection
