@extends('backend.layouts.app')

@section('content')
<div class="row page-titles">
   <div class="col-md-5 align-self-center">
      <h4 class="text-themecolor">Reports Manager</h4>
   </div>
   <div class="col-md-7 align-self-center text-right">
      <div class="d-flex justify-content-end align-items-center">
         <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{{route('dashboard')}}">Dashboard</a></li>
            <li class="breadcrumb-item">Reports Manager</li>
            <li class="breadcrumb-item active">Audit checks</li>
         </ol>
      </div>
   </div>
</div>
<div class="container-fluid">
    <div class="content-wrapper">
        @if (session('status'))
            <div class="alert alert-success alert-dismissible fade show" role="alert"><i class="far fa-check-circle"></i>
                {{ session('status') }}
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
        @endif
        @if (session('error'))
            <div class="alert alert-danger alert-dismissible fade show" role="alert"><i class="far fa-check-circle"></i>
                {{ session('error') }}
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
        @endif        

        <section class="content">
            <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-body">
                            <h4>Audit checks<p style="font-size: 10px; color:gray"></p></h4>                            
                            <br>
                            <div class="table-responsive">
                                <table id="airports" class="table table-bordered table-hover">
                                    <thead>
                                        <tr>
                                            <th>Date</th>
                                            <th>Opening Balance</th>
                                            <th>Purchased</th>
                                            <th>Consumed</th>
                                            <th>Balance</th>
                                            <th>Balance Members</th>
                                            {{-- <th>Balance Members</th> --}}
                                            <th>Difference</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @if(count($report) > 0)
                                            @php
                                                $prevBalance = 0;
                                            @endphp
                                            @foreach($report as $row)
                                                @php
                                                    $purchased = (float) $row->purchased; // Convert to number
                                                    $consumed = (float) $row->consumed;  // Convert to number
                                                    $balance = ($prevBalance + $row->purchased) - $row->consumed;
                                                @endphp
                                                <tr>
                                                    <td>{{ \Carbon\Carbon::parse($row->transaction_date)->format('d/m/y') }}</td>
                                                    <td>{{ number_format($prevBalance) }}</td>
                                                    <td>{{ number_format($purchased) }}</td>
                                                    <td>{{ number_format($consumed) }}</td>
                                                    <td>{{ number_format($balance) }}</td>

                                                    @php
                                                          $mem_bal = ($prevBalance + $purchased) - $consumed;
                                                          $diff = $balance - $mem_bal;
                                                        
                                                    @endphp
                                                    <td>{{ $mem_bal }}</td>
                                                    <td>{{ ($diff ==  0)  ? 'Nill' : $diff}}</td>
                                                </tr>
                                                @php
                                                    $prevBalance = $balance;
                                                @endphp
                                            @endforeach
                                        @else
                                            <tr>
                                                <td colspan="6">
                                                    <h6><center>No Records Found</center></h6>
                                                </td>
                                            </tr>
                                        @endif
                                    </tbody>
                                </table>
                            </div>
                            
                        </div>                       
                    </div>
                </div>
            </div>
        </section>
    </div>
</div>
@stop 