@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">
        <!-- Header -->
        <div class="mb-8">
            <div class="flex items-center justify-between mb-6">
                <div>
                    <h1 class="text-4xl font-bold text-gray-900 mb-2">تقرير انحراف الموازنة</h1>
                    <p class="text-gray-600">
                        {{ \Carbon\Carbon::parse($data['summary']['period_start'])->format('Y/m/d') }} 
                        إلى 
                        {{ \Carbon\Carbon::parse($data['summary']['period_end'])->format('Y/m/d') }}
                    </p>
                </div>
                <div class="flex gap-2">
                    <a href="?export=true&format=pdf" class="inline-flex items-center px-4 py-2 bg-red-600 hover:bg-red-700 text-white font-semibold rounded-lg transition">
                        <i class="fas fa-file-pdf ml-2"></i> PDF
                    </a>
                    <a href="?export=true&format=excel" class="inline-flex items-center px-4 py-2 bg-green-600 hover:bg-green-700 text-white font-semibold rounded-lg transition">
                        <i class="fas fa-file-excel ml-2"></i> Excel
                    </a>
                </div>
            </div>
        </div>

        <!-- Summary Cards -->
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
            <!-- Budgeted Amount -->
            <div class="bg-white rounded-xl shadow-lg p-6 border-l-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-2xl font-bold text-gray-900 mt-2">
                            {{ number_format($data['summary']['budgeted_amount'], 2) }}
                        </p>
                    </div>
                    <div class="text-blue-500 text-3xl">
                        <i class="fas fa-dollar-sign"></i>
                    </div>
                </div>
            </div>

            <!-- Actual Spending -->
            <div class="bg-white rounded-xl shadow-lg p-6 border-l-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 mt-2">
                            {{ number_format($data['summary']['actual_spending'], 2) }}
                        </p>
                    </div>
                    <div class="text-orange-500 text-3xl">
                        <i class="fas fa-shopping-cart"></i>
                    </div>
                </div>
            </div>

            <!-- Variance -->
            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 {{ $data['summary']['variance'] >= 0 ? 'border-green-500' : 'border-red-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 {{ $data['summary']['variance'] >= 0 ? 'text-green-600' : 'text-red-600' }} mt-2">
                            {{ number_format($data['summary']['variance'], 2) }}
                        </p>
                        <p class="text-xs {{ $data['summary']['variance'] >= 0 ? 'text-green-600' : 'text-red-600' }} mt-1">
                            {{ number_format($data['summary']['variance_percentage'], 2) }}%
                        </p>
                    </div>
                    <div class="{{ $data['summary']['variance'] >= 0 ? 'text-green-500' : 'text-red-500' }} text-3xl">
                        <i class="fas fa-{{ $data['summary']['variance'] >= 0 ? 'arrow-down' : 'arrow-up' }}"></i>
                    </div>
                </div>
            </div>

            <!-- Status -->
            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 {{ $data['summary']['variance_status'] === 'under_budget' ? 'border-green-500' : 'border-red-500' }}">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-gray-600 text-sm font-medium">الحالة</p>
                        <div class="mt-2">
                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium {{ $data['summary']['variance_status'] === 'under_budget' ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700' }}">
                                <span class="w-2 h-2 {{ $data['summary']['variance_status'] === 'under_budget' ? 'bg-green-500' : 'bg-red-500' }} rounded-full ml-2"></span>
                                {{ $data['summary']['variance_status'] === 'under_budget' ? 'ضمن الموازنة' : 'تجاوز الموازنة' }}
                            </span>
                        </div>
                    </div>
                    <div class="text-3xl">
                        <i class="fas {{ $data['summary']['variance_status'] === 'under_budget' ? 'fa-check-circle text-green-500' : 'fa-exclamation-circle text-red-500' }}"></i>
                    </div>
                </div>
            </div>
        </div>

        <!-- Summary Stats -->
        <div class="bg-white rounded-xl shadow-lg p-6 mb-8">
            <h2 class="text-xl font-bold text-gray-900 mb-4">إحصائيات الملخص</h2>
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                <div>
                    <p class="text-gray-600 text-sm">عدد أوامر الشراء</p>
                    <p class="text-3xl font-bold text-blue-600 mt-2">{{ $data['summary']['po_count'] }}</p>
                </div>
                <div>
                    <p class="text-gray-600 text-sm">عدد الموردين</p>
                    <p class="text-3xl font-bold text-purple-600 mt-2">{{ $data['summary']['supplier_count'] }}</p>
                </div>
                <div>
                    <p class="text-gray-600 text-sm">متوسط قيمة الأمر</p>
                    <p class="text-3xl font-bold text-indigo-600 mt-2">{{ number_format($data['summary']['average_po'], 2) }}</p>
                </div>
            </div>
        </div>

        <!-- By Supplier -->
        <div class="bg-white rounded-xl shadow-lg overflow-hidden mb-8">
            <div class="px-6 py-4 bg-gradient-to-r from-gray-800 to-gray-900 border-b border-gray-200">
                <h2 class="text-xl font-bold text-white">الانحراف حسب المورد</h2>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gray-50 border-b border-gray-200">
                        <tr>
                            <th class="px-6 py-3 text-right font-semibold text-gray-700">اسم المورد</th>
                            <th class="px-6 py-3 text-center font-semibold text-gray-700">الموازنة</th>
                            <th class="px-6 py-3 text-center font-semibold text-gray-700">الفعلي</th>
                            <th class="px-6 py-3 text-center font-semibold text-gray-700">الانحراف</th>
                            <th class="px-6 py-3 text-center font-semibold text-gray-700">النسبة</th>
                            <th class="px-6 py-3 text-center font-semibold text-gray-700">عدد الأوامر</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($data['by_supplier'] as $supplier)
                        <tr class="hover:bg-blue-50 transition-colors">
                            <td class="px-6 py-4 text-right">
                                <a href="#" class="text-blue-600 hover:text-blue-800 font-medium">
                                    {{ $supplier['supplier_name'] }}
                                </a>
                            </td>
                            <td class="px-6 py-4 text-center">{{ number_format($supplier['budgeted'], 2) }}</td>
                            <td class="px-6 py-4 text-center font-semibold">{{ number_format($supplier['actual'], 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-medium {{ $supplier['status'] === 'under' ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700' }}">
                                    {{ number_format($supplier['variance'], 2) }}
                                </span>
                            </td>
                            <td class="px-6 py-4 text-center text-sm">{{ number_format($supplier['variance_percentage'], 2) }}%</td>
                            <td class="px-6 py-4 text-center">{{ $supplier['po_count'] }}</td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="6" class="px-6 py-12 text-center text-gray-500">
                                <i class="fas fa-inbox text-4xl text-gray-400 mb-3"></i>
                                <p>لا توجد بيانات</p>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        <!-- By Category -->
        @if($data['by_category']->count() > 0)
        <div class="bg-white rounded-xl shadow-lg overflow-hidden mb-8">
            <div class="px-6 py-4 bg-gradient-to-r from-gray-800 to-gray-900 border-b border-gray-200">
                <h2 class="text-xl font-bold text-white">الانحراف حسب الفئة</h2>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gray-50 border-b border-gray-200">
                        <tr>
                            <th class="px-6 py-3 text-right font-semibold text-gray-700">الفئة</th>
                            <th class="px-6 py-3 text-center font-semibold text-gray-700">الفعلي</th>
                            <th class="px-6 py-3 text-center font-semibold text-gray-700">الانحراف</th>
                            <th class="px-6 py-3 text-center font-semibold text-gray-700">النسبة</th>
                            <th class="px-6 py-3 text-center font-semibold text-gray-700">عدد الأوامر</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($data['by_category'] as $category)
                        <tr class="hover:bg-blue-50 transition-colors">
                            <td class="px-6 py-4 text-right font-medium">{{ $category['category'] }}</td>
                            <td class="px-6 py-4 text-center">{{ number_format($category['actual'], 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-medium {{ $category['status'] === 'under' ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700' }}">
                                    {{ number_format($category['variance'], 2) }}
                                </span>
                            </td>
                            <td class="px-6 py-4 text-center text-sm">{{ number_format($category['variance_percentage'], 2) }}%</td>
                            <td class="px-6 py-4 text-center">{{ $category['po_count'] }}</td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="5" class="px-6 py-12 text-center text-gray-500">
                                لا توجد بيانات
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        @endif

        <!-- Monthly Trend -->
        @if($data['monthly_trend']->count() > 0)
        <div class="bg-white rounded-xl shadow-lg p-6">
            <h2 class="text-xl font-bold text-gray-900 mb-4">الاتجاه الشهري</h2>
            <div class="overflow-x-auto">
                <table class="min-w-full">
                    <thead class="bg-gray-50 border-b border-gray-200">
                        <tr>
                            <th class="px-6 py-3 text-right font-semibold text-gray-700">الشهر</th>
                            <th class="px-6 py-3 text-center font-semibold text-gray-700">الفعلي</th>
                            <th class="px-6 py-3 text-center font-semibold text-gray-700">عدد الأوامر</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($data['monthly_trend'] as $month)
                        <tr class="hover:bg-blue-50 transition-colors">
                            <td class="px-6 py-4 text-right font-medium">{{ $month['month_label'] }}</td>
                            <td class="px-6 py-4 text-center">{{ number_format($month['actual'], 2) }}</td>
                            <td class="px-6 py-4 text-center">{{ $month['po_count'] }}</td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="3" class="px-6 py-12 text-center text-gray-500">
                                لا توجد بيانات
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        @endif
    </div>
</div>
@endsection