@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('Account Balances', __('Account Balances')) }}</h1>
    </div>

    <div class="bg-white rounded-lg shadow-md p-6 mb-6">
        <form action="{{ route('accounting.ledger.account-balances') }}" method="GET" class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div>
                <label for="fiscal_year_id" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Fiscal Year', __('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('Select Fiscal Year', __('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('Accounting Period', __('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('All Periods', __('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 class="md:col-span-2">
                <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('Show Account Balances', __('Show Account Balances')) }}
                </button>
            </div>
        </form>
    </div>

    @if($fiscalYear)
        <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('Account Balances', __('Account Balances')) }}</h2>
                <p class="text-gray-600">
                    {{ \App\Helpers\TranslationHelper::transFromDatabase('Fiscal Year', __('Fiscal Year')) }}: {{ $fiscalYear->name }} 
                    @if($period)
                        | {{ \App\Helpers\TranslationHelper::transFromDatabase('Period', __('Period')) }}: {{ $period->name }}
                    @endif
                </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('Account Code', __('Account Code')) }}</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('Account Name', __('Account Name')) }}</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('Account Type', __('Account Type')) }}</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('Opening Balance', __('Opening Balance')) }}</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('Total Debit', __('Total 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('Total Credit', __('Total 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('Current Balance', __('Current Balance')) }}</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        @if($balances->isEmpty())
                            <tr>
                                <td colspan="7" class="p-6 py-4 text-center text-gray-500">{{ \App\Helpers\TranslationHelper::transFromDatabase('No Data Available', __('No Data Available')) }}</td>
                            </tr>
                        @else
                            @foreach($balances as $balance)
                                <tr>
                                    <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">{{ $balance['account']->code }}</td>
                                    <td class="p-6 py-4 text-sm text-gray-900">{{ $balance['account']->name }}</td>
                                    <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">{{ $balance['account']->account_type }}</td>
                                    <td class="p-6 py-4 whitespace-nowrap text-sm {{ $balance['opening_balance'] >= 0 ? 'text-green-600' : 'text-red-600' }}">
                                        {{ number_format($balance['opening_balance'], 2) }}
                                    </td>
                                    <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">{{ number_format($balance['total_debit'], 2) }}</td>
                                    <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">{{ number_format($balance['total_credit'], 2) }}</td>
                                    <td class="p-6 py-4 whitespace-nowrap text-sm font-medium {{ $balance['current_balance'] >= 0 ? 'text-green-600' : 'text-red-600' }}">
                                        {{ number_format($balance['current_balance'], 2) }}
                                    </td>
                                </tr>
                            @endforeach
                        @endif
                    </tbody>
                </table>
            </div>
        </div>
    @endif
</div>
@endsection
