﻿<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">
                {{ __('عرض السنة المالية') }}: {{ $fiscalYear->name }}
            </h2>
            <div>
                <a href="{{ route('accounting.fiscal-years.index') }}" class="inline-flex items-center p-4 py-2 bg-gray-600 border border-transparent rounded-md font-semibold text-s text-white uppercase tracking-widest hover:bg-gray-700 focus:bg-gray-700 active:bg-gray-800 focus:outline-none focus:ring-2 focus:ring-gray-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="M9.707 16.707a1 1 0 01-1.414 0l-6-6a1 1 0 010-1.414l6-6a1 1 0 011.414 1.414L5.414 9H17a1 1 0 110 2H5.414l4.293 4.293a1 1 0 010 1.414z" clip-rule="evenodd" />
                    </svg>
                    {{ __('العودة إلى القائمة') }}
                </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">
                    <div class="mb-6">
                        <h3 class="text-lg font-medium text-gray-900">{{ __('معلومات السنة المالية') }}</h3>
                        <div class="mt-4 space-y-4">
                            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                                <div>
                                    <span class="text-gray-500 text-sm">{{ __('الاسم') }}</span>
                                    <p class="text-gray-900">{{ $fiscalYear->name }}</p>
                                </div>
                                <div>
                                    <span class="text-gray-500 text-sm">{{ __('الحالة') }}</span>
                                    <p class="text-gray-900">
                                        @switch($fiscalYear->status)
                                            @case('draft')
                                                <span class="inline-flex items-center p-2.5 py-0.5 rounded-full text-s font-medium bg-gray-100 text-gray-800">
                                                    {{ __('مسودة') }}
                                                </span>
                                                @break
                                            @case('active')
                                                <span class="inline-flex items-center p-2.5 py-0.5 rounded-full text-s font-medium bg-green-100 text-green-800">
                                                    {{ __('نشطة') }}
                                                </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">
                                                    {{ __('مغلقة') }}
                                                </span>
                                                @break
                                            @default
                                                {{ $fiscalYear->status }}
                                        @endswitch
                                    </p>
                                </div>
                            </div>
                            
                            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                                <div>
                                    <span class="text-gray-500 text-sm">{{ __('تاريخ البداية') }}</span>
                                    <p class="text-gray-900">{{ $fiscalYear->start_date->format('Y-m-d') }}</p>
                                </div>
                                <div>
                                    <span class="text-gray-500 text-sm">{{ __('تاريخ النهاية') }}</span>
                                    <p class="text-gray-900">{{ $fiscalYear->end_date->format('Y-m-d') }}</p>
                                </div>
                            </div>
                            
                            <div>
                                <span class="text-gray-500 text-sm">{{ __('ملاحظات') }}</span>
                                <p class="text-gray-900">{{ $fiscalYear->notes ?? __('لا توجد ملاحظات') }}</p>
                            </div>
                        </div>
                    </div>

                    <div class="mt-8">
                        <div class="flex justify-between items-center mb-4">
                            <h3 class="text-lg font-medium text-gray-900">{{ __('الفترات المحاسبية') }}</h3>
                            @if ($fiscalYear->status !== 'closed')
                                <a href="{{ route('accounting.accounting-periods.create', ['fiscal_year_id' => $fiscalYear->id]) }}" 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>
                                    {{ __('إضافة فترة محاسبية') }}
                                </a>
                            @endif
                        </div>
                        
                        <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">
                                            {{ __('الاسم') }}
                                        </th>
                                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                            {{ __('تاريخ البداية') }}
                                        </th>
                                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                            {{ __('تاريخ النهاية') }}
                                        </th>
                                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                            {{ __('الحالة') }}
                                        </th>
                                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                            {{ __('الإجراءات') }}
                                        </th>
                                    </tr>
                                </thead>
                                <tbody class="bg-white divide-y divide-gray-200">
                                    @forelse ($accountingPeriods 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->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">
                                                            {{ __('مفتوحة') }}
                                                        </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">
                                                            {{ __('مغلقة') }}
                                                        </span>
                                                        @break
                                                    @default
                                                        {{ $period->status }}
                                                @endswitch
                                            </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->status === 'open' && $fiscalYear->status !== 'closed')
                                                            <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>
                                                            
                                                            <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('{{ __('هل أنت متأكد من إغلاق هذه الفترة المحاسبية؟ لن تتمكن من إعادة فتحها لاحقاً.') }}')">
                                                                    <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
                                                </div>
                                            </td>
                                        </tr>
                                    @empty
                                        <tr>
                                            <td colspan="5" class="p-6 py-4 whitespace-nowrap text-sm text-gray-500 text-center">
                                                {{ __('لا توجد فترات محاسبية مضافة') }}
                                            </td>
                                        </tr>
                                    @endforelse
                                </tbody>
                            </table>
                        </div>
                    </div>

                    <div class="mt-8 flex space-y-4 space-y-reverse">
                        @if ($fiscalYear->canBeEdited())
                            <a href="{{ route('accounting.fiscal-years.edit', $fiscalYear) }}" class="inline-flex items-center p-4 py-2 bg-yellow-600 border border-transparent rounded-md font-semibold text-s text-white uppercase tracking-widest hover:bg-yellow-700 focus:bg-yellow-700 active:bg-yellow-800 focus:outline-none focus:ring-2 focus:ring-yellow-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 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 ($fiscalYear->status === 'draft')
                            <a href="{{ route('accounting.fiscal-years.change-status', ['fiscalYear' => $fiscalYear, 'status' => 'active']) }}" class="inline-flex items-center p-4 py-2 bg-green-600 border border-transparent rounded-md font-semibold text-s text-white uppercase tracking-widest hover:bg-green-700 focus:bg-green-700 active:bg-green-800 focus:outline-none focus:ring-2 focus:ring-green-500 focus:ring-offset-2 transition ease-in-out duration-150" onclick="return confirm('{{ __('هل أنت متأكد من تنشيط هذه السنة المالية؟') }}')">
                                <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="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd" />
                                </svg>
                                {{ __('تنشيط') }}
                            </a>
                        @endif
                        
                        @if ($fiscalYear->status === 'active')
                            <a href="{{ route('accounting.fiscal-years.change-status', ['fiscalYear' => $fiscalYear, 'status' => 'closed']) }}" class="inline-flex items-center p-4 py-2 bg-red-600 border border-transparent rounded-md font-semibold text-s text-white uppercase tracking-widest hover:bg-red-700 focus:bg-red-700 active:bg-red-800 focus:outline-none focus:ring-2 focus:ring-red-500 focus:ring-offset-2 transition ease-in-out duration-150" onclick="return confirm('{{ __('هل أنت متأكد من إغلاق هذه السنة المالية؟ لن تتمكن من إعادة فتحها لاحقاً.') }}')">
                                <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="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>
                                {{ __('إغلاق') }}
                            </a>
                        @endif
                        
                        @if ($fiscalYear->accountingPeriods()->count() === 0 && $fiscalYear->journalEntries()->count() === 0)
                            <form action="{{ route('accounting.fiscal-years.destroy', $fiscalYear) }}" method="POST" class="inline-block">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="inline-flex items-center p-4 py-2 bg-red-600 border border-transparent rounded-md font-semibold text-s text-white uppercase tracking-widest hover:bg-red-700 focus:bg-red-700 active:bg-red-800 focus:outline-none focus:ring-2 focus:ring-red-500 focus:ring-offset-2 transition ease-in-out duration-150" onclick="return confirm('{{ __('هل أنت متأكد من حذف هذه السنة المالية؟') }}')">
                                    <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="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>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
