@extends('layouts.app')

@section('title', $entry->reference_number)

@section('content')
    <x-page-head :title="$entry->reference_number" :subtitle="$entry->description ?? 'Journal entry detail'">
        <x-slot:actions>
            <a href="{{ route('accounting.journal.index') }}" class="btn btn-outline btn-sm">Back to Journal</a>
        </x-slot:actions>
    </x-page-head>

    <div class="stat-grid">
        <x-stat-card label="Date" :value="$entry->date->format('d M Y')" />
        <x-stat-card label="Source" :value="$entry->source?->label() ?? '—'" color="teal" />
        <x-stat-card label="Posted By" :value="$entry->postedBy?->name ?? '—'" color="violet" />
        <x-stat-card
            label="Status"
            :value="$entry->status instanceof \App\Enums\JournalStatus ? $entry->status->label() : $entry->status"
            :color="$entry->status === \App\Enums\JournalStatus::Voided ? 'red' : 'green'"
        />
    </div>

    @if ($entry->void_reason)
        <x-panel title="Void Reason" style="margin-top:16px;">
            <p style="margin:0;">{{ $entry->void_reason }}</p>
        </x-panel>
    @endif

    <x-panel title="Journal Lines" style="margin-top:16px;">
        <div class="table-wrap">
            <table>
                <thead>
                    <tr>
                        <th>Account</th>
                        <th>Memo</th>
                        <th class="text-right">Debit</th>
                        <th class="text-right">Credit</th>
                    </tr>
                </thead>
                <tbody>
                    @php
                        $totalDebits = '0.00';
                        $totalCredits = '0.00';
                    @endphp
                    @foreach ($entry->lines as $line)
                        @php
                            $totalDebits = bcadd($totalDebits, (string) $line->debit_amount, 2);
                            $totalCredits = bcadd($totalCredits, (string) $line->credit_amount, 2);
                        @endphp
                        <tr>
                            <td>{{ $line->account?->code }} — {{ $line->account?->name }}</td>
                            <td>{{ $line->line_memo ?? '—' }}</td>
                            <td class="text-right">{{ bccomp((string) $line->debit_amount, '0.00', 2) !== 0 ? number_format($line->debit_amount, 2) : '—' }}</td>
                            <td class="text-right">{{ bccomp((string) $line->credit_amount, '0.00', 2) !== 0 ? number_format($line->credit_amount, 2) : '—' }}</td>
                        </tr>
                    @endforeach
                    <tr style="font-weight:700; background:#f8fafc;">
                        <td colspan="2">Totals</td>
                        <td class="text-right">{{ number_format($totalDebits, 2) }}</td>
                        <td class="text-right">{{ number_format($totalCredits, 2) }}</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </x-panel>

    @if ($canVoid)
        <x-panel title="Void Entry" style="margin-top:16px;">
            <form method="POST" action="{{ route('accounting.journal.void', $entry) }}">
                @csrf
                <div class="form-group">
                    <label class="form-label" for="void_reason">Reason</label>
                    <textarea class="input" id="void_reason" name="void_reason" rows="3" required placeholder="Reason for voiding this entry"></textarea>
                </div>
                <button type="submit" class="btn btn-outline" onclick="return confirm('Void this journal entry?')">Void Entry</button>
            </form>
        </x-panel>
    @endif
@endsection
