{{--Get the Page Format--}}
@extends('layouts.app')

{{--Set Page Title--}}
@section('title', 'Daily Sales Reports')

{{--Include any Internal JS Code--}}
@section('internal-js')
    <script>
        $(document).ready(function () {
            $('#insurers1').DataTable( {
                dom: 'lBfrtip',
                buttons: [
                    { extend: 'excel', className: 'download-buttons'},
                ],
                footerCallback: function ( row, data, start, end, display ) {
                    var api = this.api(), data;
                    var numberRenderer = $.fn.dataTable.render.number(',', '.', 2).display;

                    // Remove the formatting to get integer data for summation
                    var intVal = function ( i ) {
                        return typeof i === 'string' ? i.replace(/[\.]/g, '.').replace(/[\,]/g, '') * 1 : typeof i === 'number' ? i : 0;
                    };

                    //Comm Amount
                    totalSalesCount = api.column( 3 ).data().reduce( function (a, b) { return intVal(a) + intVal(b)}, 0 );
                    currentSalesCount = api.column( 3, { page: 'current'} ).data().reduce( function (a, b) { return intVal(a) + intVal(b)}, 0 );
                    // Update Footer - Sale Amount
                    $( api.column( 3 ).footer() ).html(currentSalesCount +'<br>('+ totalSalesCount +')').css('color', 'blue');

                    //Comm Amount
                    totalSalesAmount = api.column( 4 ).data().reduce( function (a, b) { return intVal(a) + intVal(b)}, 0 );
                    currentSalesAmount = api.column( 4, { page: 'current'} ).data().reduce( function (a, b) { return intVal(a) + intVal(b)}, 0 );
                    // Update Footer - Sale Amount
                    $( api.column( 4 ).footer() ).html(numberRenderer(currentSalesAmount) +'<br>('+ numberRenderer(totalSalesAmount) +')').css('color', 'blue');

                    @if(auth()->user()->admin == 1)
                    //PROFIT SECTION
                    totalProfitAmount = api.column( 6 ).data().reduce( function (a, b) { return intVal(a) + intVal(b)}, 0 );
                    currentProfitAmount = api.column( 6, { page: 'current'} ).data().reduce( function (a, b) { return intVal(a) + intVal(b)}, 0 );
                    // Update Footer - Sale Amount
                    $( api.column( 6 ).footer() ).html(numberRenderer(currentProfitAmount) +'<br>('+ numberRenderer(totalProfitAmount) +')').css('color', 'blue');
                    @endif
                },
            } );


            $('#date-from').on('change', function (){
                $('#date-to').attr('required', true);
            });
            $('#date-to').on('change', function (){
                $('#date-from').attr('required', true);
            });
        });
        {{--Create a Page to view a List of Items Sold. From the Page, we will have a Place to Print a Receipt--}}
        function viewSoldItems(id){
            var route_link = '/view-sold-items-on-receipt/'+id;

            $('#edit_dialog_content').html('<div style="text-align: center; margin: 20px 0 20px 0;"><img src="{{asset('images/loads/loader.gif')}}" border=0></div>')
                .load(encodeURI(route_link))
                .dialog({
                    modal: true,
                    width: 600,
                    title: "View Sold items on the Selected  Receipt",
                    open: function() {
                        $(this).parent().css({'top': 50});
                    }
                });
        }
    </script>
@endsection

{{--Include any Internal CSS Code--}}
@section('internal-css')
    <style>
        #insurers1 tr:nth-child(even) {
            background-color: #f2f2f2;
        }
        #insurers1 th, td {
            text-align: left;
            padding: 5px;
        }
        #insurers1 tr:hover {background-color: #d8d9e1;}
    </style>

@endsection

{{--Control Page Header/Links--}}
@section('headers')
    @include('headers.inventory-header')
@endsection

{{--Set Page Heading--}}
@section('heading', 'Daily Sales Report')
@section('done-by', '')

