<?php Fun::lang() ?>
<div class="page-purchases">
    <div class="preview">
        <div class="col-sm-7 paddin-5">
            <div class="col-sm-6 paddin-5">
                <div class="form-group">
                    <?php echo Form::text('barcode', null, array('placeholder' => __('administrator.barcode') ,'class' => 'form-control typeahead', "autocomplete" => "on")); ?>

                </div>
            </div>
            <div class="col-sm-6 paddin-5">
                <div class="form-group">
                    <?php echo Form::text('search', null, array('placeholder' => __('administrator.search') ,'class' => 'form-control search')); ?>

                </div>
            </div>
            <div class="product-item-list">
                <div class="item-list">
                    <div class="row my-product-list">
                    </div>
                </div>
            </div>
        </div>
        <div class="col-sm-5 paddin-5">
            <div class="check-bill">
                <div class="row">
                    <div class="check-bill-table">
                        <div class="product-view-table">
                            <div class="parent-box-pos">
                                <table class="table table-hover item-list-tb ">
                                    <thead class="text-center">
                                    <tr>
                                        <th>#</th>
                                        <th><?php echo e(__('administrator.item_name')); ?></th>
                                        <th><?php echo e(__('administrator.qty')); ?> <span class="red">*</span></th>
                                        <th><?php echo e(__('administrator.action')); ?></th>
                                    </tr>
                                    </thead>
                                    <tbody id="append_product">
                                    <?php if(@$master): ?>
                                        <?php $__currentLoopData = $master->detail; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key=>$row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <tr data-symbol="">
                                                <td>
                                                    <input type="hidden" class="barcode" value="<?php echo e($row->barcode); ?>" name="barcode[]"><?php echo e($key+1); ?>

                                                </td>
                                                <td><?php echo e($row->getItem->product_name??''); ?> (<?php echo e(__('administrator.size')); ?>

                                                    :<?php echo e($row->getSize->size??''); ?> , <?php echo e(__('administrator.color')); ?>

                                                    :<?php echo e($row->getColor->name??''); ?>)<br><?php echo e($row->barcode); ?></td>
                                                <td>
                                                    <input type="number" value="<?php echo e($row->qty); ?>"
                                                           class="form-control input-sm qty number-only" name="qty[]"/>
                                                </td>

                                                <td class="text-center"><span class="fa fa-times text-danger fa-2x  cursor remove_row"></span>
                                                </td>
                                            </tr>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
                <div class=" blog-payment" style="margin-top: 20px">
                    <div class="list-box">
                        <div class="list">
                            <div class="col-sm-6 paddin-5">
                                <label><?php echo e(__('administrator.request_to')); ?> :</label>
                                <?php echo Form::select('request_to',$branch,null,array('placeholder'=>__('administrator.please_select'),'class'=>' form-control request_to')); ?>

                            </div>
                            <div class="col-sm-6 paddin-5">
                                <label><?php echo e(__('administrator.date')); ?> :</label>
                                <?php echo Form::text('date',@$master?date('d-m-Y',strtotime($master->date)):date('d-m-Y'),array('placeholder'=>__('administrator.date'),'class'=>'date_picker form-control')); ?>

                            </div>
                        </div>
                    </div>
                    <div class="list-box">
                        <div class="list">
                            <div class="col-sm-12 paddin-5">
                                <label><?php echo e(__('administrator.note')); ?></label>
                                <?php echo Form::textarea('note', null, ['placeholder'=>__('administrator.note'),'class' => 'form-control form-control no-resize','maxlength'=>'20','rows'=>'3']); ?>

                            </div>
                        </div>
                    </div>
                    <div class="col-sm-12 paddin-5">
                        <div class="button_payment">
                            <button type="submit"
                                    class="button_info button_lg pull-right"><?php echo e(__('administrator.submit')); ?></button>
                            <button type="button"
                                    class="button_danger btn_clear_all button_lg"><?php echo e(__('administrator.remove')); ?></button>
                            <button type="button" class="button_warning button_lg"
                                    onclick="history.back(1)"><?php echo e(__('administrator.back')); ?></button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php $__env->startSection('script'); ?>
    <script type="text/javascript">
        $(document).ready(function () {
            search_product();
            $(document).on('click', 'a.page-link', function (e) {
                e.preventDefault();
                let url = $(this).attr('href');
                search_product(url);
            });
            $("#append_product").on('input', '.qty', function (e) {
                let qty = toFloat($(this).val())
                let stock = toFloat($(this).closest('tr').find('.stock').val())
                if(qty>stock) $(this).val(stock)
            });
        });

        $('.typeahead').autocomplete({
            source: function (request, response) {
                let request_to = $('.request_to').val()

                $.ajax({
                    url: "<?php echo e(Route('request-stock-scan-code')); ?>",
                    dataType: "json",
                    method: "get",
                    data: {
                        barcode: request.term,
                        request_to,
                    },
                    success: function (rows) {
                        let check = 0;
                        let table = $('.barcode');
                        let name = rows.data.name;
                        let stock = rows.data.stock;
                        let size = rows.data.size;
                        let code = rows.data.code;
                        let color = rows.data.color;
                        let count = $('#append_product tr').length;
                        let $html = `
                        <tr>
                            <td><input type="hidden" class="barcode" value="` + code + `" name="barcode[]">` + (count + 1) + `</td>
                            <td>${name} ( ${size} ,${color})<br>${code}</td>
                            <td>
                                <input type="number" value="1" class="form-control input-sm qty number-only" name="qty[]" required/>
                                <input type="hidden" value="` + stock + `" class="stock" name="stock[]"/>
                            </td>

                            <td class="text-center"><span class="fa fa-times text-danger fa-2x  cursor remove_row"></span></td>
                        </tr>

                    `;
                        if (table.length > 0) {
                            $.each(table, function (i, v) {
                                if (parseFloat(v.value) === parseFloat(code)) {
                                    let qty = $(this).closest('tr').find('.qty').val();
                                    let add_mor = parseFloat(qty) + 1;
                                    if (parseFloat(add_mor) > parseFloat(stock)) {
                                        add_mor = stock;
                                        $.alert('Out of Stock');
                                    }
                                    $(this).closest('tr').find('.qty').val(add_mor);

                                    check++;
                                }
                            });
                            if (check === 0 && code) {
                                $('#append_product').append($html);
                            }
                        } else {
                            if (code) {
                                $('#append_product').append($html);
                            }
                        }
                        $(".typeahead").val('');
                        $(".typeahead").focus();
                    }
                });
            },
            minLength: 3,
        });

        $('.search').keyup(function () {
            search_product();
        });
        $('.request_to').on('change',function () {
            search_product();
        });
        $(document).on('click', '.listings', function () {
            let check = 0;
            let table = $('.barcode');
            let name = $(this).data('name');
            let stock = $(this).data('stock');
            let size = $(this).data('size');
            let code = $(this).data('code');
            let product_id = $(this).data('product_id');
            let color = $(this).data('color');
            let symbol = $(this).data('symbol');

            let count = $('#append_product tr').length;
            let $html = `
                        <tr data-symbol="` + symbol + `">
                            <td>
                            <input type="hidden" class="product_id" value="` + product_id + `" name="product_id[]">
                            <input type="hidden" class="barcode" value="` + code + `" name="barcode[]">` + (count + 1) + `</td>
                            <td>${name} (${size},${color}) <br>${code}</td>
                            <td>
                                <input type="number" value="1" class="form-control input-sm qty number-only" name="qty[]" required autocomplete="off" />
                                <input type="hidden" value="` + stock + `" class="stock" name="stock[]"/>
                            </td>

                            <td class="text-center"><span class="fa fa-times text-danger fa-2x  cursor remove_row"></span></td>
                        </tr>

                    `;
            if (table.length > 0) {
                $.each(table, function (i, v) {
                    if (parseFloat(v.value) === parseFloat(code)) {
                        let qty = $(this).closest('tr').find('.qty').val();
                        let add_mor = parseFloat(qty) + 1;
                        if (parseFloat(add_mor) > parseFloat(stock)) {
                            add_mor = stock;
                            $.alert('Out of Stock');
                        }
                        $(this).closest('tr').find('.qty').val(add_mor);
                        check++;
                    }
                });
                if (check === 0 && code) {
                    $('#append_product').append($html);
                }
            } else {
                if (code) {
                    $('#append_product').append($html);
                }
            }
            autoOff();
        });

        $(document).on('click', '.remove_row', function () {
            $(this).closest('tr').remove();

        });

        $(document).on('click', '.btn_clear_all ', function () {
            $.confirm({
                title: 'Are you sure ?',
                theme: 'modern',
                buttons: {
                    cancel: {
                        btnClass: 'button_warning',
                        text: 'Cancel'
                    },
                    ok: {
                        btnClass: 'button_primary',
                        text: 'OK',
                        action: function () {
                            $('#append_product').html('');

                        }
                    }
                }
            });
        });

        function search_product(url = "<?php echo e(route('request-stock.create')); ?>") {
            let request_to = $('.request_to').val()
            let value = $('.search').val();
            $.ajax({
                url: url,
                method: "get",
                data: {
                    value,
                    request_to,
                },
                success: function (response) {
                    $('.my-product-list').html(response.html);
                }
            });
        }
    </script>
<?php $__env->stopSection(); ?>
<?php /**PATH /home/nsowos/public_html/shr-brand/resources/views/stock-inventory/item-request/form.blade.php ENDPATH**/ ?>