<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bank Book Report</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.5;
            color: #333;
            background: #f5f5f5;
        }

        @media print {
            body {
                background: white;
            }
            .no-print {
                display: none !important;
            }
        }

        .report-container {
            max-width: 12in;
            margin: 20px auto;
            padding: 30px;
            background: white;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        .report-header {
            border-bottom: 3px solid #2c3e50;
            margin-bottom: 20px;
            padding-bottom: 15px;
        }

        .company-logo {
            font-size: 24px;
            font-weight: bold;
            color: #2c3e50;
            margin-bottom: 5px;
        }

        .company-info {
            font-size: 11px;
            color: #666;
            line-height: 1.4;
        }

        .report-title {
            text-align: center;
            font-size: 18px;
            font-weight: bold;
            color: #2c3e50;
            margin-top: 15px;
            margin-bottom: 10px;
        }

        .report-period {
            text-align: center;
            font-size: 12px;
            color: #666;
            margin-bottom: 20px;
        }

        .summary-cards {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 15px;
            margin-bottom: 20px;
        }

        .summary-card {
            border: 1px solid #ddd;
            padding: 12px;
            border-radius: 4px;
            background: #f9f9f9;
        }

        .summary-card-label {
            font-size: 10px;
            color: #666;
            text-transform: uppercase;
            font-weight: 600;
            margin-bottom: 5px;
        }

        .summary-card-value {
            font-size: 14px;
            font-weight: bold;
            color: #2c3e50;
        }

        .bank-book-table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
            font-size: 11px;
        }

        .bank-book-table thead {
            background: #34495e;
            color: white;
        }

        .bank-book-table th {
            padding: 10px 8px;
            text-align: left;
            font-weight: 600;
            border: 1px solid #34495e;
        }

        .bank-book-table td {
            padding: 8px;
            border: 1px solid #ddd;
            border-bottom: 1px solid #e0e0e0;
        }

        .bank-book-table tbody tr:nth-child(even) {
            background: #f9f9f9;
        }

        .bank-book-table tbody tr:hover {
            background: #f0f0f0;
        }

        .text-center {
            text-align: center;
        }

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

        .badge {
            display: inline-block;
            padding: 3px 6px;
            border-radius: 3px;
            font-size: 9px;
            font-weight: 600;
        }

        .badge-success {
            background: #d4edda;
            color: #155724;
        }

        .badge-danger {
            background: #f8d7da;
            color: #721c24;
        }

        .print-button {
            display: inline-block;
            margin: 20px auto;
            padding: 10px 20px;
            background: #3498db;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12px;
        }

        .print-button:hover {
            background: #2980b9;
        }

        .no-print-container {
            text-align: center;
            margin: 20px 0;
        }

        .section-title {
            font-size: 12px;
            font-weight: bold;
            color: #2c3e50;
            margin-bottom: 8px;
            margin-top: 15px;
            padding-bottom: 5px;
            border-bottom: 2px solid #34495e;
        }

        @media print {
            .print-button {
                display: none;
            }

            .report-container {
                box-shadow: none;
                margin: 0;
                padding: 30px;
            }
        }

        @media (max-width: 1024px) {
            .summary-cards {
                grid-template-columns: repeat(2, 1fr);
            }

            .report-container {
                max-width: 100%;
                margin: 0;
                padding: 15px;
            }

            .bank-book-table {
                font-size: 10px;
            }

            .bank-book-table th,
            .bank-book-table td {
                padding: 6px 4px;
            }
        }
    </style>
