 <?php $__env->startSection('content'); ?>
    <?php Fun::lang() ?>
    <?php
        $hidden = 'hidden';
    ?>
    <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('Show Purchase Item 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-md-12 col-lg-12">
                    <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" id="basic-addon1">&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" style="margin-top: 10px">
                        <div class="row my-product-list"></div>
                    </div>
                </div>
            </div>
            <?php echo Form::open(array('route' => 'purchases-item.store','method'=>'POST','enctype'=>'multipart/form-data','id'=>'submit_post','class'=>'jform')); ?>

            <div class="col-md-7 col-lg-7 col-xl-7">
                <div class="col-md-12 col-lg-12 col-xl-12" style="height: 55vh;border: 1px solid rgba(1,1,1,0.2);padding: 0;overflow-y: scroll;border-radius: 5px">
                    <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 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 class="col-md-12 col-lg-12 col-xl-12" style="margin-top: 20px">
                    <div class="col-md-6 col-lg-6 col-xl-6 <?php echo e($hidden); ?>">
                        <div class="col-lg-6 form-group">
                            <label><?php echo e(__('administrator.total')); ?> ( <?php echo e(__('administrator.'.user_currency()->code)); ?> ):</label>
                            <input type="text" class="sub_total form-control input-sm" value="0" readonly>
                        </div>
                        <div class="col-lg-6 form-group">
                            <label><?php echo e(__('administrator.grand_total')); ?> ( <?php echo e(__('administrator.'.user_currency()->code)); ?> ):</label>
                            <input type="text" class="g_total form-control input-sm" value="0" readonly="">
                            <input type="hidden" name="g_total_hidden" class="g_total_hidden" value="0" readonly="">
                        </div>
                        <div class="col-lg-6 form-group">
                            <label><?php echo e(__('administrator.paid')); ?> ( <?php echo e(__('administrator.'.user_currency()->code)); ?> ) :</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-lg-6 form-group">
                            <label class="change"><?php echo e(__('administrator.owed_amount')); ?> ( <?php echo e(__('administrator.'.user_currency()->code)); ?> )</label>
                            <input data-status="0" type="text"
                                   class="bg-transparent border-none form-control input-sm"
                                   name="change-amount" readonly>
                        </div>
                    </div>
                    <div class=" <?php echo e($hidden?'col-md-12 col-lg-12 col-xl-12':'col-md-6 col-lg-6 col-xl-6'); ?>">
                        <div class="col-lg-12 form-group">
                            <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 class="col-lg-6 form-group">
                            <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'),'required']); ?>

                                <span id="addnew" class="input-group-addon button_info btn_add_supplier"><i class="fa fa-plus"></i></span>
                            </div>
                        </div>
                        <div class="col-lg-6 form-group">
                            <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','required']); ?>

                        </div>
                        <div class="col-lg-6 form-group">
                            <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-lg-6 form-group">
                            <label><?php echo e(__('administrator.invoice')); ?> :</label>
                            <?php echo Form::text('invoice_number',null, array('placeholder' =>__('administrator.invoice_number'),'class' => 'form-control input-sm','required')); ?>

                        </div>
                        <div class="col-lg-6 form-group">
                            <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-lg-6 form-group add-supplier">
                            <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 class="col-lg-12">
                            <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>
                <?php echo Form::close(); ?>

            </div>
        </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();
            });
            $('.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-item')); ?>',
                                    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();

                                    }
                                });
                            }
                        }

                    }
                });
            });
            $("#submit_post").submit(function () {
                let item = $('#append_product tr').length ? $('#append_product tr').length : 0;
                if (item <= 0) {
                    $.confirm({
                        title: 'No Item Selected!',
                        content: 'Please Select Any Items Before Save Data',
                        type: 'red',
                        typeAnimated: true,
                        buttons: {
                            tryAgain: {
                                text: 'Try again',
                                btnClass: 'btn-red',
                                action: function () {
                                }
                            },
                        }
                    });
                    return false;
                } else {
                    return true;
                }
            });
            $(document).on('click', 'a.page-link', function (e) {
                e.preventDefault();
                let url = $(this).attr('href');
                search_product(url);
            });

            $(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 size = $(this).data('size');
                let color = $(this).data('color');
                let code = $(this).data('code');
                let price = $(this).data('price');
                let symbol = '<?php echo e(user_currency()->symbol); ?>';
                let count = $('#append_product tr').length;
                let $html = `
                        <tr data-symbol="` + symbol + `">
                            <td><input type="hidden" class="product_unit" value="` + id + `" name="product_unit[]">` + (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 step="0.001" />
                            </td>
                            <td class="<?php echo e($hidden); ?> "><input type="number"  value="${price}"  class=" form-control input-sm cost number-only " step="0.0001" 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">${price} ${symbol}</span></td>
                            <td class="text-center <?php echo e($hidden); ?> "><span class="total_item">${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();
                autoOff();
            });

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


        function search_product($url = "<?php echo e(route('purchases-item.create')); ?>") {
            let barcode = $('.search').val();
            $.ajax({
                url: $url,
                method: "get",
                data: {
                    barcode,
                },
                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());
                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_usd = parseFloat($('.g_total_hidden').val());
            let paid = parseFloat($('.paid').val() ? $('.paid').val() : 0);

            if (g_total_usd <= paid) {
                $('.change').text('<?php echo e(__('administrator.change_amount')); ?>').removeClass('text-red').addClass('text-green');
            } else {
                $('.change').text('<?php echo e(__('administrator.owed_amount')); ?>').removeClass('text-green').addClass('text-red');
            }
            let total_usd = paid - g_total_usd;
            $('input[name="change-amount"]').val('<?php echo e(user_currency()->symbol); ?>' + addComma(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 = this_.closest('tr').find('.qty').val();
            let price = this_.closest('tr').find('.cost').val();
            let disc = this_.closest('tr').find('.disc').val();
            disc = disc ? disc : 0;
            let symbol = this_.closest('tr').data('symbol');
            let stotal = qty * 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/item/create.blade.php ENDPATH**/ ?>