<div class="modal fade fade-in-scale custom--modal gateway-modal modal-lg" id="addMoneyModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h6 class="modal-title">@lang('Topup Methods')</h6>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="row justify-content-center">
                    <div class="col-lg-12">
                        <form method="POST" class="deposit-form" id="topupModalForm">
                            @csrf
                            <input type="hidden" name="currency">
                            <div class="gateway-card">
                                <div class="row gy-sm-4 gy-3">
                                    <div class="col-lg-12">
                                        <h6 class="mb-3">@lang('Choose a Payment Option')</h6>

                                        <div class="payment-system-list is-scrollable gateway-option-list">
                                            <div class="row gy-4">
                                                <div class="col-md-6">
                                                    <label for="wallet-balance" class="payment-item wallet-wrapper">
                                                        <div class="payment-item__info">
                                                            <span class="payment-item__check"></span>
                                                            <span class="payment-item__name">{{ __(gs('site_name')) }} @lang('Wallet')</span>
                                                        </div>
                                                        <div class="payment-item__thumb">
                                                            <img class="payment-item__thumb-img" src="{{ getImage(getFilePath('gateway') . '/wallet.png', '100x40') }}" alt="@lang('payment-thumb')">
                                                        </div>
                                                        <input class="payment-item__radio from_wallet_input" id="wallet-balance" hidden type="radio" name="from_wallet" value="1">
                                                    </label>
                                                    <small class="balance">@lang('0.00')</small>
                                                </div>

                                                @foreach ($gatewayCurrency as $data)
                                                    <div class="col-md-6">
                                                        <label for="{{ titleToKey($data->name) }}" class="payment-item gateway-option">
                                                            <div class="payment-item__info">
                                                                <span class="payment-item__check"></span>
                                                                <span class="payment-item__name">{{ __($data->name) }}</span>
                                                            </div>
                                                            <div class="payment-item__thumb">
                                                                <img class="payment-item__thumb-img" src="{{ getImage(getFilePath('gateway') . '/' . $data->method->image) }}" alt="@lang('payment-thumb')">
                                                            </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>
                                                    </div>
                                                @endforeach
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-lg-12">
                                        <h6 class="mb-3">@lang('Topup Amount')</h6>
                                        <div class="form-group mb-0">
                                            <div class="input-group">
                                                <input type="number" min="0" step="any" class="form-control form--control amount" name="amount" placeholder="@lang('Enter topup amount')">
                                                <span class="input-group-text">{{ __(gs('cur_text')) }}</span>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="col-lg-12 detaiContainer">
                                        <div class="payment-system-list">
                                            <div class="deposit-info limitInfo">
                                                <div class="deposit-info__title">
                                                    <p class="text has-icon"> @lang('Limit')
                                                        <span></span>
                                                    </p>
                                                </div>
                                                <div class="deposit-info__input">
                                                    <p class="text"><span class="gateway-limit">@lang('0.00')</span>
                                                    </p>
                                                </div>
                                            </div>
                                            <div class="deposit-info processingInfo">
                                                <div class="deposit-info__title">
                                                    <p class="text has-icon">@lang('Processing Charge')
                                                        <span data-bs-toggle="tooltip" title="@lang('Processing charge for payment gateways')" class="proccessing-fee-info"><i class="las la-info-circle"></i> </span>
                                                    </p>
                                                </div>
                                                <div class="deposit-info__input">
                                                    <p class="text"><span class="processing-fee">@lang('0.00')</span>
                                                        {{ __(gs('cur_text')) }}
                                                    </p>
                                                </div>
                                            </div>

                                            <div class="deposit-info total-amount totalAmount pt-3">
                                                <div class="deposit-info__title">
                                                    <p class="text">@lang('Total')</p>
                                                </div>
                                                <div class="deposit-info__input">
                                                    <p class="text"><span class="final-amount">@lang('0.00')</span>
                                                        {{ __(gs('cur_text')) }}</p>
                                                </div>
                                            </div>

                                            <div class="deposit-info gateway-conversion d-none total-amount pt-2">
                                                <div class="deposit-info__title">
                                                    <p class="text">@lang('Conversion')</p>
                                                </div>
                                                <div class="deposit-info__input">
                                                    <p class="text"></p>
                                                </div>
                                            </div>

                                            <div class="deposit-info conversion-currency d-none total-amount pt-2">
                                                <div class="deposit-info__title">
                                                    <p class="text">
                                                        @lang('In') <span class="gateway-currency"></span>
                                                    </p>
                                                </div>
                                                <div class="deposit-info__input">
                                                    <p class="text">
                                                        <span class="in-currency"></span>
                                                    </p>
                                                </div>
                                            </div>

                                            <div class="d-none crypto-message mt-2">
                                                @lang('Conversion with')<span class="gateway-currency"></span>
                                                @lang('and final value will Show on next step')
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-lg-12">
                                        <button type="submit" class="btn btn--base w-100" disabled>
                                            @lang('Confirm Payment')
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@push('script')
    <script>
        "use script";

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

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

                calculation();
            });

            $('.gateway-input').on('change', function(e) {
                $('[name=from_wallet]').prop('checked', false);
                $(".detaiContainer").removeClass('d-none');
                $('.balance').addClass('d-none');
                gatewayChange();
            });

            $('.wallet-wrapper').on('click', function(e) {
                let $input = $(this).find('[name=from_wallet]');

                gateway = null;
                $('[name=currency]').val('');
                $(".detaiContainer").addClass('d-none');
                $('.balance').text("{{ showAmount(auth()->user()->balance) }}").removeClass('d-none');
                $('.gateway-modal').find('[name=from_wallet]').prop('checked', false);
                $input.prop('checked', true);
                $('.gateway-input').prop('checked', false);

                calculation();

                e.preventDefault();
            });

            function gatewayChange() {
                let gatewayElement = $('.gateway-input:checked');
                let methodCode = gatewayElement.val();

                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();

            function calculation() {
                if ($('.gateway-modal').find("[name=from_wallet]").prop('checked') == true) {
                    if (amount <= 0 || amount > @json(auth()->user()->balance)) {
                        $(".deposit-form button[type=submit]").attr('disabled', true);
                        console.log(200);

                    } else {
                        $(".deposit-form button[type=submit]").attr('disabled', false);
                    }
                }

                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('d-none');
                    $(".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('d-none');
                    $('.deposit-form').removeClass('adjust-height')
                }

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

            var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
            var tooltipList = tooltipTriggerList.map(function(tooltipTriggerEl) {
                return new bootstrap.Tooltip(tooltipTriggerEl)
            })
            $('.gateway-input').change();
        });
    </script>
@endpush
