<script>
    $(document).ready(function (){
        $('.inventory-listing').select2();
        $('#receipt-items').select2();
        $('#account-list').select2();
        $('#payment-modes').select2();
        $('#currency-option').select2();


        loadInventoryStockDetails();
        updatePurchasedQuantity();
        updateTotalAmountPayable();
    });

    function loadInventoryStockDetails(){
        
        $('.inventory-listing').change(function (){
            let selectedItem = $(this);
            if(selectedItem.val() != ""){
                let divToLoad = selectedItem.closest('.items-to-be-sold').find('.selected-inventory-details')
                    .html('<div class="guestDetails_input"><div class="guestDetails_inputLabel"> &nbsp;</div><div class="guestDetails_input"><img src="<?php echo e(asset('images/load-indicators.gif')); ?>" border=0></div></div>');
                /**Using AJax, fetch details to allow creation of sale order**/
                $.ajax({
                    method: "GET",
                    url: 'get-inventory-details/'+selectedItem.val(),
                    data: selectedItem.val(),
                    success: function (res){
                        if(res != ''){
                            selectedItem.closest('.items-to-be-sold').find('.selected-inventory-details').html(res);
                        }else{
                            divToLoad.html('<b class="error">No Details found for the Selected Item</b>')
                        }
                    }
                });
            }
        });
    }

    
    function updatePurchasedQuantity(){
        $(document).on('input paste change', ".itemQuantity,.sellingPrice",function (){
            var totalPrice =0;
            var currentMinimum = $(this).closest('.items-to-be-sold').find('.minimumSellingPrice').val();
            var currentQuantity = $(this).closest('.items-to-be-sold').find('.itemQuantity').val();
            var currentPrice = $(this).closest('.items-to-be-sold').find('.sellingPrice').val();

            if(parseFloat(currentPrice) < parseFloat(currentMinimum)){
                alert('Please put an amount equal to Minimum Selling Price of Higher');
                $(this).closest('.items-to-be-sold').find('.sellingPrice').val(currentMinimum).trigger('change');
                updatePurchasedQuantity();
            }

            totalPrice = currentQuantity * currentPrice;

            //Update the Total Premium Hidden field with the automated entry
            $(this).closest('.items-to-be-sold').find('.totalSellingPrice').val(totalPrice).trigger('change');
        });
    }

    
    function updateTotalAmountPayable(){
        $(document).on('change',".totalSellingPrice",function (){
            var totalAmount =0;

            $('.totalSellingPrice').each(function (){
                totalAmount += parseFloat($(this).val());
            });
            $('#total-alloc-amount-entered').val(totalAmount).addClass('error').css('font-weight','bold').attr('readonly', false);
            if(totalAmount > 0){
                $('#total-alloc-amount-entered').attr('readonly', true);
            }
        });
    }

    //Input on REF Field
    $('#ref-narration-input').on('input paste', function (){
        var ref = $(this).val();
        if(ref != ""){
            $('#ref-label').removeClass('error');
        }else{
            $('#ref-label').addClass('error');
        }
    });

    $('#account-list').on('change', function (){
        var account = $(this).val();
        $('#account-label').addClass('error');
        if(account != ""){
            $('#account-label').removeClass('error');
        }
    });

    $('#payment-modes').on('change', function (){
        var modes = $(this).val();
        $('#payment-modes-label').addClass('error');
        if(modes != ""){
            $('#payment-modes-label').removeClass('error');
        }
    });

    
    $('#payment-modes').on('change', function (){
        var mode = $(this).val();
        $('#sub-mode-div').hide();
        $('#sub-mode-list').val(null);

        $('#sub-mode-list').val(null);
        if(mode != ""){
            $('#payment-mode-label').removeClass('error');
            $.ajax({
                method: 'GET',
                url: '/sub-modes-of-payments/'+mode,
                data: mode,
                success: function (res){
                    if(res != ""){
                        $('#sub-mode-div').show();
                        $('#sub-mode-field').html(res);
                    }else{
                        $('#sub-mode-div').hide();
                    }
                }
            });
        }else{
            $('#payment-mode-label').addClass('error');
        }

    });


    
    $('#currency-option').on('change',function (){
        var currencySelected = $(this).val();
        $('#conversion-rate-div').hide();

        $.ajax({
            method: 'GET',
            url: '/currency-conversion-rate/'+currencySelected,
            data: currencySelected,
            success: function (res){
                if(res != ""){
                    $('#conversion-rate-div').show();
                    $('#conversion-rate-field').html(res);

                }else{
                    $('#conversion-rate-div').hide();
                }
            }
        });
    });

    //Date Paid Section
    $('#date-paid').on('change', function (){
        var date = $(this).val();
        $('#date-paid-label').addClass('error');
        if(date != ""){
            $('#date-paid-label').removeClass('error');
        }
    });
