@extends(branchMenu())
@section('content')
    <?php Fun::lang() ?>
    <style>
        .mt {
            margin-top: 10px
        }
    </style>
    <div class="page-purchases">
        <div class="preview">
            <div class="col-sm-7 paddin-5">
                <div class="col-sm-6 paddin-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-6 paddin-5">
                    <div class="form-group">
                        {!! Form::text('search', null, array('placeholder' => __('administrator.search') ,'class' => 'form-control search')) !!}
                    </div>
                </div>
                <div class="product-item-list">
                    <div class="item-list">
                        <div class="row my-product-list">
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-5 paddin-5">
                {!! Form::open(array('route' => 'transfer-stock.store','method'=>'POST','enctype'=>'multipart/form-data','id'=>'submit_post','class'=>'jform')) !!}
                <div class="check-bill">
                    <div class=" row">
                        <div class="list-box">
                            <div class="list">
                                <div class="col-sm-12">
                                    <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.action')}}</th>
                                                    </tr>
                                                    </thead>
                                                    <tbody id="append_product">

                                                    </tbody>
                                                </table>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-6 col-lg-6 mt">
                                    <label>{{__('administrator.date')}} :</label>
                                    {!! Form::text('date',date('d-m-Y'),array('placeholder'=>__('administrator.date'),'class'=>'date_picker form-control')) !!}
                                </div>
                                <div class="col-md-6 col-lg-6 mt">
                                    <label>{{__('administrator.total_qty')}} :</label>
                                    {!! Form::text('total_qty',0,array('class'=>'form-control','readonly')) !!}
                                </div>
                                <div class="col-md-6 col-lg-6 mt">
                                    <label>{{__('administrator.from')}} :</label>
                                    {!! Form::text('branch_id',myBranch()->name??'',array('placeholder'=>__('administrator.please_select'),'class'=>' form-control','readonly')) !!}
                                </div>
                                <div class="col-md-6 col-lg-6 mt">
                                    <label>{{__('administrator.to_branch')}} :</label>
                                    {!! Form::select('to_branch',$branch,null,array('placeholder'=>__('administrator.please_select'),'class'=>' form-control')) !!}
                                </div>
                                <div class="col-md-12 col-lg-12 mt">
                                    <label>{{__('administrator.description')}} :</label>
                                    {!! Form::textarea('note', null, ['placeholder'=>__('administrator.note'),'class' => 'form-control form-control no-resize','maxlength'=>'200','rows'=>'3']) !!}
                                </div>
                                <div class="col-md-12 col-lg-12 mt">
                                    <input type="submit" class="button_info button_lg pull-right"
                                           value="{{__('administrator.submit')}}">
                                    <button type="button"
                                            class="button_danger btn_clear_all button_lg">{{__('administrator.remove')}}</button>
                                    <button type="button" class="button_warning button_lg"
                                            onclick="history.back(1)">{{__('administrator.back')}}</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                {!! Form::close() !!}
            </div>
        </div>
    </div>

@endsection
@section('script')
    <script type="text/javascript">
        $(document).ready(function () {
            getStock();
            $(document).on('click', 'a.page-link', function (e) {
                e.preventDefault();
                getStock($(this).attr('href'));
            });
            $('.typeahead').autocomplete({
                source: function (request, response) {
                    let wh = $('select[name=from_warehouse_id]').val();
                    $.ajax({
                        url: "{{ Route('transfer-stock-scan-code') }}",
                        dataType: "json",
                        method: "get",
                        data: {
                            from_warehouse_id: wh,
                            barcode: request.term,
                        },
                        success: function (rows) {
                            let check = 0;
                            let table = $('.product_unit');
                            let id = rows.data.id;
                            let name = rows.data.name;
                            let stock = rows.data.stock;
                            let size = rows.data.size;
                            let color = rows.data.color;
                            let code = rows.data.code;
                            let symbol = rows.data.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>${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[]"/>
                            </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);

                                        check++;
                                    }
                                });
                                if (check === 0 && id) {
                                    $('#append_product').append($html);
                                }
                            } else {
                                if (id) {
                                    $('#append_product').append($html);
                                }
                            }

                            $(".typeahead").val('');
                            $(".typeahead").focus();
                            total_qty()
                        }
                    });
                },
                minLength: 3,
            });
            $('.search').on('keyup', function () {
                let wh = $('select[name=from_warehouse_id]').val();
                let value = $(this).val();

                $.ajax({
                    url: '{{route('transfer-stock.create')}}',
                    dataType: 'json',
                    method: 'get',

                    data: {
                        value: value,
                        from_warehouse_id: wh,
                    },
                    success: function (data) {
                        $('.my-product-list').html(data.html);
                    },
                    error(xhr) {
                        alertErrorFormController(xhr);
                    }
                });
            });

            $(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 symbol = $(this).data('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>${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[]"/>
                            </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) {
                        console.log(check);
                        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);
                            check++;
                        }
                    });
                    if (check === 0 && id) {
                        $('#append_product').append($html);
                    }
                } else {
                    if (id) {
                        $('#append_product').append($html);
                    }
                }
                total_qty()
            });
            $(document).on('click', '.remove_row', function () {
                $(this).closest('tr').remove();
                total_qty()
            });
            $(document).on('click', '.btn_clear_all ', 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('');

                            }
                        }
                    }
                });
                total_qty()
            });
            $('#append_product').on('keyup', '.qty ', function () {
                let stock = $(this).closest('td').find('.stock').val();
                let qty = $(this).val();
                stock = stock ? parseFloat(stock) : 0;
                qty = qty ? parseFloat(qty) : 0;
                if (qty > stock) {
                    jalert('Warning', '{{__('administrator.this_product_is_out_of_stock')}}');
                    $(this).val(stock);
                }
                total_qty()
            });

        });

        function getStock(route = "{{ route('transfer-stock.create') }}") {
            $.ajax({
                url: route,
                method: "get",
                dataType: 'json',
                success: function (response) {
                    $('.my-product-list').html(response.html);
                }
            });
        }
        function total_qty() {
            let total = 0;
            $('#append_product .qty').each(function (i,v) {
                let qty = $(this).val()
                qty = qty?parseFloat(qty):0;
                total+=qty
            })
            $('input[name=total_qty]').val(total)
        }
    </script>
@endsection
