﻿<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-x-2 rtl:space-x-reverse">
                <a href="{{ route('assets.index') }}" class="bg-primary-600 hover:bg-primary-700 text-white font-bold py-2 p-4 rounded ml-2">
                    <i class="fas fa-plus ml-1"></i> تخلص من أصل جديد
                </a>
                <a href="{{ route('assets.reports.disposal') }}" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 p-4 rounded ml-2">
                    <i class="fas fa-file-alt ml-1"></i> تقرير التخلص
                </a>
                <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>
        </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($disposals as $disposal)
                                    <tr>
                                        <td class="py-3 p-4">
                                            @if($disposal->asset)
                                                <a href="{{ route('assets.show', $disposal->asset->id) }}" class="text-blue-600 hover:text-blue-900">
                                                    {{ $disposal->asset->name }}
                                                </a>
                                            @else
                                                <span class="text-gray-500">غير متوفر</span>
                                            @endif
                                        </td>
                                        <td class="py-3 p-4">{{ $disposal->disposal_date->format('Y-m-d') }}</td>
                                        <td class="py-3 p-4">
                                            @switch($disposal->disposal_type)
                                                @case('sale')
                                                    بيع
                                                    @break
                                                @case('scrap')
                                                    تخريد
                                                    @break
                                                @case('donation')
                                                    تبرع
                                                    @break
                                                @case('theft')
                                                    سرقة
                                                    @break
                                                @case('loss')
                                                    فقدان
                                                    @break
                                                @default
                                                    أخرى
                                            @endswitch
                                        </td>
                                        <td class="py-3 p-4">{{ number_format($disposal->disposal_amount, 2) }}</td>
                                        <td class="py-3 p-4">{{ number_format($disposal->book_value_at_disposal, 2) }}</td>
                                        <td class="py-3 p-4">
                                            @if($disposal->gain_loss > 0)
                                                <span class="text-green-600">{{ number_format($disposal->gain_loss, 2) }}</span>
                                            @elseif($disposal->gain_loss < 0)
                                                <span class="text-red-600">{{ number_format($disposal->gain_loss, 2) }}</span>
                                            @else
                                                <span class="text-gray-600">0.00</span>
                                            @endif
                                        </td>
                                        <td class="py-3 p-4">
                                            @switch($disposal->approval_status)
                                                @case('pending')
                                                    <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-yellow-100 text-yellow-800">
                                                        معلق
                                                    </span>
                                                    @break
                                                @case('approved')
                                                    <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-green-100 text-green-800">
                                                        معتمد
                                                    </span>
                                                    @break
                                                @case('rejected')
                                                    <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-red-100 text-red-800">
                                                        مرفوض
                                                    </span>
                                                    @break
                                                @default
                                                    <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-gray-100 text-gray-800">
                                                        {{ $disposal->approval_status }}
                                                    </span>
                                            @endswitch
                                        </td>
                                        <td class="py-3 p-4">
                                            <div class="flex items-center space-y-2 rtl:space-y-reverse">
                                                <a href="{{ route('assets.disposals.show', $disposal->id) }}" class="text-blue-600 hover:text-blue-900">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                                
                                                @if($disposal->approval_status === 'pending')
                                                    <form action="{{ route('assets.disposals.approve', $disposal->id) }}" method="POST" class="inline">
                                                        @csrf
                                                        @method('PATCH')
                                                        <button type="submit" class="text-green-600 hover:text-green-900 m-1" onclick="return confirm('هل أنت متأكد من اعتماد عملية التخلص هذه؟')">
                                                            <i class="fas fa-check"></i>
                                                        </button>
                                                    </form>
                                                    
                                                    <form action="{{ route('assets.disposals.reject', $disposal->id) }}" method="POST" class="inline">
                                                        @csrf
                                                        @method('PATCH')
                                                        <button type="submit" class="text-red-600 hover:text-red-900 m-1" onclick="return confirm('هل أنت متأكد من رفض عملية التخلص هذه؟')">
                                                            <i class="fas fa-times"></i>
                                                        </button>
                                                    </form>
                                                    
                                                    <form action="{{ route('assets.disposals.destroy', $disposal->id) }}" method="POST" class="inline">
                                                        @csrf
                                                        @method('DELETE')
                                                        <button type="submit" class="text-red-600 hover:text-red-900 m-1" onclick="return confirm('هل أنت متأكد من حذف عملية التخلص هذه؟')">
                                                            <i class="fas fa-trash"></i>
                                                        </button>
                                                    </form>
                                                @endif
                                            </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">
                        {{ $disposals->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
