@extends('admin.accounting._layout')

@php
    $reportTitle = $config['title'] ?? 'Accounting';
    $viewType = $config['view'] ?? 'party_balance';
@endphp

@section('accounting_content')
<div class="hub-page">
    <div class="hub-page__header">
        <h1 class="hub-page__title">{{ $config['title'] }}</h1>
        @if(!empty($config['about']))
            <p class="hub-page__subtitle">{{ $config['about'] }}</p>
        @endif
    </div>
    <div class="hub-page__body">
        <form method="GET" action="{{ route('admin.accounting.show', $report_key) }}" id="accounting-filter-form" data-hub-filter-form data-auto-submit="false">
            @if($viewType !== 'party_balance')
                @php
                    $extraFilterHtml = '';
                    if (!empty($config['requires_item'])) {
                        $extraFilterHtml .= view('admin.accounting.partials.filter-item', [
                            'items' => $items,
                            'selectedItemId' => $filters['item_id'] ?? '',
                        ])->render();
                    }
                    if ($viewType === 'cash_book') {
                        $extraFilterHtml .= view('admin.accounting.partials.filter-cash-book', [
                            'filters' => $filters,
                            'paymentKinds' => $paymentKinds,
                        ])->render();
                    }
                @endphp
                <div class="hub-filter">
                    <div class="hub-filter__label">
                        <i class="ri-calendar-2-line"></i>
                        {{ $config['date_label'] ?? 'Period' }}
                    </div>
                    <div class="row g-3 align-items-end">
                        <div class="col-sm-auto">
                            <div class="hub-date-range">
                                <input type="date" name="date_from" class="form-control form-control-sm" value="{{ $date_from }}" style="min-width: 10.5rem;">
                                <span class="hub-date-range__sep">to</span>
                                <input type="date" name="date_to" class="form-control form-control-sm" value="{{ $date_to }}" style="min-width: 10.5rem;">
                            </div>
                        </div>
                        {!! $extraFilterHtml !!}
                    </div>
                    <div class="hub-date-presets">
                        <button type="button" class="btn btn-outline-secondary btn-sm" data-date-preset="this_month">This month</button>
                        <button type="button" class="btn btn-outline-secondary btn-sm" data-date-preset="last_month">Last month</button>
                        <button type="button" class="btn btn-outline-secondary btn-sm" data-date-preset="last_30">Last 30 days</button>
                        <button type="button" class="btn btn-outline-secondary btn-sm" data-date-preset="this_fy">This FY</button>
                    </div>
                    <div class="hub-filter__actions">
                        <button type="submit" class="btn btn-sm hub-btn-primary">
                            <i class="ri-filter-3-line me-1"></i> Apply
                        </button>
                        <a href="{{ route('admin.accounting.show', $report_key) }}" class="btn btn-sm btn-light border hub-btn-outline">Clear</a>
                        <a href="#" class="btn btn-sm hub-btn-primary hub-btn-export ms-sm-auto" id="accounting-export"
                           data-hub-export data-export-icon="ri-file-excel-2-line">
                            <i class="ri-file-excel-2-line"></i> Export Excel
                        </a>
                    </div>
                </div>
            @else
                <div class="hub-filter">
                    <div class="hub-filter__label">
                        <i class="ri-database-2-line"></i>
                        Live balances
                    </div>
                    <p class="text-muted small mb-0">Outstanding amounts are calculated from current ledger balances.</p>
                    <div class="hub-filter__actions">
                        <a href="#" class="btn btn-sm hub-btn-primary hub-btn-export" id="accounting-export"
                           data-hub-export data-export-icon="ri-file-excel-2-line">
                            <i class="ri-file-excel-2-line"></i> Export Excel
                        </a>
                    </div>
                </div>
            @endif
        </form>

        @if($viewType === 'item_ledger' && empty($filters['item_id']))
            @include('partials.admin.module-hub.empty', [
                'icon' => 'ri-archive-line',
                'title' => 'Select an item',
                'text' => 'Choose an item and date range above, then click Apply to view the ledger and export.',
            ])
        @elseif($viewType === 'item_ledger' && $item)
            <div class="hub-period-chip">
                <i class="ri-calendar-line"></i>
                {{ \Carbon\Carbon::parse($date_from)->format('M j, Y') }}
                —
                {{ \Carbon\Carbon::parse($date_to)->format('M j, Y') }}
            </div>
            @include('admin.accounting.partials.item-detail', ['item' => $item])
            @include('admin.accounting.partials.ledger-table', ['ledgerMode' => $ledger_mode ?? 'stock'])
        @elseif($viewType === 'cash_book')
            @include('admin.accounting.partials.cash-book-table')
        @elseif($viewType === 'profit_loss')
            @include('admin.accounting.partials.profit-loss-table')
        @elseif($viewType === 'inventory_book')
            @include('admin.accounting.partials.inventory-book-table')
        @else
            @include('admin.accounting.partials.party-balance-table')
        @endif
    </div>
</div>
@endsection

@push('scripts')
<script>
(function () {
    const form = document.getElementById('accounting-filter-form');
    const exportBtn = document.getElementById('accounting-export');
    if (!exportBtn || !form) return;

    const exportUrl = @json(route('admin.accounting.export', $report_key));

    exportBtn.addEventListener('click', function (e) {
        e.preventDefault();
        const params = new URLSearchParams(new FormData(form));
        window.location.href = exportUrl + '?' + params.toString();
    });
})();
</script>
@endpush
