@extends(branchMenu())
@section('content')
    <?php Fun::lang()?>

    <div class="page">
        <div class=container-fluid>
            <div class="top-content">
                <div class="row">
                    <div class="col-sm-12 col-xs-12 form-group">
                        <div class="title-add">
                            <div class="pull-left">
                                <h3>{{__('administrator.laonlin_stock_report')}}</h3>
                            </div>
                            <div class="pull-right">
                                @include('administrator.report-stock.sub-menu')
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="top-content">
                <div class="row">
                    <div class="col-sm-12 col-xs-12 form-group padding-0">
                        <div>
                            <div class="col-sm-3 col-md-3">
                                <div class="col-sm-3 col-md-3">
                                    {{__('administrator.warehouse')}}
                                </div>
                                <div class="form-group col-sm-9 col-md-9">
                                    {!! Form::select('warehouse', $warehouses, null, array('class' => 'form-control select2', 'placeholder' =>  __('administrator.please_select_warehouse'))) !!}
                                </div>
                            </div>
                            <div class="col-sm-3 col-md-3">
                                <div class="col-sm-3 col-md-3">
                                    {{__('administrator.item')}}
                                </div>
                                <div class="form-group col-sm-9 col-md-9">
                                    {!! Form::select('product_id', [], null, array('class' => 'form-control select2 search_product', 'placeholder' =>  __('administrator.please_select'))) !!}
                                </div>
                            </div>
                            <div class="col-sm-3 col-md-3">
                                <div class="col-sm-3 col-md-3">
                                    {{__('administrator.date')}}
                                </div>
                                <div class="form-group col-sm-9 col-md-9">
                                    {!! Form::text('date', null, array('class' => 'form-control date_picker', 'placeholder' =>  __('administrator.date'))) !!}
                                </div>
                            </div>
                            <div class="col-sm-offset-6 col-sm-6">

                                <button class=" button_success pull-right print_button" type="button"
                                        data-text="{{__('administrator.report_stock')}}"><i
                                            class="fa fa-print"></i> {{__('administrator.print')}}</button>
                                <button class=" button_info pull-right btn_search" type="button"
                                        data-text="{{__('administrator.search')}}"><i
                                            class="fa fa-search"></i> {{__('administrator.search')}}</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="wrapper-table">
                <table id="print_table" class="table table-striped table-hover  ">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th>{{__('administrator.warehouse')}}</th>
                        <th>{{__('administrator.barcode')}}</th>
                        <th>{{__('administrator.item_name')}}</th>
                        <th>{{__('administrator.old_stock')}}</th>
                        <th>{{__('administrator.import_qty')}}</th>
                        <th>{{__('administrator.export_qty')}}</th>
                        <th>{{__('administrator.balance_qty')}}</th>
                    </tr>
                    </thead>
                    <tbody class="tbody">
                    </tbody>
                    <tfoot>
                    <tr>
                        <th colspan="4" class="text-right">{{__('administrator.total')}}</th>
                        <th>0</th>
                        <th>0</th>
                        <th>0</th>
                        <th>0</th>
                    </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
@endsection
@section('script')
    <script type="text/javascript">
        function getTable() {
            $('#print_table').DataTable().destroy();
            let warehouse = $('select[name=warehouse]').val();
            let product_id = $('select[name=product_id]').val();
            let from_date = $('input[name=from_date]').val();
            let to_date = $('input[name=to_date]').val();
            $('#print_table').DataTable({
                "oLanguage": {
                    "sSearch": "{{__('administrator.search')}}:",
                    "sLengthMenu": "{{__('administrator.display')}} _MENU_ {{__('administrator.record_per_page')}}",
                    "sZeroRecords": "{{__('administrator.Nothing_foundsorry')}}",
                    "sInfo": "{{__('administrator.showing')}} _START_ {{__('administrator.to')}} _END_ {{__('administrator.of')}} _TOTAL_ {{__('administrator.record')}}",
                    "sInfoEmpty": "{{__('administrator.showing')}} 0 {{__('administrator.to')}} 0 {{__('administrator.of')}} 0 {{__('administrator.record')}}",
                    "sInfoFiltered": "({{__('administrator.filter_form')}} _MAX_ {{__('administrator.total_records')}})",
                    "oPaginate": {
                        "sFirst": "{{__('administrator.first')}}",
                        "sLast": "{{__('administrator.last')}}",
                        "sNext": "{{__('administrator.next')}}",
                        "sPrevious": "{{__('administrator.previous')}}"
                    },
                },
                ordering: false,
                serverSide: true,
                lengthMenu: [[20, 30, 50, 100, 200, 500, 1000], [20, 30, 50, 100, 200, 500, 1000]],
                ajax: {
                    url: '{{route('report-stock-v1')}}',
                    data: {
                        warehouse,
                        product_id,
                        from_date,
                        to_date
                    },
                },
                columns:
                    [
                        {data: 'DT_RowIndex', 'searchable': false},
                        {data: 'warehouse', 'searchable': true},
                        {data: 'barcode', 'name': 'nso007_stocks.barcode'},
                        {data: 'product_name', 'name': 'nso007_products.product_name'},
                        {data: 'old_stock'},
                        {data: 'in_qty'},
                        {data: 'out_qty'},
                        {data: 'stock_qty'},
                    ],
                "fnDrawCallback": function () {
                    let tr = $('.tbody tr');
                    let $total_old = 0;
                    let $total_in = 0;
                    let $total_out = 0;
                    let $total_bal = 0;
                    $.each(tr, function (ii, vv) {
                        let old = $(this).find('td:nth-child(5)').text();
                        let im = $(this).find('td:nth-child(6)').text();
                        let out = $(this).find('td:nth-child(7)').text();
                        let bal = $(this).find('td:nth-child(8)').text();
                        $total_old += parseFloat(old ? old : 0);
                        $total_in += parseFloat(im ? im : 0);
                        $total_out += parseFloat(out ? out : 0);
                        $total_bal += parseFloat(bal ? bal : 0);
                    });
                    $('#print_table').find('tfoot tr:first-child th:nth-child(2)').text($total_old ? $total_old : 0);
                    $('#print_table').find('tfoot tr:first-child th:nth-child(3)').text($total_in ? $total_in : 0);
                    $('#print_table').find('tfoot tr:first-child th:nth-child(4)').text($total_out ? $total_out : 0);
                    $('#print_table').find('tfoot tr:first-child th:nth-child(5)').text($total_bal ? $total_bal : 0);
                },
            });
        }

        $(document).ready(function () {
            getTable();
            $('.btn_search').on('click', function () {
                getTable();
            });
            $('#print_table').on('click', '.show-stock-in', function () {
                let url = $(this).data('url');
                getHtmlFormShow(url, '1200px');
            });
        });
    </script>
@stop



