<x-app-layout>
    <x-slot name="header">
        <div class="flex justify-between items-center">
            <h2 class="font-semibold text-l text-gray-800 leading-tight">
                {{ \App\Helpers\TranslationHelper::transFromDatabase('Accounting Periods', __('الفترات المحاسبية')) }}
            </h2>
            <div>
                <a href="{{ route('accounting.accounting-periods.create') }}" class="inline-flex items-center p-4 py-2 bg-blue-600 border border-transparent rounded-md font-semibold text-s text-white uppercase tracking-widest hover:bg-blue-700 focus:bg-blue-700 active:bg-blue-800 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 transition ease-in-out duration-150">
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 ml-1" viewBox="0 0 20 20" fill="currentColor">
                        <path fill-rule="evenodd" d="M10 5a1 1 0 011 1v3h3a1 1 0 110 2h-3v3a1 1 0 11-2 0v-3H6a1 1 0 110-2h3V6a1 1 0 011-1z" clip-rule="evenodd" />
                    </svg>
                    {{ \App\Helpers\TranslationHelper::transFromDatabase('Add New Accounting Period', __('إضافة فترة محاسبية جديدة')) }}
                </a>
            </div>
        </div>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7l m-auto sm:p-6 lg:p-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 bg-white border-b border-gray-200">
                    @if (session('success'))
                        <div class="bg-green-100 border border-green-400 text-green-700 p-4 py-3 rounded relative mb-4" role="alert">
                            <span class="block sm:inline">{{ session('success') }}</span>
                        </div>
                    @endif

                    @if (session('error'))
                        <div class="bg-red-100 border border-red-400 text-red-700 p-4 py-3 rounded relative mb-4" role="alert">
                            <span class="block sm:inline">{{ session('error') }}</span>
                        </div>
                    @endif

                    @if (session('info'))
                        <div class="bg-blue-100 border border-blue-400 text-blue-700 p-4 py-3 rounded relative mb-4" role="alert">
                            <span class="block sm:inline">{{ session('info') }}</span>
                        </div>
                    @endif

                    <div class="overflow--auto">
                        <table class="min-w-full divide-y divide-gray-200">
                            <thead class="bg-gray-50">
                                <tr>
                                    <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Name', __('الاسم')) }}
                                    </th>
                                    <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Fiscal Year', __('السنة المالية')) }}
                                    </th>
                                    <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Start Date', __('تاريخ البداية')) }}
                                    </th>
                                    <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('End Date', __('تاريخ النهاية')) }}
                                    </th>
                                    <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Status', __('الحالة')) }}
                                    </th>
                                    <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Journal Entries', __('القيود المحاسبية')) }}
                                    </th>
                                    <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Actions', __('الإجراءات')) }}
                                    </th>
                                </tr>
                            </thead>
                            <tbody class="bg-white divide-y divide-gray-200">
                                @forelse ($periods as $period)
                                    <tr>
                                        <td class="p-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                                            {{ $period->name }}
                                        </td>
                                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                            {{ $period->fiscalYear->name }}
                                        </td>
                                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                            {{ $period->start_date->format('Y-m-d') }}
                                        </td>
                                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                            {{ $period->end_date->format('Y-m-d') }}
                                        </td>
                                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                            @switch($period->status)
                                                @case('open')
                                                    <span class="inline-flex items-center p-2.5 py-0.5 rounded-full text-s font-medium bg-green-100 text-green-800">
                                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Open', __('مفتوحة')) }}
                                                    </span>
                                                    @break
                                                @case('closed')
                                                    <span class="inline-flex items-center p-2.5 py-0.5 rounded-full text-s font-medium bg-red-100 text-red-800">
                                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Closed', __('مغلقة')) }}
                                                    </span>
                                                    @break
                                                @default
                                                    {{ $period->status }}
                                            @endswitch
                                        </td>
                                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                            {{ $period->journalEntries()->count() }}
                                        </td>
                                        <td class="p-6 py-4 whitespace-nowrap text-sm font-medium">
                                            <div class="flex space-y-2 space-y-reverse">
                                                <a href="{{ route('accounting.accounting-periods.show', $period) }}" class="text-indigo-600 hover:text-indigo-900">
                                                    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                                                        <path d="M10 12a2 2 0 100-4 2 2 0 000 4z" />
                                                        <path fill-rule="evenodd" d="M.458 10C1.732 5.943 5.522 3 10 3s8.268 2.943 9.542 7c-1.274 4.057-5.064 7-9.542 7S1.732 14.057.458 10zM14 10a4 4 0 11-8 0 4 4 0 018 0z" clip-rule="evenodd" />
                                                    </svg>
                                                </a>
                                                
                                                @if ($period->canBeEdited())
                                                    <a href="{{ route('accounting.accounting-periods.edit', $period) }}" class="text-yellow-600 hover:text-yellow-900">
                                                        <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                                                            <path d="M13.586 3.586a2 2 0 112.828 2.828l-.793.793-2.828-2.828.793-.793zM11.379 5.793L3 14.172V17h2.828l8.38-8.379-2.83-2.828z" />
                                                        </svg>
                                                    </a>
                                                @endif
                                                
                                                @if ($period->status === 'open' && $period->canBeClosed())
                                                    <form action="{{ route('accounting.accounting-periods.close', $period) }}" method="POST" class="inline-block">
                                                        @csrf
                                                        @method('PATCH')
                                                        <button type="submit" class="text-red-600 hover:text-red-900" onclick="return confirm('{{ \App\Helpers\TranslationHelper::transFromDatabase('Are you sure you want to close this period? You will not be able to reopen it later.', __('هل أنت متأكد من إغلاق هذه الفترة المحاسبية؟ لن تتمكن من إعادة فتحها لاحقاً.')) }}')">
                                                            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                                                                <path fill-rule="evenodd" d="M5 9V7a5 5 0 0110 0v2a2 2 0 012 2v5a2 2 0 01-2 2H5a2 2 0 01-2-2v-5a2 2 0 012-2zm8-2v2H7V7a3 3 0 016 0z" clip-rule="evenodd" />
                                                            </svg>
                                                        </button>
                                                    </form>
                                                @endif
                                                
                                                @if ($period->journalEntries()->count() === 0)
                                                    <form action="{{ route('accounting.accounting-periods.destroy', $period) }}" method="POST" class="inline-block">
                                                        @csrf
                                                        @method('DELETE')
                                                        <button type="submit" class="text-red-600 hover:text-red-900" onclick="return confirm('{{ \App\Helpers\TranslationHelper::transFromDatabase('Are you sure you want to delete this period?', __('هل أنت متأكد من حذف هذه الفترة المحاسبية؟')) }}')">
                                                            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                                                                <path fill-rule="evenodd" d="M9 2a1 1 0 00-.894.553L7.382 4H4a1 1 0 000 2v10a2 2 0 002 2h8a2 2 0 002-2V6a1 1 0 100-2h-3.382l-.724-1.447A1 1 0 0011 2H9zM7 8a1 1 0 012 0v6a1 1 0 11-2 0V8zm5-1a1 1 0 00-1 1v6a1 1 0 102 0V8a1 1 0 00-1-1z" clip-rule="evenodd" />
                                                            </svg>
                                                        </button>
                                                    </form>
                                                @endif
                                            </div>
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="7" class="p-6 py-4 whitespace-nowrap text-sm text-gray-500 text-center">
                                            {{ \App\Helpers\TranslationHelper::transFromDatabase('No Accounting Periods Found', __('لا توجد فترات محاسبية مضافة')) }}
                                        </td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
