{{--
    mPDF report layout. mPDF supports only a CSS subset — no flexbox or grid,
    so everything here is tables and block elements with explicit widths.
--}}
@php
    use App\Models\Expense;
    use App\Support\TrackerPeriod;

    $labels = config('tracker.labels');
    $currency = config('tracker.currency');
@endphp
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="utf-8">
    <style>
        body {
            font-size: 9pt;
            color: #000;
        }

        .banner {
            background-color: #2e75b6;
            color: #ffffff;
            font-size: 13pt;
            font-weight: bold;
            text-align: center;
            padding: 7px 8px;
        }

        .meta {
            width: 100%;
            font-size: 8pt;
            color: #444;
            margin: 6px 0 8px;
        }

        .meta td {
            padding: 0;
        }

        .meta .right {
            text-align: right;
        }

        .filters-note {
            background-color: #eef4fb;
            border: 0.5px solid #b8d1ea;
            color: #1f4e79;
            font-size: 8pt;
            padding: 4px 7px;
            margin-bottom: 7px;
        }

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

        table.sheet th,
        table.sheet td {
            border: 0.6px solid #2e75b6;
            padding: 4px 6px;
            vertical-align: top;
        }

        table.sheet thead th {
            background-color: #ffffff;
            color: #1f4e79;
            font-weight: bold;
            text-align: left;
        }

        table.sheet td.num,
        table.sheet th.num {
            text-align: center;
            width: 6%;
        }

        table.sheet .w-date { width: 11%; }
        table.sheet .w-cat { width: 14%; }
        table.sheet .w-pay { width: 13%; }
        table.sheet .w-remark { width: 16%; }

        table.sheet td.amount,
        table.sheet th.amount {
            text-align: right;
            width: 12%;
            font-weight: bold;
        }

        table.sheet tfoot td {
            background-color: #ffffff;
            color: #1f4e79;
            font-weight: bold;
        }

        table.sheet tfoot .total-label {
            text-align: right;
        }

        h2.section {
            font-size: 10pt;
            color: #1f4e79;
            margin: 14px 0 5px;
            page-break-after: avoid;    /* keep a heading with its table */
        }

        table.summary {
            width: 100%;
            border-collapse: collapse;
            font-size: 8.5pt;
            page-break-inside: avoid;   /* never orphan a single total row */
        }

        table.summary th,
        table.summary td {
            border: 0.6px solid #c9d8e8;
            padding: 3px 6px;
        }

        table.summary th {
            background-color: #eef4fb;
            color: #1f4e79;
            text-align: left;
        }

        table.summary td.n {
            text-align: right;
            width: 18%;
        }

        table.summary td.p {
            text-align: right;
            width: 12%;
            color: #555;
        }
    </style>
</head>
<body>

<div class="banner">{{ TrackerPeriod::heading() }}</div>

<table class="meta">
    <tr>
        {{-- Lohit-Bengali has no U+00B7, so plain ASCII punctuation only here. --}}
        <td>{{ TrackerPeriod::label() }}, {{ TrackerPeriod::duration() }}</td>
        <td class="right">তৈরি: {{ now()->format('d/m/Y h:i A') }}</td>
    </tr>
</table>

@if ($filterSummary)
    <div class="filters-note">ফিল্টার: {{ $filterSummary }}</div>
@endif

<table class="sheet">
    <thead>
        <tr>
            <th class="num">#</th>
            <th class="w-date">{{ $labels['date'] }}</th>
            <th class="w-cat">{{ $labels['category'] }}</th>
            <th>{{ $labels['description'] }}</th>
            <th class="w-pay">{{ $labels['payment_method'] }}</th>
            <th class="w-remark">{{ $labels['remark'] }}</th>
            <th class="amount">{{ $labels['amount'] }}</th>
        </tr>
    </thead>
    <tbody>
        @forelse ($expenses as $i => $expense)
            <tr>
                <td class="num">{{ $i + 1 }}</td>
                <td>{{ $expense->expense_date?->format('d/m/Y') }}</td>
                <td>{{ $expense->categoryName() }}</td>
                <td>{{ $expense->description }}</td>
                <td>{{ $expense->payment_method }}</td>
                <td>{{ $expense->remark }}</td>
                <td class="amount">{{ Expense::money($expense->amount) }}</td>
            </tr>
        @empty
            <tr>
                <td colspan="7" style="text-align:center; padding:14px;">কোনো খরচ পাওয়া যায়নি</td>
            </tr>
        @endforelse
    </tbody>
    <tfoot>
        <tr>
            <td colspan="6" class="total-label">{{ $labels['total'] }}</td>
            <td class="amount">{{ Expense::money($summary['total']) }}</td>
        </tr>
    </tfoot>
</table>

@if ($summary['by_category']->isNotEmpty())
    <h2 class="section">ক্যাটাগরি অনুযায়ী খরচ</h2>
    <table class="summary">
        <thead>
            <tr>
                <th>{{ $labels['category'] }}</th>
                <th class="n">{{ $labels['amount'] }}</th>
                <th class="p">%</th>
            </tr>
        </thead>
        <tbody>
            @foreach ($summary['by_category'] as $name => $amount)
                <tr>
                    <td>{{ $name }}</td>
                    <td class="n">{{ $currency }}{{ Expense::money($amount) }}</td>
                    <td class="p">{{ $summary['total'] > 0 ? round($amount / $summary['total'] * 100) : 0 }}%</td>
                </tr>
            @endforeach
        </tbody>
    </table>

    <h2 class="section">পেমেন্ট মাধ্যম অনুযায়ী</h2>
    <table class="summary">
        <thead>
            <tr>
                <th>{{ $labels['payment_method'] }}</th>
                <th class="n">{{ $labels['amount'] }}</th>
                <th class="p">%</th>
            </tr>
        </thead>
        <tbody>
            @foreach ($summary['by_method'] as $name => $amount)
                <tr>
                    <td>{{ $name }}</td>
                    <td class="n">{{ $currency }}{{ Expense::money($amount) }}</td>
                    <td class="p">{{ $summary['total'] > 0 ? round($amount / $summary['total'] * 100) : 0 }}%</td>
                </tr>
            @endforeach
        </tbody>
    </table>

    <h2 class="section">সারসংক্ষেপ</h2>
    <table class="summary">
        <tbody>
            <tr>
                <td>এন্ট্রি সংখ্যা</td>
                <td class="n">{{ $summary['count'] }}</td>
            </tr>
            <tr>
                <td>গড় খরচ</td>
                <td class="n">{{ $currency }}{{ Expense::money(round($summary['average'])) }}</td>
            </tr>
            <tr>
                <td>সবচেয়ে বড় খরচ</td>
                <td class="n">{{ $currency }}{{ Expense::money($summary['largest']) }}</td>
            </tr>
            <tr>
                <td><b>{{ $labels['total'] }}</b></td>
                <td class="n"><b>{{ $currency }}{{ Expense::money($summary['total']) }}</b></td>
            </tr>
        </tbody>
    </table>
@endif

</body>
</html>
