<!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>Trial Balance - 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">
    <!-- DataTables -->
    <link rel="stylesheet" href="https://cdn.datatables.net/1.13.4/css/dataTables.bootstrap5.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;
        }

        /* 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;
        }

        /* Table Card */
        .table-card {
            background: white;
            border-radius: 16px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.02);
            overflow: hidden;
        }

        .table-header {
            padding: 1.2rem 1.5rem;
            border-bottom: 1px solid #f1f5f9;
        }

        .table-header h5 {
            font-weight: 600;
            color: #1e293b;
            margin: 0;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .table-header i {
            color: #4361ee;
        }

        .table-responsive {
            padding: 0 1.5rem 1.5rem 1.5rem;
        }

        .table {
            margin: 0;
        }

        .table thead th {
            background: #f8fafc;
            border-bottom: 2px solid #e2e8f0;
            color: #475569;
            font-weight: 600;
            font-size: 0.85rem;
            padding: 1rem;
            white-space: nowrap;
        }

        .table tbody td {
            padding: 1rem;
            vertical-align: middle;
            border-bottom: 1px solid #f1f5f9;
            color: #1e293b;
        }

        .table tbody tr:hover {
            background: #f8fafc;
        }

        .table tfoot td {
            padding: 1rem;
            background: #f8fafc;
            font-weight: 600;
        }

        .amount-debit {
            color: #4361ee;
            font-weight: 600;
        }

        .amount-credit {
            color: #10b981;
            font-weight: 600;
        }

        .account-code {
            font-weight: 600;
            color: #4361ee;
        }

        /* Responsive */
        @media (max-width: 768px) {
            .main-container {
                padding: 1rem;
            }
            
            .page-header {
                flex-direction: column;
                gap: 1rem;
                text-align: center;
            }
        }
    </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">Trial Balance</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>

        <!-- Balance Check -->
        @if($isBalanced)
        <div class="alert-success">
            <i class="fas fa-check-circle"></i>
            <strong>✓ Trial Balance is Balanced!</strong> Total Debits ({{ number_format($totalDebits) }}) = Total Credits ({{ number_format($totalCredits) }})
        </div>
        @else
        <div class="alert-danger">
            <i class="fas fa-exclamation-triangle"></i>
            <strong>⚠ Trial Balance is NOT Balanced!</strong> Difference: {{ number_format(abs($totalDebits - $totalCredits)) }}
        </div>
        @endif

        <!-- Trial Balance Table -->
        <div class="table-card">
            <div class="table-header">
                <h5><i class="fas fa-balance-scale me-2"></i>Trial Balance</h5>
            </div>
            <div class="table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th>Account Code</th>
                            <th>Account Name</th>
                            <th class="text-end">Debit (UGX)</th>
                            <th class="text-end">Credit (UGX)</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($trialBalance as $item)
                        <tr>
                            <td><span class="account-code">{{ $item['code'] }}</span></td>
                            <td>{{ $item['name'] }}</td>
                            <td class="text-end amount-debit">{{ $item['debit'] > 0 ? number_format($item['debit']) : '-' }}</td>
                            <td class="text-end amount-credit">{{ $item['credit'] > 0 ? number_format($item['credit']) : '-' }}</td>
                        </tr>
                        @endforeach
                    </tbody>
                    <tfoot>
                        <tr>
                            <td colspan="2" class="text-end fw-bold">TOTALS:</td>
                            <td class="text-end amount-debit fw-bold">{{ number_format($totalDebits) }}</td>
                            <td class="text-end amount-credit fw-bold">{{ number_format($totalCredits) }}</td>
                        </tr>
                    </tfoot>
                </table>
            </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/trial-balance?format=${format}&as_of_date=${asOfDate}`;
            
            setTimeout(() => Swal.close(), 1500);
        }
    </script>
</body>
</html>