@extends(branchMenu())
@section('content')
    <?php Fun::lang() ?>
    <div class="page-purchases">
        <div class="preview">
            <div class="col-sm-6 paddin-5">
                <div class="col-sm-12 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-6 paddin-5">
                {!! Form::open(array('route' => 'transfer-stock-cut-piece.store','method'=>'POST','enctype'=>'multipart/form-data','class'=>'jform')) !!}
                <div class="check-bill">
                    <div class=" row blog-payment">
                        <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>
                        </div>
                        <div class="list-box">
                            <div class="list">
                                <div class="col-sm-6 paddin-5">
                                    <label>{{__('administrator.from')}} {{__('administrator.warehouse')}} :</label>
                                    {!! Form::select('from_warehouse_id',$warehouse,$m_wh,array('placeholder'=>__('administrator.please_select'),'class'=>' form-control','id'=>'warehouse_id',)) !!}
                                </div>

                                <div class="col-sm-6 paddin-5">
                                    <label>{{__('administrator.to_tailor_shop')}} :</label>
                                    {!! Form::select('tailor_type',getTailorType(),null,array('placeholder'=>__('administrator.please_select'),'class'=>' form-control tailor_type')) !!}
                                </div>
                            </div>
                        </div>
                        <div class="list-box">
                            <div class="list">
                                <div class="col-sm-6 paddin-5">
                                    <label>{{__('administrator.to_tailor')}} :</label>
                                    {!! Form::select('to_tailor_id',[],null,array('placeholder'=>__('administrator.please_select'),'class'=>' form-control')) !!}
                                </div>
                                <div class="col-sm-6 paddin-5">
                                    <label>{{__('administrator.date')}} :</label>
                                    {!! Form::text('date',date('d-m-Y'),array('placeholder'=>__('administrator.date'),'class'=>'date_picker form-control')) !!}
                                </div>
                            </div>
                        </div>
                        <div class="list-box">
                            <div class="list">

                                <div class="col-sm-12 paddin-5">
                                    <label>{{__('administrator.description')}}</label>
                                    {!! Form::textarea('note', null, ['placeholder'=>__('administrator.note'),'class' => 'form-control form-control no-resize','maxlength'=>'200','rows'=>'3']) !!}
                                </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="{{__('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>
                {!! Form::close() !!}
            </div>
        </div>
    </div>

@endsection
@section('script')
    <script type="text/javascript">
        $(document).ready(function () {
            getWhProduct();
            $(document).on('click', 'a.page-link', function (e) {
                let wh = $('select[name=from_warehouse_id]').val();
                e.preventDefault();
                let url = $(this).attr('href');
                $.ajax({
                    url: url,
                    data: {
                        _token: '{{ csrf_token() }}',
                        warehouse: wh
                    },
                    dataType: 'json',
                    success(data) {

                        $('.my-product-list').html(data.html);
                    }
                });
            });
        });
        $(document).on('keyup', '.qty', function () {
            let stock = $(this).closest('tr').find('.stock').val();
            let qty = $(this).val();
            qty = qty ? parseFloat(qty) : 0;
            console.log(stock);
            stock = stock ? parseFloat(stock) : 0;
            if (qty > stock) {
                jalert('{{__('administrator.warning')}}', '{{__('administrator.this_product_is_out_of_stock')}}');
                $(this).val(stock);
            }
        });
        $(document).on('change', '#warehouse_id', function () {
            getWhProduct();
        });
        $(document).on('change', 'select[name=tailor_type]', function () {
            let type = $(this).val();
            $.ajax({
                url: "{{ route('find-warehouse-by-type') }}",
                method: "post",
                dataType: 'json',
                data: {
                    type: type,
                },
                success: function (response) {
                    let row = '<option value="">{{__('administrator.please_select')}}</option>';
                    $.each(response,function (ii,vv) {
                        row += '<option value="'+ii+'">'+vv+'</option>';
                    });
                    $('select[name=to_tailor_id]').html(row);
                }
            });
        });
        $('.search').on('keyup', function () {
            let value = $(this).val();
            let warehouse = $('#warehouse_id').val();
            $.ajax({
                url: "{{ route('transfer-stock-cut-piece.create') }}",
                method: "get",
                dataType: 'json',
                data: {
                    warehouse: warehouse,
                    value: value,
                    _token: '{{ csrf_token() }}',
                },
                success: function (response) {
                    $('.my-product-list').html(response.html);
                }
            });
        });

        $(document).on('click', '.listings', function () {
            let check = 0;
            let table = $('.c_barcode');
            let name = $(this).data('name');
            let stock = $(this).data('stock');
            let size = $(this).data('size');
            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="c_barcode" value="` + code + `" name="barcode[]">` + (count + 1) + `</td>
                            <td>` + (name + "  ( " + 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 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(code)) {
                        let qty = $(this).closest('tr').find('.qty').val();
                        let add_mor = parseFloat(qty) + 1;
                        if (add_mor > parseFloat(stock)) {
                            add_mor = stock;
                            $.alert('Out of Stock');
                        }
                        $(this).closest('tr').find('.qty').val(add_mor);
                        check++;
                    }
                });
                if (check === 0 && code) {
                    $('#append_product').append($html);
                }
            } else {
                if (code) {
                    $('#append_product').append($html);
                }
            }

        });

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

        $(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('');

                        }
                    }
                }
            });
        });

        function getWhProduct() {
            let id = $('#warehouse_id').val();
            $.ajax({
                url: "{{ route('transfer-stock-cut-piece.create') }}",
                method: "get",
                dataType: 'json',
                data: {
                    warehouse: id,
                    _token: '{{ csrf_token() }}',
                },
                success: function (response) {
                    $('.my-product-list').html(response.html);
                }
            });
        }
    </script>
@endsection
