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

    <div class="bg-white rounded-lg shadow-md p-6 mb-6">
        <form action="{{ route('accounting.ledger.balance-sheet') }}" method="GET" class="grid grid-cols-1 md:grid-cols-4 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>
                <label for="from_date" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('From Date', __('From Date')) }}</label>
                <input type="date" id="from_date" name="from_date" value="{{ request('from_date') }}" class="block w-full rounded-md border-gray-300 shadow-sm focus:border-primary-500 focus:ring focus:ring-primary-500 focus:ring-opacity-50">
            </div>

            <div>
                <label for="to_date" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('To Date', __('To Date')) }}</label>
                <input type="date" id="to_date" name="to_date" value="{{ request('to_date') }}" class="block w-full rounded-md border-gray-300 shadow-sm focus:border-primary-500 focus:ring focus:ring-primary-500 focus:ring-opacity-50">
            </div>

            <div class="md:col-span-4">
                <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('View Balance Sheet', __('View Balance Sheet')) }}
                </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('Balance Sheet', __('Balance Sheet')) }}</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
                    @if(request('from_date'))
                        | {{ \App\Helpers\TranslationHelper::transFromDatabase('From', __('From')) }}: {{ request('from_date') }}
                    @endif
                    @if(request('to_date'))
                        | {{ \App\Helpers\TranslationHelper::transFromDatabase('To', __('To')) }}: {{ request('to_date') }}
                    @endif
                </p>
            </div>

            <div class="mb-8">
                <h3 class="text-lg font-semibold mb-3 border-b pb-2">{{ \App\Helpers\TranslationHelper::transFromDatabase('Assets', __('Assets')) }}</h3>
                <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('Amount', __('Amount')) }}</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            @if($assets->isEmpty())
                                <tr>
                                    <td colspan="3" class="p-6 py-4 text-center text-gray-500">{{ \App\Helpers\TranslationHelper::transFromDatabase('No Assets Recorded', __('No Assets Recorded')) }}</td>
                                </tr>
                            @else
                                @foreach($assets as $asset)
                                    <tr>
                                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">{{ $asset['account']->code }}</td>
                                        <td class="p-6 py-4 text-sm text-gray-900">{{ $asset['account']->name }}</td>
                                        <td class="p-6 py-4 whitespace-nowrap text-sm text-green-600">{{ number_format($asset['balance'], 2) }}</td>
                                    </tr>
                                @endforeach
                                <tr class="bg-gray-50 font-bold">
                                    <td colspan="2" class="p-6 py-4 text-sm text-gray-900">{{ \App\Helpers\TranslationHelper::transFromDatabase('Total Assets', __('Total Assets')) }}</td>
                                    <td class="p-6 py-4 whitespace-nowrap text-sm text-green-600">{{ number_format($totalAssets, 2) }}</td>
                                </tr>
                            @endif
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="mb-8">
                <h3 class="text-lg font-semibold mb-3 border-b pb-2">{{ \App\Helpers\TranslationHelper::transFromDatabase('Liabilities', __('Liabilities')) }}</h3>
                <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('Amount', __('Amount')) }}</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            @if($liabilities->isEmpty())
                                <tr>
                                    <td colspan="3" class="p-6 py-4 text-center text-gray-500">{{ \App\Helpers\TranslationHelper::transFromDatabase('No Liabilities Recorded', __('No Liabilities Recorded')) }}</td>
                                </tr>
                            @else
                                @foreach($liabilities as $liability)
                                    <tr>
                                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">{{ $liability['account']->code }}</td>
                                        <td class="p-6 py-4 text-sm text-gray-900">{{ $liability['account']->name }}</td>
                                        <td class="p-6 py-4 whitespace-nowrap text-sm text-red-600">{{ number_format($liability['balance'], 2) }}</td>
                                    </tr>
                                @endforeach
                                <tr class="bg-gray-50 font-bold">
                                    <td colspan="2" class="p-6 py-4 text-sm text-gray-900">{{ \App\Helpers\TranslationHelper::transFromDatabase('Total Liabilities', __('Total Liabilities')) }}</td>
                                    <td class="p-6 py-4 whitespace-nowrap text-sm text-red-600">{{ number_format($totalLiabilities, 2) }}</td>
                                </tr>
                            @endif
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="mb-8">
                <h3 class="text-lg font-semibold mb-3 border-b pb-2">{{ \App\Helpers\TranslationHelper::transFromDatabase('Equity', __('Equity')) }}</h3>
                <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('Amount', __('Amount')) }}</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            @if($equity->isEmpty())
                                <tr>
                                    <td colspan="3" class="p-6 py-4 text-center text-gray-500">{{ \App\Helpers\TranslationHelper::transFromDatabase('No Equity Recorded', __('No Equity Recorded')) }}</td>
                                </tr>
                            @else
                                @foreach($equity as $eq)
                                    <tr>
                                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">{{ $eq['account']->code }}</td>
                                        <td class="p-6 py-4 text-sm text-gray-900">{{ $eq['account']->name }}</td>
                                        <td class="p-6 py-4 whitespace-nowrap text-sm text-blue-600">{{ number_format($eq['balance'], 2) }}</td>
                                    </tr>
                                @endforeach
                                <tr class="bg-gray-50 font-bold">
                                    <td colspan="2" class="p-6 py-4 text-sm text-gray-900">{{ \App\Helpers\TranslationHelper::transFromDatabase('Total Equity', __('Total Equity')) }}</td>
                                    <td class="p-6 py-4 whitespace-nowrap text-sm text-blue-600">{{ number_format($totalEquity, 2) }}</td>
                                </tr>
                            @endif
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="p-4 bg-gray-50 rounded-md">
                <h3 class="text-lg font-bold mb-2">{{ \App\Helpers\TranslationHelper::transFromDatabase('Total Liabilities and Equity', __('Total Liabilities and Equity')) }}</h3>
                <p class="text-l font-bold text-gray-900">
                    {{ number_format($totalLiabilities + $totalEquity, 2) }}
                </p>
            </div>
        </div>
    @endif
</div>
@endsection