@extends('layouts.app')

@section('title', 'Journal')

@section('content')
    <x-page-head title="Journal" subtitle="Every transaction as balanced debit & credit entries">
        <x-slot:actions>
            <x-export-menu module="journal-entries" :formats="['xlsx','csv']" />
            <a href="{{ route('accounting.journal.create') }}" class="btn btn-primary">Post Manual Entry</a>
        </x-slot:actions>
    </x-page-head>

    <x-panel>
        @if ($entries->isEmpty())
            <x-empty-state title="No journal entries" message="Post a manual entry or record a transaction from another module.">
                <x-slot:action>
                    <a href="{{ route('accounting.journal.create') }}" class="btn btn-primary">Post entry</a>
                </x-slot:action>
            </x-empty-state>
        @else
            <div class="table-wrap">
                <table>
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Reference</th>
                        <th>Description</th>
                        <th>Source</th>
                        <th>Lines</th>
                        <th>Posted By</th>
                        <th>Status</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($entries as $entry)
                        <tr>
                            <td>{{ $entry->date->format('d M Y') }}</td>
                            <td><a href="{{ route('accounting.journal.show', $entry) }}"><strong>{{ $entry->reference_number }}</strong></a></td>
                            <td>{{ Str::limit($entry->description, 60) }}</td>
                            <td>{{ $entry->source?->label() ?? '—' }}</td>
                            <td>{{ $entry->lines->count() }}</td>
                            <td>{{ $entry->postedBy?->name ?? '—' }}</td>
                                <td><x-badge variant="{{ $entry->status === \App\Enums\JournalStatus::Voided ? 'red' : 'green' }}">{{ $entry->status instanceof \App\Enums\JournalStatus ? $entry->status->label() : $entry->status }}</x-badge></td>
                        </tr>
                    @endforeach
                </tbody>
                </table>
            </div>
            <div style="margin-top:16px;">{{ $entries->links() }}</div>
        @endif
    </x-panel>
@endsection
