@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_sales_request')}}</h3>
                            </div>
                            <div class="pull-right">
                                @include('administrator.reportsales.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.branch')}}
                                </div>
                                <div class="form-group col-sm-9 col-md-9">
                                    {!! Form::select('branch_id',getBranches(),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.seller')}}
                                </div>
                                <div class="form-group col-sm-9 col-md-9">
                                    {!! Form::select('seller_id',$seller,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.customer')}}
                                </div>
                                <div class="form-group col-sm-9 col-md-9">
                                    {!! Form::select('customer_id',$customer,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.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">

                                <button type="submit" class=" button_success pull-right print_button "
                                        data-text="{{__('administrator.report_sales_request')}}"
                                        type="button"><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.branch')}}</th>
                        <th>{{__('administrator.invoice')}}</th>
                        <th>{{__('administrator.sale_date')}}</th>
                        <th>{{__('administrator.seller')}}</th>
                        <th>{{__('administrator.customer_name')}}</th>
                        <th>{{__('administrator.customer_phone')}}</th>
                        <th>{{__('administrator.total')}}</th>
                        <th class="hide_when_print" >{{__('administrator.action')}}</th>
                    </tr>
                    </thead>
                    <tbody class="tbody">

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


@section('script')


    <script type="text/javascript">
        $(document).ready(function () {
            dataTableFilter()
        });

        function dataTableFilter(customer_id = '', start_date = '', end_date = '') {
            let seller_id = $('select[name="seller_id"]').val();
            let branch_id = $('select[name="branch_id"]').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: "{{ url('reportsales-request')}}",
                    data: {

                        customer_id: customer_id,
                        start_date: start_date,
                        end_date: end_date,
                        seller_id: seller_id,
                        branch_id: branch_id,
                    },
                },
                columns:
                    [
                        {data: 'DT_RowIndex', 'searchable': false},
                        {data: 'branch_id', 'searchable': false},
                        {data: 'invoice_number', 'name': 'nso007_sales.invoice_number'},
                        {data: 'sale_date', 'searchable': false},
                        {data: 'user_id', 'searchable': false},
                        {data: 'customer_name', 'name': 'nso007_customers.customer_name'},
                        {data: 'customer_phone', 'name': 'nso007_customers.customer_phone'},
                        {data: 'amount', 'searchable': false},
                        {data: 'action', 'searchable': false},

                    ],
                "fnDrawCallback":function () {
                    let tr = $('.tbody tr');
                    let total_usd = 0;
                    let total_baht = 0;
                    let t_u='';
                    let t_b='';
                    $.each(tr,function (ii,vv) {
                        let amount = $(this).find('td:nth-child(8) span').data('amount');
                        let cur_id = $(this).find('td:nth-child(8) span').data('cur_id');
                        amount = amount?parseFloat(amount):0;
                        if(cur_id===2){
                            total_usd+=amount;
                        }
                        if(cur_id===3){
                            total_baht+=amount;
                        }
                        t_u = total_usd?('$'+total_usd.toFixed(2)):'';
                        t_b = total_baht?('฿'+total_baht.toFixed(2)):'';

                    });
                    $('.tfoot tr:first-child ').find('.green').html((t_u?(t_u+'<br>'):'')+t_b);
                }
            });
        }

        $(document).on('click', '#search', function () {
            let customer_id = $('select[name="customer_id"]').val();
            let start_date = $('input[name="from_date"]').val();
            let end_date = $('input[name="to_date"]').val();
            $('#print_table').DataTable().destroy();
            dataTableFilter(customer_id, start_date, end_date);
        });

        $(document).on('click', '.show_invoice', function () {
            let href = $(this).data('href');
            getHtmlFormShow(href,'400px');

        });
    </script>
@stop




