@extends('layouts.app')

@section('title', \App\Helpers\TranslationHelper::transFromDatabase('Journal Entries Management', 'Journal Entries Management'))

@section('content')
<div class="min-h-screen bg-gradient-to-br from-slate-50 to-slate-100 py-8 px-4 md:px-8">
    <div class="max-w-7xl mx-auto">
        <div class="mb-8">
            <div class="flex items-center justify-between mb-6">
                <div>
                    <h1 class="text-3xl font-bold text-gray-900 mb-2">{{ \App\Helpers\TranslationHelper::transFromDatabase('Journal Entries Management', 'Journal Entries Management') }}</h1>
                    <p class="text-gray-600">{{ \App\Helpers\TranslationHelper::transFromDatabase('View and manage all automatic and manual accounting entries', 'View and manage all automatic and manual accounting entries') }}</p>
                </div>
                <a href="{{ route('accounting.journal-entries.create') }}" class="inline-flex items-center px-6 py-3 bg-gradient-to-r from-blue-600 to-blue-700 text-white font-semibold rounded-lg hover:from-blue-700 hover:to-blue-800 transition-all duration-200 shadow-lg hover:shadow-xl transform hover:scale-105">
                    <i class="fas fa-plus ml-2"></i>
                    <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Add New Entry', 'Add New Entry') }}</span>
                </a>
            </div>

            <div class="bg-white rounded-xl shadow-lg border border-gray-200 p-6 mb-6">
                <form method="GET" action="{{ route('accounting.journal-entries.index') }}" class="grid grid-cols-1 md:grid-cols-4 gap-4 items-end">
                    <div>
                        <label for="date_from" class="block text-sm font-medium text-gray-700 mb-2">{{ \App\Helpers\TranslationHelper::transFromDatabase('From Date', 'From Date') }}</label>
                        <input type="date" id="date_from" name="date_from" value="{{ $filters['date_from'] ?? '' }}" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all duration-200">
                    </div>

                    <div>
                        <label for="date_to" class="block text-sm font-medium text-gray-700 mb-2">{{ \App\Helpers\TranslationHelper::transFromDatabase('To Date', 'To Date') }}</label>
                        <input type="date" id="date_to" name="date_to" value="{{ $filters['date_to'] ?? '' }}" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all duration-200">
                    </div>

                    <div>
                        <label for="entry_type" class="block text-sm font-medium text-gray-700 mb-2">{{ \App\Helpers\TranslationHelper::transFromDatabase('Entry Type', 'Entry Type') }}</label>
                        <select id="entry_type" name="entry_type" class="select2 w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all duration-200">
                            <option value="">{{ \App\Helpers\TranslationHelper::transFromDatabase('All', 'All') }}</option>
                            <option value="manual" {{ $filters['entry_type'] === 'manual' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Manual', 'Manual') }}</option>
                            <option value="automatic" {{ $filters['entry_type'] === 'automatic' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Automatic', 'Automatic') }}</option>
                            <option value="recurring" {{ $filters['entry_type'] === 'recurring' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Recurring', 'Recurring') }}</option>
                            <option value="adjustment" {{ $filters['entry_type'] === 'adjustment' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Adjustment', 'Adjustment') }}</option>
                        </select>
                    </div>

                    <div>
                        <label for="status" class="block text-sm font-medium text-gray-700 mb-2">{{ \App\Helpers\TranslationHelper::transFromDatabase('Status', 'Status') }}</label>
                        <select id="status" name="status" class="select2 w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all duration-200">
                            <option value="">{{ \App\Helpers\TranslationHelper::transFromDatabase('All', 'All') }}</option>
                            <option value="draft" {{ $filters['status'] === 'draft' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Draft', 'Draft') }}</option>
                            <option value="posted" {{ $filters['status'] === 'posted' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Posted', 'Posted') }}</option>
                            <option value="approved" {{ $filters['status'] === 'approved' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Approved', 'Approved') }}</option>
                            <option value="rejected" {{ $filters['status'] === 'rejected' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Rejected', 'Rejected') }}</option>
                        </select>
                    </div>

                    <div class="flex gap-2 col-span-1 md:col-span-4">
                        <button type="submit" class="flex-1 px-4 py-2 bg-gradient-to-r from-blue-600 to-blue-700 text-white font-medium rounded-lg hover:from-blue-700 hover:to-blue-800 transition-all duration-200 shadow-md hover:shadow-lg">
                            <i class="fas fa-search ml-2"></i>{{ \App\Helpers\TranslationHelper::transFromDatabase('Search', 'Search') }}
                        </button>
                        <a href="{{ route('accounting.journal-entries.index') }}" class="flex-1 px-4 py-2 bg-gray-300 text-gray-700 font-medium rounded-lg hover:bg-gray-400 transition-all duration-200 shadow-md hover:shadow-lg text-center">
                            <i class="fas fa-redo ml-2"></i>{{ \App\Helpers\TranslationHelper::transFromDatabase('Clear Filters', 'Clear Filters') }}
                        </a>
                    </div>
                </form>
            </div>

            <!-- رسائل التنبيه -->
            @if (session('success'))
            <div class="bg-green-50 border-l-4 border-green-500 p-4 mb-6 rounded-r-lg">
                <div class="flex">
                    <div class="flex-shrink-0">
                        <i class="fas fa-check-circle text-green-500 text-xl"></i>
                    </div>
                    <div class="mr-3">
                        <p class="text-green-700 font-medium">{{ session('success') }}</p>
                    </div>
                </div>
            </div>
            @endif

            @if (session('error'))
            <div class="bg-red-50 border-l-4 border-red-500 p-4 mb-6 rounded-r-lg">
                <div class="flex">
                    <div class="flex-shrink-0">
                        <i class="fas fa-exclamation-circle text-red-500 text-xl"></i>
                    </div>
                    <div class="mr-3">
                        <p class="text-red-700 font-medium">{{ session('error') }}</p>
                    </div>
                </div>
            </div>
            @endif

            @if (session('info'))
            <div class="bg-blue-50 border-l-4 border-blue-500 p-4 mb-6 rounded-r-lg">
                <div class="flex">
                    <div class="flex-shrink-0">
                        <i class="fas fa-info-circle text-blue-500 text-xl"></i>
                    </div>
                    <div class="mr-3">
                        <p class="text-blue-700 font-medium">{{ session('info') }}</p>
                    </div>
                </div>
            </div>
            @endif
        </div>

        <!-- الجدول الرئيسي -->
        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gradient-to-r from-gray-800 to-gray-900 text-white">
                        <tr>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('#', '#') }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Entry Number', 'Entry Number') }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Date', 'Date') }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Accounting Period', 'Accounting Period') }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Amount', 'Amount') }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Type', 'Type') }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Status', 'Status') }}</th>
                            <th class="px-6 py-4 text-center font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Actions', 'Actions') }}</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse ($entries as $entry)
                        <tr class="hover:bg-blue-50 transition-colors duration-150 border-b border-gray-100">
                            <td class="px-6 py-4 text-gray-600 font-medium">{{ $loop->iteration }}</td>
                            <td class="px-6 py-4">
                                <div class="font-semibold text-gray-900">{{ $entry->reference_number }}</div>
                            </td>
                            <td class="px-6 py-4 text-gray-700">{{ $entry->entry_date->format('Y-m-d') }}</td>
                            <td class="px-6 py-4 text-gray-700">{{ $entry->accountingPeriod->name ?? '-' }}</td>
                            <td class="px-6 py-4 text-gray-700 font-semibold">
                                {{ number_format($entry->total_debit, 2) }}
                            </td>
                            <td class="px-6 py-4">
                                @if ($entry->isAutomatic())
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-orange-100 text-orange-700" title="Entry generated from another document">
                                    <i class="fas fa-cog ml-1"></i>
                                    {{ \App\Helpers\TranslationHelper::transFromDatabase('Automatic', 'Automatic') }}
                                </span>
                                @else
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-blue-100 text-blue-700">
                                    <i class="fas fa-pen ml-1"></i>
                                    {{ \App\Helpers\TranslationHelper::transFromDatabase('Manual', 'Manual') }}
                                </span>
                                @endif
                            </td>
                            <td class="px-6 py-4">
                                @switch($entry->status)
                                    @case('draft')
                                        <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-gray-100 text-gray-700">
                                            <span class="w-2 h-2 bg-gray-500 rounded-full ml-2"></span>
                                            {{ \App\Helpers\TranslationHelper::transFromDatabase('Draft', 'Draft') }}
                                        </span>
                                        @break
                                    @case('posted')
                                        <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-blue-100 text-blue-700">
                                            <span class="w-2 h-2 bg-blue-500 rounded-full ml-2"></span>
                                            {{ \App\Helpers\TranslationHelper::transFromDatabase('Posted', 'Posted') }}
                                        </span>
                                        @break
                                    @case('approved')
                                        <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-green-100 text-green-700">
                                            <span class="w-2 h-2 bg-green-500 rounded-full ml-2"></span>
                                            {{ \App\Helpers\TranslationHelper::transFromDatabase('Approved', 'Approved') }}
                                        </span>
                                        @break
                                    @case('rejected')
                                        <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-red-100 text-red-700">
                                            <span class="w-2 h-2 bg-red-500 rounded-full ml-2"></span>
                                            {{ \App\Helpers\TranslationHelper::transFromDatabase('Rejected', 'Rejected') }}
                                        </span>
                                        @break
                                    @default
                                        <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-gray-100 text-gray-700">
                                            {{ $entry->status }}
                                        </span>
                                @endswitch
                            </td>
                            <td class="px-6 py-4">
                                <div class="flex items-center justify-center gap-1">
                                    <a href="{{ route('accounting.journal-entries.show', $entry) }}" class="inline-flex items-center px-3 py-2 rounded-lg bg-blue-100 text-blue-600 hover:bg-blue-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" title="View details">
                                        <i class="fas fa-eye mr-1"></i>
                                        <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('View', 'View') }}</span>
                                    </a>

                                    @if ($entry->canBeEdited())
                                        <a href="{{ route('accounting.journal-entries.edit', $entry) }}" class="inline-flex items-center px-3 py-2 rounded-lg bg-amber-100 text-amber-600 hover:bg-amber-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" title="Edit">
                                            <i class="fas fa-edit mr-1"></i>
                                            <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Edit', 'Edit') }}</span>
                                        </a>
                                    @elseif ($entry->isAutomatic() && $entry->status === 'draft')
                                        <button disabled class="inline-flex items-center px-3 py-2 rounded-lg bg-gray-100 text-gray-400 cursor-not-allowed text-sm font-medium whitespace-nowrap" title="Cannot edit automatic entry">
                                            <i class="fas fa-edit mr-1"></i>
                                            <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Edit', 'Edit') }}</span>
                                        </button>
                                    @endif

                                    @if ($entry->canBeDeleted())
                                        <form action="{{ route('accounting.journal-entries.destroy', $entry) }}" method="POST" class="inline">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="inline-flex items-center px-3 py-2 rounded-lg bg-red-100 text-red-600 hover:bg-red-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" title="Delete" onclick="return confirm('{{ \App\Helpers\TranslationHelper::transFromDatabase('Are you sure you want to delete this entry?', 'Are you sure you want to delete this entry?') }}')">
                                                <i class="fas fa-trash mr-1"></i>
                                                <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Delete', 'Delete') }}</span>
                                            </button>
                                        </form>
                                    @elseif ($entry->isAutomatic() && $entry->status === 'draft')
                                        <button disabled class="inline-flex items-center px-3 py-2 rounded-lg bg-gray-100 text-gray-400 cursor-not-allowed text-sm font-medium whitespace-nowrap" title="Cannot delete automatic entry">
                                            <i class="fas fa-trash mr-1"></i>
                                            <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Delete', 'Delete') }}</span>
                                        </button>
                                    @endif

                                    @if ($entry->canBePosted())
                                        <form action="{{ route('accounting.journal-entries.post', $entry) }}" method="POST" class="inline">
                                            @csrf
                                            @method('PATCH')
                                            <button type="submit" class="inline-flex items-center px-3 py-2 rounded-lg bg-green-100 text-green-600 hover:bg-green-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" title="Post" onclick="return confirm('Are you sure you want to post this entry?')">
                                                <i class="fas fa-check mr-1"></i>
                                                <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Post', 'Post') }}</span>
                                            </button>
                                        </form>
                                    @endif

                                    @if ($entry->canBeApproved())
                                        <form action="{{ route('accounting.journal-entries.approve', $entry) }}" method="POST" class="inline">
                                            @csrf
                                            @method('PATCH')
                                            <button type="submit" class="inline-flex items-center px-3 py-2 rounded-lg bg-teal-100 text-teal-600 hover:bg-teal-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" title="Approve" onclick="return confirm('Are you sure you want to approve this entry?')">
                                                <i class="fas fa-thumbs-up mr-1"></i>
                                                <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Approve', 'Approve') }}</span>
                                            </button>
                                        </form>
                                    @endif

                                    <!-- زر الرفض -->
                                    @if ($entry->canBeRejected())
                                        <form action="{{ route('accounting.journal-entries.reject', $entry) }}" method="POST" class="inline">
                                            @csrf
                                            @method('PATCH')
                                            <button type="submit" class="inline-flex items-center px-3 py-2 rounded-lg bg-pink-100 text-pink-600 hover:bg-pink-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" title="رفض" onclick="return confirm('هل أنت متأكد من رفض هذا القيد؟')">
                                                <i class="fas fa-times mr-1"></i>
                                                <span>رفض</span>
                                            </button>
                                        </form>
                                    @endif
                                </div>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="9" class="px-6 py-12 text-center">
                                <div class="flex flex-col items-center justify-center">
                                    <i class="fas fa-inbox text-gray-400 text-4xl mb-3"></i>
                                    <p class="text-gray-600 font-medium">لا توجد قيود يومية حالياً</p>
                                    <p class="text-gray-500 text-sm mt-1">ابدأ بإضافة قيد يومية جديد</p>
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <!-- التصفح -->
            @if ($entries->hasPages())
            <div class="px-6 py-4 bg-gray-50 border-t border-gray-200">
                <div class="flex justify-center">
                    {{ $entries->appends(request()->query())->links() }}
                </div>
            </div>
            @endif
        </div>
    </div>
</div>
@endsection
