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

    <div class="bg-white rounded-lg shadow-md p-6 mb-6">
        <form action="{{ route('accounting.ledger.trial-balance') }}" 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('Display Trial Balance', __('Display Trial Balance')) }}
                </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('Trial Balance', __('Trial Balance')) }}</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-x-auto">
                <table class="min-w-full divide-y divide-gray-200 border">
                    <thead class="bg-gray-50">
                        <tr>
                            <th rowspan="2" scope="col" class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider border-b border-l">{{ \App\Helpers\TranslationHelper::transFromDatabase('Account Code', __('Account Code')) }}</th>
                            <th rowspan="2" scope="col" class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider border-b border-l">{{ \App\Helpers\TranslationHelper::transFromDatabase('Account Name', __('Account Name')) }}</th>
                            <th colspan="2" scope="col" class="px-6 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider border-b border-l">{{ \App\Helpers\TranslationHelper::transFromDatabase('Opening Balance', __('Opening Balance')) }}</th>
                            <th colspan="2" scope="col" class="px-6 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider border-b border-l">{{ \App\Helpers\TranslationHelper::transFromDatabase('Period Transactions', __('Period Transactions')) }}</th>
                            <th colspan="2" scope="col" class="px-6 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider border-b">{{ \App\Helpers\TranslationHelper::transFromDatabase('Ending Balance', __('Ending Balance')) }}</th>
                        </tr>
                        <tr>
                            <th scope="col" class="px-6 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider border-b border-l">{{ \App\Helpers\TranslationHelper::transFromDatabase('Debit', __('Debit')) }}</th>
                            <th scope="col" class="px-6 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider border-b border-l">{{ \App\Helpers\TranslationHelper::transFromDatabase('Credit', __('Credit')) }}</th>
                            <th scope="col" class="px-6 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider border-b border-l">{{ \App\Helpers\TranslationHelper::transFromDatabase('Debit', __('Debit')) }}</th>
                            <th scope="col" class="px-6 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider border-b border-l">{{ \App\Helpers\TranslationHelper::transFromDatabase('Credit', __('Credit')) }}</th>
                            <th scope="col" class="px-6 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider border-b border-l">{{ \App\Helpers\TranslationHelper::transFromDatabase('Debit', __('Debit')) }}</th>
                            <th scope="col" class="px-6 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider border-b">{{ \App\Helpers\TranslationHelper::transFromDatabase('Credit', __('Credit')) }}</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        @if($balances->isEmpty())
                            <tr>
                                <td colspan="8" class="px-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="px-6 py-4 whitespace-nowrap text-sm text-gray-900 border-l">{{ $balance['account']->code }}</td>
                                    <td class="px-6 py-4 text-sm text-gray-900 border-l">{{ $balance['account']->name }}</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 border-l text-center">
                                        {{ number_format($balance['opening_debit'], 2) }}
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 border-l text-center">
                                        {{ number_format($balance['opening_credit'], 2) }}
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 border-l text-center">
                                        {{ number_format($balance['period_debit'], 2) }}
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 border-l text-center">
                                        {{ number_format($balance['period_credit'], 2) }}
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 border-l text-center">
                                        {{ number_format($balance['ending_debit'], 2) }}
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 text-center">
                                        {{ number_format($balance['ending_credit'], 2) }}
                                    </td>
                                </tr>
                            @endforeach
                            <tr class="bg-gray-50 font-bold">
                                <td colspan="2" class="px-6 py-4 text-sm text-gray-900 text-center border-l">{{ \App\Helpers\TranslationHelper::transFromDatabase('Total', __('Total')) }}</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 border-l text-center">{{ number_format($totalOpeningDebit, 2) }}</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 border-l text-center">{{ number_format($totalOpeningCredit, 2) }}</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 border-l text-center">{{ number_format($totalPeriodDebit, 2) }}</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 border-l text-center">{{ number_format($totalPeriodCredit, 2) }}</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 border-l text-center">{{ number_format($totalEndingDebit, 2) }}</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 text-center">{{ number_format($totalEndingCredit, 2) }}</td>
                            </tr>
                        @endif
                    </tbody>
                </table>
            </div>
        </div>
    @endif
</div>
@endsection