{{--Page Content Goes here--}}
@section('content')
    <div class="inputDivider" style="margin-top: 10px; text-align: left; background-color: lightcyan">
        {!! Form::open(['action'=>['Modules\InventoryDashboardController@showSalesReport'], 'method'=>'POST', 'id'=>'filter-sales-report', 'class'=>'fields-small']) !!}
        <div class="guestDetails_input_right">
            <div class="guestDetails_inputLabel">
                &nbsp;
            </div>
            <div class="guestDetails_input_right">
                <button type="submit" name="search" style="cursor: pointer"><i class="fa fa-search" aria-hidden="true"></i></button>
            </div>
        </div>
        <div class="guestDetails_input_right">
            <div class="guestDetails_inputLabel">
                Date Added To:<span class="error">*</span>
            </div>
            <div class="guestDetails_input_right">
                <input type="datetime-local" id="date-to" name="to"  class="input-text-height" value="{{$search['to'] ?? date('Y-m-d H:i')}}">
            </div>
        </div>
        <div class="guestDetails_input_right">
            <div class="guestDetails_inputLabel">
                Date Added From:<span class="error">*</span>
            </div>
            <div class="guestDetails_input_right">
                <input type="datetime-local" id="date-from" name="from"  class="input-text-height" value="{{$search['from'] ?? date('Y-m-d H:i')}}">
            </div>
        </div>
        <div class="guestDetails_inputDividerNone" style="height: 0"></div>
        {!! Form::close() !!}
    </div>
    <div class="guestDetails_inputDivider"></div>
    <!--View and Setup your Insurers-->
    <div>
        <!--Export Links/Buttons-->
        <span onclick="this.parentElement.style.display='none'" class="topright" style="margin-left: 20px">&times</span>
        <table id="insurers1" style="width: 100%; border-collapse: collapse; ">
            <thead>
            <tr style="background-color: #a1b5d3">
                <th>No</th>
                <th>Item Name.</th>
                <th>Unit Sell Price</th>
                <th>Quantity</th>
                <th>Total Amount Paid</th>
                @if(auth()->user()->admin == 1)
                    <th>Unit Buy Price</th>
                    <th>Profit Made</th>
                @endif
                <th>Mode</th>
                <th>REF</th>
                <th>Date Sold</th>
                <th>User</th>
                <th>View Receipt</th>
            </tr>
            </thead>
            <tbody>
            @foreach($list as $row)
                @if($row->item)
                    <tr>
                        <td class="ar"></td>
                        <td><b class="error">{!!  strtoupper($row->item->name) !!}</b></td>
                        <td>{!! number_format($row->unit_price_sell,2) !!}</td>
                        <td style="font-weight: bold" class="error">{{abs($row->quantity)}}</td>
                        <td style="font-weight: bold">{!! number_format($row->total_price_sell,2) !!}</td>
                        @if(auth()->user()->admin == 1)
                            <td>{!! number_format($row->unit_price_buy,2) !!}</td>
                            <td style="font-weight: bold" class="success">{!! number_format($row->profit,2) !!}</td>
                        @endif
                        @if($row->receipt)
                            <td>@if($row->receipt->modeOfPayment != null)
                                    {!! $row->receipt->modeOfPayment->name !!}
                                    @if($row->receipt->subModeOfPayment != null)
                                        - {!! $row->receipt->subModeOfPayment->name !!}
                                    @endif
                                @endif</td>
                            <td>{!! $row->receipt->ref !!}</td>
                        @else
                            <td></td>
                            <td></td>
                        @endif
                        <td>{!! date('d/m/Y H:i:s a', strtotime($row->created_at)) !!}</td>
                        <td>
                            @if($row->user)
                                {!! $row->user->firstName." ".$row->user->lastName !!}
                            @endif
                        </td>
                        <td><i class="fa fa-eye error" onclick="viewSoldItems('{{$row->invoice_id}}')" style="cursor: pointer; font-size: 14px" title="Click to View Receipt"></i></td>
                    </tr>
                @endif
            @endforeach
            </tbody>
            <tfoot>
            <tr style="background-color: #a1b5d3">
                <th>No</th>
                <th>Item Name.</th>
                <th>Unit Sell Price</th>
                <th>Quantity</th>
                <th>Total Amount Paid</th>
                @if(auth()->user()->admin == 1)
                    <th>Unit Buy Price</th>
                    <th>Profit Made</th>
                @endif
                <th>Mode</th>
                <th>REF</th>
                <th>Date Sold</th>
                <th>User</th>
                <th>View Receipt</th>
            </tr>
            </tfoot>
        </table>

    </div>
@endsection
{{--System Hints Page--}}
@section('modal_heading')
    <h1>Sales Report</h1>
@endsection
@section('modal_title')
    <h3><u>Dashboard Functionalities</u></h3>
    <ol><b>
            <li>View Sales per a Specified Period</li>
            <li>Filter through available Sales Reports</li>
            <li>Export Sales Report</li>
        </b>
    </ol>
@endsection
