@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_payment')}}</h3>
                            </div>
                            <div class="pull-right">
                                @include('administrator.reportpayment.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.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 supplier']) !!}
                                </div>
                            </div>
                            <div class="col-sm-3 col-md-3">
                                <div class="col-sm-3 col-md-3" style="margin-top: 5px">
                                    {{__('administrator.status')}}
                                </div>
                                <div class="form-group col-sm-9 col-md-9">
                                    {!! Form::select('status', getPaymentStatus(), null, ['placeholder'=>__('administrator.please_select'),'class' => 'form-control supplier']) !!}
                                </div>
                            </div>
                            <div class="col-sm-3 col-md-3">
                                <div class="col-sm-3 col-md-3" style="margin-top: 5px">
                                    {{__('administrator.user')}}
                                </div>
                                <div class="form-group col-sm-9 col-md-9">
                                    {!! Form::select('user_id', $user, null, ['placeholder'=>__('administrator.please_select'),'class' => 'form-control supplier']) !!}
                                </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','required')) !!}
                                </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','required')) !!}
                                </div>
                            </div>



                            <div class="col-sm-offset-6 col-sm-6">
                                {{--{!! Form::open(array('route' => 'reportsales_print','method'=>'GET')) !!}--}}
                                <button type="submit" class=" button_success pull-right print_button" data-text="{{__('administrator.report_stock_in_payment')}}"
                                        type="button"><i  class="fa fa-print"></i> {{__('administrator.print')}}</button>
                                {{--{!! Form::close() !!}--}}
                                <button class=" button_primary pull-right" onclick="searching()"
                                        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.invoice')}}</th>
                        <th>{{__('administrator.date')}}</th>
                        <th>{{__('administrator.user')}}</th>
                        <th>{{__('administrator.suppliers')}}</th>
                        <th>{{__('administrator.contact')}}</th>
                        <th>{{__('administrator.amount')}}</th>
                        <th>{{__('administrator.refund')}}</th>
                        <th>{{__('administrator.remain_amount')}}</th>
                        <th>{{__('administrator.status')}}</th>
                    </tr>
                    </thead>
                    <tbody class="tbody">

                    </tbody>
                    <tr class="tfoot">
                        <th colspan="6" class="text-right">{{__('administrator.total_amount')}}</th>
                        <th class="total_amt green">0</th>
                        <th class="total_paid green">0</th>
                        <th class="total_remain green">0</th>
                        <th></th>
                    </tr>
                </table>
            </div>
        </div>
    </div>
@endsection



@section('script')


    <script type="text/javascript">
        $(document).ready(function () {
            dataTableFilter()
        });
        function dataTableFilter(supplier_id = '', start_date = '', end_date = '', status = '') {
            $('#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: "{{ url('report-purchase-payment')}}",
                    data: {
                        supplier_id,
                        status,
                        start_date,
                        end_date,
                    },
                },
                columns:
                    [
                        {data: 'DT_RowIndex', 'searchable': false},
                        {data: 'invoice_number','name':'nso007_purchases.invoice_number'},
                        {data: 'payment_date','searchable': false},
                        {data: 'user_id','searchable': false},
                        {data: 'supplier_name','name': 'nso007_suppliers.supplier_name'},
                        {data: 'supplier_phone','name': 'nso007_suppliers.supplier_phone'},
                        {data: 'payment_amount','searchable': false},
                        {data: 'paid_amount','searchable': false},
                        {data: 'remain','searchable': false},
                        {data: 'payment_status','searchable': false},
                    ],
                "fnDrawCallback":function () {
                    let tr = $('.tbody tr');
                    let total_amt_usd = 0,total_amt_riel = 0,total_paid_riel = 0,total_paid_usd = 0,total_remain_riel = 0,total_remain_usd = 0,ta_r='',ta_u='',tp_r='',tp_u='',tr_r='',tr_u='';
                    $.each(tr,function (ii,vv) {
                        let amt_row = $(this).find('.payment_amount')
                        let paid_row = $(this).find('.paid_amount')
                        let remain_row = $(this).find('.remain_amount')
                        let cur_id = amt_row.data('currency_id');

                        let amount = amt_row.data('amount')
                        amount = amount?parseFloat(amount):0;

                        let paid = paid_row.data('amount')
                        paid = paid?parseFloat(paid):0;

                        let remain = remain_row.data('amount')
                        remain = remain?parseFloat(remain):0;

                        if(cur_id===1){
                            total_amt_riel+=amount;
                            total_paid_riel+=paid;
                            total_remain_riel+=remain;
                        }
                        if(cur_id===2){
                            total_amt_usd+=amount;
                            total_paid_usd+=paid;
                            total_remain_usd+=remain;
                        }
                    });
                    ta_r = total_amt_riel?('៛'+addComma(total_amt_riel.toFixed(2))):'';
                    ta_u = total_amt_usd?('$'+addComma(total_amt_usd.toFixed(2))):'';

                    tp_r = total_paid_riel?('៛'+addComma(total_paid_riel.toFixed(2))):'';
                    tp_u = total_paid_usd?('$'+addComma(total_paid_usd.toFixed(2))):'';

                    tr_r = total_remain_riel?('៛'+addComma(total_remain_riel.toFixed(2))):'';
                    tr_u = total_remain_usd?('$'+addComma(total_remain_usd.toFixed(2))):'';


                    $('.tfoot .total_amt').html((ta_r?(ta_r+'<br>'):'0')+ta_u);
                    $('.tfoot .total_paid').html((tp_r?(tp_r+'<br>'):'0')+tp_u);
                    $('.tfoot .total_remain').html((tr_r?(tr_r+'<br>'):'0')+tr_u);
                },
            });
        }
        $(document).on('click', '#search', function () {
            let supplier_id = $('select[name="supplier_id"]').val();
            let status = $('select[name="status"]').val();
            let start_date = $('input[name="from_date"]').val();
            let end_date = $('input[name="to_date"]').val();
            $('#print_table').DataTable().destroy();
            dataTableFilter(supplier_id, start_date, end_date,status);
        });
    </script>
@stop




