<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>Balance Sheet - Accounting</title>
    
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome 6 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <!-- SweetAlert2 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
            background: #f5f7fb;
        }

        /* Navbar */
        .navbar {
            background: white;
            padding: 0.75rem 2rem;
            box-shadow: 0 2px 10px rgba(0,0,0,0.03);
        }

        .navbar-brand {
            font-weight: 600;
            color: #2d3748;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .brand-icon {
            background: #4361ee;
            color: white;
            width: 35px;
            height: 35px;
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1rem;
        }

        .nav-link {
            color: #64748b !important;
            font-weight: 500;
            padding: 0.5rem 1.2rem !important;
            margin: 0 0.2rem;
            border-radius: 8px;
            transition: all 0.2s;
        }

        .nav-link:hover, .nav-link.active {
            background: #f1f5f9;
            color: #4361ee !important;
        }

        .nav-link i {
            margin-right: 8px;
            font-size: 0.9rem;
        }

        .user-avatar {
            width: 38px;
            height: 38px;
            background: #e2e8f0;
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 600;
            color: #475569;
        }

        /* Main Container */
        .main-container {
            padding: 2rem;
            max-width: 1400px;
            margin: 0 auto;
        }

        /* Page Header */
        .page-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 2rem;
        }

        .page-header h1 {
            font-size: 1.8rem;
            font-weight: 600;
            color: #1e293b;
            margin-bottom: 0.3rem;
        }

        .page-header p {
            color: #64748b;
        }

        .btn-primary {
            background: #4361ee;
            border: none;
            padding: 0.6rem 1.5rem;
            border-radius: 10px;
            font-weight: 500;
            display: flex;
            align-items: center;
            gap: 8px;
            transition: all 0.2s;
        }

        .btn-primary:hover {
            background: #3651d4;
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(67, 97, 238, 0.2);
        }

        .btn-outline-secondary {
            border: 1px solid #e2e8f0;
            color: #64748b;
            padding: 0.6rem 1.5rem;
            border-radius: 10px;
            font-weight: 500;
            background: white;
            transition: all 0.2s;
        }

        .btn-outline-secondary:hover {
            background: #f1f5f9;
            border-color: #4361ee;
            color: #4361ee;
        }

        /* Stats Cards */
        .stats-row {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 1.5rem;
            margin-bottom: 2rem;
        }

        .stat-card {
            border-radius: 16px;
            padding: 1.5rem;
            color: white;
        }

        .stat-card.assets { background: #4361ee; }
        .stat-card.liabilities { background: #10b981; }
        .stat-card.equity { background: #f97316; }

        .stat-label {
            font-size: 0.9rem;
            opacity: 0.9;
            margin-bottom: 0.5rem;
        }

        .stat-value {
            font-size: 2rem;
            font-weight: 700;
        }

        /* Alert */
        .alert-success {
            background: #e3f7ec;
            border: none;
            color: #10b981;
            padding: 1rem 1.5rem;
            border-radius: 12px;
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 1.5rem;
        }

        .alert-danger {
            background: #fee9e9;
            border: none;
            color: #ef4444;
            padding: 1rem 1.5rem;
            border-radius: 12px;
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 1.5rem;
        }

        /* Two Column Layout */
        .two-column {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 1.5rem;
        }

        /* Section Cards */
        .section-card {
            background: white;
            border-radius: 16px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.02);
            overflow: hidden;
            margin-bottom: 1.5rem;
        }

        .section-header {
            padding: 1rem 1.5rem;
            background: #f8fafc;
            border-bottom: 1px solid #e2e8f0;
            font-weight: 600;
            color: #1e293b;
        }

        .section-header i {
            color: #4361ee;
            margin-right: 8px;
        }

        .section-body {
            padding: 0;
        }

        .account-row {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 1rem 1.5rem;
            border-bottom: 1px solid #f1f5f9;
        }

        .account-row:last-child {
            border-bottom: none;
        }

        .account-info h6 {
            font-weight: 600;
            color: #1e293b;
            margin-bottom: 0.2rem;
        }

        .account-info small {
            color: #64748b;
        }

        .account-amount {
            font-weight: 600;
            color: #1e293b;
        }

        .total-row {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 1rem 1.5rem;
            background: #f8fafc;
            border-top: 2px solid #e2e8f0;
            font-weight: 700;
        }

        /* Responsive */
        @media (max-width: 992px) {
            .two-column {
                grid-template-columns: 1fr;
            }
        }

        @media (max-width: 768px) {
            .main-container {
                padding: 1rem;
            }
            
            .page-header {
                flex-direction: column;
                gap: 1rem;
                text-align: center;
            }

            .stats-row {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <!-- Navbar -->
   
    <!-- Main Container -->
    <div class="main-container">
        <!-- Page Header -->
        <div class="page-header">
            <div>
                <div class="d-flex align-items-center gap-3 mb-2">
                    <a href="{{ route('accounting.dashboard') }}" class="btn btn-sm btn-outline-secondary py-1 px-2">
                        <i class="fas fa-arrow-left"></i>
                    </a>
                    <h1 class="mb-0">Balance Sheet</h1>
                </div>
                <p>As of {{ $asOfDate->format('F d, Y') }}</p>
            </div>
            <div class="d-flex gap-2">
                <div class="dropdown">
                    <button class="btn-outline-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown">
                        <i class="fas fa-download me-2"></i>Export
                    </button>
                    <ul class="dropdown-menu">
                        <li><a class="dropdown-item" href="#" onclick="exportReport('pdf')"><i class="fas fa-file-pdf me-2 text-danger"></i>PDF</a></li>
                        <li><a class="dropdown-item" href="#" onclick="exportReport('excel')"><i class="fas fa-file-excel me-2 text-success"></i>Excel</a></li>
                        <li><a class="dropdown-item" href="#" onclick="exportReport('csv')"><i class="fas fa-file-csv me-2 text-primary"></i>CSV</a></li>
                    </ul>
                </div>
                <button class="btn-primary" onclick="window.print()">
                    <i class="fas fa-print me-2"></i>Print
                </button>
            </div>
        </div>

        <!-- Stats Cards -->
        <div class="stats-row">
            <div class="stat-card assets">
                <div class="stat-label">Total Assets</div>
                <div class="stat-value">{{ number_format($totalAssets) }}</div>
            </div>
            <div class="stat-card liabilities">
                <div class="stat-label">Total Liabilities</div>
                <div class="stat-value">{{ number_format($totalLiabilities) }}</div>
            </div>
            <div class="stat-card equity">
                <div class="stat-label">Total Equity</div>
                <div class="stat-value">{{ number_format($totalEquity) }}</div>
            </div>
        </div>

        <!-- Balance Check -->
        @if($isBalanced)
        <div class="alert-success">
            <i class="fas fa-check-circle"></i>
            <strong>✓ Balance Sheet is Balanced!</strong> Assets = Liabilities + Equity
        </div>
        @else
        <div class="alert-danger">
            <i class="fas fa-exclamation-triangle"></i>
            <strong>⚠ Balance Sheet is NOT Balanced!</strong> Difference: {{ number_format(abs($totalAssets - ($totalLiabilities + $totalEquity))) }}
        </div>
        @endif

        <!-- Two Column Layout -->
        <div class="two-column">
            <!-- Assets Column -->
            <div>
                <div class="section-card">
                    <div class="section-header">
                        <i class="fas fa-chart-line"></i> ASSETS
                    </div>
                    <div class="section-body">
                        @foreach($assets as $item)
                        <div class="account-row">
                            <div class="account-info">
                                <h6>{{ $item['account']->code }}</h6>
                                <small>{{ $item['account']->name }}</small>
                            </div>
                            <div class="account-amount">{{ number_format($item['balance']) }}</div>
                        </div>
                        @endforeach
                        <div class="total-row">
                            <span>Total Assets</span>
                            <span>{{ number_format($totalAssets) }}</span>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Liabilities & Equity Column -->
            <div>
                <!-- Liabilities -->
                <div class="section-card">
                    <div class="section-header">
                        <i class="fas fa-arrow-down"></i> LIABILITIES
                    </div>
                    <div class="section-body">
                        @foreach($liabilities as $item)
                        <div class="account-row">
                            <div class="account-info">
                                <h6>{{ $item['account']->code }}</h6>
                                <small>{{ $item['account']->name }}</small>
                            </div>
                            <div class="account-amount">{{ number_format($item['balance']) }}</div>
                        </div>
                        @endforeach
                        <div class="total-row">
                            <span>Total Liabilities</span>
                            <span>{{ number_format($totalLiabilities) }}</span>
                        </div>
                    </div>
                </div>

                <!-- Equity -->
                <div class="section-card">
                    <div class="section-header">
                        <i class="fas fa-chart-pie"></i> EQUITY
                    </div>
                    <div class="section-body">
                        @foreach($equities as $item)
                        <div class="account-row">
                            <div class="account-info">
                                <h6>{{ $item['account']->code }}</h6>
                                <small>{{ $item['account']->name }}</small>
                            </div>
                            <div class="account-amount">{{ number_format($item['balance']) }}</div>
                        </div>
                        @endforeach
                        <div class="total-row">
                            <span>Total Equity</span>
                            <span>{{ number_format($totalEquity) }}</span>
                        </div>
                    </div>
                </div>

                <!-- Total Liabilities & Equity -->
                <div class="section-card">
                    <div class="section-header" style="background: #4361ee; color: white;">
                        <i class="fas fa-balance-scale"></i> TOTAL LIABILITIES & EQUITY
                    </div>
                    <div class="section-body">
                        <div class="total-row">
                            <span>Total Liabilities & Equity</span>
                            <span>{{ number_format($totalLiabilities + $totalEquity) }}</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <!-- SweetAlert2 -->
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

    <script>
        function exportReport(format) {
            const asOfDate = '{{ $asOfDate->format("Y-m-d") }}';
            
            Swal.fire({
                title: 'Exporting Report...',
                text: `Preparing ${format.toUpperCase()} file`,
                allowOutsideClick: false,
                showConfirmButton: false,
                willOpen: () => Swal.showLoading()
            });

            // Simulate export - replace with actual export URL
            window.location.href = `/accounting/reports/export/balance-sheet?format=${format}&as_of_date=${asOfDate}`;
            
            setTimeout(() => Swal.close(), 1500);
        }
    </script>
</body>
</html>