﻿<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>
            <a href="{{ route('assets.index') }}" class="bg-gray-500 hover:bg-gray-700 text-white font-bold py-2 p-4 rounded">
                <i class="fas fa-arrow-right ml-1"></i> العودة للقائمة
            </a>
        </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">
                    @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.asset-list') }}" method="GET" class="grid grid-cols-1 md:grid-cols-4 gap-4">
                            <div>
                                <label for="category_id" class="block text-sm font-medium text-gray-700 mb-1">فئة الأصل</label>
                                <select id="category_id" name="category_id" class="mt-1 block w-full py-2 p-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                                    <option value="">جميع الفئات</option>
                                    @foreach($categories as $category)
                                        <option value="{{ $category->id }}" {{ request('category_id') == $category->id ? 'selected' : '' }}>
                                            {{ $category->name }}
                                        </option>
                                    @endforeach
                                </select>
                            </div>

                            <div>
                                <label for="status" class="block text-sm font-medium text-gray-700 mb-1">الحالة</label>
                                <select id="status" name="status" class="mt-1 block w-full py-2 p-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                                    <option value="">جميع الحالات</option>
                                    <option value="active" {{ request('status') == 'active' ? 'selected' : '' }}>نشط</option>
                                    <option value="maintenance" {{ request('status') == 'maintenance' ? 'selected' : '' }}>قيد الصيانة</option>
                                    <option value="disposed" {{ request('status') == 'disposed' ? 'selected' : '' }}>تم التخلص منه</option>
                                </select>
                            </div>

                            <div>
                                <label for="purchase_date_from" class="block text-sm font-medium text-gray-700 mb-1">تاريخ الشراء من</label>
                                <input type="date" id="purchase_date_from" name="purchase_date_from" value="{{ request('purchase_date_from') }}" class="mt-1 block w-full py-2 p-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                            </div>

                            <div>
                                <label for="purchase_date_to" class="block text-sm font-medium text-gray-700 mb-1">تاريخ الشراء إلى</label>
                                <input type="date" id="purchase_date_to" name="purchase_date_to" value="{{ request('purchase_date_to') }}" class="mt-1 block w-full py-2 p-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                            </div>

                            <div class="md:col-span-4 flex justify-end">
                                <button type="submit" class="inline-flex justify-center py-2 p-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
                                    <i class="fas fa-filter ml-1"></i> تطبيق الفلاتر
                                </button>
                                <a href="{{ route('assets.reports.asset-list') }}" class="mr-2 inline-flex justify-center py-2 p-4 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
                                    <i class="fas fa-eraser ml-1"></i> إعادة تعيين
                                </a>
                                <button type="button" onclick="window.print()" class="mr-2 inline-flex justify-center py-2 p-4 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
                                    <i class="fas fa-print ml-1"></i> طباعة
                                </button>
                            </div>
                        </form>
                    </div>

                    <!-- ملخص التقرير -->
                    <div class="mb-6">
                        <h3 class="text-lg font-medium text-gray-700 mb-4">ملخص التقرير</h3>
                        <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
                            <div class="bg-blue-50 p-4 rounded-lg">
                                <h4 class="text-sm font-medium text-blue-800">إجمالي الأصول</h4>
                                <p class="text-2l font-bold text-blue-600">{{ $assets->count() }}</p>
                            </div>
                            <div class="bg-green-50 p-4 rounded-lg">
                                <h4 class="text-sm font-medium text-green-800">إجمالي تكلفة الشراء</h4>
                                <p class="text-2l font-bold text-green-600">{{ number_format($assets->sum('purchase_cost'), 2) }}</p>
                            </div>
                            <div class="bg-yellow-50 p-4 rounded-lg">
                                <h4 class="text-sm font-medium text-yellow-800">إجمالي القيمة الحالية</h4>
                                <p class="text-2l font-bold text-yellow-600">{{ number_format($assets->sum('current_value'), 2) }}</p>
                            </div>
                            <div class="bg-red-50 p-4 rounded-lg">
                                <h4 class="text-sm font-medium text-red-800">إجمالي الإهلاك</h4>
                                <p class="text-2l font-bold text-red-600">{{ number_format($assets->sum('purchase_cost') - $assets->sum('current_value'), 2) }}</p>
                            </div>
                        </div>
                    </div>

                    <!-- جدول الأصول -->
                    <div class="overflow--auto">
                        <table class="min-w-full bg-white border border-gray-200">
                            <thead class="bg-gray-100">
                                <tr>
                                    <th class="py-3 p-4 text-right border-b">رمز الأصل</th>
                                    <th class="py-3 p-4 text-right border-b">اسم الأصل</th>
                                    <th class="py-3 p-4 text-right border-b">الفئة</th>
                                    <th class="py-3 p-4 text-right border-b">تاريخ الشراء</th>
                                    <th class="py-3 p-4 text-right border-b">تكلفة الشراء</th>
                                    <th class="py-3 p-4 text-right border-b">القيمة الحالية</th>
                                    <th class="py-3 p-4 text-right border-b">الإهلاك المتراكم</th>
                                    <th class="py-3 p-4 text-right border-b">الحالة</th>
                                    <th class="py-3 p-4 text-right border-b">الموقع</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-gray-200">
                                @forelse($assets as $asset)
                                    <tr>
                                        <td class="py-3 p-4 border-b">{{ $asset->asset_code }}</td>
                                        <td class="py-3 p-4 border-b">{{ $asset->name }}</td>
                                        <td class="py-3 p-4 border-b">{{ $asset->category ? $asset->category->name : 'غير محدد' }}</td>
                                        <td class="py-3 p-4 border-b">{{ $asset->purchase_date->format('Y-m-d') }}</td>
                                        <td class="py-3 p-4 border-b">{{ number_format($asset->purchase_cost, 2) }}</td>
                                        <td class="py-3 p-4 border-b">{{ number_format($asset->current_value, 2) }}</td>
                                        <td class="py-3 p-4 border-b">{{ number_format($asset->purchase_cost - $asset->current_value, 2) }}</td>
                                        <td class="py-3 p-4 border-b">
                                            @if($asset->status == 'active')
                                                <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-green-100 text-green-800">
                                                    نشط
                                                </span>
                                            @elseif($asset->status == 'maintenance')
                                                <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-yellow-100 text-yellow-800">
                                                    قيد الصيانة
                                                </span>
                                            @elseif($asset->status == 'disposed')
                                                <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-red-100 text-red-800">
                                                    تم التخلص منه
                                                </span>
                                            @else
                                                <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-gray-100 text-gray-800">
                                                    {{ $asset->status }}
                                                </span>
                                            @endif
                                        </td>
                                        <td class="py-3 p-4 border-b">{{ $asset->location ?: 'غير محدد' }}</td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="9" class="py-3 p-4 text-center text-gray-500">
                                            لا توجد أصول مطابقة للفلاتر المحددة
                                        </td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- أنماط الطباعة -->
    <style>
        @media print {
            body * {
                visibility: hidden;
            }
            .bg-white, .bg-white * {
                visibility: visible;
            }
            .bg-white {
                position: absolute;
                left: 0;
                top: 0;
                width: 100%;
            }
            .mb-6.bg-gray-50 {
                display: none;
            }
            button, a {
                display: none !important;
            }
        }
    </style>
</x-app-layout>
