@extends($activeTemplate . 'layouts.master')
@section('content')
    <div class="w-full">
        <form action="{{ route('user.deposit.insert') }}" method="post" class="deposit-form">
            @csrf
            <input type="hidden" name="currency">

            <div class="flex flex-col lg:flex-row gap-6 items-start">

                {{-- ================================ --}}
                {{-- LEFT — Gateway Selection         --}}
                {{-- ================================ --}}
                <div class="w-full lg:w-1/2">
                    <div class="bg-white rounded-2xl shadow-soft border border-primary-100 overflow-hidden">

                        <div class="bg-gradient-to-br from-primary-900 to-primary-700 px-6 py-5">
                            <h5 class="text-white font-semibold text-base">@lang('Select Payment Method')</h5>
                            <p class="text-primary-200 text-xs mt-1">@lang('Choose your preferred payment gateway below')</p>
                        </div>

                        {{-- gateway-option-list, gateway-option, payment-item* classes are --}}
                        {{-- used by jQuery and the gateway CSS — do not rename              --}}
                        <div class="p-4">
                            <div class="payment-system-list is-scrollable gateway-option-list flex flex-col gap-2">
                                @foreach ($gatewayCurrency as $data)
                                    <label for="{{ titleToKey($data->name) }}"
                                           class="payment-item gateway-option @if ($loop->index > 4) hidden @endif cursor-pointer flex items-center justify-between px-4 py-3 rounded-xl border-2 border-primary-100 hover:border-primary-400 hover:bg-primary-50 transition-all duration-200">
                                        <div class="payment-item__info flex items-center gap-3">
                                            <span class="payment-item__check"></span>
                                            <span class="payment-item__name text-sm font-medium text-primary-800">{{ __($data->name) }}</span>
                                        </div>
                                        <div class="payment-item__thumb">
                                            <img class="payment-item__thumb-img h-7 object-contain" src="">
                                        </div>
                                        <input class="payment-item__radio gateway-input"
                                               id="{{ titleToKey($data->name) }}"
                                               hidden
                                               data-gateway='@json($data)'
                                               type="radio"
                                               name="gateway"
                                               value="{{ $data->method_code }}"
                                               @checked(old('gateway', $loop->first) == $data->method_code)
                                               data-min-amount="{{ showAmount($data->min_amount) }}"
                                               data-max-amount="{{ showAmount($data->max_amount) }}">
                                    </label>
                                @endforeach

                                @if ($gatewayCurrency->count() > 4)
                                    <button type="button"
                                            class="more-gateway-option w-full flex items-center justify-center gap-2 px-4 py-3 rounded-xl border-2 border-dashed border-primary-200 text-primary-500 hover:border-primary-400 hover:text-primary-700 hover:bg-primary-50 transition-all duration-200 text-sm font-medium">
                                        <span class="payment-item__btn-text">@lang('Show All Payment Options')</span>
                                        <span class="payment-item__btn__icon"><i class="fas fa-chevron-down text-xs"></i></span>
                                    </button>
                                @endif
                            </div>
                        </div>
                    </div>
                </div>

                {{-- ================================ --}}
                {{-- RIGHT — Amount & Summary         --}}
                {{-- ================================ --}}
                <div class="w-full lg:w-1/2 flex flex-col gap-4">

                    {{-- Amount Input --}}
                    <div class="bg-white rounded-2xl shadow-soft border border-primary-100 overflow-hidden">
                        <div class="bg-gradient-to-br from-primary-900 to-primary-700 px-6 py-5">
                            <h5 class="text-white font-semibold text-base">@lang('Deposit Amount')</h5>
                            <p class="text-primary-200 text-xs mt-1">@lang('Enter the amount you wish to deposit')</p>
                        </div>
                        <div class="px-6 py-5">
                            <div class="flex rounded-xl border-2 border-primary-200 focus-within:border-primary-500 transition-colors duration-200 overflow-hidden">
                                <span class="px-4 py-3 bg-primary-50 text-primary-700 font-semibold text-sm border-r-2 border-primary-200 shrink-0">
                                    {{ gs('cur_sym') }}
                                </span>
                                <input type="number"
                                       step="any"
                                       class="amount flex-1 px-4 py-3 text-sm text-primary-900 placeholder-primary-300 focus:outline-none bg-white"
                                       name="amount"
                                       placeholder="@lang('Enter Amount')"
                                       value="{{ old('amount') }}"
                                       autocomplete="off">
                            </div>
                            <p class="mt-2 text-xs text-primary-400">
                                @lang('Limit'): <span class="gateway-limit font-medium text-primary-600">@lang('0.00')</span>
                            </p>
                        </div>
                    </div>

                    {{-- Fee & Summary Card --}}
                    <div class="bg-white rounded-2xl shadow-soft border border-primary-100 overflow-hidden">
                        <div class="bg-gradient-to-br from-primary-900 to-primary-700 px-6 py-4">
                            <h5 class="text-white font-semibold text-sm">@lang('Deposit Summary')</h5>
                        </div>
                        <div class="divide-y divide-primary-100">

                            {{-- Processing Charge --}}
                            <div class="deposit-info flex items-center justify-between px-5 py-3.5 hover:bg-primary-50 transition-colors duration-150">
                                <span class="deposit-info__title text-xs text-primary-500 font-medium flex items-center gap-1.5">
                                    @lang('Processing Charge')
                                    {{-- proccessing-fee-info used by JS tooltip update — do not rename --}}
                                    <span data-bs-toggle="tooltip"
                                          title="@lang('Processing charge for payment gateways')"
                                          class="proccessing-fee-info text-primary-400 cursor-help">
                                        <i class="las la-info-circle"></i>
                                    </span>
                                </span>
                                <div class="deposit-info__input text-sm font-bold text-primary-900">
                                    <span class="processing-fee">@lang('0.00')</span> {{ __(gs('cur_text')) }}
                                </div>
                            </div>

                            {{-- Total --}}
                            <div class="deposit-info flex items-center justify-between px-5 py-3.5 hover:bg-primary-50 transition-colors duration-150">
                                <span class="deposit-info__title text-xs text-primary-500 font-medium">@lang('Total')</span>
                                <span class="deposit-info__input text-sm font-bold text-secondary-500">
                                    <span class="final-amount">@lang('0.00')</span> {{ __(gs('cur_text')) }}
                                </span>
                            </div>

                            {{-- Conversion Rate (hidden by default) --}}
                            {{-- gateway-conversion, conversion-currency toggled by JS — do not rename --}}
                            <div class="deposit-info gateway-conversion hidden flex items-center justify-between px-5 py-3.5 hover:bg-primary-50 transition-colors duration-150">
                                <span class="deposit-info__title text-xs text-primary-500 font-medium">@lang('Conversion')</span>
                                <span class="deposit-info__input text-sm font-bold text-primary-900">
                                    <span class="text"></span>
                                </span>
                            </div>

                            {{-- In Currency (hidden by default) --}}
                            <div class="deposit-info conversion-currency hidden flex items-center justify-between px-5 py-3.5 hover:bg-primary-50 transition-colors duration-150">
                                <span class="deposit-info__title text-xs text-primary-500 font-medium">
                                    @lang('In') <span class="gateway-currency"></span>
                                </span>
                                <span class="deposit-info__input text-sm font-bold text-primary-900">
                                    <span class="in-currency"></span>
                                </span>
                            </div>

                        </div>

                        {{-- Crypto Notice (hidden by default) --}}
                        {{-- crypto-message toggled by JS — do not rename --}}
                        <div class="crypto-message hidden mx-4 mb-4 mt-1 flex items-start gap-2.5 p-3.5 bg-primary-50 border border-primary-200 rounded-xl">
                            <i class="las la-info-circle text-primary-500 text-base shrink-0 mt-0.5"></i>
                            <p class="text-xs text-primary-600 leading-relaxed">
                                @lang('Conversion with') <span class="gateway-currency font-semibold"></span> @lang('and final value will show on next step')
                            </p>
                        </div>
                    </div>

                    {{-- Submit --}}
                    <button type="submit"
                            class="w-full py-3 rounded-xl text-sm font-semibold bg-primary-900 text-white hover:bg-primary-700 transition-all duration-200 shadow-glow disabled:opacity-50 disabled:cursor-not-allowed disabled:shadow-none"
                            disabled>
                        @lang('Confirm Deposit')
                    </button>

                </div>
            </div>
        </form>
    </div>
