@extends('layouts.app')
@section('title', $entry->entry_number)
@section('page_title', 'Journal Entry ' . $entry->entry_number)
@section('breadcrumbs')
    <li class="breadcrumb-item"><a href="{{ route('journals.index') }}">Journals</a></li><li class="breadcrumb-item active">{{ $entry->entry_number }}</li>
@endsection
@section('page_actions')
    @can('accounting.delete')
        @unless($entry->source_type)
            <form method="POST" action="{{ route('journals.destroy', $entry) }}" data-confirm="Delete this entry?">@csrf @method('DELETE')<button class="btn btn-outline-danger"><i class="bi bi-trash me-1"></i>Delete</button></form>
        @endunless
    @endcan
@endsection

@section('content')
    <div class="card">
        <div class="card-header d-flex justify-content-between">
            <span><i class="bi bi-journal-text me-1"></i>{{ $entry->description }}</span>
            <span class="badge bg-soft-info text-capitalize">{{ $entry->type }}</span>
        </div>
        <div class="card-body">
            <dl class="row small mb-3">
                <dt class="col-sm-2 text-muted">Date</dt><dd class="col-sm-4">{{ $entry->date->format('d M Y') }}</dd>
                <dt class="col-sm-2 text-muted">Reference</dt><dd class="col-sm-4">{{ $entry->reference ?: '—' }}</dd>
                @if($entry->source_type)<dt class="col-sm-2 text-muted">Source</dt><dd class="col-sm-10"><span class="badge bg-light text-dark">{{ class_basename($entry->source_type) }} #{{ $entry->source_id }} (auto-posted)</span></dd>@endif
            </dl>
            <table class="table align-middle">
                <thead><tr><th>Account</th><th>Narration</th><th class="text-end">Debit</th><th class="text-end">Credit</th></tr></thead>
                <tbody>
                    @foreach($entry->lines as $line)
                        <tr>
                            <td class="fw-semibold">{{ $line->account?->label }}</td>
                            <td class="small text-muted">{{ $line->narration }}</td>
                            <td class="text-end">{{ $line->debit > 0 ? number_format($line->debit, 2) : '' }}</td>
                            <td class="text-end">{{ $line->credit > 0 ? number_format($line->credit, 2) : '' }}</td>
                        </tr>
                    @endforeach
                </tbody>
                <tfoot class="table-light">
                    <tr><th colspan="2">Totals</th><th class="text-end">{{ number_format($entry->total_debit, 2) }}</th><th class="text-end">{{ number_format($entry->total_credit, 2) }}</th></tr>
                </tfoot>
            </table>
        </div>
    </div>
@endsection
