@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"  style="overflow: hidden">
                {!! Form::open(array('route' => 'transfer-stock-material.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.unit')}} <span class="red">*</span></th>
                                                        <th>{{__('administrator.qty')}} <span class="red">*</span></th>
                                                        <th>{{__('administrator.price')}} <span class="red">*</span></th>
                                                        <th>{{__('administrator.total')}}</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>
                        @php
                            $currency = user_currency()->symbol??'';
                        @endphp
                        <div class="list-box">
                            <div class="list">
                                <div class="col-sm-6 paddin-5">
                                    <label>{{__('administrator.total')}} ( {{$currency}} ):</label>
                                    {!! Form::text('gtotal',null,array('placeholder'=>__('administrator.total'),'class'=>' form-control gtotal','readonly')) !!}
                                </div>
                                <div class="col-sm-6 paddin-5">
                                    <label>{{__('administrator.account_receivable')}} ( {{$currency}} ) :</label>
                                    {!! Form::text('account_receivable',0,array('placeholder'=>__('administrator.cash_advance'),'class'=>' 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">
        let append_product = $('#append_product');
        $(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('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-material.create') }}",
                method: "get",
                dataType: 'json',
                data: {
                    warehouse: warehouse,
                    value: value,
                    _token: '{{ csrf_token() }}',
                },
                success: function (response) {
                    $('.my-product-list').html(response.html);
                }
            });
        });
        append_product.on('keyup','.price', function () {
            total();
        });
        $(document).on('click', '.listings', function () {
            let id = $(this).data('id');
            let name = $(this).data('name');
            let unit = $(this).data('unit');
            let barcode = $(this).data('barcode');
            let count = $('#append_product tr').length;
            let pselect = '{{__('administrator.please_select')}}';
            let $html = `
                        <tr class="check${id}" >
                            <td><input type="hidden" class="product_id" value="` + id + `" name="product_id[]">` + (count + 1) + `</td>
                            <td>` + (name + "  ( " + barcode + " ) ") + `</td>
                            <td >
                            <select name="unit[]" class="form-control select2 unit" style="width: 100%">
                            <option value="">${pselect}</option>
                            ${unit}
                            </select>
                            </td>
                            <td>
                                <input type="number" step="0.001" value="1" class="form-control input-sm qty number-only" name="qty[]" required/>
                            </td>
                            <td>
                                <input type="text" value="0" class="form-control input-sm price number-only" name="price[]" required/>
                            </td>
                            <td>
                                <input type="number" step="0.001" value="0" class="form-control input-sm total number-only" name="total[]" readonly/>
                            </td>
                            <td class="text-center"><span class="fa fa-times text-danger fa-2x  cursor remove_row"></span></td>
                        </tr>
                    `;
            let check =  $('#append_product tr').hasClass('check'+id);
            if(!check){
                append_product.append($html);
            }
            append_product.find('.select2').select2();
        });
        $(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('');

                        }
                    }
                }
            });
        });
        append_product.on('change', '.unit,.qty', function () {
            let this_ = $(this).closest('tr');
            let unit = this_.find('.unit').val();
            let qty = this_.find('.qty').val();
            let product_id = this_.find('.product_id').val();
            let from_warehouse_id = $('select[name=from_warehouse_id]').val();
            $.get('{{route('transfer-stock-material-check-stock')}}',{unit,qty,product_id,from_warehouse_id},function (data) {
                let price = parseFloat(data.price);
                let nqty = parseFloat(data.qty);
                let total_amount = price*nqty;
                this_.find('.qty').val(nqty);
                this_.find('.price').val(price);
                this_.find('.total').val(total_amount.toFixed(2));
                total();
            });
        });

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

        function total() {
            let total_c = $('#append_product tr');
            let total = 0;
            $.each(total_c,function (ii,vv) {
                let this_ = $(this);
                let price = this_.find('.price').val();
                let qty = this_.find('.qty').val();
                price = price?parseFloat(price):0;
                qty = qty?parseFloat(qty):0;
                let s_total = price * qty;
                total+= s_total ;
                this_.find('.total').val(s_total);
            });
            $('.gtotal').val(total.toFixed(2));
        }
    </script>
@endsection
