﻿<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">
                {{ __('إهلاك الأصول') }}
            </h2>
            <div class="flex space-y-2 space-y-reverse">
                @can('create', App\Models\AssetDepreciation::class)
                <x-button-link href="{{ route('assets.depreciations.create') }}" class="bg-green-600 hover:bg-green-700">
                    <i class="fas fa-plus ml-1"></i> {{ __('إضافة إهلاك جديد') }}
                </x-button-link>
                @endcan
                
                <form action="{{ route('assets.calculate-depreciation') }}" method="POST" class="inline">
                    @csrf
                    <div class="flex items-center">
                        <input type="date" name="depreciation_date" value="{{ date('Y-m-d') }}" required
                            class="rounded-r-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 ml-1">
                        <x-button type="submit" class="bg-blue-600 hover:bg-blue-700 rounded-l-md">
                            <i class="fas fa-calculator ml-1"></i> {{ __('حساب الإهلاك الشهري') }}
                        </x-button>
                    </div>
                </form>
            </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

                    <div class="overflow--auto">
                        <table class="min-w-full bg-white border border-gray-200">
                            <thead>
                                <tr>
                                    <th class="p-6 py-3 border-b border-gray-200 bg-gray-50 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ __('الأصل') }}
                                    </th>
                                    <th class="p-6 py-3 border-b border-gray-200 bg-gray-50 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ __('الفئة') }}
                                    </th>
                                    <th class="p-6 py-3 border-b border-gray-200 bg-gray-50 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ __('تاريخ الإهلاك') }}
                                    </th>
                                    <th class="p-6 py-3 border-b border-gray-200 bg-gray-50 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ __('مبلغ الإهلاك') }}
                                    </th>
                                    <th class="p-6 py-3 border-b border-gray-200 bg-gray-50 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ __('الإهلاك المتراكم') }}
                                    </th>
                                    <th class="p-6 py-3 border-b border-gray-200 bg-gray-50 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ __('القيمة الدفترية') }}
                                    </th>
                                    <th class="p-6 py-3 border-b border-gray-200 bg-gray-50 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ __('الحالة') }}
                                    </th>
                                    <th class="p-6 py-3 border-b border-gray-200 bg-gray-50 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($depreciations as $depreciation)
                                <tr>
                                    <td class="p-6 py-4 whitespace-nowrap">
                                        <a href="{{ route('assets.show', $depreciation->asset) }}" class="text-indigo-600 hover:text-indigo-900">
                                            {{ $depreciation->asset->name }} ({{ $depreciation->asset->asset_code }})
                                        </a>
                                    </td>
                                    <td class="p-6 py-4 whitespace-nowrap">
                                        {{ $depreciation->asset->category->name ?? 'غير محدد' }}
                                    </td>
                                    <td class="p-6 py-4 whitespace-nowrap">
                                        {{ $depreciation->depreciation_date->format('Y-m-d') }}
                                    </td>
                                    <td class="p-6 py-4 whitespace-nowrap">
                                        {{ number_format($depreciation->depreciation_amount, 2) }}
                                    </td>
                                    <td class="p-6 py-4 whitespace-nowrap">
                                        {{ number_format($depreciation->accumulated_depreciation, 2) }}
                                    </td>
                                    <td class="p-6 py-4 whitespace-nowrap">
                                        {{ number_format($depreciation->book_value, 2) }}
                                    </td>
                                    <td class="p-6 py-4 whitespace-nowrap">
                                        @if($depreciation->status == 'pending')
                                            <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-yellow-100 text-yellow-800">
                                                {{ __('معلق') }}
                                            </span>
                                        @elseif($depreciation->status == 'posted')
                                            <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-green-100 text-green-800">
                                                {{ __('مرحل') }}
                                            </span>
                                        @endif
                                    </td>
                                    <td class="p-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                                        <div class="flex items-center space-y-2 space-y-reverse">
                                            @if($depreciation->status == 'pending')
                                                @can('update', $depreciation)
                                                <a href="{{ route('assets.depreciations.edit', $depreciation) }}" class="text-indigo-600 hover:text-indigo-900">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                @endcan
                                                
                                                @can('delete', $depreciation)
                                                <form action="{{ route('assets.depreciations.destroy', $depreciation) }}" method="POST" onsubmit="return confirm('هل أنت متأكد من حذف هذا الإهلاك؟');" class="inline">
                                                    @csrf
                                                    @method('DELETE')
                                                    <button type="submit" class="text-red-600 hover:text-red-900">
                                                        <i class="fas fa-trash"></i>
                                                    </button>
                                                </form>
                                                @endcan
                                                
                                                @can('update', $depreciation)
                                                <form action="{{ route('assets.depreciations.post', $depreciation) }}" method="POST" class="inline">
                                                    @csrf
                                                    <button type="submit" class="text-green-600 hover:text-green-900" title="ترحيل الإهلاك">
                                                        <i class="fas fa-check-circle"></i>
                                                    </button>
                                                </form>
                                                @endcan
                                            @else
                                                <a href="{{ route('accounting.journal-entries.show', $depreciation->journal_entry_id) }}" class="text-blue-600 hover:text-blue-900" title="عرض القيد المحاسبي">
                                                    <i class="fas fa-file-invoice-dollar"></i>
                                                </a>
                                            @endif
                                        </div>
                                    </td>
                                </tr>
                                @empty
                                <tr>
                                    <td colspan="8" class="p-6 py-4 whitespace-nowrap text-center text-gray-500">
                                        {{ __('لا توجد عمليات إهلاك') }}
                                    </td>
                                </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                    
                    <div class="mt-4">
                        {{ $depreciations->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>