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

@push('styles_top')

@endpush

@section('content')
    <section class="cart-banner position-relative text-center">
        <h1 class="font-30 text-white font-weight-bold">{{ trans('cart.checkout') }}</h1>
        @php
            if (empty($paymentCharge) && !empty($order)) {
                $paymentCharge = \App\Support\PaymentCurrency::forEasyKashGateway(
                    (float) ($order->total_amount ?? $total ?? 0),
                    $order->currency ?? ($orderCurrency ?? 'EGP')
                );
            }
            $payAmount = $paymentCharge['amount'] ?? $total;
            $payCurrency = $paymentCharge['currency'] ?? 'EGP';
            $payLabel = $payCurrency === 'EGP'
                ? formatPaymentPrice($payAmount, 'EGP')
                : (\App\Support\PaymentCurrency::sign($payCurrency) . handlePriceFormat($payAmount));
        @endphp
        <span class="payment-hint font-20 text-white d-block">
            {{ $payLabel }} {{ trans('cart.for_items',['count' => $count]) }}
            @if(!empty($paymentCharge['original_currency']) && ($paymentCharge['original_currency'] ?? '') === 'USD')
                <small class="d-block font-14 opacity-75">({{ $paymentCharge['original_amount'] }} USD × {{ $paymentCharge['usd_to_egp_rate'] }} = {{ $payAmount }} EGP)</small>
            @endif
        </span>
    </section>

    <section class="container mt-45">
        @include('web.default.includes.payment_failure_alert', ['order' => $order ?? null])

        @if($errors->any())
            <div class="alert alert-danger mt-20">
                @foreach($errors->all() as $error)
                    <div>{{ $error }}</div>
                @endforeach
            </div>
        @endif

        @if(empty($order->id) || (int) $order->id < 1)
            <div class="alert alert-danger mt-20 text-right">
                تعذر إنشاء الطلب بشكل صحيح. ارجع إلى <a href="/cart">السلة</a> وأعد عملية الشراء.
            </div>
        @else
        <form action="/credit" method="post" id="paymentCreditForm" class=" mt-25">
            {{ csrf_field() }}
            <input type="hidden" name="order_id" value="{{ (int) $order->id }}">

            @if(isset($carts))
                @foreach($carts as $cart)
                    @if(isset($cart->reserve_meeting_id))
                        <input type="hidden" name="meeting" value="{{$order->id}}">
                    @endif
                @endforeach
            @endif

            @if(isset($subscribe))
                <input type="hidden" name="subscribe" value="{{$order->id}}">
            @endif
            @if(isset($discountId))
                <input type="hidden" name="discountId" value="{{$discountId}}">
            @endif

            @include('web.default.cart.includes.billing_information')

            <h2 class="section-title mt-30">{{ trans('financial.select_a_payment_gateway') }}</h2>

            <div class="row">
                {{--                    @foreach($paymentChannels as $paymentChannel)--}}
                {{--                        <div class="col-6 col-lg-4 mb-40 charge-account-radio">--}}
                {{--                            <input type="radio" name="gateway" id="{{ $paymentChannel->title }}" data-class="{{ $paymentChannel->class_name }}" value="{{ $paymentChannel->id }}">--}}
                {{--                            <label for="{{ $paymentChannel->title }}" class="rounded-sm p-20 p-lg-45 d-flex flex-column align-items-center justify-content-center">--}}
                {{--                                <img src="{{ $paymentChannel->image }}" width="120" height="60" alt="">--}}

                {{--                                <p class="mt-30 mt-lg-50 font-weight-500 text-dark-blue">--}}
                {{--                                    {{ trans('financial.pay_via') }}--}}
                {{--                                    <span class="font-weight-bold font-14">{{ $paymentChannel->title }}</span>--}}
                {{--                                </p>--}}
                {{--                            </label>--}}
                {{--                        </div>--}}
                {{--                    @endforeach--}}
                {{--                @endif--}}
                <div class="col-6 col-lg-4 mb-40 charge-account-radio">
                    <input type="radio" name="gateway" id="easykash" data-class="easykash" value="easykash" checked>

                    <label for="easykash" class="rounded-sm p-20 p-lg-45 d-flex flex-column align-items-center justify-content-center">
                        <img src="/assets/default/img/activity/visa.jpeg" width="120" height="60" alt="">

                        <p class="mt-30 mt-lg-50 font-weight-500 text-dark-blue">
                            الدفع
                            <span class="font-weight-bold font-14">EasyKash (فيزا / محفظة / فوري)</span>
                        </p>
                    </label>
                </div>

                <div class="col-6 col-lg-4 mb-40 charge-account-radio">
                    <input type="radio" @if(empty($userCharge) or ($total > $userCharge)) disabled @endif name="gateway" id="offline" value="offline">
                    <label for="offline" class="rounded-sm p-20 p-lg-45 d-flex flex-column align-items-center justify-content-center">
                        <img src="/assets/default/img/activity/pay.svg" width="120" height="60" alt="">

                        <p class="mt-30 mt-lg-50 font-weight-500 text-dark-blue">
                            {{ trans('financial.account') }}
                            <span class="font-weight-bold">{{ trans('financial.charge') }}</span>
                        </p>

                        <span class="mt-5">{{ $currency }}{{ $userCharge }}</span>
                    </label>
                </div>
            </div>


            <p class="font-14 text-gray mt-30 mb-0">{{ trans('update.easykash_checkout_hint') }}</p>

            <div class="d-flex align-items-center justify-content-between mt-20 flex-wrap">
                <span class="font-16 font-weight-500 text-gray mb-15 mb-md-0">
                    {{ trans('financial.total_amount') }}
                    @if(!empty($paymentCharge['original_currency']))
                        {{ formatPaymentPrice($total, $orderCurrency ?? 'USD') }}
                        — {{ trans('update.pay_with_easykash') }}: <strong>{{ formatPaymentPrice($payAmount, 'EGP') }}</strong>
                    @else
                        {{ $payLabel ?? formatPaymentPrice($payAmount, $payCurrency ?? 'EGP') }}
                    @endif
                </span>
                <button type="button" id="paymentSubmit" class="btn btn-primary btn-lg">{{ trans('update.pay_with_easykash') }}</button>
            </div>
        </form>

        @endif

        @if(!empty($razorpay) and $razorpay)
            <form action="/payments/verify/Razorpay" method="get">
                <input type="hidden" name="order_id" value="{{ $order->id }}">

                <script src="https://checkout.razorpay.com/v1/checkout.js"
                        data-key="{{ env('RAZORPAY_API_KEY') }}"
                        data-amount="{{ (int)($order->total_amount * 100) }}"
                        data-buttontext="product_price"
                        data-description="Rozerpay"
                        data-currency="{{ currency() }}"
                        data-image="{{ $generalSettings['logo'] }}"
                        data-prefill.name="{{ $order->user->full_name }}"
                        data-prefill.email="{{ $order->user->email }}"
                        data-theme.color="#43d477">
                </script>
            </form>
        @endif
    </section>

@endsection

@push('scripts_bottom')
    <script src="/assets/default/js/parts/payment.min.js"></script>
    <script>
        jQuery(function ($) {
            $('input[name="gateway"]:checked').trigger('change');
        });
    </script>
@endpush
