@extends('layouts.app')

@section('title', 'تقرير النفايات والمردودات')

@section('content')
<div class="min-h-screen bg-gradient-to-br from-slate-50 to-slate-100 py-8 px-4 md:px-8">
    <div class="max-w-7xl mx-auto">
        
        <!-- HEADER -->
        <div class="mb-8">
            <div class="flex items-center justify-between mb-6">
                <div>
                    <h1 class="text-3xl font-bold text-gray-900 mb-2">
                        <i class="fas fa-trash-alt text-red-600 ml-2"></i>النفايات والمردودات
                    </h1>
                    <p class="text-gray-600">سجل الأصناف التالفة والنفايات والمرتجع للمخازن</p>
                </div>
                <div class="flex gap-2">
                    <button onclick="printReport()" class="inline-flex items-center px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white font-semibold rounded-lg transition-all duration-200">
                        <i class="fas fa-print mr-2"></i> طباعة
                    </button>
                    <button onclick="exportReport('pdf')" class="inline-flex items-center px-4 py-2 bg-red-600 hover:bg-red-700 text-white font-semibold rounded-lg transition-all duration-200">
                        <i class="fas fa-file-pdf mr-2"></i> PDF
                    </button>
                </div>
            </div>
        </div>

        <!-- STATS -->
        <div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-6">
            <div class="bg-white rounded-xl shadow-md p-6 border-b-4 border-red-500">
                <p class="text-gray-600 text-sm font-medium mb-1">إجمالي سجلات النفايات</p>
                <p class="text-3xl font-bold text-gray-900">{{ $statistics['waste_records'] }}</p>
            </div>

            <div class="bg-white rounded-xl shadow-md p-6 border-b-4 border-blue-500">
                <p class="text-gray-600 text-sm font-medium mb-1">إجمالي سجلات المردودات</p>
                <p class="text-3xl font-bold text-gray-900">{{ $statistics['return_records'] }}</p>
            </div>

            <div class="bg-white rounded-xl shadow-md p-6 border-b-4 border-orange-500">
                <p class="text-gray-600 text-sm font-medium mb-1">إجمالي الكمية المتأثرة</p>
                <p class="text-3xl font-bold text-gray-900">{{ number_format($statistics['total_quantity'], 2) }}</p>
            </div>

            <div class="bg-white rounded-xl shadow-md p-6 border-b-4 border-purple-500">
                <p class="text-gray-600 text-sm font-medium mb-1">إجمالي القيمة المفقودة</p>
                <p class="text-3xl font-bold text-gray-900">{{ number_format($statistics['total_cost'], 2) }} ريال</p>
            </div>
        </div>

        <!-- DATA TABLE -->
        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gray-100 text-gray-800 border-b-2 border-gray-200">
                        <tr>
                            <th class="px-6 py-4 text-right font-bold text-sm">التاريخ</th>
                            <th class="px-6 py-4 text-right font-bold text-sm">رقم المستند</th>
                            <th class="px-6 py-4 text-right font-bold text-sm">اسم الصنف</th>
                            <th class="px-6 py-4 text-center font-bold text-sm">النوع</th>
                            <th class="px-6 py-4 text-center font-bold text-sm">الكمية</th>
                            <th class="px-6 py-4 text-right font-bold text-sm">التكلفة الإجمالية</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($wasteReturns as $record)
                        <tr class="hover:bg-red-50 transition-colors duration-150 border-b border-gray-100">
                            <td class="px-6 py-4 text-gray-600 text-sm">{{ $record->document_date }}</td>
                            <td class="px-6 py-4 font-mono text-xs font-bold">{{ $record->document_number }}</td>
                            <td class="px-6 py-4 font-bold text-gray-900">{{ $record->item_name }}</td>
                            <td class="px-6 py-4 text-center">
                                @if($record->document_type == 'waste')
                                    <span class="px-2 py-1 bg-red-100 text-red-700 rounded text-xs font-bold">نفايات / تالف</span>
                                @elseif($record->document_type == 'return')
                                    <span class="px-2 py-1 bg-blue-100 text-blue-700 rounded text-xs font-bold">مردودات</span>
                                @else
                                    <span class="px-2 py-1 bg-gray-100 text-gray-700 rounded text-xs font-bold">أخرى</span>
                                @endif
                            </td>
                            <td class="px-6 py-4 text-center font-bold">{{ number_format($record->quantity, 2) }}</td>
                            <td class="px-6 py-4 text-right font-bold text-red-600">{{ number_format($record->total_cost, 2) }} ريال</td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="6" class="px-6 py-12 text-center text-gray-500 italic">لا توجد سجلات حالياً</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

    </div>
</div>

<script>
function printReport() { window.print(); }
function exportReport(format) {
    const params = new URLSearchParams({ report: 'waste-returns', format: format });
    window.location.href = '{{ route("inventory.reports.export") }}?' + params.toString();
}
</script>
@endsection
