<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Custom Account Statement of Account — Ledger</title>
    <style>
        body {
            font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
            color: #334155;
            font-size: 12px;
            line-height: 1.5;
        }
        .header {
            margin-bottom: 20px;
            border-bottom: 2px solid #ea580c;
            padding-bottom: 15px;
        }
        .logo-text {
            font-size: 22px;
            font-weight: 800;
            color: #ea580c;
            letter-spacing: -0.5px;
        }
        .title {
            font-size: 16px;
            font-weight: bold;
            text-transform: uppercase;
            text-align: right;
            color: #475569;
        }
        .meta-table {
            width: 100%;
            margin-bottom: 20px;
            border-collapse: collapse;
        }
        .meta-table td {
            vertical-align: top;
            width: 50%;
            padding: 0;
        }
        .summary-box {
            background-color: #f8fafc;
            border: 1px solid #e2e8f0;
            border-radius: 8px;
            padding: 12px 16px;
            margin-bottom: 25px;
        }
        .summary-title {
            font-weight: bold;
            color: #1e293b;
            margin-bottom: 8px;
            font-size: 13px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        .summary-grid {
            width: 100%;
            border-collapse: collapse;
        }
        .summary-grid td {
            padding: 4px 0;
            font-size: 12px;
        }
        .summary-value {
            font-weight: bold;
            text-align: right;
            font-family: monospace;
            font-size: 13px;
        }
        .section-title {
            font-size: 13px;
            font-weight: bold;
            color: #ea580c;
            margin-top: 20px;
            margin-bottom: 10px;
            border-left: 3px solid #ea580c;
            padding-left: 8px;
            text-transform: uppercase;
        }
        .ledger-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        .ledger-table th {
            background-color: #f8fafc;
            color: #475569;
            font-weight: bold;
            text-align: left;
            padding: 6px 8px;
            border-bottom: 1px solid #cbd5e1;
            border-top: 1px solid #cbd5e1;
            font-size: 10px;
            text-transform: uppercase;
        }
        .ledger-table td {
            padding: 6px 8px;
            border-bottom: 1px solid #f1f5f9;
            font-size: 11px;
            color: #334155;
            vertical-align: middle;
        }
        .amount-col {
            text-align: right;
            font-family: monospace;
        }
        .footer {
            margin-top: 40px;
            font-size: 9px;
            color: #94a3b8;
            text-align: center;
            border-top: 1px solid #f1f5f9;
            padding-top: 8px;
        }
    </style>
</head>
<body>

    <!-- Header -->
    <table class="meta-table">
        <tr>
            <td>
                <span class="logo-text">{{ $businessName }}</span><br>
                <span style="color: #64748b; font-size: 10px;">Custom Account Statement</span>
            </td>
            <td style="text-align: right;">
                <span class="title">Statement of Account</span><br>
                <span style="color: #64748b; font-size: 10px;">Generated: {{ date('d-m-Y') }}</span>
            </td>
        </tr>
    </table>

    <div class="header"></div>

    <!-- Account Details -->
    <table class="meta-table" style="margin-bottom: 15px;">
        <tr>
            <td>
                <strong style="color: #1e293b; font-size: 13px;">Account Profile</strong><br>
                Name: {{ $account->name }}<br>
                Email: {{ $account->email ?? 'N/A' }}<br>
                Type: {{ ucfirst($account->account_type) }}
            </td>
            <td style="text-align: right;">
                <span style="color: #64748b; font-size: 10px;">Account Created: {{ $account->created_at->format('d-m-Y') }}</span>
            </td>
        </tr>
    </table>

    <!-- Financial Summary Box -->
    <div class="summary-box">
        <div class="summary-title">Ledger Financial Summary</div>
        <table class="summary-grid">
            <tr>
                <td>Opening Balance</td>
                <td class="summary-value" style="color: #475569;">{{ $currencySymbol }}{{ number_format($openingBalance, 2) }}</td>
            </tr>
            <tr>
                <td>Total Cash In (+)</td>
                <td class="summary-value" style="color: #15803d;">{{ $currencySymbol }}{{ number_format($totalCashIn, 2) }}</td>
            </tr>
            <tr>
                <td>Total Cash Out (-)</td>
                <td class="summary-value" style="color: #b91c1c;">{{ $currencySymbol }}{{ number_format($totalCashOut, 2) }}</td>
            </tr>
            <tr>
                <td>Project Shares (-)</td>
                <td class="summary-value" style="color: #b91c1c;">{{ $currencySymbol }}{{ number_format($totalProjectCostShares, 2) }}</td>
            </tr>
            <tr style="border-top: 1.5px solid #cbd5e1;">
                <td style="font-weight: bold; padding-top: 6px; color: #1e293b;">Net Account Balance</td>
                <td class="summary-value" style="font-weight: bold; color: #ea580c; padding-top: 6px; font-size: 14px;">
                    {{ $currencySymbol }}{{ number_format($balance, 2) }}
                </td>
            </tr>
        </table>
    </div>

    <!-- Running Ledger Section -->
    <div class="section-title">Account Activity Running Ledger</div>
    <table class="ledger-table">
        <thead>
            <tr>
                <th style="width: 12%;">Date</th>
                <th style="width: 38%;">Description / Details</th>
                <th style="width: 15%;">Project</th>
                <th style="width: 11%; text-align: right;">Cash In (+)</th>
                <th style="width: 11%; text-align: right;">Cash Out (-)</th>
                <th style="width: 13%; text-align: right;">Balance</th>
            </tr>
        </thead>
        <tbody>
            <!-- Opening Balance Row -->
            <tr>
                <td>{{ $account->created_at ? $account->created_at->format('d-m-Y') : date('d-m-Y') }}</td>
                <td><strong>Opening Balance</strong></td>
                <td>-</td>
                <td class="amount-col" style="color: #15803d;">{{ $openingBalance >= 0 ? $currencySymbol . number_format($openingBalance, 2) : '-' }}</td>
                <td class="amount-col" style="color: #b91c1c;">{{ $openingBalance < 0 ? $currencySymbol . number_format(abs($openingBalance), 2) : '-' }}</td>
                <td class="amount-col" style="font-weight: bold;">{{ $currencySymbol }}{{ number_format($openingBalance, 2) }}</td>
            </tr>

            @php
                $running = $openingBalance;
                $txns = collect();
                
                foreach($account->cashIns->where('payment_status', 'paid') as $in) {
                    $desc = ($in->paymentType ? $in->paymentType->name : 'Payment') . ' Received';
                    if ($in->sender_bank_name) {
                        $desc .= ' - ' . $in->sender_bank_name;
                    }
                    $txns->push([
                        'date' => $in->date,
                        'desc' => $desc,
                        'project' => $in->consignment ? $in->consignment->title : '-',
                        'inflow' => $in->amount,
                        'outflow' => 0,
                    ]);
                }

                foreach($account->consignments as $consignment) {
                    if ($consignment->investor_share_type === 'fixed') {
                        $costShare = $consignment->investor_share_value;
                        $shareLabel = 'Fixed Share';
                    } else {
                        $projectOutflows = $consignment->cashOuts->where('payment_status', 'paid')->sum('amount') 
                                           + $consignment->expenses->sum('amount');
                        $costShare = $projectOutflows * ($consignment->investor_share_value / 100);
                        $shareLabel = number_format($consignment->investor_share_value, 1) . '% Cost Share';
                    }
                    $txns->push([
                        'date' => $consignment->created_at,
                        'desc' => 'Consignment Project Share: ' . $consignment->title . ' (' . $shareLabel . ')',
                        'project' => $consignment->title,
                        'inflow' => 0,
                        'outflow' => $costShare,
                    ]);
                }
                
                foreach($account->cashOuts->where('payment_status', 'paid') as $out) {
                    $desc = ($out->paymentType ? $out->paymentType->name : 'Payout');
                    if ($out->guest_bank_name) {
                        $desc .= ' - ' . $out->guest_bank_name;
                    }
                    $txns->push([
                        'date' => $out->date,
                        'desc' => $desc,
                        'project' => $out->consignment ? $out->consignment->title : '-',
                        'inflow' => 0,
                        'outflow' => $out->amount,
                    ]);
                }
                
                $txns = $txns->sortBy('date');
            @endphp
            
            @foreach($txns as $t)
                @php
                    $running += ($t['inflow'] - $t['outflow']);
                @endphp
                <tr>
                    <td>{{ $t['date']->format('d-m-Y') }}</td>
                    <td>{{ $t['desc'] }}</td>
                    <td>{{ $t['project'] }}</td>
                    <td class="amount-col" style="color: #15803d;">{{ $t['inflow'] > 0 ? $currencySymbol . number_format($t['inflow'], 2) : '-' }}</td>
                    <td class="amount-col" style="color: #b91c1c;">{{ $t['outflow'] > 0 ? $currencySymbol . number_format($t['outflow'], 2) : '-' }}</td>
                    <td class="amount-col" style="font-weight: bold;">{{ $currencySymbol }}{{ number_format($running, 2) }}</td>
                </tr>
            @endforeach
        </tbody>
    </table>

    <div class="footer">
        Statement of account issued on behalf of {{ $businessName }}. Confirmed ledger activity.
    </div>

</body>
</html>