@extends(getTemplate().'.layouts.app')


@section('content')

    @if(!empty($order) && $order->status === \App\Models\Order::$fail)
        @include('web.default.includes.payment_failure_alert', ['order' => $order])
    @endif

    @if(!empty($order) && $order->status === \App\Models\Order::$paid)
        <div class="no-result default-no-result my-50 d-flex align-items-center justify-content-center flex-column">
            <div class="no-result-logo">
                <img src="/assets/default/img/no-results/search.png" alt="">
            </div>
            <div class="d-flex align-items-center flex-column mt-30 text-center">
                <h2>{{ trans('cart.success_pay_title') }}</h2>
                <p class="mt-5 text-center">{!! trans('cart.success_pay_msg') !!}</p>
                @if(!empty($purchasedCourseUrl))
                    <a href="{{ $purchasedCourseUrl }}" class="btn btn-sm btn-primary mt-20">{{ trans('update.go_to_learning_page') }}</a>
                @endif
                <a href="/panel" class="btn btn-sm {{ !empty($purchasedCourseUrl) ? 'btn-outline-primary mt-10' : 'btn-primary mt-20' }}">{{ trans('public.my_panel') }}</a>
            </div>
        </div>
    @endif

    @if(!empty($paymentPending) && $paymentPending)
        <div class="no-result default-no-result my-50 d-flex align-items-center justify-content-center flex-column" id="paymentPendingBlock">
            <div class="d-flex align-items-center flex-column mt-30 text-center">
                <h2>جاري تأكيد الدفع</h2>
                <p class="mt-5 text-center">تم استلام طلبك. سيتم تفعيل المحتوى خلال لحظات بعد تأكيد الدفع من EasyKash.</p>
                <a href="/panel" class="btn btn-sm btn-primary mt-20">{{ trans('public.my_panel') }}</a>
            </div>
        </div>
        @if(!empty($order))
        <script>
            (function () {
                var orderId = @json($order->id);
                var attempts = 0;
                var timer = setInterval(function () {
                    attempts++;
                    if (attempts > 40) {
                        clearInterval(timer);
                        return;
                    }
                    fetch('/payments/status/' + orderId + '?json=1', {
                        headers: { 'Accept': 'application/json', 'X-Requested-With': 'XMLHttpRequest' }
                    }).then(function (r) { return r.json(); }).then(function (data) {
                        if (data && data.paid) {
                            clearInterval(timer);
                            window.location.reload();
                        }
                    }).catch(function () {});
                }, 3000);
            })();
        </script>
        @endif
    @endif

    @if(!empty($order) && $order->status === \App\Models\Order::$fail)
        <div class="no-result status-failed my-50 d-flex align-items-center justify-content-center flex-column">
            <div class="no-result-logo">
                <img src="/assets/default/img/no-results/failed_pay.png" alt="">
            </div>
            <div class="d-flex align-items-center flex-column mt-30 text-center">
                <h2>{{ trans('cart.failed_pay_title') }}</h2>
                @if(empty(\App\Support\PaymentFailure::resolveReason($order)))
                    <p class="mt-5 text-center">{!! nl2br(e(trans('cart.failed_pay_msg'))) !!}</p>
                @endif
                <p class="mt-10 text-gray font-14">
                    رقم الطلب: #{{ $order->id }}
                    @if(!empty($order->currency))
                        @php $charge = \App\Support\PaymentCurrency::forEasyKashGateway((float)($order->total_amount ?? $order->amount ?? 0), $order->currency); @endphp
                        — {{ $order->total_amount ?? $order->amount }} {{ $order->currency }}
                        @if(!empty($charge['original_currency']))
                            <span class="text-gray font-14">(الدفع عبر EasyKash: {{ $charge['amount'] }} EGP)</span>
                        @endif
                    @endif
                </p>
                <a href="/cart" class="btn btn-sm btn-primary mt-20">إعادة المحاولة من السلة</a>
                <a href="/panel" class="btn btn-sm btn-outline-primary mt-10">{{ trans('public.my_panel') }}</a>
            </div>
        </div>
    @endif


@endsection
