@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">
        <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.sales.item-profitability-detailed', ['from_date' => $fromDate, 'to_date' => $toDate]) }}" 
                       class="bg-blue-600 hover:bg-blue-700 text-white font-semibold py-2 px-4 rounded-lg transition-colors">
                        <i class="fas fa-search-plus ml-1"></i> عرض التفاصيل
                    </a>
                </div>
            </div>
        </div>

        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200 p-6 mb-6">
            <h2 class="text-xl font-bold text-gray-900 mb-6">فلاتر البحث</h2>
            
            <form method="GET" class="grid grid-cols-1 md:grid-cols-4 gap-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">من التاريخ</label>
                    <input type="date" name="from_date" value="{{ \Carbon\Carbon::parse($fromDate)->format('Y-m-d') }}" 
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">إلى التاريخ</label>
                    <input type="date" name="to_date" value="{{ \Carbon\Carbon::parse($toDate)->format('Y-m-d') }}" 
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                </div>
                <div class="flex items-end">
                    <button type="submit" class="w-full bg-blue-600 hover:bg-blue-700 text-white font-semibold py-2 px-4 rounded-lg transition-colors">
                        <i class="fas fa-search ml-1"></i> بحث
                    </button>
                </div>
            </form>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-6">
            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-blue-500">
                <p class="text-gray-600 text-sm">إجمالي المبيعات</p>
                <p class="text-3xl font-bold text-blue-600">{{ number_format($data['total_sales'], 2) }}</p>
            </div>

            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-amber-500">
                <p class="text-gray-600 text-sm">إجمالي التكلفة</p>
                <p class="text-3xl font-bold text-amber-600">{{ number_format($data['total_cost'], 2) }}</p>
            </div>

            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-green-500">
                <p class="text-gray-600 text-sm">إجمالي الربح</p>
                <p class="text-3xl font-bold text-green-600">{{ number_format($data['total_profit'], 2) }}</p>
            </div>

            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-purple-500">
                <p class="text-gray-600 text-sm">متوسط هامش الربح</p>
                <p class="text-3xl font-bold text-purple-600">{{ $data['average_margin'] }}%</p>
            </div>
        </div>

        <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['items'] as $item)
                        <tr class="hover:bg-blue-50 transition-colors">
                            <td class="px-6 py-4 text-gray-600 font-medium">{{ $item->code }}</td>
                            <td class="px-6 py-4">
                                <div class="font-semibold text-gray-900">{{ $item->name }}</div>
                            </td>
                            <td class="px-6 py-4 text-gray-600">{{ number_format($item->total_quantity, 2) }}</td>
                            <td class="px-6 py-4 font-semibold text-gray-900">{{ number_format($item->total_sales, 2) }}</td>
                            <td class="px-6 py-4 text-amber-600">{{ number_format($item->total_cost, 2) }}</td>
                            <td class="px-6 py-4 font-bold {{ $item->total_profit >= 0 ? 'text-green-600' : 'text-red-600' }}">
                                {{ number_format($item->total_profit, 2) }}
                            </td>
                            <td class="px-6 py-4 text-center">
                                @php
                                    $margin = $item->total_sales > 0 ? round(($item->total_profit / $item->total_sales) * 100, 2) : 0;
                                @endphp
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium {{ $margin >= 0 ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700' }}">
                                    {{ $margin }}%
                                </span>
                            </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>
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                    @if(count($data['items']) > 0)
                    <tfoot class="bg-gray-50 font-bold">
                        <tr>
                            <td colspan="2" class="px-6 py-4 text-gray-900 text-left">الإجمالي</td>
                            <td class="px-6 py-4 text-gray-900">{{ number_format($data['items']->sum('total_quantity'), 2) }}</td>
                            <td class="px-6 py-4 text-blue-600">{{ number_format($data['total_sales'], 2) }}</td>
                            <td class="px-6 py-4 text-amber-600">{{ number_format($data['total_cost'], 2) }}</td>
                            <td class="px-6 py-4 text-green-600">{{ number_format($data['total_profit'], 2) }}</td>
                            <td class="px-6 py-4 text-center">
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-bold bg-blue-100 text-blue-700">
                                    {{ $data['average_margin'] }}%
                                </span>
                            </td>
                        </tr>
                    </tfoot>
                    @endif
                </table>
            </div>
        </div>
    </div>
</div>
@endsection
