@extends('admin.layouts.app')
@section('panel')
    <div class="row mb-none-30">
        <div class="col-lg-12">
            <form action="{{ route('admin.plan.store') }}" method="POST">
                @csrf
                <div class="card">
                    <div class="card-body">
                        <div class="row">
                            <div class="form-group col-lg-4">
                                <label>@lang('Name')</label>
                                <input type="text" class="form-control form-control-lg" name="name"
                                    value="{{ old('name') }}" required="">
                            </div>
                            <div class="form-group col-lg-4">
                                <label>@lang('Price')</label>
                                <div class="input-group mb-3">
                                    <input type="number" class="form-control form-control-lg" name="price"
                                        value="{{ old('price') }}" id="planAmount" required="" step="any">
                                    <div class="input-group-text">
                                        {{ __($general->cur_text) }}
                                    </div>
                                </div>
                            </div>
                            <div class="form-group col-lg-4">
                                <label>@lang('Referral Bonus')</label>
                                <div class="input-group mb-3">
                                    <input type="number" class="form-control form-control-lg" id="referralBonus"
                                        name="referral_bonus" value="{{ old('referral_bonus') }}" required=""
                                        step="any">
                                    <div class="input-group-text">
                                        {{ __($general->cur_text) }}
                                    </div>
                                </div>
                            </div>
                            <div class="form-group col-lg-4">
                                <label>@lang('Discount Amount ')</label>
                                <div class="input-group mb-3">
                                    <input type="number" class="form-control form-control-lg" name="discount_amount"
                                        id="discount_amount" aria-describedby="basic-addon2" step="any" required>
                                    <div class="input-group-text">
                                        <select name="discount_type" class="form-select form-select-sm" id="discount_type"
                                            aria-label=".form-select-sm example">
                                            <option value="INR">
                                                INR</option>
                                            <option value="%">
                                                %</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group col-lg-4">
                                <label>@lang('Uplink Payment')</label>
                                <div class="input-group mb-3">
                                    <input type="number" class="form-control form-control-lg" name="uplink_payment"
                                        id="uplink_payment" aria-describedby="basic-addon2" step="any" required>
                                    <div class="input-group-text">
                                        <select name="uplink_payment_type" class="form-select form-select-sm" id="uplink_payment_type"
                                            aria-label=".form-select-sm example">
                                            <option value="INR">
                                                INR</option>
                                            <option value="%">
                                                %</option>
                                        </select>
                                    </div>
                                </div>
                            </div>

                        </div>
                        <h4 class="text-center my-4">@lang('Level Commissions')</h4>
                        <div class="row">
                            @for ($i = 0; $i < $general->matrix_height; $i++)
                                <div class="form-group col-lg-3">
                                    <label>@lang('Level '){{ $i + 1 }}</label>
                                    <div class="input-group mb-3">
                                        <input type="number" class="form-control form-control-lg commissionAmount"
                                            name="level[{{ $i + 1 }}]" step="any" required="">
                                        <div class="input-group-text">
                                            {{ __($general->cur_text) }}
                                        </div>
                                    </div>
                                </div>
                            @endfor
                        </div>
                        <div class="text-center mb-4">
                            <div class="adminGain"></div>
                            <div class="adminLoss"></div>
                        </div>
                    </div>
                    <div class="card-footer">
                        <button type="submit" class="btn btn--primary w-100 h-45">@lang('Plan Create')</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
@endsection

@push('breadcrumb-plugins')
    <x-back route="{{ route('admin.plan.index') }}" />
@endpush

@push('script')
    <script>
        "use strict";
        (function($) {
            function planPriceCommission() {
                var levelAmount = 0;
                var planAmount = $('#planAmount').val();
                var referralBonus = $('#referralBonus').val();
                var discountAmount = parseFloat($('#discount_amount').val()) || 0;
                var discountType = $('#discount_type').val();

                var discountValue = 0;

                if (discountType === '%') {
                    var discountValue = ( discountAmount / 100 ) * planAmount;
                } else {
                    var discountValue = discountAmount;
                }
                var uplinkPayment = parseFloat($('#uplink_payment').val()) || 0;
                var uplinkPaymentType = $('#uplink_payment_type').val();

                var uplinkPaymentValue = 0;

                if (uplinkPaymentType === '%') {
                    var uplinkPaymentValue = (uplinkPayment / 100) * planAmount;
                } else {
                    var uplinkPaymentValue = uplinkPayment;
                }



                $('.commissionAmount').each(function() {
                    if ($(this).val() != '') {
                        levelAmount += +$(this).val();
                    }
                });

                var totalAmount = Number(levelAmount) + Number(referralBonus) + discountValue + uplinkPaymentValue;
                var currency = "{{ __($general->cur_text) }}";
                var finalAmount = planAmount - totalAmount;

                if (planAmount > totalAmount) {
                    $('.adminGain').html('<strong class="text--success">@lang('Admin Benefit') : ' + parseFloat(finalAmount)
                        .toFixed(2) +
                        ' ' + currency + '</strong>');
                    $('.adminLoss').empty();
                } else {
                    $('.adminLoss').html('<strong class="text--danger">@lang('Admin Loss') : ' + parseFloat(finalAmount)
                        .toFixed(2) +
                        ' ' + currency + '</strong>');
                    $('.adminGain').empty();
                }
            };

            $(document).on('change', '.commissionAmount, #planAmount, #referralBonus, #discount_amount, #discount_type, #uplink_payment, #uplink_payment_type', function() {
                planPriceCommission();
            });
        })(jQuery);
    </script>
@endpush
