﻿<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">
                {{ __('تفاصيل الفترة المحاسبية') }}: {{ $period->name }}
            </h2>
            <div>
                <a href="{{ route('accounting.accounting-periods.index') }}" class="inline-flex items-center p-4 py-2 bg-gray-300 border border-transparent rounded-md font-semibold text-s text-gray-700 uppercase tracking-widest hover:bg-gray-400 active:bg-gray-500 focus:outline-none focus:border-gray-500 focus:ring ring-gray-300 disabled:opacity-25 transition ease-in-out duration-150">
                    {{ __('العودة إلى القائمة') }}
                </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-8">
                        <h3 class="text-lg font-medium text-gray-900 mb-4">{{ __('معلومات الفترة المحاسبية') }}</h3>
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <div>
                                <div class="mb-4">
                                    <h4 class="text-sm font-medium text-gray-500">{{ __('اسم الفترة المحاسبية') }}</h4>
                                    <p class="mt-1 text-sm text-gray-900">{{ $period->name }}</p>
                                </div>
                                <div class="mb-4">
                                    <h4 class="text-sm font-medium text-gray-500">{{ __('السنة المالية') }}</h4>
                                    <p class="mt-1 text-sm text-gray-900">{{ $period->fiscalYear->name }}</p>
                                </div>
                                <div class="mb-4">
                                    <h4 class="text-sm font-medium text-gray-500">{{ __('تاريخ البداية') }}</h4>
                                    <p class="mt-1 text-sm text-gray-900">{{ $period->start_date->format('Y-m-d') }}</p>
                                </div>
                                <div class="mb-4">
                                    <h4 class="text-sm font-medium text-gray-500">{{ __('تاريخ النهاية') }}</h4>
                                    <p class="mt-1 text-sm text-gray-900">{{ $period->end_date->format('Y-m-d') }}</p>
                                </div>
                            </div>
                            <div>
                                <div class="mb-4">
                                    <h4 class="text-sm font-medium text-gray-500">{{ __('الحالة') }}</h4>
                                    <p class="mt-1 text-sm">
                                        @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
                                    </p>
                                </div>
                                <div class="mb-4">
                                    <h4 class="text-sm font-medium text-gray-500">{{ __('عدد القيود المحاسبية') }}</h4>
                                    <p class="mt-1 text-sm text-gray-900">{{ $journalEntries->count() }}</p>
                                </div>
                                <div class="mb-4">
                                    <h4 class="text-sm font-medium text-gray-500">{{ __('ملاحظات') }}</h4>
                                    <p class="mt-1 text-sm text-gray-900">{{ $period->notes ?: __('لا توجد ملاحظات') }}</p>
                                </div>
                            </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 ($period->status === 'open')
                                <a href="{{ route('accounting.journal-entries.create', ['period_id' => $period->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>
                                        <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 ($journalEntries as $entry)
                                        <tr>
                                            <td class="p-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                                                {{ $entry->entry_number }}
                                            </td>
                                            <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                                {{ $entry->entry_date->format('Y-m-d') }}
                                            </td>
                                            <td class="p-6 py-4 text-sm text-gray-500 max-w-s truncate">
                                                {{ $entry->description }}
                                            </td>
                                            <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                                {{ number_format($entry->total_debit, 2) }}
                                            </td>
                                            <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                                {{ number_format($entry->total_credit, 2) }}
                                            </td>
                                            <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                                @switch($entry->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('posted')
                                                        <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
                                                    @default
                                                        {{ $entry->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.journal-entries.show', $entry) }}" 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>
                                                </div>
                                            </td>
                                        </tr>
                                    @empty
                                        <tr>
                                            <td colspan="7" class="p-6 py-4 whitespace-nowrap text-sm text-gray-500 text-center">
                                                {{ __('لا توجد قيود محاسبية مضافة لهذه الفترة') }}
                                            </td>
                                        </tr>
                                    @endforelse
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
