@extends('layouts.admin.app')

@section('title', translate('Balance Sheet'))

@section('content')
    <div class="content container-fluid">
        <div class="page-header">
            <h1 class="page-header-title">
                <span class="page-header-icon">
                    <i class="tio-receipt-outlined"></i>
                </span>
                <span>{{ translate('Balance Sheet') }}</span>
            </h1>
        </div>

        <div class="card mb-3">
            <div class="card-body">
                <form action="{{ route('admin.report.balance-sheet') }}" method="get">
                    <div class="row g-3">
                        <div class="col-sm-6 col-md-3">
                            <select class="form-control js-gl-filter-select" name="filter">
                                <option value="all_time" {{ request('filter', 'all_time') === 'all_time' ? 'selected' : '' }}>{{ translate('All Time') }}</option>
                                <option value="this_year" {{ request('filter') === 'this_year' ? 'selected' : '' }}>{{ translate('This Year') }}</option>
                                <option value="previous_year" {{ request('filter') === 'previous_year' ? 'selected' : '' }}>{{ translate('Previous Year') }}</option>
                                <option value="this_month" {{ request('filter') === 'this_month' ? 'selected' : '' }}>{{ translate('This Month') }}</option>
                                <option value="this_week" {{ request('filter') === 'this_week' ? 'selected' : '' }}>{{ translate('This Week') }}</option>
                                <option value="custom" {{ request('filter') === 'custom' ? 'selected' : '' }}>{{ translate('Custom') }}</option>
                            </select>
                        </div>
                        <div class="col-sm-6 col-md-2 js-gl-from-wrapper {{ request('filter') === 'custom' ? '' : 'd-none' }}">
                            <input type="date" name="from" value="{{ request('from') }}" class="form-control">
                        </div>
                        <div class="col-sm-6 col-md-2 js-gl-to-wrapper {{ request('filter') === 'custom' ? '' : 'd-none' }}">
                            <input type="date" name="to" value="{{ request('to') }}" class="form-control">
                        </div>
                        <div class="col-sm-6 col-md-1">
                            <button class="btn btn--primary btn-block" type="submit">{{ translate('Go') }}</button>
                        </div>
                        <div class="col-sm-6 col-md-1">
                            <a href="{{ route('admin.report.balance-sheet') }}" class="btn btn--reset btn-block">{{ translate('Reset') }}</a>
                        </div>
                        <div class="col-sm-12 col-md-3">
                            <div class="text-muted pt-2">{{ translate('As of') }}: {{ $as_of->toDateString() }}</div>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <div class="row g-3 mb-3">
            <div class="col-sm-6 col-lg-3">
                <div class="card h-100"><div class="card-body"><h6>{{ translate('Total Assets') }}</h6><h3>{{ \App\CentralLogics\Helpers::format_currency($totals['assets']) }}</h3></div></div>
            </div>
            <div class="col-sm-6 col-lg-3">
                <div class="card h-100"><div class="card-body"><h6>{{ translate('Liabilities + Equity') }}</h6><h3>{{ \App\CentralLogics\Helpers::format_currency($totals['liabilities_and_equity']) }}</h3></div></div>
            </div>
            <div class="col-sm-6 col-lg-3">
                <div class="card h-100"><div class="card-body"><h6>{{ translate('Difference') }}</h6><h3>{{ \App\CentralLogics\Helpers::format_currency($totals['difference']) }}</h3></div></div>
            </div>
            <div class="col-sm-6 col-lg-3">
                <div class="card h-100">
                    <div class="card-body">
                        <h6>{{ translate('Status') }}</h6>
                        <h3>
                            <span class="badge badge-{{ $totals['is_balanced'] ? 'success' : 'danger' }}">
                                {{ $totals['is_balanced'] ? translate('Balanced') : translate('Mismatch') }}
                            </span>
                        </h3>
                    </div>
                </div>
            </div>
        </div>

        <div class="row g-3">
            <div class="col-lg-6">
                <div class="card h-100">
                    <div class="card-header border-0"><h4 class="card-title mb-0">{{ translate('Assets') }}</h4></div>
                    <div class="table-responsive">
                        <table class="table table-borderless table-thead-bordered mb-0">
                            <thead class="thead-light">
                            <tr><th>{{ translate('Account') }}</th><th class="text-right">{{ translate('Balance') }}</th></tr>
                            </thead>
                            <tbody>
                            @forelse($assets as $row)
                                <tr><td>{{ $row['code'] }} - {{ $row['name'] }}</td><td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($row['balance']) }}</td></tr>
                            @empty
                                <tr><td colspan="2" class="text-center py-4">{{ translate('No asset balance found') }}</td></tr>
                            @endforelse
                            </tbody>
                            <tfoot><tr class="border-top font-weight-bold"><td>{{ translate('Total Assets') }}</td><td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($totals['assets']) }}</td></tr></tfoot>
                        </table>
                    </div>
                </div>
            </div>

            <div class="col-lg-6">
                <div class="card h-100">
                    <div class="card-header border-0"><h4 class="card-title mb-0">{{ translate('Liabilities and Equity') }}</h4></div>
                    <div class="table-responsive">
                        <table class="table table-borderless table-thead-bordered mb-0">
                            <thead class="thead-light">
                            <tr><th>{{ translate('Account') }}</th><th class="text-right">{{ translate('Balance') }}</th></tr>
                            </thead>
                            <tbody>
                            @forelse($liabilities as $row)
                                <tr><td>{{ $row['code'] }} - {{ $row['name'] }}</td><td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($row['balance']) }}</td></tr>
                            @empty
                                <tr><td colspan="2" class="text-center py-4">{{ translate('No liability balance found') }}</td></tr>
                            @endforelse
                            @foreach($equity as $row)
                                <tr><td>{{ $row['code'] }} - {{ $row['name'] }}</td><td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($row['balance']) }}</td></tr>
                            @endforeach
                            <tr>
                                <td>{{ translate('Retained Earnings') }}</td>
                                <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($retained_earnings) }}</td>
                            </tr>
                            </tbody>
                            <tfoot><tr class="border-top font-weight-bold"><td>{{ translate('Total Liabilities and Equity') }}</td><td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($totals['liabilities_and_equity']) }}</td></tr></tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

@push('script_2')
    <script>
        "use strict";
        $('.js-gl-filter-select').on('change', function () {
            const isCustom = $(this).val() === 'custom';
            $('.js-gl-from-wrapper, .js-gl-to-wrapper').toggleClass('d-none', !isCustom);
        });
    </script>
@endpush