</head>
<body>
    <div class="no-print-container no-print">
        <button class="print-button" onclick="window.print()">🖨️ Print Report</button>
    </div>

    <div class="report-container">
        <!-- Report Header -->
        <div class="report-header">
            <div class="company-logo">JANNAT ACCOUNTING SYSTEM</div>
            <div class="company-info">
                Bank Book Report<br>
                Generated on {{ now()->format('d M Y, H:i A') }}
            </div>
        </div>

        <!-- Report Title -->
        <div class="report-title">BANK BOOK</div>
        <div class="report-period">
            Period: <strong>{{ \Carbon\Carbon::parse($fromDate)->format('d M Y') }}</strong> to
            <strong>{{ \Carbon\Carbon::parse($toDate)->format('d M Y') }}</strong>
            @if($bankAccountId)
                <br>
                Account: <strong>{{ $bankAccounts->find($bankAccountId)->bank_name ?? '' }} - {{ $bankAccounts->find($bankAccountId)->account_name ?? '' }}</strong>
            @else
                <br>
                Account: <strong>All Accounts</strong>
            @endif
        </div>

        <!-- Summary Cards -->
        <div class="summary-cards">
            <div class="summary-card">
                <div class="summary-card-label">Opening Balance</div>
                <div class="summary-card-value">{{ number_format($openingBalance, 2) }}</div>
            </div>
            <div class="summary-card">
                <div class="summary-card-label">Total Deposits</div>
                <div class="summary-card-value">{{ number_format($totalDeposits, 2) }}</div>
            </div>
            <div class="summary-card">
                <div class="summary-card-label">Total Withdrawals</div>
                <div class="summary-card-value">{{ number_format($totalWithdrawals, 2) }}</div>
            </div>
            <div class="summary-card">
                <div class="summary-card-label">Closing Balance</div>
                <div class="summary-card-value">{{ number_format($closingBalance, 2) }}</div>
            </div>
        </div>

        <!-- Transactions Table -->
        <div class="section-title">Transaction Details</div>
        <table class="bank-book-table">
            <thead>
                <tr>
                    <th style="width: 8%;">Date</th>
                    <th style="width: 8%;">Type</th>
                    <th style="width: 18%;">Account</th>
                    <th style="width: 20%;">Details</th>
                    <th style="width: 10%;">Deposits</th>
                    <th style="width: 10%;">Withdrawals</th>
                    <th style="width: 10%;">Balance</th>
                </tr>
            </thead>
            <tbody>
                @forelse($transactions as $transaction)
                    <tr>
                        <td class="text-center">{{ $transaction->date->format('d/m/y') }}</td>
                        <td class="text-center">
                            @if($transaction->type == 'income')
                                <span class="badge badge-success">Deposit</span>
                            @else
                                <span class="badge badge-danger">Withdrawal</span>
                            @endif
                        </td>
                        <td>{{ $transaction->bankAccount->bank_name ?? '-' }}</td>
                        <td>{{ substr($transaction->details ?? '-', 0, 30) }}</td>
                        <td class="text-end">
                            @if($transaction->type == 'income')
                                {{ number_format($transaction->amount, 2) }}
                            @else
                                -
                            @endif
                        </td>
                        <td class="text-end">
                            @if($transaction->type == 'expense')
                                {{ number_format($transaction->amount, 2) }}
                            @else
                                -
                            @endif
                        </td>
                        <td class="text-end"><strong>{{ number_format($transaction->balance_after, 2) }}</strong></td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="7" class="text-center">No transactions found for the selected period.</td>
                    </tr>
                @endforelse
            </tbody>
        </table>

        <!-- Verification Section -->
        <table class="bank-book-table" style="margin-top: 30px;">
            <tr style="background: #ecf0f1;">
                <td><strong>Opening Balance</strong></td>
                <td style="text-align: right;"><strong>{{ number_format($openingBalance, 2) }}</strong></td>
            </tr>
            <tr style="background: #ecf0f1;">
                <td><strong>Add: Total Deposits</strong></td>
                <td style="text-align: right;"><strong>{{ number_format($totalDeposits, 2) }}</strong></td>
            </tr>
            <tr style="background: #ecf0f1;">
                <td><strong>Less: Total Withdrawals</strong></td>
                <td style="text-align: right;"><strong>({{ number_format($totalWithdrawals, 2) }})</strong></td>
            </tr>
            <tr style="background: #34495e; color: white; font-weight: bold;">
                <td>Closing Balance</td>
                <td style="text-align: right;">{{ number_format($closingBalance, 2) }}</td>
            </tr>
        </table>

        <!-- Footer -->
        <div style="margin-top: 40px; padding-top: 20px; border-top: 1px solid #ddd; text-align: center; font-size: 10px; color: #666;">
            <p>This is a computer-generated report. No signature required.</p>
            <p>Report Generated on {{ now()->format('d M Y') }} at {{ now()->format('H:i A') }}</p>
        </div>
    </div>
</body>
</html>

