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

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

        /**Changing Bill Type to Display the Bill Balance and what to be requisitioned**/
        $('#bills-list').change(function (){
            let bill = $(this).val();
            $('#requisition-div').hide();
            if(bill != ""){
                $.ajax({
                    method: 'GET',
                    url: '/get-bill-balance/'+bill,
                    data: bill,
                    success: function (res){
                        if(res != ""){
                            $('#requisition-div').show();
                            $('#current-bill-balance').val(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();
                    }
                }
            });
        });
    });
</script>
<?php echo Form::open(['action'=>['Payments\RequisitionController@storeCreateNewVendorRequisition', 'id'=>$id], 'id'=>'create-vendor-requisition', 'method'=>'POST']); ?>

<div class="error" style="text-align: center;">On this Window, You can Create a New Requisition to Pay a Vendor Bill</div>
<div class="guestDetails_inputDivider"></div>
<button type="submit" disabled style="display: none" aria-hidden="true"></button>
<div class="guestDetails_inputDivider1"></div>
<?php if($vendor->billMasterVendor->sum('amount')): ?>
    <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: 240px" id="requisition-list">
                <option value="">--Select an Item--</option>
                <option value="bill-payment">BILL PAYMENT</option>
            </select>
        </div>
    </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: 240px" 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', $vendor->name.'/'.$vendor->phone ?? '', ['class'=>'input-text-height', 'size'=>90]); ?>

        </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="bill" style="width: 500px" id="bills-list">
                <option value="">--Select a Bill to Pay for--</option>
                <?php $__currentLoopData = $bills; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $bill): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <?php if($bill->billBalance($bill->id) > 0): ?>
                        <option value="<?php echo e($bill->id); ?>"><?php echo $bill->ref." - ".$bill->narration; ?></option>
                    <?php endif; ?>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </select>
        </div>
    </div>
    <div class="guestDetails_inputDivider1"></div>
    <div id="requisition-div" style="display: none">
        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel">
                Current Bill Balance <span class="error">*</span>
            </div>
            <div class="guestDetails_input">
                <?php echo Form::text('current-bill-balance', '', ['id'=>'current-bill-balance','class'=>'input-text-height', 'size'=>15, 'readonly'=>true]); ?>

            </div>
        </div>
        <div class="guestDetails_input">
            <div class="guestDetails_inputLabel">
                <span class="error">Requisition Amount *</span>
            </div>
            <div class="guestDetails_input">
                <?php echo Form::number('req-amount', '', ['class'=>'input-text-height', 'style'=>'width: 110px']); ?>

            </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>

    <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 else: ?>
    <div class="error" style="text-align: center;"><b>This Vendor has no Pending Bills to allow Creation of a Requisition</b></div>
<?php endif; ?>
<?php echo Form::close(); ?>

<?php /**PATH /home/zedarroot/public_html/edwulp/resources/views/finance/vendors/create-vendor-requisition.blade.php ENDPATH**/ ?>