@extends('layouts.admin')

@section('title', 'Journal Entries')

@section('page-title', 'Journal Entries')

@section('header-actions')
    <a href="{{ route('admin.journal-entries.create') }}" class="px-4 py-2 bg-primary text-white rounded-lg hover:bg-blue-600 transition-colors flex items-center gap-2">
        <span class="material-symbols-outlined text-lg">add</span>
        <span>New Entry</span>
    </a>
@endsection

@section('content')
<div class="max-w-7xl mx-auto">
    <div class="bg-white rounded-xl shadow-sm border border-[#e5e7eb] overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full text-left">
                <thead class="bg-background-light border-b border-[#e5e7eb]">
                    <tr>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Entry #</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Date</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Description</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider text-right">Debit</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider text-right">Credit</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Status</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider text-right">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-[#e5e7eb]">
                    @forelse($entries as $entry)
                        <tr class="hover:bg-background-light/50 transition-colors">
                            <td class="py-4 px-4">
                                <a href="{{ route('admin.journal-entries.show', $entry) }}" class="text-primary hover:underline font-medium">
                                    {{ $entry->entry_number }}
                                </a>
                            </td>
                            <td class="py-4 px-4 text-sm text-[#617589]">
                                {{ $entry->entry_date->format('M d, Y') }}
                            </td>
                            <td class="py-4 px-4 text-sm text-[#111418] text-gray-900">
                                {{ Str::limit($entry->description, 50) }}
                            </td>
                            <td class="py-4 px-4 text-sm font-medium text-[#111418] text-gray-900 text-right">
                                {{ number_format($entry->total_debit, 2) }}
                            </td>
                            <td class="py-4 px-4 text-sm font-medium text-[#111418] text-gray-900 text-right">
                                {{ number_format($entry->total_credit, 2) }}
                            </td>
                            <td class="py-4 px-4">
                                <span class="px-2 py-1 rounded text-xs font-medium {{ $entry->status === 'posted' ? 'bg-green-100 text-green-800' : 'bg-gray-100 text-gray-800' }}">
                                    {{ ucfirst($entry->status) }}
                                </span>
                            </td>
                            <td class="py-4 px-4 text-right">
                                <div class="flex items-center justify-end gap-2">
                                    <a href="{{ route('admin.journal-entries.show', $entry) }}" class="p-2 text-[#617589] hover:text-primary hover:bg-primary/10 rounded transition-colors" title="View">
                                        <span class="material-symbols-outlined text-lg">visibility</span>
                                    </a>
                                    @if($entry->status === 'draft')
                                        <form method="POST" action="{{ route('admin.journal-entries.post', $entry) }}" class="inline">
                                            @csrf
                                            <button type="submit" class="p-2 text-[#617589] hover:text-green-600 hover:bg-green-50 rounded transition-colors" title="Post">
                                                <span class="material-symbols-outlined text-lg">check_circle</span>
                                            </button>
                                        </form>
                                    @endif
                                    @if($entry->status === 'posted' && !$entry->is_reversed)
                                        <form method="POST" action="{{ route('admin.journal-entries.reverse', $entry) }}" onsubmit="return confirm('Are you sure you want to reverse this entry?')" class="inline">
                                            @csrf
                                            <button type="submit" class="p-2 text-[#617589] hover:text-red-600 hover:bg-red-50 rounded transition-colors" title="Reverse">
                                                <span class="material-symbols-outlined text-lg">undo</span>
                                            </button>
                                        </form>
                                    @endif
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="py-12 px-4 text-center text-[#617589]">
                                <span class="material-symbols-outlined text-4xl mb-2 block">book</span>
                                <p>No journal entries found</p>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        @if($entries->hasPages())
            <div class="px-4 py-4 border-t border-[#e5e7eb]">
                {{ $entries->links() }}
            </div>
        @endif
    </div>
</div>
@endsection



