 <?php $__env->startSection('content'); ?>
    <?php Fun::lang() ?>
    <?php
        $hidden = 'hidden';
    ?>
    <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('Show Purchase Material 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-lg-12">
                    <div class="input-group">
                        <?php echo Form::text('barcode', null, array('placeholder' => __('administrator.search_product_name') ,'class' => 'form-control typeahead', "autocomplete" => "on")); ?>

                        <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-material.store','method'=>'POST','enctype'=>'multipart/form-data','id'=>'submit_post','class'=>'jform')); ?>

            <div class="col-xs-12 col-sm-7 col-md-7 col-lg-7">
                <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><?php echo e(__('administrator.unit')); ?></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 $currency = user_currency()->code; ?>
                    <div class="blog-payment" style="margin-top: 20px">
                        <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.'.user_currency()->code)); ?> ):</label>
                                        <input type="text" class="sub_total form-control input-sm"
                                               value="<?php echo e(0,2); ?> ៛"
                                               readonly="">
                                    </div>
                                    <div class="col-sm-6 paddin-5">
                                        <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="<?php echo e(0); ?>"
                                               readonly="">
                                        <input type="hidden" class="g_total_hidden form-control input-sm" value="<?php echo e(0); ?>"
                                               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.'.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-sm-6 paddin-5">
                                        <label class="change"><?php echo e(__('administrator.owed_amount')); ?>

                                            (<?php echo e(__('administrator.'.user_currency()->code)); ?> )</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'),'required']); ?>

                                            <span class="input-group-addon btn_add_supplier" id="addnew"><i class="fa fa-plus"></i></span>
                                        </div>
                                    </div>
                                    <div class="col-sm-6 paddin-5 add-supplier">
                                        <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>
                            </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','required')); ?>

                                    </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 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>
                            </div>
                            <div class="col-sm-12 paddin-5">
                                <div class="button_payment">
                                    <input type="submit" class="button_info button_lg pull-right" value="<?php echo e(__('administrator.purchase')); ?>">
                                    <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>
                <?php echo Form::close(); ?>

            </div>
        </div>
    </div>


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

            $.ajax({
                url: "<?php echo e(route('purchases-material.create')); ?>",
                method: "get",
                data: {
                    value: value,
                },
                success: function (response) {
                    $('.my-product-list').html(response);
                }
            });
        }
        $(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 supplier_name = $(document).find('.supplier_name').val();
                                let supplier_phone = $(document).find('.supplier_phone').val();
                                $.ajax({
                                    type: 'GET',
                                    url: '<?php echo e(route('add-new-supplier-for-material')); ?>',
                                    data: {
                                        supplier_name,
                                        supplier_phone,
                                    },
                                    dataType: 'json',
                                    success: function (data) {
                                        $('#addnew').modal('hide');
                                        $('.get_supplier').append('<option value="' + data.supplier.id + '">' + data.supplier.supplier_name + '</option>').val(data.supplier.id);
                                        $.alert('supplier Added');
                                    }
                                });
                            }
                        }

                    }
                });
            });
            get_purchase_addcart();
            $(document).on('change', '.select_color', function () {
                update_data($(this));
            });
            $(document).on('change', '.select_unit', function () {
                update_data($(this));
            });
            $("#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');
                $.ajax({
                    url: url,
                    data: {},
                    dataType: 'html',
                    success(data) {
                        $('.my-product-list').html(data);
                    }
                });
            });
        });

        $(document).on('click', '.add-product', function () {
            let id = $(this).data('id');
            $.ajax({
                url: "<?php echo e(route('purchases-material-addcart')); ?>",
                method: "post",
                data: {
                    value: id,
                    _token: '<?php echo e(csrf_token()); ?>',
                },
                dataType: 'json',
                success: function (response) {
                    if (response === 'success') {
                        get_purchase_addcart();
                    } else {
                        $.alert(response);
                    }
                }
            });
            $('input[name=paid]').val(0);
            $('input[name=change-amount]').val(0);

        });

        function get_purchase_addcart() {
            clear_number();
            $.ajax({
                url: "<?php echo e(route('get-purchases-material-addcart')); ?>",
                method: "get",
                dataType: 'text',
                success: function (response) {
                    $('#append_product').html(response);
                    get_total_all();
                    $('input').attr('autocomplete','off');
                    $('select').select2();
                }
            });
        }

        $(document).on('click', '.purchase_removecart', function () {
            let id = $(this).data('id');
            $.ajax({
                url: '<?php echo e(route("purchases-material-removecart")); ?>',
                method: "POST",
                dataType: 'json',
                data: {
                    _token: '<?php echo e(csrf_token()); ?>',
                    id: id
                },
                success: function (response) {

                    if (response['status'] == 'success') {
                        get_purchase_addcart();
                    } else {
                        alert(response['status']);
                    }
                }
            });
        });

        $(document).on('blur', '.item-list-tb .number-only', function () {
            clear_number();
            update_data($(this));
        });

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

        function get_total_all() {
            let total = 0;
            let total_qty_ = 0;
            let discount = parseFloat($('input[name=discount]').val());
            discount = discount ? discount : 0;

            $('.amount-hidden').each(function () {
                let value_total = parseFloat($(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(parseFloat(g_total).toFixed(2));

            $('.g_total_hidden').val(parseFloat(g_total));

            $('.sub_total').val(parseFloat(total).toFixed(2));
            $('.total_qty_').val(parseFloat(total_qty_));

        }

        function update_data($this) {
            if ($('#append_product tr').length > 0) {
                let cost = $this.closest('tr').find('.cost').val();
                let qty = $this.closest('tr').find('.qty').val();
                let value = $this.closest('tr').find('.product_id').val();
                let discount = $this.closest('tr').find('.dis').val();
                let unit = $this.closest('tr').find('.select_unit').val();
                let barcode = $this.closest('tr').find('.select_unit').val();
                let count_row = $this.closest('tr').find('.count_row').val();
                $.ajax({
                    url: "<?php echo e(route('purchases-material-addcart')); ?>",
                    method: "POST",
                    dataType: 'json',
                    data: {
                        _token: '<?php echo e(csrf_token()); ?>',
                        qty: qty,
                        cost: cost,
                        value: value,
                        count_row: count_row,
                        discount: discount,
                        unit_id: unit,
                        barcode: barcode,
                        con: 1,
                    },
                    success: function (response) {
                        if (response === 'update') {
                            get_purchase_addcart();
                        } else {
                            alert(response);
                        }
                    }
                });
            }
        }

        function clear_number() {
            $('.blog-payment .number-only').val(0);
            $('.g_total').val(0);
            $('input[name=change-amount]').val(0);
        }

        $(document).on('keyup', '.paid', function () {
            let g_total = parseFloat($('.g_total_hidden').val());
            let total = parseFloat($('.paid').val() ? $('.paid').val() : 0);
            if (g_total <= total) {
                $('.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');
            }
            total = total - g_total;
            $('input[name="change-amount"]').val('<?php echo e(user_currency()->symbol); ?>' + Math.abs(parseFloat(total).toFixed(2)));
        });
        $(document).on('click', '.purchase_remove_session ', function () {
            if (confirm("Are you sure")) {
                $.ajax({
                    url: '<?php echo e(route("purchases-material-remove-session")); ?>',
                    method: "POST",
                    dataType: 'json',
                    data: {
                        _token: '<?php echo e(csrf_token()); ?>'
                    },
                    success: function (response) {
                        $('#append_product').html('');
                        $('.g_total').val(parseFloat(0).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,'));

                        $('.sub_total').val(parseFloat(0).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,'));
                    }
                });
                $('#append_product').html('');
            }
        });


    </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/material/create.blade.php ENDPATH**/ ?>