@extends('layouts.app')

@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">
        <!-- Page 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">المنتجات المشتراة</h1>
                    <p class="text-gray-600">تقرير شامل للمنتجات والأصناف المشتراة من الموردين</p>
                </div>
                <div class="flex gap-2">
                    <a href="{{ route('reports.purchase.products-purchased', ['export' => 'true', 'format' => 'excel']) }}" 
                       class="inline-flex items-center px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition-colors">
                        <i class="fas fa-file-excel mr-2"></i>
                        تصدير Excel
                    </a>
                    <a href="{{ route('reports.purchase.products-purchased', ['export' => 'true', 'format' => 'pdf']) }}" 
                       class="inline-flex items-center px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 transition-colors">
                        <i class="fas fa-file-pdf mr-2"></i>
                        تصدير PDF
                    </a>
                </div>
            </div>
        </div>

        <!-- Summary Cards -->
        @if(!empty($data))
        <div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-8">
            <div class="bg-white rounded-lg shadow p-6 border-t-4 border-blue-500">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-gray-600 text-sm font-medium">إجمالي المنتجات</p>
                        <p class="text-3xl font-bold text-gray-900">{{ count($data) }}</p>
                    </div>
                    <div class="text-blue-500 text-3xl">
                        <i class="fas fa-cube"></i>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-lg shadow p-6 border-t-4 border-green-500">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-gray-600 text-sm font-medium">إجمالي الكميات</p>
                        <p class="text-3xl font-bold text-gray-900">{{ number_format($data->sum('total_quantity'), 0) }}</p>
                    </div>
                    <div class="text-green-500 text-3xl">
                        <i class="fas fa-boxes"></i>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-lg shadow p-6 border-t-4 border-purple-500">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-gray-600 text-sm font-medium">إجمالي التكاليف</p>
                        <p class="text-2xl font-bold text-gray-900">{{ number_format($data->sum('total_cost'), 2) }} ر.س</p>
                    </div>
                    <div class="text-purple-500 text-3xl">
                        <i class="fas fa-money-bill-wave"></i>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-lg shadow p-6 border-t-4 border-orange-500">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-gray-600 text-sm font-medium">متوسط السعر</p>
                        <p class="text-2xl font-bold text-gray-900">
                            {{ number_format($data->count() > 0 ? $data->sum('total_cost') / $data->sum('total_quantity') : 0, 2) }} ر.س
                        </p>
                    </div>
                    <div class="text-orange-500 text-3xl">
                        <i class="fas fa-chart-line"></i>
                    </div>
                </div>
            </div>
        </div>
        @endif

        <!-- Filters Section -->
        <div class="bg-white rounded-lg shadow-lg p-6 mb-8 border border-gray-200">
            <form method="GET" class="flex gap-4 flex-wrap items-end">
                <div class="flex-1 min-w-xs">
                    <label class="block text-sm font-medium text-gray-700 mb-2">البحث بالاسم</label>
                    <input type="text" name="search" value="{{ request('search') }}" 
                           placeholder="ابحث عن اسم المنتج..." 
                           class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                </div>
                <button type="submit" class="px-6 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors">
                    <i class="fas fa-search mr-2"></i>بحث
                </button>
            </form>
        </div>

        <!-- Products 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-gradient-to-r from-gray-800 to-gray-900 text-white">
                        <tr>
                            <th class="px-6 py-4 text-right font-semibold text-sm">#</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">اسم المنتج</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">الكمية الإجمالية</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">التكلفة الإجمالية</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">متوسط السعر</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">عدد الموردين</th>
                            <th class="px-6 py-4 text-center font-semibold text-sm">النسبة المئوية</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($data as $item)
                        <tr class="hover:bg-blue-50 transition-colors duration-150 border-b border-gray-100">
                            <td class="px-6 py-4 text-gray-600 font-medium">{{ $loop->iteration }}</td>
                            <td class="px-6 py-4">
                                <div class="font-semibold text-gray-900">{{ $item['product_name'] ?? 'غير معروف' }}</div>
                                <p class="text-xs text-gray-500">ID: {{ $item['product_id'] }}</p>
                            </td>
                            <td class="px-6 py-4">
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-blue-100 text-blue-700">
                                    {{ number_format($item['total_quantity'], 0) }} وحدة
                                </span>
                            </td>
                            <td class="px-6 py-4">
                                <span class="font-bold text-gray-900">{{ number_format($item['total_cost'], 2) }} ر.س</span>
                            </td>
                            <td class="px-6 py-4">
                                <span class="text-gray-700">
                                    @if($item['total_quantity'] > 0)
                                        {{ number_format($item['total_cost'] / $item['total_quantity'], 2) }} ر.س
                                    @else
                                        -
                                    @endif
                                </span>
                            </td>
                            <td class="px-6 py-4">
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-purple-100 text-purple-700">
                                    {{ $item['supplier_count'] ?? 1 }} موردين
                                </span>
                            </td>
                            <td class="px-6 py-4 text-center">
                                <div class="flex items-center justify-center">
                                    <div class="w-20 bg-gray-200 rounded-full h-2">
                                        <div class="bg-green-500 h-2 rounded-full" 
                                             style="width: {{ ($item['total_cost'] / $data->sum('total_cost') * 100) ?? 0 }}%">
                                        </div>
                                    </div>
                                </div>
                                <p class="text-xs text-gray-600 mt-1">
                                    {{ number_format(($item['total_cost'] / $data->sum('total_cost') * 100) ?? 0, 1) }}%
                                </p>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="7" class="px-6 py-12 text-center">
                                <div class="flex flex-col items-center justify-center">
                                    <i class="fas fa-inbox text-gray-400 text-4xl mb-3"></i>
                                    <p class="text-gray-600 font-medium">لا توجد بيانات حالياً</p>
                                    <p class="text-gray-500 text-sm mt-1">لم يتم شراء أي منتجات بعد</p>
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <!-- Footer Summary -->
            @if(!empty($data) && count($data) > 0)
            <div class="bg-gradient-to-r from-gray-50 to-gray-100 px-6 py-4 border-t border-gray-200">
                <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                    <div>
                        <p class="text-gray-600 text-sm">إجمالي المنتجات</p>
                        <p class="text-2xl font-bold text-gray-900">{{ count($data) }}</p>
                    </div>
                    <div>
                        <p class="text-gray-600 text-sm">إجمالي الكميات المشتراة</p>
                        <p class="text-2xl font-bold text-gray-900">{{ number_format($data->sum('total_quantity'), 0) }} وحدة</p>
                    </div>
                    <div>
                        <p class="text-gray-600 text-sm">إجمالي المصروفات</p>
                        <p class="text-2xl font-bold text-green-600">{{ number_format($data->sum('total_cost'), 2) }} ر.س</p>
                    </div>
                </div>
            </div>
            @endif
        </div>

        <!-- Top Products by Cost -->
        @if(!empty($data) && count($data) > 0)
        <div class="mt-8 bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
            <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                <h2 class="text-xl font-bold text-gray-900">
                    <i class="fas fa-star text-yellow-500 mr-2"></i>
                    أعلى 10 منتجات بالتكلفة
                </h2>
            </div>
            <div class="p-6">
                <div class="space-y-4">
                    @foreach($data->take(10) as $item)
                    <div class="flex items-center justify-between p-4 bg-gray-50 rounded-lg hover:bg-blue-50 transition-colors">
                        <div class="flex-1">
                            <h3 class="font-semibold text-gray-900">{{ $item['product_name'] ?? 'غير معروف' }}</h3>
                            <p class="text-sm text-gray-600">{{ number_format($item['total_quantity'], 0) }} وحدة × {{ number_format($item['total_cost'] / max($item['total_quantity'], 1), 2) }} ر.س</p>
                        </div>
                        <div class="text-right">
                            <p class="font-bold text-green-600 text-lg">{{ number_format($item['total_cost'], 2) }} ر.س</p>
                            <p class="text-xs text-gray-500">{{ number_format(($item['total_cost'] / $data->sum('total_cost') * 100), 1) }}% من الإجمالي</p>
                        </div>
                    </div>
                    @endforeach
                </div>
            </div>
        </div>
        @endif

        <!-- Print Button -->
        <div class="mt-8 flex justify-center">
            <button onclick="window.print()" class="inline-flex items-center px-6 py-3 bg-gray-600 text-white rounded-lg hover:bg-gray-700 transition-colors">
                <i class="fas fa-print mr-2"></i>
                طباعة التقرير
            </button>
        </div>
    </div>
</div>

@push('scripts')
<script>
    // Optional: Add any JavaScript functionality here
    document.addEventListener('DOMContentLoaded', function() {
        console.log('Products purchased report loaded');
    });
</script>
@endpush
@endsection