<?php $__env->startSection('title'); ?>
    <title>Application Users</title>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('banner'); ?>
    <?php echo $__env->make('backend.partials.banner', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('menu'); ?>
    <div class="col-md-10 col-md-offset-1" style="background-color: #7ebb7b">
        <div class="breadcrumbs">
            <div class="container">
                <div class="row">
                    <div class="col-sm-8">
                        <ul>
                            <li><a href="<?php echo url('admin/home'); ?>">Home</a></li>
                            <li class="active">Purchase</li>
                        </ul><!-- end breadcrumb -->
                    </div><!-- end col -->
                </div><!-- end row -->
            </div><!-- end container -->
        </div><!-- end breadcrumbs -->
    </div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.1/bootstrap3-typeahead.min.js"></script>

    <meta name="csrf-token" content="<?php echo e(csrf_token()); ?>">

    <?php echo $__env->make('backend.messages.flashmessage', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>

    <div>

        <div class="container spark-screen">
            <div class="row">
                <div class="col-md-6 col-md-offset-2">
                    <br/>
                    <div><h3>Edit Purchase : <?php echo $refno; ?></h3></div>
                    <div style="background-color: #ff0000;height: 2px">&nbsp;</div>
                    <br/>


                    <div class="search" id="search">
                        <br/>
                        <?php echo Form::open(['url'=>'edit.purchase.index', 'method' => 'GET']); ?>


                        <table width="50%" class="table table-responsive table-hover" >

                            <tr>
                                <td width="5%"><label for="type" class="control-label">Purchase Ref No</label></td>
                                <td width="10%"><?php echo Form::select('refno',(['' => 'Please Select'] + $purchase), null , array('id' => 'refno', 'class' => 'form-control')); ?></td>
                                <td width="10%"><button name="submittype" type="submit" value="print" class="btn btn-primary btn-approve pull-right">Submit</button></td>
                            </tr>

                        </table>

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


                    </div>
                </div>

                <div style="width: 5px"></div>
            </div>
        </div>
    </div>
    <?php $item_row = 0; ?>
    <?php if(!empty($data)): ?>
        <?php echo Form::open(['url'=>'edit.purchase.update']); ?>

        <?php echo e(csrf_field()); ?>

        <?php echo Form::hidden('refno',$refno); ?>

        <div class="form-group col-md-12" style="background-color: rgba(177, 245, 174, 0.33)">
            <?php echo Form::label('items', 'Items', ['class' => 'control-label']); ?>

            <div class="table-responsive">
                <table class="table table-bordered" id="items">
                    <thead>
                    <tr style="background-color: #f9f9f9;">
                        <th width="5%"  class="text-center">Action</th>
                        <th width="40%" class="text-left">Product</th>
                        <th width="5%" class="text-center">Quantity</th>
                        <th width="10%" class="text-right">Unit Price</th>
                        <th width="10%" class="text-right">Tax</th>
                        <th width="10%" class="text-right">Sub Total</th>
                    </tr>
                    </thead>
                    <tbody>

                    <?php $__currentLoopData = $data->items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $i=>$row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

                        <tr id="item-row-<?php echo e($item_row); ?>">
                            <td class="text-center" style="vertical-align: middle;">
                                <button type="button" onclick="$(this).tooltip('destroy'); $('#item-row-<?php echo e($item_row); ?>').remove(); totalItem();" data-toggle="tooltip" title="<?php echo e(trans('general.delete')); ?>" class="btn btn-xs btn-danger"><i class="fa fa-trash"></i></button>
                            </td>
                            <td>
                                <input class="form-control typeahead" required="required" value="<?php echo $row->item->name; ?>" name="item[<?php echo e($item_row); ?>][name]" type="text" id="item-name-<?php echo e($item_row); ?>" autocomplete="off">
                                <input value="<?php echo $row->product_id; ?>" name="item[<?php echo e($item_row); ?>][item_id]" type="hidden" id="item-id-<?php echo e($item_row); ?>">
                                <input value="<?php echo $row->id; ?>" name="item[<?php echo e($item_row); ?>][tr_id]" type="hidden" id="item-trid-<?php echo e($item_row); ?>">
                            </td>
                            <td>
                                <input class="form-control text-center" required="required" name="item[<?php echo e($item_row); ?>][quantity]" type="text" id="item-quantity-<?php echo e($item_row); ?>" value="<?php echo $row->quantity; ?>">
                            </td>
                            <td>
                                <input class="form-control text-right" required="required" name="item[<?php echo e($item_row); ?>][price]" type="text" id="item-price-<?php echo e($item_row); ?>" value="<?php echo $row->unit_price; ?>">
                            </td>
                            <td>
                                <?php echo Form::select('item[' . $item_row . '][tax]',$taxes , $row->tax_id, ['id'=> 'item-tax-'. $item_row, 'class' => 'form-control', 'placeholder' => trans('general.form.select.field', ['field' => trans_choice('general.taxes', 1)])]); ?>

                            </td>
                            <td class="text-right" style="vertical-align: middle;">
                                <span id="item-total-<?php echo e($item_row); ?>">0</span>
                            </td>
                        </tr>
                        <?php $item_row++; ?>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                    <tr id="addItem">
                        <td class="text-center"><button type="button" onclick="addItem();" data-toggle="tooltip" title="<?php echo e(trans('general.add')); ?>" class="btn btn-xs btn-primary" data-original-title="<?php echo e(trans('general.add')); ?>"><i class="fa fa-plus"></i></button></td>
                        <td class="text-right" colspan="5"></td>
                    </tr>
                    <tr>
                        <td class="text-right" colspan="5"><strong><?php echo e(trans('purchase.sub_total')); ?></strong></td>
                        <td class="text-right"><span id="sub-total">0</span></td>
                    </tr>
                    <tr>
                        <td class="text-right" colspan="5"><strong><?php echo e(trans_choice('general.taxes', 1)); ?></strong></td>
                        <td class="text-right"><span id="tax-total">0</span></td>
                    </tr>
                    <tr>
                        <td class="text-right" colspan="5"><strong><?php echo e(trans('purchase.total')); ?></strong></td>
                        <td class="text-right"><span id="grand-total">0</span></td>
                    </tr>
                    </tbody>
                </table>
            </div>

            <div class="col-md-6">
                <?php echo Form::submit('SUBMIT',['class'=>'btn btn-primary button-control']); ?>

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


            <?php echo Form::open(['url'=>'admin.home', 'method' => 'GET']); ?>


            <div class="col-md-6">
                <?php echo Form::submit('EXIT',['class'=>'btn btn-primary button-control pull-right']); ?>

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


        </div>

    <?php endif; ?>

<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>

<script type="text/javascript">

    var item_row = '<?php echo e($item_row); ?>';

    function addItem() {
        html  = '<tr id="item-row-' + item_row + '">';
        html += '  <td class="text-center" style="vertical-align: middle;">';
        html += '      <button type="button" onclick="$(this).tooltip(\'destroy\'); $(\'#item-row-' + item_row + '\').remove(); totalItem();" data-toggle="tooltip" title="<?php echo e(trans('general.delete')); ?>" class="btn btn-xs btn-danger"><i class="fa fa-trash"></i></button>';
        html += '  </td>';
        html += '  <td>';
        html += '      <input class="form-control typeahead" required="required" placeholder="<?php echo e(trans('general.form.enter', ['field' => trans_choice('purchase.item_name', 1)])); ?>" name="item[' + item_row + '][name]" type="text" id="item-name-' + item_row + '" autocomplete="off">';
        html += '      <input name="item[' + item_row + '][item_id]" type="hidden" id="item-id-' + item_row + '">';
        html += '  </td>';
        html += '  <td>';
        html += '      <input class="form-control text-center" required="required" name="item[' + item_row + '][quantity]" type="text" id="item-quantity-' + item_row + '">';
        html += '  </td>';
        html += '  <td>';
        html += '      <input class="form-control text-right" required="required" name="item[' + item_row + '][price]" type="text" id="item-price-' + item_row + '">';
        html += '  </td>';

        html += '  <td>';
        html += '      <select class="form-control select" name="item[' + item_row + '][tax]" id="item-tax-' + item_row + '">';
        html += '         <option selected="selected" value=""><?php echo e(trans('general.form.select.field', ['field' => trans_choice('general.taxes', 1)])); ?></option>';
        <?php $__currentLoopData = $taxes; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $tax_key => $tax_value): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                html += '         <option value="<?php echo e($tax_key); ?>"><?php echo e($tax_value); ?></option>';
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                html += '      </select>';
        html += '  </td>';

        html += '  <td class="text-right" style="vertical-align: middle;">';
        html += '      <span id="item-total-' + item_row + '">0</span>';
        html += '  </td>';

        $('#items tbody #addItem').before(html);
        //$('[rel=tooltip]').tooltip();

        $('[data-toggle="tooltip"]').tooltip('hide');

        
                
                


                
                
                

                item_row++;
    }

    $(document).ready(function(){
        //Date picker
        var minday = new Date();

        $('#pdate').datepicker({
            numberOfMonths: 1,
            showButtonPanel: true,
            minDate: minday,
            dateFormat: 'dd/mm/yy',
            maxDate: minday
        });


                
                
                


        var autocomplete_path = "<?php echo e(url('autocomplete.productlist')); ?>";

        $(document).on('click', '.form-control.typeahead', function() {
            input_id = $(this).attr('id').split('-');

            item_id = parseInt(input_id[input_id.length-1]);

            $(this).typeahead({
                minLength: 3,
                displayText:function (data) {
                    return data.name;
                },
                source: function (query, process) {
                    $.ajax({
                        url: autocomplete_path,
                        type: 'GET',
                        dataType: 'JSON',
                        data: 'query=' + query ,
                        success: function(data) {
                            return process(data);
                        }
                    });
                },
                afterSelect: function (data) {
                    $('#item-id-' + item_id).val(data.item_id);
                    $('#item-quantity-' + item_id).val('1');
                    $('#item-price-' + item_id).val(data.unit_price);

                    $('#item-tax-' + item_id).val(data.tax_id);

                    // This event Select2 Stylesheet
                    $('#item-tax-' + item_id).trigger('change');

                    $('#item-total-' + item_id).html(data.total);

                    totalItem();0
                }
            });
        });

        $(document).on('change', '#items tbody select', function(){
            totalItem();
        });

        $(document).on('keyup', '#items tbody .form-control', function(){
            totalItem();
        });

        $(document).on('change', '#customer_id', function (e) {
            $.ajax({
                url: '<?php echo e(url("incomes/customers/currency")); ?>',
                type: 'GET',
                dataType: 'JSON',
                data: 'customer_id=' + $(this).val(),
                success: function(data) {
                    $('#currency_code').val(data.currency_code);

                    // This event Select2 Stylesheet
                    $('#currency_code').trigger('change');
                }
            });
        });
    });

    function totalItem() {
        $.ajax({
            url: '<?php echo e(url("products/totalItem")); ?>',
            type: 'POST',
            dataType: 'JSON',
            data: $('#currency_code, #items input[type=\'text\'],#items input[type=\'hidden\'], #items textarea, #items select'),
            headers: { 'X-CSRF-TOKEN': '<?php echo e(csrf_token()); ?>' },
            success: function(data) {
                if (data) {
                    $.each( data.items, function( key, value ) {
                        $('#item-total-' + key).html(value);
                    });

                    $('#sub-total').html(data.sub_total);
                    $('#tax-total').html(data.tax_total);
                    $('#grand-total').html(data.grand_total);
                }
            }
        });
    }
</script>

<?php $__env->stopPush(); ?>
<?php echo $__env->make('backend.layouts.master', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>