@extends('layouts.app')

@section('title', 'General Ledger')

@section('content')
    <x-page-head title="General Ledger" subtitle="All entries reorganized by account" />

    <x-panel title="Filter">
        <form method="GET" action="{{ route('accounting.gl.index') }}" style="display:flex; gap:12px; flex-wrap:wrap; align-items:end;">
            <div class="form-group">
                <label class="form-label" for="account_id">Account</label>
                <select class="input" id="account_id" name="account_id">
                    @foreach ($accounts as $account)
                        <option value="{{ $account->id }}" @selected($selectedAccountId == $account->id)>
                            {{ $account->code }} — {{ $account->name }}
                        </option>
                    @endforeach
                </select>
            </div>
            <div class="form-group">
                <label class="form-label" for="from">From</label>
                <input class="input" type="date" id="from" name="from" value="{{ $from }}">
            </div>
            <div class="form-group">
                <label class="form-label" for="to">To</label>
                <input class="input" type="date" id="to" name="to" value="{{ $to }}">
            </div>
            <button type="submit" class="btn btn-primary">Apply</button>
            @if ($ledger)
                <a href="{{ route('accounting.gl.export', ['format' => 'pdf', 'account_id' => $selectedAccountId, 'from' => $from, 'to' => $to]) }}" class="btn btn-outline">PDF</a>
                <a href="{{ route('accounting.gl.export', ['format' => 'csv', 'account_id' => $selectedAccountId, 'from' => $from, 'to' => $to]) }}" class="btn btn-outline">CSV</a>
                <a href="{{ route('accounting.gl.export', ['format' => 'xlsx', 'account_id' => $selectedAccountId, 'from' => $from, 'to' => $to]) }}" class="btn btn-outline">Excel</a>
            @endif
        </form>
    </x-panel>

    @if ($ledger)
        <div class="stat-grid" style="margin-top:16px;">
            <x-stat-card label="Opening Balance" :value="number_format($ledger['opening_balance'], 2)" />
            <x-stat-card label="Total Debits" :value="number_format($ledger['total_debits'], 2)" color="teal" />
            <x-stat-card label="Total Credits" :value="number_format($ledger['total_credits'], 2)" color="violet" />
            <x-stat-card label="Closing Balance" :value="number_format($ledger['closing_balance'], 2)" color="green" />
        </div>

        <x-panel :title="$ledger['account']->name" :subtitle="'Account '.$ledger['account']->code" style="margin-top:16px;">
            @if ($ledger['entries']->isEmpty())
                <x-empty-state title="No ledger activity" message="No posted journal lines for this account in the selected period." />
            @else
            <div class="table-wrap">
                <table>
                    <thead>
                        <tr>
                            <th>Date</th>
                            <th>Reference</th>
                            <th>Description</th>
                            <th class="text-right">Debit</th>
                            <th class="text-right">Credit</th>
                            <th class="text-right">Balance</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($ledger['entries'] as $entry)
                            <tr>
                                <td>{{ $entry['date'] }}</td>
                                <td>{{ $entry['reference_number'] }}</td>
                                <td>{{ $entry['description'] }}</td>
                                <td class="text-right">{{ $entry['debit_amount'] !== '0.00' ? number_format($entry['debit_amount'], 2) : '—' }}</td>
                                <td class="text-right">{{ $entry['credit_amount'] !== '0.00' ? number_format($entry['credit_amount'], 2) : '—' }}</td>
                                <td class="text-right"><strong>{{ number_format($entry['running_balance'], 2) }}</strong></td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            @endif
        </x-panel>
    @else
        <x-panel style="margin-top:16px;">
            <x-empty-state title="No accounts" message="Create accounts in the chart of accounts first." />
        </x-panel>
    @endif
@endsection
