@extends(branchMenu())
@section('content')
    <?php Fun::lang()?>
    @php
        $from_date = @$_GET['from_date'];
        $to_date = @$_GET['to_date'];
        $user_id = @$_GET['user_id'];
        $iloveucustomer = @$_GET['iloveucustomer'];
        $product_id = @$_GET['product_id'];
        $sale_type = @$_GET['sale_type'];
    @endphp
    <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_sale_detail')}}</h3>
                            </div>
                            <div class="pull-right">
                                @if(myBranch()->main)
                                    @include('report.sale-report.sub-menu')
                                @endif
                            </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('user_id',$seller,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.customer')}}
                                </div>
                                <div class="form-group col-sm-9 col-md-9">
                                    {!! Form::select('customer_id',[],null, ['placeholder'=>__('administrator.please_select'),'class' => 'form-control search_customer']) !!}
                                </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', $from_date, 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', $to_date, 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_sale_detail')}}"><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.sale_date')}}</th>
                        <th>{{__('administrator.invoice')}}</th>
                        <th>{{__('administrator.seller')}}</th>
                        <th>{{__('administrator.customer_name')}}</th>
                        <th>{{__('administrator.barcode')}}</th>
                        <th>{{__('administrator.products')}}</th>
                        <th>{{__('administrator.sale_price')}}</th>
                        <th>{{__('administrator.item_discount')}}</th>
                        <th>{{__('administrator.total_discount')}}</th>
                        <th>{{__('administrator.sale_qty')}}</th>
                        <th>{{__('administrator.total')}}</th>
                    </tr>
                    </thead>
                    <tbody class="tbody">

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

@section('script')

    <script type="text/javascript">

        $(document).ready(function () {
            dataTableFilter()
        });

        function dataTableFilter(type = '', user_id = '', customer_id = '', product_id = '', start_date = '', end_date = '') {
            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: tbLeng,
                ajax: {
                    url: "{{ route('report-sale-detail') }}",
                    data: {
                        type,
                        user_id,
                        customer_id,
                        product_id,
                        start_date,
                        end_date,
                        branch_id,
                    }
                },
                columns:
                    [
                        {data: 'DT_RowIndex', 'searchable': false},
                        {data: 'branch_id', 'searchable': false},
                        {data: 'sale_date', 'searchable': false},
                        {data: 'invoice_number', 'name': 'nso007_sales.invoice_number'},
                        {data: 'user_id', 'searchable': false},
                        {data: 'customer_id', 'searchable': false},
                        {data: 'barcode', 'name': 'nso007_sale_details.barcode'},
                        {data: 'product_name', 'name': 'nso007_products.product_name'},
                        {data: 'sale_price', 'searchable': false},
                        {data: 'dis', 'searchable': false},
                        {data: 'total_discount', 'searchable': false},
                        {data: 'qty', 'searchable': false},
                        {data: 'amount', 'searchable': false},

                    ],
                "fnDrawCallback": function () {
                    let tr = $('.tbody tr');
                    let total_usd = 0;
                    let total_riel = 0;
                    let t_u = '';
                    let t_b = '';
                    let total_qty = 0;
                    $.each(tr, function (ii, vv) {
                        let row_data = $(this).find('td:nth-child(13) span')
                        let amount = row_data.data('amount');
                        let cur_id = row_data.data('cur_id');
                        let qty = $(this).find('td:nth-child(12)').text();
                        qty = qty ? parseFloat(qty) : 0;
                        total_qty += qty;
                        amount = amount ? parseFloat(amount) : 0;
                        if (cur_id === 2) {
                            total_usd += amount;
                        }
                        if (cur_id === 1) {
                            total_riel += amount;
                        }
                        t_u = total_usd ? ('$' + addComma(total_usd.toFixed(2))) : '';
                        t_b = total_riel ? ('៛' + addComma(total_riel.toFixed(2))) : '';

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

        $(document).on('click', '#search', function () {
            let type = $('select[name="type"]').val();
            let user_id = $('select[name="user_id"]').val();
            let customer_id = $('select[name="customer_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, user_id, customer_id, product_id, start_date, end_date);
        });
    </script>
@stop




