﻿<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>
            @can('create', App\Models\Asset::class)
            <a href="{{ route('assets.create') }}" class="bg-primary-600 hover:bg-primary-700 text-white font-bold py-2 p-4 rounded">
                <i class="fas fa-plus ml-1"></i> إضافة أصل جديد
            </a>
            @endcan
        </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">
                            <thead class="bg-gray-100">
                                <tr>
                                    <th class="py-3 p-4 text-right">رمز الأصل</th>
                                    <th class="py-3 p-4 text-right">اسم الأصل</th>
                                    <th class="py-3 p-4 text-right">الفئة</th>
                                    <th class="py-3 p-4 text-right">تاريخ الشراء</th>
                                    <th class="py-3 p-4 text-right">تكلفة الشراء</th>
                                    <th class="py-3 p-4 text-right">القيمة الحالية</th>
                                    <th class="py-3 p-4 text-right">الحالة</th>
                                    <th class="py-3 p-4 text-right">الإجراءات</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-gray-200">
                                @forelse($assets as $asset)
                                    <tr>
                                        <td class="py-3 p-4">{{ $asset->asset_code }}</td>
                                        <td class="py-3 p-4">{{ $asset->name }}</td>
                                        <td class="py-3 p-4">{{ $asset->category ? $asset->category->name : 'غير محدد' }}</td>
                                        <td class="py-3 p-4">{{ $asset->purchase_date->format('Y-m-d') }}</td>
                                        <td class="py-3 p-4">{{ number_format($asset->purchase_cost, 2) }}</td>
                                        <td class="py-3 p-4">{{ number_format($asset->current_value, 2) }}</td>
                                        <td class="py-3 p-4">
                                            @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">
                                            <div class="flex items-center space-y-2 space-y-reverse">
                                                @can('view', $asset)
                                                <a href="{{ route('assets.show', $asset) }}" class="text-blue-600 hover:text-blue-900">
                                                    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />
                                                    </svg>
                                                </a>
                                                @endcan
                                                
                                                @can('update', $asset)
                                                <a href="{{ route('assets.edit', $asset) }}" class="text-yellow-600 hover:text-yellow-900" title="تعديل">
                                                    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
                                                    </svg>
                                                </a>

                                                @if($asset->status == 'active')
                                                <a href="{{ route('assets.disposal.create', $asset) }}" class="text-red-500 hover:text-red-700 mx-1" title="تخلص من الأصل">
                                                    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
                                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
                                                    </svg>
                                                </a>
                                                @endif
                                                @endcan
                                                
                                                @can('delete', $asset)
                                                <form action="{{ route('assets.destroy', $asset) }}" method="POST" class="inline-block">
                                                    @csrf
                                                    @method('DELETE')
                                                    <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" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
                                                        </svg>
                                                    </button>
                                                </form>
                                                @endcan
                                            </div>
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="8" class="py-3 p-4 text-center text-gray-500">
                                            لا توجد أصول مضافة حتى الآن
                                        </td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                    
                    <div class="mt-4">
                        {{ $assets->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>