</script>
<?php echo Form::open(['action'=>['Modules\InventoryDashboardController@storeMakeASale'],'method'=>'POST', 'id'=>'make-a-sale-function']); ?>

<div class="error" style="text-align: center;">On this Window, You can make a Sale and Select Items to be Sold</div>
<div class="guestDetails_inputDivider"></div>
<button type="submit" disabled style="display: none" aria-hidden="true"></button>
    <div class="items-to-be-sold">
        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel">
                Select Item to be Sold <span class="error">*</span>
            </div>
            <div class="guestDetails_input">
                <select name="sold-item[]" class="inventory-listing" style="width: 250px">
                    <option value="">--Select Item--</option>
                    <?php $__currentLoopData = $list; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo $row->id; ?>"><?php echo $row->name; ?></option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
            </div>
        </div>
        <div class="selected-inventory-details"></div>
    </div>
<div class="guestDetails_inputDivider"></div>
<div class="added-fields"></div>
<button type="button" id="add-field" style="height: 25px">+Add Another Field</button><br><br>



<div style="background-color: lightcyan" class="inputDetails">
    <div class="guestDetails_inputDivider1"></div>
    <div class="fields">
        <div id="bank-details">
            <div id="bank-accounts-div">
                <div class="guestDetails_input" >
                    <div class="guestDetails_inputLabel">
                        <?php echo Form::label('bank-account', 'Select Account', ['class'=>'error', 'id'=>'account-label']); ?>

                        <span class="error">*</span>
                    </div>
                    <div class="guestDetails_input">
                        <select name="account" id="account-list" style="width: 180px">
                            <option value="">--Select Account Receiving Payment--</option>
                            <?php if(app('BankAccounts') != null): ?>
                                <?php $__currentLoopData = app('BankAccounts'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($row->id); ?>"><?php echo e(strtoupper($row->acc_name)); ?></option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            <?php endif; ?>
                        </select>
                    </div>
                </div>
            </div>
            <div class="guestDetails_input">
                <div class="guestDetails_inputLabel">
                    <?php echo Form::label('payment-mode', 'Select Mode of Payment', ['class'=>'error', 'id'=>'payment-mode-label']); ?>

                    <span class="error">*</span>
                </div>
                <div class="guestDetails_input">
                    <select name="payment-mode" id="payment-modes" style="width: 150px">
                        <option value="">--Select Payment Mode--</option>
                        <?php if(app('CompanyPaymentModes') != null): ?>
                            <?php $__currentLoopData = app('CompanyPaymentModes'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $mode): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($mode->id); ?>"><?php echo e($mode->name); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        <?php endif; ?>
                    </select>
                </div>
            </div>
            <div class="guestDetails_input" id="sub-mode-div" style="display: none">
                <div class="guestDetails_inputLabel">
                    <?php echo Form::label('payment-mode', 'Sub-Mode of Payment:', ['class'=>'error', 'id'=>'sub-mode-label']); ?>

                    <span class="error">*</span>
                </div>
                <div class="guestDetails_input" id="sub-mode-field"></div>
            </div>
            <div class="guestDetails_input" >
                <div class="guestDetails_inputLabel">
                    <?php echo Form::label('payment-ref', 'Enter Ref Number.',['class'=>'error', 'id'=>'ref-label']); ?> <span class="error">*</span>
                </div>
                <div class="guestDetails_input">
                    <?php echo Form::text('ref', '',['size'=>30, 'id'=>'ref-narration-input','class'=>'input-text-height', 'placeholder'=>'Enter REF Details for the Payment']); ?>

                </div>
            </div>
        </div>
        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel">
                Payee <span class="error">*</span>
            </div>
            <div class="guestDetails_input">
                <?php echo Form::text('payee', 'Customer', ['class'=>'input-text-height', 'size'=>20]); ?>

            </div>
        </div>
        <div class="guestDetails_inputDivider1"></div>
        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel">
                Total Amount Payable <span class="error">*</span>
            </div>
            <div class="guestDetails_input">
                <?php echo Form::text('total-amount', '', ['class'=>'input-text-height', 'id'=>'total-alloc-amount-entered', 'size'=>15, 'readonly'=>true]); ?>

            </div>
        </div>
        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel">
                Currency <span class="error">*</span>
            </div>
            <div class="guestDetails_input">
                <select name="currency" style="width: 120px" id="currency-option">
                    <option value="<?php echo e(app('BaseCurrency')->id); ?>"><?php echo app('BaseCurrency')->abbr; ?></option>
                    <?php $__currentLoopData = app('OtherCurrency'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $other): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo $other->id; ?>"><?php echo $other->abbr; ?></option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
            </div>
        </div>
        <div class="guestDetails_input" id="conversion-rate-div" style="display: none">
            <div class="guestDetails_inputLabel">
                <i class="" id="conversion-rate-label">Conversion Rate:</i> <span class="error">*</span>
            </div>
            <div class="guestDetails_input" id="conversion-rate-field"></div>
        </div>
        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel">
                Enter Date Paid <span class="error">*</span>
            </div>
            <div class="guestDetails_input">
                <?php echo Form::date('date-paid', date('Y-m-d'), ['id'=>'date-paid', 'class'=>'input-text-height', 'max'=>date('Y-m-d')]); ?>

            </div>
        </div>
        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel">
                <?php echo Form::label('narration', 'Narration'); ?> <span class="error">*</span>
            </div>
            <div class="guestDetails_input">
                <?php echo Form::text('narration', 'Purchase of Items',['class'=>'input-text-height','size'=>40]); ?>

            </div>
        </div>
        <div class="guestDetails_inputDivider1"></div>
    </div>
</div>
<?php echo Form::close(); ?>


<script>
    $(document).ready(function () {
        //add and remove vehicle fields
        var addButton = $('#add-field'); //Add button selector
        var wrapper = $('.added-fields'); //Input field wrapper
        var x = 1;
        var fieldHTML = '<div class="items-to-be-sold">' +
            '<button type="button" id="remove-button"> <i class="fa fa-trash"></i> </button>' +
            '<div class="guestDetails_input">\n' +
            '  <div class="guestDetails_inputLabel">Select Item to be Sold <span class="error">*</span></div>\n' +
            '    <div class="guestDetails_input">\n' +
            '<select name="sold-item[]" class="inventory-listing" style="width: 250px">'+
            '<option value="">--Select Item--</option>'+
            <?php $__currentLoopData = $list; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                '<option value="<?php echo e($row->id); ?>"><?php echo $row->name; ?></option>'+
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            '</select>'+
            '    </div>\n' +
            '</div>\n' +
            '<div class="selected-inventory-details"></div>\n'+
            '<div class="guestDetails_inputDivider input_divider1"></div>'+
            '</div>';
        //Once add button is clicked
        $(addButton).click(function(){
            x++; //Increment field counter

            $(wrapper).append(fieldHTML); //Add field html
            $(wrapper).find('.inventory-listing').select2(); //Add the Select 2 Option

            loadInventoryStockDetails();
            updatePurchasedQuantity();
            updateTotalAmountPayable();

        });
        //Once remove button is clicked
        $(wrapper).on('click', '#remove-button', function(e){
            e.preventDefault();
            $(this).parent('div').remove(); //Remove field html
            x--; //Decrement field counter

            var totalAmount =0;

            $('.totalSellingPrice').each(function (){
                totalAmount += parseFloat($(this).val());
            });
            $('#total-alloc-amount-entered').val(totalAmount).addClass('error').css('font-weight','bold').attr('readonly', false);
            if(totalAmount > 0){
                $('#total-alloc-amount-entered').attr('readonly', true);
            }
        });
    });
</script>
<?php /**PATH C:\xampp\htdocs\edwulp_chemist\resources\views/inventory/make-a-sale.blade.php ENDPATH**/ ?>