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

<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>کیش لیجر</title>
    <style>
        body {
            font-family: 'Noto Nastaliq Urdu', 'Jameel Noori Nastaleeq', 'Arial', sans-serif;
            direction: rtl;
            font-size: 14px;
            margin: 20px;
        }

        .table, .tcell, .thead {
            text-align: center;
            border: 1px solid black;
        }

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

        .tcell {
            padding: 4px 6px;
            word-break: break-word;
        }

        .thead {
            background-color: #f1f1f1;
            font-weight: bold;
        }

        @media print {
            body {
                margin: 0;
                font-size: 12px;
            }
            .table {
                page-break-inside: avoid;
            }
        }
    </style>
</head>

<body>

<div>
    <table style="width: 100%; margin-bottom: 10px;">
        <tr>
            <td style="width:33.33%; text-align: right">{{ $business_name->name }}</td>
            <td style="width:33.33%; text-align: center"><b>کیش لیجر</b></td>
            <td style="width:33.33%; text-align: left">
                <span style="font-weight: bold">سے:</span> <u>{{ $data['form_date'] }}</u><br>
                <span style="font-weight: bold">تک:</span> <u>{{ $data['to_date'] }}</u>
            </td>
        </tr>
    </table>
</div>

<br>

<table class="table">
    <thead>
    <tr>
        <th class="thead" style="width: 12%;">تاریخ</th>
        <th class="thead" style="width: 28%;">تفصیل</th>
        <th class="thead" style="width: 25%;">اکاؤنٹ کا نام</th>
        <th class="thead" style="width: 11%;">ڈیبٹ</th>
        <th class="thead" style="width: 11%;">کریڈٹ</th>
        <th class="thead" style="width: 13%;">بیلنس</th>
    </tr>
    </thead>
    <tbody>
    @php
        $balance = 0;
        $total_debit = 0;
        $total_credit = 0;
    @endphp

    @forelse ($data['ledgers'] as $ledger)
        @php
            $balance += $ledger->debit - $ledger->credit;
            $total_debit += $ledger->debit;
            $total_credit += $ledger->credit;
        @endphp
        <tr>
            <td class="tcell">{{ date('d-m-Y', strtotime($ledger->date)) }}</td>
            <td class="tcell">{{ $ledger->description }}</td>
            <td class="tcell">{{ $ledger->account_name ?? '---' }}</td>
            <td class="tcell">{{ number_format($ledger->debit, 0) }}</td>
            <td class="tcell">{{ number_format($ledger->credit, 0) }}</td>
            <td class="tcell">{{ number_format($balance, 0) }}</td>
        </tr>
    @empty
        <tr>
            <td class="tcell" colspan="6">کوئی ریکارڈ موجود نہیں</td>
        </tr>
    @endforelse

    <tr>
        <th class="tcell" colspan="3" 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">{{ number_format($total_debit - $total_credit, 0) }}</td>
    </tr>
    </tbody>
</table>

</body>
</html>
