@extends('layouts.app')

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

@section('content')
<div class="min-h-screen bg-gray-50 py-8 px-4 md:px-8">
    <div class="max-w-7xl mx-auto">
        <!-- Header -->
        <div class="mb-8 flex flex-col md:flex-row md:items-center md:justify-between gap-4">
            <div>
                <h1 class="text-3xl font-bold text-gray-900 mb-2">
                    <i class="fas fa-history text-blue-600 ml-2"></i>مقارنة الفترات
                </h1>
                <p class="text-gray-600">تحليل التغير في مستويات المخزون بين فترتين زمنيتين</p>
            </div>
            <div class="flex gap-2">
                <button onclick="window.print()" class="bg-white border border-gray-300 text-gray-700 px-4 py-2 rounded-lg hover:bg-gray-50 transition-colors">
                    <i class="fas fa-print ml-1"></i> طباعة
                </button>
            </div>
        </div>

        <!-- Filters -->
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6 mb-8">
            <form method="GET" action="{{ route('inventory.reports.periods-comparison') }}" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-5 gap-4 items-end">
                <div class="space-y-2">
                    <label class="text-xs font-bold text-gray-500 uppercase tracking-wider">الفترة الأولى من</label>
                    <input type="date" name="period1_from" value="{{ $filters['period1_from'] }}" class="w-full rounded-lg border-gray-300 focus:border-blue-500 focus:ring-blue-500 shadow-sm">
                </div>
                <div class="space-y-2">
                    <label class="text-xs font-bold text-gray-500 uppercase tracking-wider">الفترة الأولى إلى</label>
                    <input type="date" name="period1_to" value="{{ $filters['period1_to'] }}" class="w-full rounded-lg border-gray-300 focus:border-blue-500 focus:ring-blue-500 shadow-sm">
                </div>
                <div class="space-y-2">
                    <label class="text-xs font-bold text-gray-500 uppercase tracking-wider">الفترة الثانية من</label>
                    <input type="date" name="period2_from" value="{{ $filters['period2_from'] }}" class="w-full rounded-lg border-gray-300 focus:border-blue-500 focus:ring-blue-500 shadow-sm">
                </div>
                <div class="space-y-2">
                    <label class="text-xs font-bold text-gray-500 uppercase tracking-wider">الفترة الثانية إلى</label>
                    <input type="date" name="period2_to" value="{{ $filters['period2_to'] }}" class="w-full rounded-lg border-gray-300 focus:border-blue-500 focus:ring-blue-500 shadow-sm">
                </div>
                <div>
                    <button type="submit" class="w-full bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 transition-colors font-bold">
                        <i class="fas fa-sync-alt ml-1"></i> تحديث
                    </button>
                </div>
            </form>
        </div>

        <!-- Data Table -->
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
            <div class="overflow-x-auto">
                <table class="w-full text-right border-collapse">
                    <thead>
                        <tr class="bg-gray-50 border-b border-gray-200">
                            <th class="px-6 py-4 font-bold text-gray-700 text-sm">اسم الصنف</th>
                            <th class="px-6 py-4 font-bold text-gray-700 text-sm text-center">الفترة الأولى</th>
                            <th class="px-6 py-4 font-bold text-gray-700 text-sm text-center">الفترة الثانية</th>
                            <th class="px-6 py-4 font-bold text-gray-700 text-sm text-center">التغيير</th>
                            <th class="px-6 py-4 font-bold text-gray-700 text-sm text-center">نسبة التغير</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100">
                        @forelse($comparisonData as $item)
                        <tr class="hover:bg-gray-50 transition-colors">
                            <td class="px-6 py-4 font-medium text-gray-900">{{ $item->name }}</td>
                            <td class="px-6 py-4 text-center">{{ number_format($item->period1_value, 2) }}</td>
                            <td class="px-6 py-4 text-center">{{ number_format($item->period2_value, 2) }}</td>
                            <td class="px-6 py-4 text-center">
                                @php $diff = $item->period2_value - $item->period1_value; @endphp
                                <span class="{{ $diff >= 0 ? 'text-green-600' : 'text-red-600' }} font-bold">
                                    {{ $diff > 0 ? '+' : '' }}{{ number_format($diff, 2) }}
                                </span>
                            </td>
                            <td class="px-6 py-4 text-center">
                                @php 
                                    $percent = $item->period1_value > 0 ? ($diff / $item->period1_value) * 100 : 0;
                                @endphp
                                <span class="px-2 py-1 rounded text-xs font-bold {{ $diff >= 0 ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700' }}">
                                    {{ number_format($percent, 1) }}%
                                </span>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="5" class="px-6 py-12 text-center text-gray-500 italic">لا توجد بيانات للمقارنة في الفترات المختارة</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
@endsection
