{{-- resources/views/dashboard/print.blade.php --}}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>📊 Excel‑style Dashboard – {{ auth()->user()->name }}</title>
    <style>
        /* ===== RESET – clean spreadsheet look ===== */
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: 'Calibri', 'Arial', 'Helvetica', sans-serif; /* Excel default */
            background: white;
            color: #1e1e1e;
            font-size: 10.5pt;
            line-height: 1.3;
            margin: 0.8in 0.6in; /* standard print margins */
        }

        .print-wrap {
            width: 100%;
            margin: 0 auto;
            overflow-x: auto;
        }

        /* ----- header (company identity) ----- */
        .company-header {
            display: flex;
            align-items: center;
            gap: 15px;
            margin-bottom: 25px;
            border-bottom: 2px solid #4f4f4f;
            padding-bottom: 12px;
        }
        .logo-placeholder {
            width: 70px;
            height: 70px;
            background: #f3f3f3;
            border: 1px solid #aaa;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 20pt;
            color: #2b5797;
            font-weight: 600;
        }
        .company-details {
            flex: 1;
        }
        .company-name {
            font-size: 22pt;
            font-weight: 600;
            letter-spacing: 0.5px;
            color: #1e3a5f;
        }
        .report-title {
            font-size: 14pt;
            color: #2e2e2e;
            margin-top: 4px;
        }
        .print-date {
            font-size: 9pt;
            color: #5e5e5e;
            margin-top: 4px;
        }

        /* ----- spreadsheet tables ----- */
        table {
            width: 100%;
            border-collapse: collapse;
            margin: 12px 0 8px;
            table-layout: auto;
            border: 1px solid #a0a0a0; /* outer border like Excel range */
            word-wrap: break-word;
            page-break-inside: avoid;
        }
        th, td {
            border: 0.5px solid #b0b0b0; /* fine gridlines */
            padding: 6px 8px;
            vertical-align: top;
            word-wrap: break-word;
            max-width: 200px;
        }
        th {
            background-color: #e4e4e4;   /* classic light gray header */
            font-weight: 600;
            text-align: center;
            font-size: 10pt;
            color: #1e1e1e;
            border-bottom: 1px solid #7f7f7f;
        }
        td {
            background-color: white;
        }
        /* alternating row shading – subtle */
        tbody tr:nth-child(even) td {
            background-color: #fafafa;
        }
        /* numbers */
        .text-end, td.text-end, th.text-end {
            text-align: right;
        }
        .text-success { color: #006100; font-weight: 500; } /* excel green */
        .text-danger  { color: #9c0006; font-weight: 500; } /* excel red */
        .text-info    { color: #1f4d7a; font-weight: 500; } /* deep blue */

        /* inline badges (like type indicators) */
        .badge {
            display: inline-block;
            background: #e8e8e8;
            padding: 2px 8px;
            font-size: 9pt;
            border: 0.5px solid #aaa;
            border-radius: 2px;
            font-weight: 400;
        }

        /* section headings – like sheet tabs */
        .section-title {
            font-size: 16pt;
            font-weight: 600;
            margin: 30px 0 6px;
            border-bottom: 2px solid #4f4f4f;
            padding-bottom: 4px;
            color: #1e3a5f;
            clear: both;
        }

        /* subheading inside sections */
        .subsection {
            font-weight: 600;
            margin: 16px 0 4px;
            font-size: 12pt;
            color: #2e2e2e;
        }

        /* stats summary as a compact table */
        .stats-table td {
            text-align: center;
            font-weight: 600;
            background: #f8f8f8;
        }
        .stats-table td p {
            font-size: 9pt;
            font-weight: 400;
            color: #444;
            margin: 5px 0 0;
        }

        /* chart data turned into a mini table */
        .chart-data-table {
            width: auto;
            min-width: 350px;
            margin: 10px 0;
        }
        .chart-data-table td, .chart-data-table th {
            padding: 5px 12px;
        }

        /* footer with address */
        .company-footer {
            margin-top: 45px;
            border-top: 2px solid #4f4f4f;
            padding-top: 12px;
            font-size: 9.5pt;
            text-align: center;
            color: #2e2e2e;
        }
        .company-address {
            line-height: 1.5;
        }

        @media print {
            body { margin: 0.5in; }
            table { page-break-inside: avoid; }
            .print-wrap { width: 100%; overflow: visible; }
        }
    </style>
</head>
<body>
<div class="print-wrap">

    <!-- ========== HEADER : COMPANY & TITLE ========= -->
    <div class="company-header">
        <div class="logo-placeholder">
            @if(isset($companyLogo) && $companyLogo)
                <img src="{{ route('dashboard.logo', basename($companyLogo)) }}" alt="logo" style="max-width:100%; max-height:100%;">
            @else
                <span>📁</span>
            @endif
        </div>
        <div class="company-details">
            <div class="company-name">{{ $companyName ?? 'YOUR COMPANY NAME' }}</div>
            <div class="print-date">
                Generated: {{ now()->format('d M Y, h:i A') }}
                @if(request()->query('month'))
                    | Period: {{ \Carbon\Carbon::createFromFormat('Y-m', request()->query('month'))->format('F Y') }}
                @else
                    | Period: Current month
                @endif
            </div>
        </div>
    </div>

    <!-- ========== ACCOUNT BALANCES ========= -->
    <div class="section-title">💰 ACCOUNT BALANCES</div>
    <table>
        <thead>
            <tr>
                <th style="width: 65%;">Account / Source</th>
                <th class="text-end">Balance (৳)</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><strong>Cash in Hand</strong></td>
                <td class="text-end text-success">{{ number_format($cashBalance, 2) }}</td>
            </tr>
            @if(isset($bankAccounts) && $bankAccounts->count() > 0)
                @foreach($bankAccounts as $acc)
                <tr>
                    <td>🏦 {{ $acc->account_name }} ({{ $acc->bank_name }})</td>
                    <td class="text-end text-success">{{ number_format($acc->balance, 2) }}</td>
                </tr>
                @endforeach
            @endif
            @if(isset($digitalAccounts) && $digitalAccounts->count() > 0)
                @foreach($digitalAccounts as $acc)
                <tr>
                    <td>📱 {{ $acc->provider }} - {{ $acc->account_number }}</td>
                    <td class="text-end text-info">{{ number_format($acc->balance, 2) }}</td>
                </tr>
                @endforeach
            @endif
        </tbody>
    </table>

    <!-- ========== SALES SUMMARY & CASH RECEIVED BY METHOD ========= -->
    <div style="display: flex; gap: 30px; margin-bottom: 20px; flex-wrap: wrap;">
        <!-- SALES SUMMARY -->
        <div style="flex: 1; min-width: 280px; overflow: hidden;">
            <div class="section-title">💼 SALES SUMMARY</div>
            <table style="width: 100%; min-width: 280px;">
                <tbody>
                    <tr><th>Total Sales</th><td class="text-end">{{ number_format($totalSales, 2) }}</td></tr>
                    <tr><th>Total Payment</th><td class="text-end">{{ number_format($totalPayment, 2) }}</td></tr>
                    <tr><th>Due</th><td class="text-end">{{ number_format($totalDue, 2) }}</td></tr>
                </tbody>
            </table>
        </div>

        <!-- CASH RECEIVED BY METHOD -->
        <div style="flex: 1; min-width: 280px; overflow: hidden;">
            <div class="section-title">💰 CASH RECEIVED </div>
            <table style="width: 100%; min-width: 280px;">
                <thead>
                    <tr><th>Method</th><th class="text-end">Amount (৳)</th></tr>
                </thead>
                <tbody>
                    @foreach($cashBy as $method => $amt)
                    <tr>
                        <td>{{ $method ?: 'Other' }}</td>
                        <td class="text-end">{{ number_format($amt, 2) }}</td>
                    </tr>
                    @endforeach
                    <tr style="font-weight: 600; background:#f0f0f0;">
                        <td>Total</td>
                        <td class="text-end">{{ number_format($cashBy->sum(), 2) }}</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

    <!-- ========== DAY WISE SALES & SEGMENT WISE SALE ========= -->
    <div style="display: flex; gap: 30px; flex-wrap: wrap;">
        <!-- DAY WISE SALES -->
        <div style="flex: 1; min-width: 280px; overflow: hidden;">
            <div class="section-title">📅 DAY WISE SALES</div>
            <table style="width: 100%; min-width: 280px;">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th class="text-end">Qty</th>
                        <th class="text-end">Sales (৳)</th>
                        <th class="text-end">COGS (৳)</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($dayGroups as $day => $rows)
                    <tr>
                        <td>{{ $day }}</td>
                        <td class="text-end">{{ $rows->sum('qty') }}</td>
                        <td class="text-end">{{ number_format($rows->sum('sales'), 2) }}</td>
                        <td class="text-end">{{ number_format($rows->sum('cogs'), 2) }}</td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>

        <!-- SEGMENT WISE SALE -->
        <div style="flex: 1; min-width: 280px; overflow: hidden;">
            <div class="section-title">📊 SEGMENT WISE SALE</div>
            <table style="width: 100%; min-width: 280px;">
                <thead>
                    <tr>
                        <th>Segment</th>
                        <th class="text-end">Qty</th>
                        <th class="text-end">Amount</th>
                        <th class="text-end">KPI</th>
                        <th class="text-end">Revenue</th>

                    </tr>
                </thead>
                <tbody>
                    @foreach($segmentGroups as $seg => $rows)
                    <tr>
                        <td>{{ $seg }}</td>
                        <td class="text-end">{{ $rows->sum('qty') }}</td>
                        <td class="text-end">{{ number_format($rows->sum('amount'), 2) }}</td>
                        <td class="text-end">{{ number_format($rows->sum('kpi'), 2) }}</td>
                        <td class="text-end">{{ number_format($rows->sum('revenue'), 2) }}</td>

                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
    <!-- ========== RECENT TRANSACTIONS ========= -->
    <div class="section-title">📋 RECENT TRANSACTIONS</div>
    <table>
        <thead>
            <tr>
                <th>Date</th>
                <th>Voucher</th>
                <th>Details</th>
                <th>Type</th>
                <th>Method</th>
                <th class="text-end">Amount</th>
            </tr>
        </thead>
        <tbody>
            @forelse($recentTransactions as $t)
            <tr>
                <td>{{ $t->date->format('d M') }}</td>
                <td>{{ $t->voucher_no }}</td>
                <td>{{ Str::limit($t->details, 22) }}</td>
                <td>
                    @if($t->type == 'income')
                        <span class="badge">Income</span>
                    @elseif($t->type == 'expense')
                        <span class="badge">Expense</span>
                    @else
                        <span class="badge">Transfer</span>
                    @endif
                </td>
                <td>
                    @if($t->payment_method == 'cash')
                        Cash
                    @elseif($t->payment_method == 'bank' && $t->bankAccount)
                        {{ $t->bankAccount->bank_name }}
                    @elseif($t->payment_method == 'digital' && $t->digitalPayment)
                        {{ $t->digitalPayment->provider }}
                    @else
                        —
                    @endif
                </td>
                <td class="text-end {{ $t->type == 'income' ? 'text-success' : 'text-danger' }}">
                    {{ $t->type == 'income' ? '+' : '-' }}৳{{ number_format($t->amount, 2) }}
                </td>
            </tr>
            @empty
            <tr>
                <td colspan="6" style="text-align:center;">No recent transactions</td>
            </tr>
            @endforelse
        </tbody>
    </table>

    <!-- ========== FOOTER WITH ADDRESS ========= -->
    <div class="company-footer">
        <div class="company-address">
            {{ $companyAddress ?? '123 Business Street, Suite 100, Dhaka – 1205, Bangladesh' }}<br>
            <span style="font-size:9pt;">{{ $companyEmail ?? 'info@yourcompany.com' }} | {{ $companyPhone ?? '+880 2 1234 5678' }}</span>
        </div>
        <div style="margin-top:8px; font-size:8.5pt;">Printed from Accounting Dashboard – Excel‑style report</div>
    </div>

</div><!-- .print-wrap -->

<script>
    window.onload = function() { window.print(); };
</script>
</body>
</html>
