@extends('layouts.app')
@section('title', 'Balance Sheet')

@section('breadcrumb')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
    <li class="breadcrumb-item active">Balance Sheet</li>
@endsection

@section('content')
@php
    $companyId = auth()->user()->company_id;
    $money = function ($amount) use ($companyId) {
        $amount = (float) $amount;
        if (abs($amount) < 0.005) return '—';
        $formatted = \App\Helpers\NumberHelper::formatAmount(abs($amount), $companyId);
        return $amount < 0 ? '(' . $formatted . ')' : $formatted;
    };
    $asOfQuery = ['date_to' => $asOfDate];
    $balanced = abs($totals['difference']) < 0.01;
@endphp

<div class="page-header">
    <div>
        <h1 class="page-title">Balance Sheet</h1>
        <p class="page-subtitle">Assets, liabilities, and equity as of a selected date</p>
    </div>
    <span class="badge {{ $balanced ? 'bg-success' : 'bg-danger' }} px-3 py-2">
        {{ $balanced ? 'Balanced' : 'Difference: ' . $money($totals['difference']) }}
    </span>
</div>

<div class="card mb-3">
    <div class="card-body">
        <form method="GET" action="{{ route('reports.balance-sheet') }}">
            <div class="row g-3 align-items-end">
                <div class="col-12 col-md-3">
                    <label class="form-label fw-semibold">Fiscal Year</label>
                    <select name="fiscal_year_id" class="form-select">
                        @foreach($fiscalYears as $fy)
                            <option value="{{ $fy->id }}" {{ (int) $selectedFiscalYearId === $fy->id ? 'selected' : '' }}>
                                {{ $fy->name }}{{ $fy->is_current ? ' (Current)' : '' }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="col-6 col-md-2">
                    <label class="form-label fw-semibold">As of Date</label>
                    <input type="date" name="as_of_date" class="form-control" value="{{ $asOfDate }}">
                </div>
                <div class="col-12 col-md-2">
                    <div class="form-check mb-2">
                        <input class="form-check-input" type="checkbox" name="include_zero" value="1" id="includeZero" {{ $includeZero ? 'checked' : '' }}>
                        <label class="form-check-label" for="includeZero">Include zero rows</label>
                    </div>
                </div>
                <div class="col-6 col-md-1">
                    <button type="submit" class="btn btn-primary w-100"><i class="bi bi-search"></i></button>
                </div>
                <div class="col-6 col-md-1">
                    <a href="{{ route('reports.balance-sheet') }}" class="btn btn-outline-secondary w-100"><i class="bi bi-x-lg"></i></a>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-header d-flex flex-wrap justify-content-between align-items-center gap-2">
        <div>
            <strong>Balance Sheet</strong>
            <span class="text-muted ms-2">As of {{ \Carbon\Carbon::parse($asOfDate)->format('d M Y') }}</span>
        </div>
        <small class="text-muted">Click account rows to drill down to General Ledger.</small>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover mb-0" style="min-width:760px">
                <thead class="table-light">
                    <tr>
                        <th>Account</th>
                        <th class="text-end" style="width:180px">Balance</th>
                    </tr>
                </thead>
                <tbody>
                    <tr class="table-primary"><td colspan="2" class="fw-bold text-uppercase">Assets</td></tr>
                    @foreach(['bank', 'accounts_receivable', 'other_current_assets', 'fixed_assets', 'other_assets'] as $section)
                        @php $group = $groups[$section]; @endphp
                        <tr class="table-secondary"><td colspan="2" class="fw-semibold">{{ $group['label'] }}</td></tr>
                        @forelse($group['accounts'] as $account)
                        <tr>
                            <td>
                                <a href="{{ route('reports.general-ledger', array_merge(['account_id' => $account->id], $asOfQuery)) }}" class="text-decoration-none fw-semibold">
                                    {{ $account->code ? '[' . $account->code . '] ' : '' }}{{ $account->name }}
                                </a>
                            </td>
                            <td class="text-end">{{ $money($account->report_amount) }}</td>
                        </tr>
                        @empty
                        <tr><td class="text-muted ps-4">No accounts</td><td class="text-end">—</td></tr>
                        @endforelse
                        <tr class="table-light"><td class="text-end fw-semibold">Total {{ $group['label'] }}</td><td class="text-end fw-semibold">{{ $money($group['total']) }}</td></tr>
                    @endforeach
                    <tr class="table-primary"><td class="text-end fw-bold">Total Assets</td><td class="text-end fw-bold">{{ $money($totals['assets']) }}</td></tr>

                    <tr class="table-primary"><td colspan="2" class="fw-bold text-uppercase">Liabilities</td></tr>
                    @foreach(['accounts_payable', 'credit_card', 'other_current_liabilities', 'long_term_liabilities'] as $section)
                        @php $group = $groups[$section]; @endphp
                        <tr class="table-secondary"><td colspan="2" class="fw-semibold">{{ $group['label'] }}</td></tr>
                        @forelse($group['accounts'] as $account)
                        <tr>
                            <td>
                                <a href="{{ route('reports.general-ledger', array_merge(['account_id' => $account->id], $asOfQuery)) }}" class="text-decoration-none fw-semibold">
                                    {{ $account->code ? '[' . $account->code . '] ' : '' }}{{ $account->name }}
                                </a>
                            </td>
                            <td class="text-end">{{ $money($account->report_amount) }}</td>
                        </tr>
                        @empty
                        <tr><td class="text-muted ps-4">No accounts</td><td class="text-end">—</td></tr>
                        @endforelse
                        <tr class="table-light"><td class="text-end fw-semibold">Total {{ $group['label'] }}</td><td class="text-end fw-semibold">{{ $money($group['total']) }}</td></tr>
                    @endforeach
                    <tr class="table-primary"><td class="text-end fw-bold">Total Liabilities</td><td class="text-end fw-bold">{{ $money($totals['liabilities']) }}</td></tr>

                    <tr class="table-primary"><td colspan="2" class="fw-bold text-uppercase">Equity</td></tr>
                    @php $group = $groups['equity']; @endphp
                    @forelse($group['accounts'] as $account)
                    <tr>
                        <td>
                            <a href="{{ route('reports.general-ledger', array_merge(['account_id' => $account->id], $asOfQuery)) }}" class="text-decoration-none fw-semibold">
                                {{ $account->code ? '[' . $account->code . '] ' : '' }}{{ $account->name }}
                            </a>
                        </td>
                        <td class="text-end">{{ $money($account->report_amount) }}</td>
                    </tr>
                    @empty
                    <tr><td class="text-muted ps-4">No equity account balances</td><td class="text-end">—</td></tr>
                    @endforelse
                    <tr>
                        <td class="fw-semibold">Retained Earnings</td>
                        <td class="text-end">{{ $money($totals['prior_earnings']) }}</td>
                    </tr>
                    <tr>
                        <td class="fw-semibold">Current Period Earnings</td>
                        <td class="text-end">{{ $money($totals['current_earnings']) }}</td>
                    </tr>
                    <tr class="table-primary"><td class="text-end fw-bold">Total Equity</td><td class="text-end fw-bold">{{ $money($totals['equity']) }}</td></tr>
                    <tr class="table-dark"><td class="text-end fw-bold">Total Liabilities & Equity</td><td class="text-end fw-bold">{{ $money($totals['liabilities_and_equity']) }}</td></tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
@endsection
