@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.report_stock_in_detail')}}</h3>
                            </div>
                            <div class="pull-right">
                                @include('administrator.reportpurchases.sub-menu')
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="top-content">
                <div class="row">
                    <div class="col-sm-12 col-xs-12 form-group paddin-0">
                        <div>
                            <div class="col-sm-3 col-md-3">
                                <div class="col-sm-3 col-md-3" style="margin-top: 5px">
                                    {{__('administrator.type')}}
                                </div>
                                <div class="form-group col-sm-9 col-md-9">
                                    {!! Form::select('type',$type,null, ['placeholder'=>__('administrator.please_select'),'class' => 'form-control']) !!}
                                </div>
                            </div>
                            <div class="col-sm-3 col-md-3">
                                <div class="col-sm-3 col-md-3" style="margin-top: 5px">
                                    {{__('administrator.supplier')}}
                                </div>
                                <div class="form-group col-sm-9 col-md-9">
                                    {!! Form::select('supplier_id',$supplier,null, ['placeholder'=>__('administrator.please_select'),'class' => 'form-control select2']) !!}
                                </div>
                            </div>
                            <div class="col-sm-3 col-md-3">
                                <div class="col-sm-3 col-md-3" style="margin-top: 5px">
                                    {{__('administrator.product')}}
                                </div>
                                <div class="form-group col-sm-9 col-md-9">
                                    {!! Form::select('product_id',[],null, ['placeholder'=>__('administrator.please_select'),'class' => 'form-control search_product']) !!}
                                </div>
                            </div>
                            <div class="col-sm-3 col-md-3">
                                <div class="col-sm-3 col-md-3" style="margin-top: 5px">
                                    {{__('administrator.from_date')}}
                                </div>
                                <div class="form-group col-sm-9 col-md-9">
                                    {!! Form::text('from_date',null, array('placeholder' => __('administrator.from_date'),'class' => 'form-control date_picker','autocomplete' => 'off','')) !!}
                                </div>
                            </div>
                            <div class="col-sm-3 col-md-3">
                                <div class="col-sm-3 col-md-3" style="margin-top: 5px">
                                    {{__('administrator.to_date')}}
                                </div>
                                <div class="form-group col-sm-9 col-md-9">
                                    {!! Form::text('to_date',null, array('placeholder' => __('administrator.to_date'),'class' => 'form-control date_picker','autocomplete' => 'off','')) !!}
                                </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_payment')}}"><i
                                            class="fa fa-print "></i> {{__('administrator.print')}}</button>
                                <button class=" button_primary pull-right" id="search"
                                        type="submit"> {{__('administrator.search')}}</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="wrapper-table">
                <table id="print_table" width="100%" class="table table-striped table-hover ">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th>{{__('administrator.type')}}</th>
                        <th>{{__('administrator.purchase_date')}}</th>
                        <th>{{__('administrator.invoice')}}</th>
                        <th>{{__('administrator.supplier_name')}}</th>
                        <th>{{__('administrator.supplier_phone')}}</th>
                        <th>{{__('administrator.products')}}</th>
                        <th>{{__('administrator.barcode')}}</th>
                        @can('Show Purchase Item Price')
                            <th>{{__('administrator.cost')}}</th>
                            <th>{{__('administrator.discount')}}</th>
                        @endcan
                        <th>{{__('administrator.qty')}}</th>
                        @can('Show Purchase Item Price')
                            <th>{{__('administrator.total')}}</th>
                        @endcan
                    </tr>
                    </thead>
                    <tbody class="tbody">

                    </tbody>
                    @php $col_span=6;  @endphp
                    @can('Show Purchase Item Price')
                        @php $col_span=9; @endphp
                    @endcan
                    <tr class="tfoot">
                        <th colspan="{{$col_span}}" class="text-right"></th>
                        <th class="text-right">{{__('administrator.total')}}</th>
                        <th class="green f_total_item">0</th>
                        <th class="green f_total">0</th>
                    </tr>
                </table>
            </div>
        </div>
    </div>
@endsection
@section('script')
    <script type="text/javascript">
        $(document).ready(function () {
            dataTableFilter();
            $(document).on('click', '#search', function () {
                let type = $('select[name="type"]').val();
                let supplier_id = $('select[name="supplier_id"]').val();
                let product_id = $('select[name="product_id"]').val();
                let start_date = $('input[name="from_date"]').val();
                let end_date = $('input[name="to_date"]').val();
                $('#print_table').DataTable().destroy();
                dataTableFilter(type, supplier_id, product_id, start_date, end_date);
            });
        });

        function dataTableFilter(type = '', supplier_id = '', product_id = '', start_date = '', end_date = '') {
            $('#print_table').DataTable().destroy();
            $('#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: tbLeng,
                ajax: {
                    url: "{{ route('report-purchases-detail') }}",
                    data: {
                        type,
                        supplier_id,
                        product_id,
                        start_date,
                        end_date,
                    }
                },
                columns:
                    [
                        {data: 'DT_RowIndex', 'searchable': false},
                        {data: 'type', 'searchable': false},
                        {data: 'purchase_date', 'searchable': false},
                        {data: 'invoice_number', 'name': 'nso007_purchases.invoice_number'},
                        {data: 'supplier_id', 'searchable': false},
                        {data: 'supplier_phone', 'searchable': false},
                        {data: 'product_id', 'searchable': false},
                        {data: 'barcode', 'name': 'nso007_purchase_details.barcode'},
                            @can('Show Purchase Item Price')
                        {
                            data: 'cost', 'searchable': false
                        },
                        {data: 'dis', 'searchable': false},
                            @endcan
                        {
                            data: 'qty', 'searchable': false
                        },
                            @can('Show Purchase Item Price')

                        {
                            data: 'amount', 'searchable': false
                        },
                        @endcan

                    ],
                "fnDrawCallback": function (settings, json) {
                            @can('Show Purchase Item Price')
                    let table = $('#print_table .usd_hidden');
                    let total = 0;
                    $.each(table, function (ii, vv) {
                        total += parseFloat(vv.value);
                    });
                    $('.tfoot .green').text(total.toFixed(2));
                            @endcan
                    let tr = $('.tbody tr');
                    let total_usd = 0;
                    let total_riel = 0;
                    let t_u='';
                    let t_r='';
                    let total_qty=0;
                    $.each(tr,function (ii,vv) {
                        let amount = $(this).find('td:nth-child(12) span').data('amount');
                        let cur_id = $(this).find('td:nth-child(12) span').data('cur_id');
                        let qty = $(this).find('td:nth-child(11)').text();
                        qty = qty?parseFloat(qty):0;
                        total_qty+=qty;
                        amount = amount?parseFloat(amount):0;
                        if(cur_id===1){
                            total_riel+=amount;
                        }
                        if(cur_id===2){
                            total_usd+=amount;
                        }
                        t_r = total_riel?('៛'+addComma(total_riel.toFixed(2))):'';
                        t_u = total_usd?('$'+addComma(total_usd.toFixed(2))):'';

                    });
                    $('tr.tfoot:first-child ').find('.f_total_item').html(total_qty);
                    $('tr.tfoot:first-child ').find('.f_total').html((t_r?(t_r+'<br>'):'')+t_u);
                },
            });
        }

    </script>
@stop




