<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>{{ $data['driver']->name }} Ledger</title>
    <style>
        body {
            font-family: 'Noto Nastaliq Urdu', 'Jameel Noori Nastaleeq', 'Arial', sans-serif;
            direction: rtl;
        }
        .table,
        .tcell,
        .thead {
            text-align: center;
            border: 1px solid black;
            text-align: center;
        }

        .table {
            width: 100%;
            border-collapse: collapse;
        }

    </style>
</head>

<body>
<table style="width: 100%;border-collapse: collapse; margin-bottom: 10px">
    <tr>
        <td style="width:33.33%; text-align: left"><h4>{{ $business_name->name }}</h4></td>
        <td style="width:33.33%; text-align: center"><b>Driver Ledger</b></td>
        <td style="width:33.33%; text-align: right"></td>
    </tr>
</table>

<table style="width: 100%;border-collapse: collapse; margin-bottom: 10px">
    <tr>
        <td style="width:33.33%; text-align: left">
            <h4>{{ $data['driver']->name }}</h4>
            @php
                $joining_date = $data['driver']->joining_date;
                $resign_date = $data['driver']->resign_date;
                if($resign_date !=null){
                          $avg = $data['driver']->calculate_job_days($joining_date, $resign_date);
                }else{
                    $avg = 'NO Resigned';
                }
            @endphp

        </td>
        <td style="width:33.33%; text-align: center">
            <span style="font-weight: bold">Joining Date:</span> <span
                style="text-decoration: underline">{{ date('M j, Y',strtotime($joining_date)) }}</span><br>
            <span style="font-weight: bold">Resign Date:</span>
            <span
                style="text-decoration: underline">{{ $resign_date==null? 'N/A': date('M j, Y',strtotime($resign_date)) }}</span><br>
            <span style="font-weight: bold">Avg:</span>
            <span style="text-decoration: underline">{{ $avg}}</span>
        </td>
        <td style="width:33.33%; text-align: right">
            <span style="font-weight: bold">From Date:</span> <span
                style="text-decoration: underline">{{ $data['form_date'] }}</span><br>
            <span style="font-weight: bold">To Date:</span> <span
                style="text-decoration: underline">{{ $data['to_date'] }}</span>
        </td>
    </tr>
</table>

<table class="table">
    <thead>
    <tr>
        <th class="thead">Date updated</th>
        <th class="thead">Particular</th>
        <th class="thead">Vehicle</th>
        <th class="thead">Trip Salary</th>
        <th class="thead">Trip Salary Days</th>
        <th class="thead">Debit</th>
        <th class="thead">Credit</th>
        <th class="thead">Balance</th>
        {{-- <th class="thead">Status</th> --}}
    </tr>
    </thead>
    <tbody>
    @php
        $balance = 0;
        $total_debit = 0;
        $total_credit = 0;
        $total_Paid_salary_days=0;


    @endphp
    @forelse ($data['ledgers'] as $key =>$ledger)

        @php
            $balance= ( $balance + $ledger->debit) - $ledger->credit;
            $show_balance =number_format($balance,0);

            if($balance<0){
                $show_balance .=" Dr";
            }else{
                $show_balance .=' Cr';
            }
                $total_debit = $total_debit + $ledger->debit;
                $total_credit = $total_credit + $ledger->credit;
        @endphp


        @if((int)$ledger->debit > 0  || (int)$ledger->credit > 0)



            <tr>
                <td class="thead">{{ date('M j, Y', strtotime($ledger->date)) }}</td>
                <td class="thead">{{ $ledger->description }}</td>
                <td class="thead">
                    @if($ledger->trip)
                        @if($ledger->trip->vehicle)
                            {{  $ledger->trip->vehicle->vehicle_no }}
                        @else
                            Vehicle Not Found
                        @endif
                    @else
                        Trio Not Found
                    @endif
                </td>

                <td class="thead"> {{ $ledger->driver_trip_salary }} </td>
                <td class="thead">

                    @php $days=0; @endphp

                    @if($ledger->trip)
                        @if($ledger->trip->tripSalary)
                            @foreach($ledger->trip->tripSalary->salary_details as $Driverday)
                            <?php
                                if($data['driver']->id == $Driverday->driver->id)
                                {
                                        $days = $Driverday->trip_days;
                                }
                            ?>
                            @endforeach
                        @else
                            Vehicle Not Found
                        @endif
                    @else
                        Trio Not Found
                    @endif
                    Days: {{ $days }}

                    @if($ledger->description != '')
                        @php
                            $explodedTripDesc = explode(" ", $ledger->description);
                            $salary='';
                        @endphp
                        @if(!isset($explodedTripDesc[5], $explodedTripDesc[6]))
                           @php
                                $salary = $explodedTripDesc[5];
                            @endphp
                        @endif

                        @if($salary == 'salary')
                            @php
                                $total_Paid_salary_days += $days;
                            @endphp

                        @endif
                    @endif



                </td>
                <td class="thead">{{ number_format($ledger->debit,0) }}  </td>
                <td class="thead">{{ number_format($ledger->credit,0) }}   </td>
                <td class="thead">{{ $show_balance }}</td>
            </tr>

        @endif

    @empty
        <tr>
            <td class="tcell" style="text-align: center" colspan="7"> No Data Available</td>
        </tr>
    @endforelse
    <tr>
        <th class="tcell" style="text-align: right" colspan="4"> Total:</th>
        <td class="tcell" colspan="" style="text-align: center; padding-left:30px">
             {{ $total_Paid_salary_days }}
        </td>
        <td class="tcell" colspan="" style="text-align: center; padding-left:30px">
            {{ number_format($total_debit, 0) }}
        </td>
        <td class="tcell" colspan="" style="text-align: center; padding-left:30px">
            {{ number_format($total_credit, 0) }}
        </td>
        <td class="tcell" colspan="" style="text-align: center; padding-left:30px">
            {{ number_format(  $total_debit-$total_credit , 0) }}
        </td>

    </tr>
    </tbody>
</table>

{{-- <div>
<div style="text-align: right;">
    Total: {{ $data['total_amount'] }}
</div>
</div> --}}
</body>

</html>
