<!DOCTYPE html>
<html lang="ur" dir="rtl">

<head>
    <title>General Ledger - Print</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            font-size: 12px;
        }

        .container {
            width: 95%;
            margin: auto;
        }

        h2 {
            text-align: center;
            margin-bottom: 2px;
        }

        h4 {
            text-align: center;
            margin-top: 0;
            margin-bottom: 5px;
            font-weight: normal;
        }

        .khata-row {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 8px;
            font-size: 12px;
            direction: rtl;
        }

        .khata-row .khata-name  { font-weight: bold; font-size: 13px; }
        .khata-row .khata-label { font-size: 12px; }
        .khata-row .khata-code  { font-weight: bold; font-size: 12px; }

        /* Khata type badge */
        .type-badge {
            display: inline-block;
            padding: 2px 8px;
            font-size: 10px;
            font-weight: bold;
            border-radius: 3px;
            margin-bottom: 6px;
        }
        .type-badge.customer { background: #cfe2ff; color: #0a3d91; border: 1px solid #9ec5fe; }
        .type-badge.supplier { background: #e2e3e5; color: #383d41; border: 1px solid #d6d8db; }

        .date-line {
            text-align: right;
            font-size: 10px;
            margin-bottom: 6px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 4px;
            direction: rtl;
        }

        th {
            padding: 7px 8px;
            text-align: center;
            border: 1px solid #000;
            font-size: 12px;
            background: #fff;
            white-space: nowrap;
        }

        td {
            padding: 7px 8px;
            text-align: right;
            vertical-align: middle;
            border-left: 1px solid #000;
            border-right: 1px solid #000;
            border-top: none;
            border-bottom: 1px dashed #555;
            font-size: 12px;
        }

        td.col-date    { text-align: center; white-space: nowrap; width: 75px; }
        td.col-detail  { text-align: right; font-weight: bold; font-size: 13px; }
        td.col-amount  { text-align: center; white-space: nowrap; width: 105px; }
        td.col-balance { text-align: center; font-weight: bold; white-space: nowrap; width: 115px; }
        td.col-drcr    { text-align: center; font-weight: bold; font-size: 11px; width: 32px; }

        tr.opening-row td {
            text-align: center !important;
            font-weight: bold;
            background: #f9f9f9;
        }

        tr.total-row td {
            border-top: 2px solid #000 !important;
            border-bottom: 1px solid #000 !important;
            background: #f0f0f0;
            font-weight: bold;
            text-align: center;
            font-size: 12px;
        }

        .no-print {
            margin-top: 20px;
            display: block;
            padding: 10px 20px;
            background: #007bff;
            color: white;
            border: none;
            cursor: pointer;
            font-size: 14px;
        }

        @media print {
            .no-print   { display: none; }
            tr          { page-break-inside: avoid; }
            thead       { display: table-header-group; }
            tfoot       { display: table-footer-group; }
        }
    </style>
</head>

<body>
    <div class="container">

        {{-- Company Header --}}
        <h2>M.D.S</h2>
        <h4>
            @if($khataType === 'customer')
                Customer Ledger — کسٹمر کھاتہ
            @else
                Supplier Ledger — سپلائر کھاتہ
            @endif
        </h4>

        {{-- Type badge --}}
        <div style="text-align:center; margin-bottom:6px;">
            @if($khataType === 'customer')
                <span class="type-badge customer">&#128100; Customer</span>
            @else
                <span class="type-badge supplier">&#128666; Supplier</span>
            @endif
        </div>

        {{-- Khata Info Row --}}
        <div class="khata-row">
            <div class="khata-name">کھاتہ &nbsp; {{ $khataName }}</div>
            <div class="khata-label">کھا تہ رجسٹر</div>
            <div class="khata-code">کھاتہ کوڈ &nbsp; {{ $selectedKhata }}</div>
        </div>

        {{-- Date Range --}}
        @if ($fromDate || $toDate)
            <div class="date-line">
                مورخہ:
                {{ $fromDate ? \Carbon\Carbon::parse($fromDate)->format('d/m/Y') : '—' }}
                تا
                {{ $toDate  ? \Carbon\Carbon::parse($toDate)->format('d/m/Y')  : '—' }}
            </div>
        @endif

        {{-- Ledger Table --}}
        @if (empty($ledgerData) || count($ledgerData) <= 1)
            <p style="text-align:center; padding:30px; color:#888;">کوئی اندراج نہیں ملا۔</p>
        @else
            <table>
                <thead>
                    <tr>
                        <th colspan="2">میزان</th>
                        <th>کریڈٹ رقم</th>
                        <th>ڈ یبٹ رقم</th>
                        <th>تفصیل</th>
                        <th>تاریخ</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($ledgerData as $row)
                        @if ($row['is_opening'])
                            <tr class="opening-row">
                                <td class="col-drcr"></td>
                                <td class="col-balance">0</td>
                                <td colspan="3" class="col-detail"><strong>Opening Balance</strong></td>
                                <td class="col-date">{{ $row['date'] }}</td>
                            </tr>
                        @else
                            <tr>
                                <td class="col-drcr">{{ $row['dr_cr'] }}</td>
                                <td class="col-balance">{{ number_format($row['balance'], 0) }}</td>
                                <td class="col-amount">{{ $row['credit'] ? number_format($row['credit'], 0) : '' }}</td>
                                <td class="col-amount">{{ $row['debit']  ? number_format($row['debit'],  0) : '' }}</td>
                                <td class="col-detail">{{ $row['detail'] }}</td>
                                <td class="col-date">{{ $row['date'] }}</td>
                            </tr>
                        @endif
                    @endforeach

                    {{-- کُل row --}}
                    <tr class="total-row">
                        <td colspan="2">کُل</td>
                        <td>{{ number_format($totalCredit, 0) }}</td>
                        <td>{{ number_format($totalDebit,  0) }}</td>
                        <td colspan="2"></td>
                    </tr>
                </tbody>
            </table>
        @endif

        <button class="no-print" onclick="window.print()">Print Report</button>

    </div>
</body>
</html>