<?php $__env->startSection('content'); ?>
    <?php
        Fun::lang();
    $type = request('postype') === 'rldf8e7sdf' ? 'retail' : 'wholesale';
    $type1 = request('postype') !== 'rldf8e7sdf' ? 'retail' : 'wholesale';
    $pos_request = request('sale_req');
    ?>

    <?php echo Form::open(array('route' => 'pos.store','method'=>'POST','enctype'=>'multipart/form-data','id'=>'submit_post','class'=>'jform')); ?>

    <div class="col-sm-12 col-md-12 ">
        <div class="" style="">
            <h2 class="pull-left">
                <b><?php echo e(__('administrator.pos')); ?> <?php echo e(request('sale_req')?__('administrator.request'):''); ?></b>
            </h2>
        </div>
    </div>
    <div class="col-xs-12 col-sm-12 col-md-5 col-lg-5" style="margin-top: 5px">
        <div class="col-xs-12 col-sm-12 col-md-5 col-lg-5">
            <div class="form-group">
                <?php echo Form::text('barcode', null, array('placeholder' => __('administrator.barcode') ,'class' =>
                'form-control typeahead barcode', "autocomplete" => "on")); ?>

            </div>
        </div>
        <div class="col-md-7 col-lg-7">
            <div class="input-group">
                <?php echo Form::text('search', null, array('placeholder' => __('administrator.search') ,'class' =>'form-control search')); ?>

                <span class="input-group-addon button_primary btn_search_pos">&nbsp;&nbsp;&nbsp;&nbsp;<i class="fa fa-search"></i>&nbsp;&nbsp;&nbsp;&nbsp;</span>
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <div class="switch-field">
                <?php
                $count_member_type = 0;
                foreach (getMemberType() as $id => $item) {
                    echo "
                         <input type='radio' id='radio-$id' name='member_type' value='$id' " . ($count_member_type === 0 ? 'checked' : '') . " />
                         <label for='radio-$id'>$item</label>
                        ";
                    $count_member_type++;
                }
                ?>
            </div>
        </div>
        <div class="product-item-list">
            <div class="item-list">
                <div class="row my-product-list">
                </div>
            </div>
        </div>
    </div>
    <?php
    $postype = request('postype') === 'rldf8e7sdf' ? 'retail' : 'wholesale';
    ?>
    <?php echo Form::hidden('sale_type',$postype); ?>

    <div class="col-xs-12 col-sm-12 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="parent-box-pos">
                            <table class="table table-hover item-list-tb ">
                                <thead class="text-center">
                                <tr>
                                    <th><?php echo e(__('administrator.item_name')); ?></th>
                                    <th style="width: 100px"><?php echo e(__('administrator.qty')); ?> <span class="red">*</span></th>
                                    <th><?php echo e(__('administrator.price')); ?><span class="red">*</span></th>
                                    <th>Off (<?php echo e(__('administrator.'.user_currency()->code)); ?>)</th>
                                    <th><?php echo e(__('administrator.subtotal')); ?></th>
                                    <th><?php echo e(__('administrator.total')); ?></th>
                                    <th>
                                        <button type="button" class="button_danger fa fa-trash" onclick="clear_table();"></button>
                                    </th>
                                </tr>
                                </thead>
                                <tbody id="append_product"></tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            <div class="blog-payment">
                <div class="row">
                    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 ">
                        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-6 ">
                            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 "><br></div>
                            <div class="col-xs-12 col-sm-12 col-md-12 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-xs-12 col-sm-12 col-md-6 col-lg-6 form-group">
                                <label class="required"><?php echo e(__('administrator.cashier')); ?></label>
                                <input type="text" name="cashier_name" class="form-control input-sm" readonly=""
                                       value="<?php echo e(findUser(Auth::user()->id)->name); ?>">
                            </div>
                            <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 form-group">
                                <label class="required"><?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-xs-12 col-sm-12 col-md-6 col-lg-6">
                                <label class="required"><?php echo e(__('administrator.customer')); ?> </label>
                                <div class="input-group">
                                    <?php echo Form::select('customer',$customer_array,$customer->id??null, ['placeholder'=>__('administrator.please_select'),'class' => 'form-control input-sm search_customer']); ?>

                                    <span class="input-group-addon button_primary add_customer" ><i class="fa fa-plus"></i></span>
                                </div>
                            </div>
                            <?php if($pos_request): ?>
                            <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
                                <label class="required"><?php echo e(__('administrator.request_to')); ?> </label>
                                <?php echo Form::select('branch_id',getBranches(),null, ['placeholder'=>__('administrator.please_select'),'class' => 'form-control input-sm select2']); ?>

                            </div>
                            <?php endif; ?>
                            <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6"><br>
                                <textarea name="note" rows="2" placeholder="<?php echo e(__('administrator.note')); ?>" class="form-control no-resize"></textarea>
                            </div>
                        </div>
                        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-6">
                            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 "><br></div>

                            <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 form-group">
                                <label><?php echo e(__('administrator.discount')); ?> (<?php echo e(__('administrator.'.user_currency()->code)); ?>)</label>
                                <?php echo Form::text('discount',0, array('placeholder'=>__('administrator.discount'),'class' => 'form-control input-sm number-only total-discount' ,'')); ?>

                            </div>
                            <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 form-group">
                                <label><?php echo e(__('administrator.payment_method')); ?></label>
                                <?php echo Form::select('payment_method_id', getPaymentMethod() , null , ['placeholder'=>__('administrator.please_select'),'class' => 'form-control select2']); ?>

                            </div>
                            <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 form-group">
                                <label><?php echo e(__('administrator.delivery_fee_by')); ?></label>
                                <?php echo Form::select('delivery_fee_by', getFeeBy() ,null, ['placeholder'=>__('administrator.please_select'),'class' => 'form-control']); ?>

                            </div>
                            <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 form-group">
                                <label><?php echo e(__('administrator.delivery_fee')); ?> (<?php echo e(__('administrator.'.user_currency()->code)); ?>)</label>
                                <?php echo Form::text('delivery_fee', 0 ,['placeholder'=>__('administrator.delivery_fee'),'class' => 'form-control number-only']); ?>

                            </div>
                            <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 form-group">
                                <label><?php echo e(__('administrator.more_fee')); ?></label>
                                <?php echo Form::number('more_fee' ,null, ['placeholder'=>__('administrator.more_fee'),'class' => 'form-control','step'=>'0.001']); ?>

                            </div>
                            <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 form-group">
                                <label><?php echo e(__('administrator.reason')); ?></label>
                                <?php echo Form::textarea('reason_fee', null,['placeholder'=>__('administrator.reason'),'class' => 'form-control','rows'=>1]); ?>

                            </div>
                            <div class="clearfix"></div>
                            <div class="col-xs-12 col-sm-12 col-md-6 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-xs-12 col-sm-12 col-md-6 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" class="g_total_hidden form-control input-sm" value="0" name="g_total_hidden">
                            </div>
                            <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
                                <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-xs-12 col-sm-12 col-md-6 col-lg-6">
                                <label class="change"><?php echo e(__('administrator.owed_amount')); ?> (<?php echo e(__('administrator.'.user_currency()->code)); ?>)</label>
                                <input data-status="0" type="text" class=" form-control input-sm" name="change-amount" readonly>
                            </div>
                            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12"><br></div>
                            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                                <button type="button" class="button_warning button_lg" onclick="window.location.replace('<?php echo e(route('sale-history.index')); ?>')"><?php echo e(__('administrator.back')); ?></button>
                                <button type="submit" class="button_info button_lg pull-right"><?php echo e(__('administrator.submit')); ?></button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php echo Form::close(); ?>

