@extends('layouts.app')

@section('content')
<div class="container m-auto p-4 py-6">
    <div class="flex justify-between items-center mb-6">
        <h1 class="text-2l font-bold text-gray-800">{{ \App\Helpers\TranslationHelper::transFromDatabase('Ledger.Index.Title', __('Account Ledger')) }}</h1>
    </div>

    <div class="bg-white rounded-lg shadow-md p-6 mb-6">
        <form action="{{ route('accounting.ledger.index') }}" method="GET" class="grid grid-cols-1 md:grid-cols-3 gap-4">
            <div>
                <label for="fiscal_year_id" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Ledger.Form.FiscalYear', __('Fiscal Year')) }}</label>
                <select id="fiscal_year_id" name="fiscal_year_id" class="select2 block w-full rounded-md border-gray-300 shadow-sm focus:border-primary-500 focus:ring focus:ring-primary-500 focus:ring-opacity-50" onchange="this.form.submit()">
                    <option value="">{{ \App\Helpers\TranslationHelper::transFromDatabase('Ledger.Form.SelectFiscalYear', __('-- Select Fiscal Year --')) }}</option>
                    @foreach($fiscalYears as $year)
                        <option value="{{ $year->id }}" {{ request('fiscal_year_id') == $year->id ? 'selected' : '' }}>
                            {{ $year->name }} ({{ $year->start_date }} - {{ $year->end_date }})
                        </option>
                    @endforeach
                </select>
            </div>

            <div>
                <label for="accounting_period_id" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Ledger.Form.AccountingPeriod', __('Accounting Period')) }}</label>
                <select id="accounting_period_id" name="accounting_period_id" class="select2 block w-full rounded-md border-gray-300 shadow-sm focus:border-primary-500 focus:ring focus:ring-primary-500 focus:ring-opacity-50" {{ count($periods) > 0 ? '' : 'disabled' }}>
                    <option value="">{{ \App\Helpers\TranslationHelper::transFromDatabase('Ledger.Form.AllPeriods', __('-- All Periods --')) }}</option>
                    @foreach($periods as $p)
                        <option value="{{ $p->id }}" {{ request('accounting_period_id') == $p->id ? 'selected' : '' }}>
                            {{ $p->name }} ({{ $p->start_date }} - {{ $p->end_date }})
                        </option>
                    @endforeach
                </select>
            </div>

            <div>
                <label for="account_id" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Ledger.Form.Account', __('Account')) }}</label>
                <select id="account_id" name="account_id" class="select2 block w-full rounded-md border-gray-300 shadow-sm focus:border-primary-500 focus:ring focus:ring-primary-500 focus:ring-opacity-50" required>
                    <option value="">{{ \App\Helpers\TranslationHelper::transFromDatabase('Ledger.Form.SelectAccount', __('-- Select Account --')) }}</option>
                    @foreach($accounts as $acc)
                        <option value="{{ $acc->id }}" {{ request('account_id') == $acc->id ? 'selected' : '' }}>
                            {{ $acc->code }} - {{ $acc->name }}
                        </option>
                    @endforeach
                </select>
            </div>

            <div class="md:col-span-3">
                <button type="submit" class="p-4 py-2 bg-primary-600 text-white rounded-md hover:bg-primary-700 focus:outline-none focus:ring-2 focus:ring-primary-500 focus:ring-offset-2">
                    {{ \App\Helpers\TranslationHelper::transFromDatabase('Ledger.Button.ViewLedger', __('View Ledger')) }}
                </button>
            </div>
        </form>
    </div>

    @if($account)
        <div class="bg-white rounded-lg shadow-md p-6">
            <div class="mb-4">
                <h2 class="text-l font-semibold text-gray-800">{{ \App\Helpers\TranslationHelper::transFromDatabase('Ledger.Display.LedgerTitle', __('Account Ledger')) }}: {{ $account->name }} ({{ $account->code }})</h2>
                <p class="text-gray-600">
                    {{ \App\Helpers\TranslationHelper::transFromDatabase('Ledger.Display.FiscalYear', __('Fiscal Year')) }}: {{ $fiscalYear->name }} 
                    @if($period)
                        | {{ \App\Helpers\TranslationHelper::transFromDatabase('Ledger.Display.Period', __('Period')) }}: {{ $period->name }}
                    @endif
                </p>
            </div>

            <div class="mb-4 p-3 bg-gray-50 rounded-md">
                <p class="font-semibold">{{ \App\Helpers\TranslationHelper::transFromDatabase('Ledger.Display.OpeningBalance', __('Opening Balance')) }}: <span class="{{ $openingBalance >= 0 ? 'text-green-600' : 'text-red-600' }}">{{ number_format($openingBalance, 2) }}</span></p>
                <p class="font-semibold">{{ \App\Helpers\TranslationHelper::transFromDatabase('Ledger.Display.CurrentBalance', __('Current Balance')) }}: <span class="{{ $currentBalance >= 0 ? 'text-green-600' : 'text-red-600' }}">{{ number_format($currentBalance, 2) }}</span></p>
            </div>

            <div class="overflow--auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">{{ \App\Helpers\TranslationHelper::transFromDatabase('Ledger.Table.Date', __('Date')) }}</th>
                            <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">{{ \App\Helpers\TranslationHelper::transFromDatabase('Ledger.Table.ReferenceNumber', __('Reference Number')) }}</th>
                            <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">{{ \App\Helpers\TranslationHelper::transFromDatabase('Ledger.Table.Description', __('Description')) }}</th>
                            <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">{{ \App\Helpers\TranslationHelper::transFromDatabase('Ledger.Table.Branch', __('Branch')) }}</th>
                            <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">{{ \App\Helpers\TranslationHelper::transFromDatabase('Ledger.Table.Debit', __('Debit')) }}</th>
                            <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">{{ \App\Helpers\TranslationHelper::transFromDatabase('Ledger.Table.Credit', __('Credit')) }}</th>
                            <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">{{ \App\Helpers\TranslationHelper::transFromDatabase('Ledger.Table.Balance', __('Balance')) }}</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        @if($transactions->isEmpty())
                            <tr>
                                <td colspan="7" class="p-6 py-4 text-center text-gray-500">{{ \App\Helpers\TranslationHelper::transFromDatabase('Ledger.Empty.NoTransactions', __('No transactions found for this account')) }}</td>
                            </tr>
                        @else
                            @foreach($transactions as $transaction)
                                <tr>
                                    <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">{{ $transaction->journalEntry->entry_date }}</td>
                                    <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">{{ $transaction->journalEntry->reference_number }}</td>
                                    <td class="p-6 py-4 text-sm text-gray-900">{{ $transaction->description }}</td>
                                    <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">{{ $transaction->journalEntry->branch?->name ?? '-' }}</td>
                                    <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">{{ number_format($transaction->debit_amount, 2) }}</td>
                                    <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">{{ number_format($transaction->credit_amount, 2) }}</td>
                                    <td class="p-6 py-4 whitespace-nowrap text-sm {{ $transaction->balance >= 0 ? 'text-green-600' : 'text-red-600' }}">
                                        {{ number_format($transaction->balance, 2) }}
                                    </td>
                                </tr>
                            @endforeach
                        @endif
                    </tbody>
                </table>
            </div>
        </div>
    @endif
</div>
@endsection
