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

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

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

        .report-container {
            max-width: 11in;
            margin: 20px auto;
            padding: 40px;
            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;
        }

        .pl-table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }

        .pl-table tr {
            border-bottom: 1px solid #ddd;
        }

        .pl-table td {
            padding: 8px;
        }

        .pl-table td:first-child {
            text-align: left;
        }

        .pl-table td:last-child {
            text-align: right;
        }

        .section-header {
            background: #34495e;
            color: white;
            font-weight: bold;
            padding: 10px;
        }

        .section-total {
            background: #ecf0f1;
            font-weight: bold;
            border-top: 2px solid #34495e;
            border-bottom: 2px solid #34495e;
        }

        .grand-total {
            background: #34495e;
            color: white;
            font-weight: bold;
            border-top: 3px solid #2c3e50;
        }

        .item {
            padding-left: 20px;
        }

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

        .breakdown-table thead {
            background: #ecf0f1;
            border-top: 2px solid #34495e;
            border-bottom: 2px solid #34495e;
        }

        .breakdown-table th {
            padding: 8px;
            text-align: left;
            font-weight: 600;
            color: #2c3e50;
        }

        .breakdown-table td {
            padding: 6px 8px;
            border-bottom: 1px solid #e0e0e0;
        }

        .breakdown-table tbody tr:last-child {
            border-top: 2px solid #34495e;
            font-weight: bold;
            background: #ecf0f1;
        }

        .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;
        }

        .breakdown-section {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 40px;
            margin-top: 30px;
        }

        .breakdown-column h4 {
            font-size: 12px;
            color: #2c3e50;
            margin-bottom: 10px;
            border-bottom: 2px solid #34495e;
            padding-bottom: 5px;
        }

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

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

        @media (max-width: 768px) {
            .report-container {
                max-width: 100%;
                margin: 0;
                padding: 20px;
            }

            .breakdown-section {
                grid-template-columns: 1fr;
                gap: 20px;
            }
        }
    </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">
                Profit & Loss Report<br>
                Generated on {{ now()->format('d M Y, H:i A') }}
            </div>
        </div>

        <!-- Report Title -->
        <div class="report-title">PROFIT & LOSS STATEMENT</div>
        <div class="report-period">
            For the period of <strong>{{ \Carbon\Carbon::create()->month((int)$month)->format('F') }} {{ (int)$year }}</strong>
        </div>

        <!-- Income Section -->
        <table class="pl-table">
            <tr class="section-header">
                <td colspan="2">INCOME</td>
            </tr>
            <tr>
                <td>Total Sales Income</td>
                <td>{{ number_format($income, 2) }}</td>
            </tr>
            <tr class="section-total">
                <td>Total Income</td>
                <td>{{ number_format($income, 2) }}</td>
            </tr>
        </table>

        <!-- Expenses Section -->
        <table class="pl-table">
            <tr class="section-header">
                <td colspan="2">EXPENSES</td>
            </tr>
            @if($expenseBreakdown->count() > 0)
                @foreach($expenseBreakdown as $expense)
                    <tr class="item">
                        <td>{{ $expense->expenseType->name ?? 'Unknown Expense' }}</td>
                        <td>{{ number_format($expense->total, 2) }}</td>
                    </tr>
                @endforeach
            @else
                <tr class="item">
                    <td colspan="2">No expenses recorded</td>
                </tr>
            @endif
            <tr class="section-total">
                <td>Total Expenses</td>
                <td>{{ number_format($expenses, 2) }}</td>
            </tr>
        </table>

        <!-- Net Profit/Loss Section -->
        <table class="pl-table">
            <tr class="grand-total">
                <td>NET PROFIT / (LOSS)</td>
                <td>{{ number_format($netProfit, 2) }}</td>
            </tr>
        </table>

        <!-- Breakdown Tables -->
        @if($expenseBreakdown->count() > 0 || $segmentExpenses->count() > 0)
            <div class="breakdown-section">
                @if($expenseBreakdown->count() > 0)
                    <div class="breakdown-column">
                        <h4>Expense Breakdown by Type</h4>
                        <table class="breakdown-table">
                            <thead>
                                <tr>
                                    <th>Type</th>
                                    <th style="width: 35%;" class="text-end">Amount</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($expenseBreakdown as $item)
                                    <tr>
                                        <td>{{ $item->expenseType->name ?? 'Unknown' }}</td>
                                        <td style="text-align: right;">{{ number_format($item->total, 2) }}</td>
                                    </tr>
                                @endforeach
                                <tr>
                                    <td>Total</td>
                                    <td style="text-align: right;">{{ number_format($expenses, 2) }}</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                @endif

                @if($segmentExpenses->count() > 0)
                    <div class="breakdown-column">
                        <h4>Segment-wise Expenses</h4>
                        <table class="breakdown-table">
                            <thead>
                                <tr>
                                    <th>Segment</th>
                                    <th style="width: 35%;" class="text-end">Amount</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($segmentExpenses as $item)
                                    <tr>
                                        <td>{{ $item->segment->name ?? 'Unknown' }}</td>
                                        <td style="text-align: right;">{{ number_format($item->total, 2) }}</td>
                                    </tr>
                                @endforeach
                                <tr>
                                    <td>Total</td>
                                    <td style="text-align: right;">{{ number_format($expenses, 2) }}</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                @endif
            </div>
        @endif

        <!-- 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>
