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

    <?php Fun::lang() ?>
    <?php
        $hidden = 'hidden';
    ?>
    <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('Show Purchase Fabric Price')): ?>
        <?php
            $hidden = '';
        ?>
    <?php endif; ?>
    <div class="page-purchases">
        <div class="preview">
            <div class="col-xs-12 col-sm-12 col-md-5 col-lg-5 ">
                <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
                    <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-xs-12 col-sm-12 col-md-6 col-lg-6">
                    <div class="input-group">
                        <?php echo Form::text('search', null, array('placeholder' => __('administrator.search') ,'class' => 'form-control search')); ?>

                        <span class="input-group-addon button_info btn_search_pos">&nbsp;&nbsp;&nbsp;&nbsp;<i class="fa fa-search"></i>&nbsp;&nbsp;&nbsp;&nbsp;</span>
                    </div>
                </div>
                <div class="product-item-list col-xs-12 col-sm-12 col-md-12 col-lg-12">
                    <div class="item-list">
                        <div class="row my-product-list">
                        </div>
                    </div>
                </div>
            </div>
            <?php echo Form::open(array('route' => 'purchases-fabric.store','method'=>'POST','enctype'=>'multipart/form-data','class'=>'jform')); ?>

            <div class="col-sm-7 paddin-5">
                <div class="check-bill">
                    <div class="row">
                        <div class="check-bill-table">
                            <div class="product-view-table">
                                <div class="cart-list">
                                    <table class="table table-hover item-list-tb "
                                           style="border: 1px solid rgba(1,1,1,0.3)">
                                        <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>U/M <span class="red">*</span></th>
                                            <th class="<?php echo e($hidden); ?>"><?php echo e(__('administrator.cost')); ?><span class="red">*</span></th>
                                            <th class="<?php echo e($hidden); ?>"><?php echo e(__('administrator.discount')); ?>(%)</th>
                                            <th class="<?php echo e($hidden); ?>"><?php echo e(__('administrator.total')); ?></th>
                                            <th class="<?php echo e($hidden); ?>"><?php echo e(__('administrator.amount')); ?></th>
                                            <th><?php echo e(__('administrator.action')); ?></th>
                                        </tr>
                                        </thead>
                                        <tbody id="append_product"></tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php $symbol = user_currency()->code; ?>
                    <div class="blog-payment">
                        <div class="col-md-12"><br></div>
                        <div class="col-sm-6 <?php echo e($hidden); ?>">
                            <div class="list-box">
                                <div class="list">
                                    <div class="col-sm-6 paddin-5">
                                        <label><?php echo e(__('administrator.total')); ?> (<?php echo e(__('administrator.'.$symbol)); ?>)</label>
                                        <input type="text" class="sub_total form-control input-sm" value="0.00 ៛" readonly="">
                                    </div>
                                    <div class="col-sm-6 paddin-5">
                                        <label><?php echo e(__('administrator.grand_total')); ?> (<?php echo e(__('administrator.'.$symbol)); ?>)</label>
                                        <input type="text" class="g_total form-control input-sm" value="0.00 ៛" readonly="">
                                        <input type="hidden" class="g_total_hidden" name="g_total_hidden">
                                    </div>
                                </div>
                            </div>
                            <div class="list-box">
                                <div class="list">
                                    <div class="col-sm-6 paddin-5">
                                        <label><?php echo e(__('administrator.paid')); ?> (<?php echo e(__('administrator.'.$symbol)); ?>)</label>
                                        <input type="text"
                                               class="form-control input-sm number-only border bg-transparent paid"
                                               name="paid" value="" placeholder="0.00">
                                    </div>
                                    <div class="col-sm-6 paddin-5">
                                        <label class="change"><?php echo e(__('administrator.owed_amount')); ?>

                                            (<?php echo e(__('administrator.'.$symbol)); ?>)</label>
                                        <input type="text" value="0"
                                               class="bg-transparent border-none form-control input-sm"
                                               name="change-amount" readonly>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class=" <?php echo e($hidden?'col-sm-12':'col-sm-6'); ?>">
                            <div class="list-box">
                                <div class="list">
                                    <div class="col-sm-12 paddin-5">
                                        <label><?php echo e(__('administrator.total_qty')); ?> :</label>
                                        <?php echo Form::text('total_qty', 0, ['class' => 'form-control input-sm total_qty_','placeholder'=>__('administrator.total_qty'),'readonly']); ?>

                                    </div>
                                </div>
                            </div>
                            <div class="list-box">
                                <div class="list">
                                    <div class="col-sm-6 paddin-5">
                                        <label><?php echo e(__('administrator.supplier')); ?> :</label>
                                        <div class="input-group">
                                            <?php echo Form::select('supplier_id', $suppliers, null, ['class' => 'form-control input-sm select2 get_supplier','placeholder'=>__('administrator.please_select')]); ?>

                                            <span class="input-group-addon cursor btn_add_supplier" >
                                            <span class="fa fa-plus"></span>
                                        </span>
                                        </div>
                                    </div>
                                    <div class="col-sm-6 paddin-5">
                                        <label><?php echo e(__('administrator.warehouse')); ?> :</label>
                                        <?php echo Form::select('warehouse_id', $warehouse, null, ['placeholder'=>__('administrator.please_select'),'class' => 'form-control input-sm select2','id'=>'warehouse_id']); ?>

                                    </div>
                                </div>
                            </div>
                            <div class="list-box">
                                <div class="list">
                                    <div class="col-sm-6 paddin-5">
                                        <label><?php echo e(__('administrator.discount')); ?> (%):</label>
                                        <?php echo Form::text('discount',null, array('placeholder' =>__('administrator.discount'),'class' => 'form-control input-sm number-only total-discount' ,'')); ?>

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

                                    </div>
                                </div>
                            </div>
                            <div class="list-box">
                                <div class="list">
                                    <div class="col-sm-6 paddin-5">
                                        <label><?php echo e(__('administrator.date')); ?> :</label>
                                        <input type="text" value="<?php echo e(date('d-m-Y')); ?>"
                                               class="date_picker form-control input-sm" name="date">
                                    </div>
                                    <div class="col-sm-6 paddin-5">
                                        <label><?php echo e(__('administrator.cashier')); ?> :</label>
                                        <input type="text" name="cashier_name" class="form-control input-sm" readonly=""
                                               value="<?php echo e(Auth::user()->name); ?>">
                                    </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.purchase')); ?></button>
                                   <button type="button" class="button_danger purchase_remove_session 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>
            <?php echo Form::close(); ?>


        </div>
    </div>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('script'); ?>
    <script type="text/javascript">
        $(document).ready(function () {
            search_product();
            $('input[name=search]').keypress(function (e) {
                // e.preventDefault();
                var key = e.which;
                if(key == 13)  // the enter key code
                {
                    search_product();
                }
            });
            $('.btn_search_pos').on('click',function () {
                search_product();
            });
        });


        $(document).ready(function () {
            $('.btn_add_supplier').on('click', function () {
                $.confirm({
                    title: 'Add Supplier',
                    theme: 'modern',
                    content: '<input type="text" name="supplier_name" class="form-control supplier_name"required placeholder="<?php echo e(__('administrator.supplier_name')); ?>" maxlength="40" />&nbsp;&nbsp;&nbsp;<input  class="form-control supplier_phone"required placeholder="<?php echo e(__('administrator.supplier_phone')); ?>" maxlength="40" type="text" name="supplier_phone" />',
                    buttons: {
                        cancel: {
                            text: 'Cancel',
                            btnClass: 'button_warning',

                        },
                        submit: {
                            text: 'Submit',
                            btnClass: 'button_primary',
                            action: function () {
                                let name = $(document).find('.supplier_name').val();
                                let phone = $(document).find('.supplier_phone').val();
                                $.ajax({
                                    type: 'GET',
                                    url: '<?php echo e(route('add-new-supplier-for-fabric')); ?>',
                                    data: {
                                        supplier_name: name,
                                        supplier_phone: phone,
                                    },
                                    dataType: 'json',
                                    success: function (data) {
                                        $('#addnew').modal('hide');
                                        $('.get_supplier').append('<option value="' + data.supplier.id + '">' + data.supplier.supplier_name + '</option>');
                                        $.alert('supplier Added');
                                        $('.get_supplier').val(data.supplier.id).change();
                                    }
                                });
                            }
                        }
                    }
                });
            });
            $(document).on('click', 'a.page-link', function (e) {
                e.preventDefault();
                let url = $(this).attr('href');
                $.ajax({
                    url: url,
                    data: {},
                    dataType: 'html',
                    success(data) {
                        $('.my-product-list').html(data);
                    }
                });
            });
        });

        $(".typeahead").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "<?php echo e(Route('purchases-fabric-scan-code')); ?>",
                    dataType: "json",
                    method: "get",
                    data: {
                        barcode: request.term,
                    },
                    success: function (rows) {
                        let check = 0;
                        let table = $('.product_unit');
                        let id = rows.data.id;
                        let name = rows.data.name;
                        let stock = rows.data.stock;
                        let unit = rows.data.unit;
                        let code = rows.data.code;
                        let price = rows.data.price;
                        let symbol = rows.data.symbol;
                        let qty_per_unit = rows.data.qty_per_unit;
                        let count = $('#append_product tr').length;
                        price = price?price:0;
                        let total_price = parseFloat(qty_per_unit) * parseFloat(price);

                        let $html = `
                        <tr data-symbol="` + symbol + `">
                            <td>
                            <input type="hidden" class="product_unit" value="` + id + `" name="product_unit[]">
                            <input type="hidden" class="qty_per_unit" value="` + qty_per_unit + `" name="qty_per_unit[]">
                            ` + (count + 1) + `</td>
                           <td>` + code + ` (` + qty_per_unit + ` ` + unit + `)<br>` + name + `</td>
                            <td>
                                <input type="number" value="1" class="form-control input-sm qty number-only" name="qty[]" required/>
                            </td>
                             <td>
                                <input type="number" step="0.001" value="1" class="form-control input-sm qty_per_unit number-only" name="qty_per_unit[]" required />
                            </td>
                            <td class="<?php echo e($hidden); ?>"><input type="number" step="0.001" value="` + price + `"  class="form-control input-sm cost number-only " required name="cost[]" /></td>
                            <td class="<?php echo e($hidden); ?>"><input type="number" value="0" class="form-control input-sm disc number-only" name="dis[]" min="0" max="100" maxlength="3" /></td>
                            <td class="text-center <?php echo e($hidden); ?>"><span class="subtotal_item">` + (total_price) + symbol + `</span></td>
                            <td class="text-center <?php echo e($hidden); ?>"><span class="total_item">` + (total_price) + symbol + `</span><input type="hidden" class="amount-hidden" name="amount_hidden" value="` + (price) + `"></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(id)) {
                                    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);
                                    gen_item_total($(this));
                                    check++;
                                }
                            });
                            if (check === 0 && id) {
                                $('#append_product').append($html);
                            }
                        } else {
                            if (id) {
                                $('#append_product').append($html);
                            }
                        }
                        get_total_all();
                        $(".typeahead").val('');
                        $(".typeahead").focus();
                    }
                });
            },
            minLength: 3,
        });

        $(document).on('click', '.add-product', function () {
            let id = $(this).data('id');
            let check = 0;
            let table = $('.product_unit');
            let name = $(this).data('name');
            let stock = $(this).data('stock');
            let code = $(this).data('code');
            let price = $(this).data('price');
            let qty_per_unit = $(this).data('qty_per_unit');
            let symbol = '<?php echo e(user_currency()->symbol); ?>';
            let count = $('#append_product tr').length;
            let total_price = parseFloat(qty_per_unit?qty_per_unit:1) * parseFloat(price);
            let $html = `
                        <tr data-symbol="` + symbol + `">
                            <td>
                            <input type="hidden" class="product_id" value="` + id + `" name="product_id[]">` + (count + 1) + `
                            </td>
                            <td>` + name + `<br>` + code + `</td>
                            <td>
                                <input type="number"  step="0.001" value="1" class="form-control input-sm qty number-only" name="qty[]" required/>
                            </td>
                            <td>
                                 <input type="number" step="0.001" value="1" class="form-control input-sm qty_per_unit number-only" name="qty_per_unit[]" required />
                            </td>
                            <td class="<?php echo e($hidden); ?>"><input  step="0.001" type="number"  value="` + (price?price:0) + `"  class="form-control input-sm cost number-only " required name="cost[]" /></td>
                            <td class="<?php echo e($hidden); ?>"><input  step="0.001" type="number" value="0" class="form-control input-sm disc number-only" name="dis[]" min="0" max="100" maxlength="3" /></td>
                            <td class="text-center <?php echo e($hidden); ?>"><span class="subtotal_item">` + (total_price?total_price:0) + symbol + `</span></td>
                            <td class="text-center <?php echo e($hidden); ?>"><span class="total_item">` + (total_price?total_price:0) + symbol + `</span><input type="hidden" class="amount-hidden" name="amount_hidden" value="` + (total_price?total_price:0) + `"></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(id)) {
                        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);
                        gen_item_total($(this));
                        check++;
                    }
                });
                if (check === 0 && id) {
                    $('#append_product').append($html);
                }
            } else {
                if (id) {
                    $('#append_product').append($html);
                }
            }
            get_total_all();
            $('input').attr('autocomplete','off');

        });

        $(document).on('click', '.remove_row', function () {
            $(this).closest('tr').remove();
            get_total_all();
        });
        function search_product() {
            let value = $('.search').val();
            $.ajax({
                url: "<?php echo e(route('purchases-fabric.create')); ?>",
                method: "get",
                data: {
                    barcode: value,
                    _token: '<?php echo e(csrf_token()); ?>',
                },
                success: function (response) {
                    $('.my-product-list').html(response);
                }
            });
        }
        function get_total_all() {
            let total= 0;
            let discount = parseFloat($('input[name=discount]').val());
            discount = discount ? discount : 0;
            let total_qty_ = 0;
            $('.amount-hidden').each(function () {
                let value_total = ($(this).val().replace(/\,/g, ''));
                let qty = $(this).closest('tr').find('.qty').val();
                qty = qty ? qty : 0;
                total_qty_ += parseFloat(qty);
                total += parseFloat(value_total);
            });
            let g_total = total - (total * discount / 100);
            $('.g_total').val(addComma(parseFloat(g_total).toFixed(2)));
            $('.g_total_hidden').val(parseFloat(g_total));
            $('.sub_total').val(addComma(parseFloat(total).toFixed(2)));
            $('.total_qty_').val(parseFloat(total_qty_));

        }

        $('.total-discount').on('keyup', function () {
            get_total_all();
        });

        $(document).on('keyup', '.paid', function () {
            let g_total = parseFloat($('.g_total_hidden').val());
            let paid = parseFloat($('.paid').val() ? $('.paid').val() : 0);

            if (g_total <= paid) {
                $('.change').text('<?php echo e(__('administrator.change_amount')); ?>');
                $('.change').removeClass('text-red');
                $('.change').addClass('text-green');
            } else {
                $('.change').text('<?php echo e(__('administrator.owed_amount')); ?>');
                $('.change').removeClass('text-green');
                $('.change').addClass('text-red');
            }
            let total_usd = paid - g_total;
            $('input[name="change-amount"]').val('<?php echo e(user_currency()->symbol); ?>' + Math.abs(parseFloat(total_usd).toFixed(2)));
        });

        $(document).on('click', '.purchase_remove_session ', 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('');
                            get_total_all();
                        }
                    }
                }
            });
        });

        $(document).on('keyup', '.number-only', function () {
            gen_item_total($(this));
        });

        function gen_item_total(this_) {
            let qty_per_unit = this_.closest('tr').find('.qty_per_unit').val();
            qty_per_unit = qty_per_unit ? qty_per_unit : 0;
            let qty = this_.closest('tr').find('.qty').val();
            qty = qty ? qty : 0;
            let price = this_.closest('tr').find('.cost').val();
            price = price ? price : 0;

            let disc = this_.closest('tr').find('.disc').val();
            disc = disc ? disc : 0;
            disc = disc ? disc : 0;
            let symbol = this_.closest('tr').data('symbol');
            let total_price = parseFloat(qty_per_unit) * parseFloat(qty);
            let stotal = total_price * price;
            let total = stotal - (stotal * disc / 100);
            this_.closest('tr').find('.subtotal_item').text(addComma(stotal.toFixed(2)) + '' + symbol);
            this_.closest('tr').find('.total_item').text(addComma(total.toFixed(2)) + '' + symbol);
            this_.closest('tr').find('.amount-hidden').val(total);
            get_total_all();
        }
    </script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make(branchMenu(), \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/nsowos/public_html/shr-brand/resources/views/stock-inventory/purchases/fabric/create.blade.php ENDPATH**/ ?>