@extends(branchMenu())
@section('content')
    <?php Fun::lang();
    $type = request('postype') === 'rldf8e7sdf' ? 'retail' : 'wholesale';
    $pos_request = request('sale_req');

    ?>

    <div class="page-pos_clothess">
        <div class="preview">
            <div class="col-sm-12 col-md-12 ">
                <div class="" style="">
                    <h2 class="pull-left">
                        <b>{{__('administrator.'.$type)}} {{ request('sale_req')?__('administrator.request'):'' }}</b>
                    </h2>
                </div>
            </div>
            <div class="col-sm-5" style="margin-top: 5px">
                <div class="col-sm-5">
                    <div class="form-group">
                        {!! Form::text('barcode', null, array('placeholder' => __('administrator.barcode') ,'class' => 'form-control typeahead', "autocomplete" => "on")) !!}
                    </div>
                </div>
                <div class="col-sm-5">
                    <div class="form-group">
                        {!! Form::text('search', null, array('placeholder' => __('administrator.search') ,'class' => 'form-control search')) !!}
                    </div>
                </div>
                <div class="col-sm-2">
                    <div class="form-group">
                        <button type="button" class="button_info btn_search_pos">{{__('administrator.search')}}</button>
                    </div>
                </div>
                <div class="product-item-list">
                    <div class="item-list">
                        <div class="row my-product-list">
                        </div>
                    </div>
                </div>
            </div>
            {!! Form::model($master, ['route' => ['sale-request.update', $master->id],'files'=>true,'class'=>'jform','method'=>'PATCH']) !!}
            <?php
            $postype = request('postype') === 'rldf8e7sdf' ? 'retail' : 'wholesale';

            $currency = findCurrency($master->currency_id);

            ?>
            {!! Form::hidden('sale_type',$postype) !!}
            <div class="col-sm-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>#</th>
                                            <th>{{__('administrator.item_name')}}</th>
                                            <th>{{__('administrator.qty')}} <span class="red">*</span></th>
                                            <th>{{__('administrator.price')}}<span class="red">*</span></th>
                                            <th>{{__('administrator.discount')}}(%)</th>
                                            <th>{{__('administrator.subtotal')}}</th>
                                            <th>{{__('administrator.total')}}</th>
                                            <th>{{__('administrator.action')}}</th>
                                        </tr>
                                        </thead>
                                        <tbody id="append_product">
                                        @foreach($master->detail as $key=>$row)

                                            <tr data-symbol="{{$currency->symbol??''}}">
                                                <td><input type="hidden" class="barcode_item" value="{{$row->barcode}}" name="barcode_item[]">{{$key+1}}</td>
                                                <td>{{$row->product->product_name??''}} ( {{$row->size->size??''}},{{$row->color->name??''}} )<br>{{$row->barcode}}</td>
                                                <td>
                                                    <input type="number" value="{{$row->qty}}" class="form-control input-sm qty number-only" name="qty[]" required/>
                                                    <input type="hidden" value="99999" class="stock" name="stock[]"/>
                                                </td>
                                                <td><input type="text"  value="{{$row->sale_price}}"  class="form-control input-sm retail_price number-only " required name="retail_price[]" /></td>
                                                <td><input type="text" value="{{$row->dis??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">0</span></td>
                                                <td class="text-center"><span class="total_item">0</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>
                                        @endforeach
                                        </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-12 "><br></div>
                                <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 ">
                                    <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 form-group">
                                        <label class="required">{{__('administrator.cashier')}}</label>
                                        <input type="text" name="cashier_name" class="form-control input-sm" readonly
                                               value="{{findUser($master->user_id)->name}}">
                                    </div>
                                    <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 form-group">
                                        <label class="required">{{__('administrator.date')}}</label>
                                        <input type="text" value="{{myDate($master->sale_date)}}"
                                               class="date_picker form-control input-sm" name="date">
                                    </div>
                                    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
                                        <label class="required">{{__('administrator.customer')}} </label>
                                        <?php
                                          $customer = findCustomer($master->customer_id);
                                        ?>
                                        {!! Form::select('customer',[$customer->id=>$customer->name],$customer->id, ['placeholder'=>__('administrator.please_select'),'class' => 'form-control input-sm search_customer']) !!}
                                        <button type="button" class="btn btn-xs btn btn-primary add_customer"><i class="fa fa-plus"></i></button>
                                    </div>
                                    @if($pos_request)
                                        <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
                                            <label class="required">{{__('administrator.request_to')}} </label>
                                            {!! Form::select('branch_id',getBranches(),null, ['placeholder'=>__('administrator.please_select'),'class' => 'form-control input-sm select2']) !!}
                                        </div>
                                    @endif
                                    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12"><br>
                                        <textarea name="note" rows="2" placeholder="{{__('administrator.note')}}" class="form-control no-resize">{{$master->note}}</textarea>
                                    </div>
                                </div>
                                <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
                                    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
                                        <label>{{__('administrator.total_qty')}} :</label>
                                        {!! 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-6 col-md-6 col-lg-6">
                                        <label>{{__('administrator.discount')}} (%)</label>
                                        {!! Form::text('discount',null, array('placeholder'=>__('administrator.discount'),'class' => 'form-control input-sm number-only total-discount' ,'')) !!}
                                    </div>
                                    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
                                        <label>{{__('administrator.payment_method')}}</label>
                                        {!! Form::select('payment_method_id', getPaymentMethod() , null , ['placeholder'=>__('administrator.please_select'),'class' => 'form-control select2']) !!}
                                    </div>
                                    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 display-none">
                                        <label>{{__('administrator.delivery_fee_by')}}</label>
                                        {!! Form::select('delivery_fee_by', getFeeBy() , 2 ,['placeholder'=>__('administrator.please_select'),'class' => 'form-control select2']) !!}
                                    </div>
                                    <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
                                        <label>{{__('administrator.delivery_fee')}}
                                            ({{__('administrator.'.user_currency()->code)}})</label>
                                        {!! Form::text('delivery_fee', null ,
                                        ['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>{{__('administrator.total')}} ({{__('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>{{__('administrator.grand_total')}} ({{__('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>{{__('administrator.paid')}} ({{__('administrator.'.user_currency()->code)}})</label>
                                        <input type="text" class="form-control input-sm number-only border bg-transparent paid" name="paid" value="{{$master->paid_amount}}" placeholder="0.00">
                                    </div>
                                    <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
                                        <label class="change">{{__('administrator.owed_amount')}} ({{__('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('{{route('all-sale-history.index')}}')">{{__('administrator.back')}}</button>
                                        <button type="submit" class="button_info button_lg pull-right">{{__('administrator.submit')}}</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                {!! Form::close() !!}
            </div>
        </div>
    </div>

@endsection
@section('script')
    <script type="text/javascript">
        $(document).ready(function () {
            gen_item_total();
            $('.add_customer').on('click', function () {
            });
            $(document).on('click', 'a.page-link', function (e) {
                e.preventDefault();
                let url = $(this).attr('href');
                search_product(url);
            });
        });
        $(".typeahead").autocomplete({
            source: function (request, response) {
                let branch_id = $('input[name=branch_id]').val();
                let warehouse = $('.warehouse').val();
                let postype = '{{request('postype')}}';
                $.ajax({
                    url: "{{ Route('pos-scan-code') }}",
                    dataType: "json",
                    method: "get",
                    data: {
                        barcode: request.term,
                        branch_id,
                        warehouse,
                        postype
                    },
                    success: function (rows) {
                        let check = 0;
                        let table = $('.barcode_item');
                        let id = rows.data.id;
                        let name = rows.data.name;
                        let stock = rows.data.stock;
                        let size = rows.data.size;
                        let code = rows.data.code;
                        let price = rows.data.price;
                        let symbol = rows.data.symbol;
                        let count = $('#append_product tr').length;
                        let $html = `
                        <tr data-symbol="` + symbol + `">
                            <td><input type="hidden" class="barcode_item" value="` + id + `" name="barcode_item[]">` + (count + 1) + `</td>
                            <td>` + (name + "  ( Size: " + size + " )") + `<br>` + code + `</td>
                            <td>
                                <input type="number" value="1" class="form-control input-sm qty number-only" name="qty[]" required/>
                                <input type="hidden" value="` + stock + `" class="stock" name="stock[]"/>
                            </td>
                            <td><input type="text"  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) {
                                $('#append_product').append($html);
                            }
                        } else {
                            if (id) {
                                $('#append_product').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 = $('.barcode_item');
            let name = $(this).data('name');
            let stock = $(this).data('stock');
            let size = $(this).data('size');
            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><input type="hidden" class="barcode_item" value="` + id + `" name="barcode_item[]">` + (count + 1) + `</td>
                            <td>` + (name + "  ( Size: " + size + " )") + `<br>` + code + `</td>
                            <td>
                                <input type="number" value="1" class="form-control input-sm qty number-only" name="qty[]" required/>
                                <input type="hidden" value="` + stock + `" class="stock" name="stock[]"/>
                            </td>
                            <td><input type="text"  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) {
                    $('#append_product').append($html);
                }
            } else {
                if (id) {
                    $('#append_product').append($html);
                }
            }
            get_total_all();
        });

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

        $(document).on('keyup', '.number-only', function () {
            gen_item_total($(this));
        });
        $(document).ready(function () {
            $('input[name=search]').keypress(function (e) {
                // e.preventDefault();
                var key = e.which;
                if (key == 13)  // the enter key code
                {
                    search_product();
                }
            });
            $('.warehouse,select[name=branch_id]').on('change', function () {
                search_product();
            });
            $('select[name=branch_id]').val({{$master->branch_id}}).change();

            $('.btn_search_pos').on('click', function () {
                search_product();
            });
        });

        function search_product(url = "{{ route('pos.create') }}") {
            let barcode = $('.search').val();
            let branch_id = $('select[name=branch_id]').val();
            let warehouse = $('.warehouse').val();
            let postype = '{{request('postype')}}';
            $.ajax({
                url: url,
                method: "get",
                data: {
                    barcode,
                    warehouse,
                    branch_id,
                    postype,
                },
                success: function (response) {
                    $('.my-product-list').html(response.html);
                }
            });
        }

        function gen_item_total() {
            let table = $('#append_product tr');
            $.each(table,function (ii,vv) {
                this_ = $(this);
                let qty = this_.find('.qty').val();
                let price = this_.find('.retail_price').val();
                let disc = this_.find('.disc').val();
                let stock = this_.find('.stock').val();
                if (parseFloat(qty) > parseFloat(stock)) {
                    qty = stock;
                    this_.find('.qty').val(qty);
                    $.alert('Out of Stock');
                }
                disc = disc ? disc : 0;
                let symbol = '{{$currency->symbol}}';
                let stotal = qty * price;
                let total = stotal - (stotal * disc / 100);
                this_.find('.subtotal_item').text(stotal + '' + symbol);
                this_.find('.total_item').text(total + '' + symbol);
                this_.find('.amount-hidden').val(total);
            });

            get_total_all();
        }

        function get_total_all() {
            let total = 0;
            let discount = parseFloat($('input[name=discount]').val());
            let delivery_fee_by = parseFloat($('select[name=delivery_fee_by]').val());
            let delivery_fee = parseFloat($('input[name=delivery_fee ]').val());
            discount = discount ? discount : 0;
            delivery_fee_by = delivery_fee_by ? delivery_fee_by : 0;
            delivery_fee = delivery_fee ? delivery_fee : 0;
            let symbol = '{{$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 - (total * discount / 100);
             g_total += delivery_fee;


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

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

            $('.sub_total').val(symbol + '' + 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('{{__('administrator.change_amount')}}');
                $('.change').removeClass('text-red');
                $('.change').addClass('text-green');
            } else {
                $('.change').text('{{__('administrator.owed_amount')}}');
                $('.change').removeClass('text-green');
                $('.change').addClass('text-red');
            }
            paid = paid - g_total;
            $('input[name="change-amount"]').val('{{$currency->symbol}}' + '' + Math.abs(parseFloat(paid).toFixed(2)));
        });
        $(document).on('click', '.pos_clothes_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();
                        }
                    }
                }
            });
        });
    </script>
@endsection
