<!doctype html>
<html lang="ur" dir="rtl">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>{{ $data['broker']->name }} لیجر</title>
    <style>
        body {
            font-family: 'Noto Nastaliq Urdu', 'Jameel Noori Nastaleeq', 'Arial', sans-serif;
            direction: rtl;
        }
        .table, .tcell, .thead {
            border: 1px solid black;
            text-align: right;
        }
        .table {
            width: 100%;
            border-collapse: collapse;
        }
        .tcell {
            padding: 6px;
        }
        .thead {
            background-color: #f1f1f1;
            font-weight: bold;
        }
        .positive-balance { color: green; font-weight: bold; }
        .zero-balance { color: goldenrod; font-weight: bold; }
        .negative-balance { color: red; font-weight: bold; }
    </style>
</head>
<body>

<table style="width: 100%; margin-bottom: 10px;">
    <tr>
        <td style="width:33.33%; text-align: right;"><h4>{{ $business_name->name }}</h4></td>
        <td style="width:33.33%; text-align: center;"><strong>بروکر لیجر</strong></td>
        <td style="width:33.33%; text-align: left;">
            <strong>سے:</strong> {{ $data['form_date'] }}<br>
            <strong>تک:</strong> {{ $data['to_date'] }}
        </td>
    </tr>
</table>

<table style="width: 100%; margin-bottom: 10px;">
    <tr>
        <td><h4>{{ $data['broker']->name }}</h4></td>
    </tr>
</table>

<table class="table">
    <thead>
    <tr>
        <th class="thead">تاریخ</th>
        <th class="thead">تفصیل</th>
        <th class="thead">گاڑی نمبر</th>
        <th class="thead">ٹرپ نمبر</th>
        <th class="thead">مال کا نام</th>
        <th class="thead">ڈیبٹ</th>
        <th class="thead">کریڈٹ</th>
        <th class="thead">بیلنس</th>
    </tr>
    </thead>
    <tbody>
    @php
        $balance = 0;
        $total_debit = 0;
        $total_credit = 0;
    @endphp

    @forelse($data['ledgers'] as $ledger)
        @php
            $balance = ($balance + $ledger->credit) - $ledger->debit;
            $total_debit += $ledger->debit;
            $total_credit += $ledger->credit;

            $tripDetail = \App\Models\TripDetail::find($ledger->trip_detail_id);
            $trip = $tripDetail?->trip;
            $vehicle = $tripDetail?->vehicle;

            $tripNo = $trip?->trip_no ?? $trip?->id ?? '---';
            $vehicleNo = $vehicle?->vehicle_no ?? '---';
            $itemName = $tripDetail?->item_name ?? '---';
        @endphp
        <tr>
            <td class="tcell">{{ date('d-m-Y', strtotime($ledger->date)) }}</td>
            <td class="tcell">{{ $ledger->description }}</td>
            <td class="tcell">{{ $vehicleNo }}</td>
            <td class="tcell">{{ $tripNo }}</td>
            <td class="tcell">{{ $itemName }}</td>
            <td class="tcell">{{ number_format($ledger->debit, 0) }}</td>
            <td class="tcell">{{ number_format($ledger->credit, 0) }}</td>
            <td class="tcell">
                <span class="
                    @if($balance == 0) positive-balance
                    @elseif($balance < 0) zero-balance
                    @else negative-balance
                    @endif
                ">{{ number_format($balance, 0) }}</span>
            </td>
        </tr>
    @empty
        <tr>
            <td class="tcell" colspan="8">کوئی ڈیٹا موجود نہیں</td>
        </tr>
    @endforelse

    <tr>
        <th class="tcell" colspan="5" style="text-align: center;">کل</th>
        <td class="tcell">{{ number_format($total_debit, 0) }}</td>
        <td class="tcell">{{ number_format($total_credit, 0) }}</td>
        <td class="tcell">
            <span class="
                @if(($total_credit - $total_debit) == 0) positive-balance
                @elseif(($total_credit - $total_debit) < 0) zero-balance
                @else negative-balance
                @endif
            ">{{ number_format($total_credit - $total_debit, 0) }}</span>
        </td>
    </tr>
    </tbody>
</table>

</body>
</html>
