<script>
    $(document).ready(function (){
        $('#requisition-list').select2();
        $('#insurer-listings').select2();
        $('#debit-list').select2();
        $('#currency-option').select2();

        /**Anytime we Chose a Requisition Type, Display affected ledgers**/
        $('#requisition-list').change(function (){
            let type = $(this).val();
            $('#insurer-list-div').hide();
            $('#office-stationary-requisition-div').hide();
            $('#kitchen-consumables-requisition-div').hide();
            $('#premium-refund-requisition-div').hide();
            $("#debit-list option[value !='']").remove();
            if(type != ""){
                if(type == 'remit-premium'){
                    $('#insurer-list-div').show();
                }else if(type == 'office-stationary'){
                    $('#office-stationary-requisition-div').show();
                }else if(type == 'kitchen-consumables'){
                    $('#kitchen-consumables-requisition-div').show();
                }else if(type == 'premium-refund'){
                    $('#premium-refund-requisition-div').show();
                }
                $.ajax({
                    method: 'GET',
                    url: '/requisition-debit-accounts/'+type,
                    data: type,
                    success: function (res){
                        if(res != ""){
                            $('#debit-list').append(res);
                        }
                    }
                });
            }
        });

        
        $('#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();
                    }
                }
            });
        });

        /**Anytime someone searches for a Risk Note Number to Refund Premium, do as below**/
        $('#refund-premium').click( function (){
            let dnNo = $('#dn-no').val();
            $('#my-policy-details').html('<div style="text-align: center; margin: 20px 0 20px 0;"><img src="<?php echo e(asset('images/loads/loader.gif')); ?>" border=0></div>');
            if(dnNo != ""){
                /**Using AJAX, get the DN Number Details**/
                $.ajax({
                    method: 'GET',
                    url: '/get-policy-premium-balance-for-refund/'+dnNo,
                    success: function (res){
                        if(res != "exists"){
                            $('#my-policy-details').html(res);
                        }else{
                            alert('There was a System Error. Please Filter the Report again');
                            return false;
                        }
                    }
                });
            }else{
                alert('Please Enter Debit Note Number to Proceed');
                return false;
            }
        });

        /**Anytime a User clicks an office Item to requisition for, activate the Quantity and price input boxes**/
        $('.activatorItem').click(function (){
            if($(this).is(':checked')){
                $(this).closest('.stationary-row').find('.quantityRow').attr('readonly', false);
                $(this).closest('.stationary-row').find('.priceRow').attr('readonly', false);
            }else{
                $(this).closest('.stationary-row').find('.quantityRow').attr('readonly', true);
                $(this).closest('.stationary-row').find('.priceRow').attr('readonly', true);
            }
        });

        /**Anytime the user changes the Value of Quantity, get the total price**/
        $(document).on('input paste change', ".quantityRow,.priceRow",function (){
            var totalPrice =0;
            var currentQuantity = $(this).closest('.stationary-row').find('.quantityRow').val();
            var currentPrice = $(this).closest('.stationary-row').find('.priceRow').val();

            totalPrice = currentQuantity * currentPrice;

            //Update the Total Premium Hidden field with the automated entry
            $(this).closest('.stationary-row').find('.totalPriceRow').val(totalPrice).trigger('change');

        });

        /**For Office Stationery Requisition, sum up the total amounts to form Requisition AMount**/
        $(document).on('change',".totalPriceRow",function (){
            var totalAmount =0;

            $('.totalPriceRow').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);
            }
        });


        /**Anytime a User clicks a Kitchen Consumable Item to requisition for, activate the Quantity and price input boxes**/
        $('.activatorKitchenItem').click(function (){
            if($(this).is(':checked')){
                $(this).closest('.kitchen-consumables-row').find('.kitchenQuantityRow').attr('readonly', false);
                $(this).closest('.kitchen-consumables-row').find('.kitchenPriceRow').attr('readonly', false);
            }else{
                $(this).closest('.kitchen-consumables-row').find('.kitchenQuantityRow').attr('readonly', true);
                $(this).closest('.kitchen-consumables-row').find('.kitchenPriceRow').attr('readonly', true);
            }
        });

        /**Anytime the user changes the Value of Quantity, get the total price**/
        $(document).on('input paste change', ".kitchenQuantityRow,.kitchenPriceRow",function (){
            var totalPrice =0;
            var currentQuantity = $(this).closest('.kitchen-consumables-row').find('.kitchenQuantityRow').val();
            var currentPrice = $(this).closest('.kitchen-consumables-row').find('.kitchenPriceRow').val();

            totalPrice = currentQuantity * currentPrice;

            //Update the Total Premium Hidden field with the automated entry
            $(this).closest('.kitchen-consumables-row').find('.kitchenTotalPriceRow').val(totalPrice).trigger('change');

        });

        /**For Kitchen Consumables Requisition, sum up the total amounts to form Requisition Amount**/
        $(document).on('change',".kitchenTotalPriceRow",function (){
            var totalAmount =0;

            $('.kitchenTotalPriceRow').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 echo Form::open(['action'=>'Payments\RequisitionController@storeCreateNewRequisition', 'id'=>'create-new-requisition', 'method'=>'POST']); ?>

<div class="error" style="text-align: center;">On this Window, You can Create a New Requisition</div>
<div class="guestDetails_inputDivider"></div>
<button type="submit" disabled style="display: none" aria-hidden="true"></button>
<div class="guestDetails_inputDivider1"></div>
<div class="guestDetails_input">
    <div class="guestDetails_inputLabel">
        Select a Requisition Type <span class="error">*</span>
    </div>
    <div class="guestDetails_input">
        <select name="requisition-type" style="width: 500px" id="requisition-list">
            <option value="">--Select an Item--</option>
            <option value="bank-transfer">BANK TRANSFER</option>
            <option value="topup-pettycash">PETTY CASH - TOPUP</option>
            <option value="payroll">PAYROLL</option>
            <option value="kitchen-consumables">KITCHEN CONSUMABLES</option>
            <option value="office-stationary">OFFICE STATIONARY</option>
        </select>
    </div>
</div>


<div id="office-stationary-requisition-div" style="display: none" >
    <div class="guestDetails_inputDivider1"></div>
    <div class="inputDivider" style="background-color: lightcyan">
        <div class="fields" >
            
            <table style="width: 100%; border-collapse: collapse">
                <?php $__currentLoopData = \App\Models\OfficeStationery::where('status', 1)->orderBy('name', 'asc')->get(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <tr class="stationary-row" >
                        <td style="width: 5%"><?php echo Form::checkbox('activatorOfficeStationery['.$row->id.']', $row->id,false,['class'=>'activatorItem']); ?></td>
                        <td style="width: 20%"><b><?php echo $row->name; ?></b></td>
                        <td style="width: 25%"> Quantity<span class="error">*</span> <?php echo Form::number('quantity['.$row->id.']', '0',['style'=>'width:80px','class'=>'input-text-height quantityRow', 'readonly'=>true]); ?></td>
                        <td style="width: 25%">Unit Price<span class="error">*</span> <?php echo Form::number('price['.$row->id.']', '0',['style'=>'width:80px','class'=>'input-text-height priceRow', 'readonly'=>true]); ?></td>
                        <td style="width: 25%">Total Price<span class="error">*</span> <?php echo Form::text('total['.$row->id.']', '0',['style'=>'width:80px','class'=>'input-text-height totalPriceRow', 'readonly'=>true]); ?></td>
                    </tr>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </table>
            <div class="guestDetails_inputDividerNone"></div>
        </div>
    </div>
</div>


<div id="kitchen-consumables-requisition-div" style="display: none" >
    <div class="guestDetails_inputDivider1"></div>
    <div class="inputDivider" style="background-color: lightcyan">
        <div class="fields" >
            
            <table style="width: 100%; border-collapse: collapse">
                <?php $__currentLoopData = \App\Models\KitchenConsumable::where('status', 1)->orderBy('name', 'asc')->get(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <tr class="kitchen-consumables-row" >
                        <td style="width: 5%"><?php echo Form::checkbox('activatorKitchenConsumable['.$row->id.']', $row->id,false,['class'=>'activatorKitchenItem']); ?></td>
                        <td style="width: 20%"><b><?php echo $row->name; ?></b></td>
                        <td style="width: 25%"> Quantity<span class="error">*</span> <?php echo Form::number('kitchen-quantity['.$row->id.']', '0',['style'=>'width:80px','class'=>'input-text-height kitchenQuantityRow', 'readonly'=>true]); ?></td>
                        <td style="width: 25%">Unit Price<span class="error">*</span> <?php echo Form::number('kitchen-price['.$row->id.']', '0',['style'=>'width:80px','class'=>'input-text-height kitchenPriceRow', 'readonly'=>true]); ?></td>
                        <td style="width: 25%">Total Price<span class="error">*</span> <?php echo Form::text('kitchen-total['.$row->id.']', '0',['style'=>'width:80px','class'=>'input-text-height kitchenTotalPriceRow', 'readonly'=>true]); ?></td>
                    </tr>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </table>
            <div class="guestDetails_inputDividerNone"></div>
        </div>
    </div>
</div>
<div class="guestDetails_inputDivider1"></div>
<div class="guestDetails_input">
    <div class="guestDetails_inputLabel">
        Debited Account (DR) <span class="error">*</span>
    </div>
    <div class="guestDetails_input">
        <select name="debit-type" style="width: 500px" id="debit-list">
            <option value="">--Select Account to Pay To--</option>
        </select>
    </div>
</div>
<div class="guestDetails_inputDivider1"></div>
<div class="guestDetails_input">
    <div class="guestDetails_inputLabel">
        Payee <span class="error">*</span>
    </div>
    <div class="guestDetails_input">
        <?php echo Form::text('payee', '', ['class'=>'input-text-height', 'size'=>90]); ?>

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

    </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">
        Requisition Date <span class="error">*</span>
    </div>
    <div class="guestDetails_input">
        <?php echo Form::date('req-date', '', ['class'=>'input-text-height', 'style'=>'width:90px']); ?>

    </div>
</div>
<div class="guestDetails_inputDivider1"></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::textarea('narration', '',['style'=>'width:500px; height:60px']); ?>

    </div>
</div>
<?php echo Form::close(); ?>

<?php /**PATH C:\xampp\htdocs\finewood\resources\views/payments/create-new-requisition.blade.php ENDPATH**/ ?>