<?php $__env->stopSection(); ?>
<?php $__env->startSection('script'); ?>
    <script type="text/javascript">

        $('.barcode').keydown(function(event) {

            if (event.keyCode == 13) {

                event.preventDefault();
                return false;
            }
        });

        let tb = $('#append_product');
        $(document).ready(function () {
            search_product();
            $(document).on('click', 'a.page-link', function (e) {
                e.preventDefault();
                let url = $(this).attr('href');
                search_product(url);
            });

            $('.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');
                }
                paid = paid - g_total;
                $('input[name="change-amount"]').val('<?php echo e(user_currency()->symbol); ?>' + '' + Math.abs(parseFloat(paid).toFixed(2)));
            });

            $(".typeahead").autocomplete({
                source: function (request, response) {
                    let branch_id = $('input[name=branch_id]').val();
                    let member_type = $('input[name=member_type]:checked').val();

                    $.ajax({
                        url: "<?php echo e(Route('pos-scan-code')); ?>",
                        dataType: "json",
                        method: "get",
                        data: {
                            barcode: request.term,
                            member_type,
                            postype: '<?php echo e(request('postype')); ?>',
                            branch_id,
                        },
                        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 size = rows.data.size;
                            let color = rows.data.color;
                            let code = rows.data.code;
                            let price = rows.data.price;
                            let symbol = rows.data.symbol;
                            let $html = `
                        <tr data-symbol="` + symbol + `">
                            <td>${code}<br>${name} ( ${size},${color} )</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[]"/>
                                <input type="hidden" class="product_unit" value="` + id + `" name="product_unit[]">
                            </td>
                            <td><input type="text" <?php if(auth()->user()->user_type==='seller'|| !myBranch()->main): ?> readonly <?php endif; ?>  value="` + price + `"  class="form-control input-sm retail_price number-only " required name="retail_price[]" /></td>
                            <td><input type="text"  value="0" class="form-control input-sm disc number-only" name="dis[]" min="0" max="100" maxlength="3" /></td>
                            <td class="text-center"><span class="subtotal_item">` + (price) + symbol + `</span></td>
                            <td class="text-center"><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) {
                                    tb.append($html);
                                }
                            } else {
                                if (id) {
                                    tb.append($html);
                                }
                            }
                            get_total_all();
                            $(".typeahead").val('');
                            $(".typeahead").focus();
                        }
                    });
                },
                minLength: 3,
            });
            $(document).on('click', '.listings', 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 = $(this).data('symbol');
                let count = $('#append_product tr').length;
                let $html = `
                        <tr data-symbol="` + symbol + `">
                            <td>${code}<br>${name} ( ${size},${color} )</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[]"/>
                                <input type="hidden" class="product_unit" value="` + id + `" name="product_unit[]">
                            </td>
                            <td><input type="text"  <?php if(auth()->user()->user_type==='seller' || !myBranch()->main): ?> readonly <?php endif; ?> value="` + price + `"  class="form-control input-sm retail_price number-only " required name="retail_price[]" /></td>
                            <td><input type="text" value="0" class="form-control input-sm disc number-only" name="dis[]" /></td>
                            <td class="text-center"><span class="subtotal_item">` + (price) + symbol + `</span></td>
                            <td class="text-center"><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) {
                        tb.append($html);
                    }
                } else {
                    if (id) {
                        tb.append($html);
                    }
                }
                get_total_all();
            });

            $(document).on('click', '.remove_row', function () {
                $(this).closest('tr').remove();
                get_total_all();
            });
            $(document).on('input', 'input[name=member_type]', function (e) {
                tb.html('');
                get_total_all();
                search_product();
            });
            $(document).on('keyup', 'input[name=delivery_fee],input[name=more_fee]', function (e) {
                get_total_all();
            });
            $(document).on('change', 'select[name=delivery_fee_by]', function (e) {
                get_total_all();
            });
            $(document).on('click', '.add_customer', function () {
                getHtmlForm('<?php echo e(route('customer.create')); ?>?pos=1', '<?php echo e(__('administrator.create')); ?> <?php echo e(__('administrator.customer')); ?>', '600px')
            });
            $(document).on('keyup', '.number-only', function () {
                gen_item_total($(this));
            });
            $('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();
            });
            $('select[name=branch_id]').on('change', function () {
                search_product();
            });
        });

        function search_product(url = "<?php echo e(route('pos.create')); ?>") {
            let barcode = $('.search').val();
            let branch_id = $('select[name=branch_id]').val();
            let member_type = $('input[name=member_type]:checked').val();
            $.ajax({
                url: url,
                method: "get",
                data: {
                    barcode,
                    branch_id,
                    member_type,
                },
                success: function (response) {
                    $('.my-product-list').html(response.html);
                }
            });
        }

        function gen_item_total(this_) {
            let qty = this_.closest('tr').find('.qty').val();
            let price = this_.closest('tr').find('.retail_price').val();
            let disc = this_.closest('tr').find('.disc').val();
            let stock = this_.closest('tr').find('.stock').val();
            if (parseFloat(qty) > parseFloat(stock)) {
                qty = stock;
                this_.closest('tr').find('.qty').val(qty);
                $.alert('Out of Stock');
            }
            disc = disc ? disc : 0;
            let symbol = '<?php echo e(user_currency()->symbol); ?>';
            let stotal = qty * price;
            let total = stotal -  disc;
            this_.closest('tr').find('.subtotal_item').text(stotal.toFixed(2) + '' + symbol);
            this_.closest('tr').find('.total_item').text(total.toFixed(2) + '' + symbol);
            this_.closest('tr').find('.amount-hidden').val(total.toFixed(2));
            get_total_all();
        }

        function get_total_all() {
            let total = 0;
            let discount = parseFloat($('input[name=discount]').val());
            let delivery_fee = parseFloat($('input[name=delivery_fee ]').val());
            let delivery_by = parseFloat($('select[name=delivery_fee_by ]').val());
            let more_fee = toFloat($('input[name=more_fee]').val())
            discount = discount ? discount : 0;
            delivery_fee = delivery_fee ? delivery_fee : 0;
            let symbol = '<?php echo e(user_currency()->symbol); ?>';
            let total_qty_ = 0;
            $('.amount-hidden').each(function () {
                let value_total = parseFloat($(this).val());
                total += parseFloat(value_total);
                let qty = $(this).closest('tr').find('.qty').val();
                qty = qty ? qty : 0;
                total_qty_ += parseFloat(qty);
            });
            let g_total = total - discount +more_fee;
            if(delivery_by == '2'){
                g_total += delivery_fee;
            }



            $('.g_total').val(symbol + '' + addComma(g_total.toFixed(2)));

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

            $('.sub_total').val(symbol + '' + addComma(total.toFixed(2)));

            $('.total_qty_').val(total_qty_);

        }

        function clear_table() {
            $.confirm({
                title: 'ប្រាកដដែរឬទេ?',
                content: 'ផលិតផលដែលបានបញ្ចូលនិងត្រូវលុបចេញ៕',
                theme: 'modern',
                buttons: {
                    cancel: {
                        btnClass: 'button_warning',
                        text: 'Cancel',
                    },
                    ok: {
                        btnClass: 'button_primary',
                        text: 'OK',
                        action: function () {
                            tb.html('');
                            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/pos/clothes-pos/create.blade.php ENDPATH**/ ?>