@endsection

@push('script')
    <script>
        "use strict";
        (function ($) {

            var amount = parseFloat($('.amount').val() || 0);
            var gateway, minAmount, maxAmount;

            $('.amount').focus();

            $('.amount').on('input', function (e) {
                amount = parseFloat($(this).val());
                if (!amount) amount = 0;
                calculation();
            });

            $('.gateway-input').on('change', function (e) {
                gatewayChange();
            });

            function gatewayChange() {
                let gatewayElement = $('.gateway-input:checked');
                gateway     = gatewayElement.data('gateway');
                minAmount   = gatewayElement.data('min-amount');
                maxAmount   = gatewayElement.data('max-amount');

                let processingFeeInfo =
                    `${parseFloat(gateway.percent_charge).toFixed(2)}% with ${parseFloat(gateway.fixed_charge).toFixed(2)} {{ __(gs('cur_text')) }} charge for payment gateway processing fees`;
                $(".proccessing-fee-info").attr("data-bs-original-title", processingFeeInfo);
                calculation();
            }

            gatewayChange();

            $(".more-gateway-option").on("click", function (e) {
                let paymentList = $(".gateway-option-list");
                paymentList.find(".gateway-option").removeClass("hidden");
                $(this).addClass('hidden');
                paymentList.animate({ scrollTop: (paymentList.height() - 60) }, 'slow');
            });

            function calculation() {
                if (!gateway) return;
                $(".gateway-limit").text(minAmount + " - " + maxAmount);

                let percentCharge      = 0;
                let fixedCharge        = 0;
                let totalPercentCharge = 0;

                if (amount) {
                    percentCharge      = parseFloat(gateway.percent_charge);
                    fixedCharge        = parseFloat(gateway.fixed_charge);
                    totalPercentCharge = parseFloat(amount / 100 * percentCharge);
                }

                let totalCharge = parseFloat(totalPercentCharge + fixedCharge);
                let totalAmount = parseFloat((amount || 0) + totalPercentCharge + fixedCharge);

                $(".final-amount").text(totalAmount.toFixed(2));
                $(".processing-fee").text(totalCharge.toFixed(2));
                $("input[name=currency]").val(gateway.currency);
                $(".gateway-currency").text(gateway.currency);

                if (amount < Number(gateway.min_amount) || amount > Number(gateway.max_amount)) {
                    $(".deposit-form button[type=submit]").attr('disabled', true);
                } else {
                    $(".deposit-form button[type=submit]").removeAttr('disabled');
                }

                if (gateway.currency != "{{ gs('cur_text') }}" && gateway.method.crypto != 1) {
                    $('.deposit-form').addClass('adjust-height');
                    $(".gateway-conversion, .conversion-currency").removeClass('hidden');
                    $(".gateway-conversion").find('.deposit-info__input .text').html(
                        `1 {{ __(gs('cur_text')) }} = <span class="rate">${parseFloat(gateway.rate).toFixed(2)}</span> <span class="method_currency">${gateway.currency}</span>`
                    );
                    $('.in-currency').text(parseFloat(totalAmount * gateway.rate).toFixed(gateway.method.crypto == 1 ? 8 : 2));
                } else {
                    $(".gateway-conversion, .conversion-currency").addClass('hidden');
                    $('.deposit-form').removeClass('adjust-height');
                }

                if (gateway.method.crypto == 1) {
                    $('.crypto-message').removeClass('hidden');
                } else {
                    $('.crypto-message').addClass('hidden');
                }
            }

            var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
            var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
                return new bootstrap.Tooltip(tooltipTriggerEl);
            });

            $('.gateway-input').change();

        })(jQuery);
    </script>
@endpush