﻿<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-l text-gray-800 leading-tight">
            {{ __('تقرير الإهلاك') }}
        </h2>
    </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">
                    @include('assets.reports._nav')
                    
                    <!-- فلاتر التقرير -->
                    <div class="mb-6 bg-gray-50 p-4 rounded-lg">
                        <h3 class="text-lg font-medium text-gray-700 mb-4">{{ __('فلاتر التقرير') }}</h3>
                        
                        <form action="{{ route('assets.reports.depreciation') }}" method="GET" class="grid grid-cols-1 md:grid-cols-3 gap-4">
                            <div>
                                <x-label for="asset_id" :value="__('الأصل')" />
                                <select id="asset_id" name="asset_id" 
                                    class="block mt-1 w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">
                                    <option value="">{{ __('جميع الأصول') }}</option>
                                    @foreach($assets as $asset)
                                    <option value="{{ $asset->id }}" {{ request('asset_id') == $asset->id ? 'selected' : '' }}>
                                        {{ $asset->name }} ({{ $asset->asset_code }})
                                    </option>
                                    @endforeach
                                </select>
                            </div>
                            
                            <div>
                                <x-label for="category_id" :value="__('الفئة')" />
                                <select id="category_id" name="category_id" 
                                    class="block mt-1 w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">
                                    <option value="">{{ __('جميع الفئات') }}</option>
                                    @foreach($categories as $category)
                                    <option value="{{ $category->id }}" {{ request('category_id') == $category->id ? 'selected' : '' }}>
                                        {{ $category->name }}
                                    </option>
                                    @endforeach
                                </select>
                            </div>
                            
                            <div>
                                <x-label for="status" :value="__('الحالة')" />
                                <select id="status" name="status" 
                                    class="block mt-1 w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">
                                    <option value="">{{ __('جميع الحالات') }}</option>
                                    <option value="pending" {{ request('status') == 'pending' ? 'selected' : '' }}>{{ __('معلق') }}</option>
                                    <option value="posted" {{ request('status') == 'posted' ? 'selected' : '' }}>{{ __('مرحل') }}</option>
                                </select>
                            </div>
                            
                            <div>
                                <x-label for="date_from" :value="__('من تاريخ')" />
                                <x-input id="date_from" type="date" name="date_from" :value="request('date_from')" class="block mt-1 w-full" />
                            </div>
                            
                            <div>
                                <x-label for="date_to" :value="__('إلى تاريخ')" />
                                <x-input id="date_to" type="date" name="date_to" :value="request('date_to')" class="block mt-1 w-full" />
                            </div>
                            
                            <div class="flex items-end">
                                <x-button type="submit" class="bg-indigo-600 hover:bg-indigo-700 w-full">
                                    <i class="fas fa-filter ml-1"></i> {{ __('تطبيق الفلاتر') }}
                                </x-button>
                            </div>
                        </form>
                    </div>
                    
                    <!-- نتائج التقرير -->
                    <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>
                                </tr>
                            </thead>
                            <tbody class="bg-white divide-y divide-gray-200">
                                @forelse($depreciations as $depreciation)
                                <tr>
                                    <td class="p-6 py-4 whitespace-nowrap">
                                        {{ $depreciation->asset->name }} ({{ $depreciation->asset->asset_code }})
                                    </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>
                                </tr>
                                @empty
                                <tr>
                                    <td colspan="7" class="p-6 py-4 whitespace-nowrap text-center text-gray-500">
                                        {{ __('لا توجد بيانات متطابقة مع الفلاتر المحددة') }}
                                    </td>
                                </tr>
                                @endforelse
                            </tbody>
                            <tfoot>
                                <tr class="bg-gray-50">
                                    <td colspan="3" class="p-6 py-3 text-right font-medium">{{ __('المجموع') }}</td>
                                    <td class="p-6 py-3 font-medium">{{ number_format($depreciations->sum('depreciation_amount'), 2) }}</td>
                                    <td colspan="3"></td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                    
                    <!-- أزرار التصدير -->
                    <div class="mt-6 flex justify-end">
                        <x-button-link href="{{ route('assets.reports.depreciation', array_merge(request()->all(), ['eport' => 'pdf'])) }}" class="bg-red-600 hover:bg-red-700 ml-2">
                            <i class="fas fa-file-pdf ml-1"></i> {{ __('تصدير PDF') }}
                        </x-button-link>
                        
                        <x-button-link href="{{ route('assets.reports.depreciation', array_merge(request()->all(), ['eport' => 'ecel'])) }}" class="bg-green-600 hover:bg-green-700">
                            <i class="fas fa-file-ecel ml-1"></i> {{ __('تصدير Ecel') }}
                        </x-button-link>
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>