@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-4xl font-bold text-gray-900 mb-2">
                        <i class="fas fa-clipboard-check text-blue-600 ml-2"></i>
                        تقرير امتثال العقود
                    </h1>
                    <p class="text-gray-600">متابعة التزام أوامر الشراء بمواعيد التسليم والشروط</p>
                </div>
            </div>
        </div>

        <!-- Filter Section -->
        <div class="bg-white rounded-xl shadow-lg p-6 mb-8 border border-gray-200">
            <form method="GET" action="{{ route('reports.purchase.contract-compliance') }}" class="flex flex-wrap gap-4 items-end">
                <div class="flex-1 min-w-[250px]">
                    <label for="supplier_id" class="block text-sm font-semibold text-gray-700 mb-2">المورد</label>
                    <select name="supplier_id" id="supplier_id" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                        <option value="">جميع الموردين</option>
                        @foreach(App\Models\Supplier::where('company_id', auth()->user()->company_id)->get() as $supplier)
                            <option value="{{ $supplier->id }}" @selected(request('supplier_id') == $supplier->id)>
                                {{ $supplier->name }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <button type="submit" class="px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white font-semibold rounded-lg transition-all duration-200">
                    <i class="fas fa-filter ml-2"></i>تصفية
                </button>
                <a href="{{ route('reports.purchase.contract-compliance') }}" class="px-6 py-2 bg-gray-500 hover:bg-gray-600 text-white font-semibold rounded-lg transition-all duration-200">
                    <i class="fas fa-redo ml-2"></i>إعادة تعيين
                </a>
            </form>
        </div>

        <!-- Summary Cards -->
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-5 gap-6 mb-8">
            <!-- Total POs Card -->
            <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-3xl font-bold text-gray-900 mt-2">{{ $data['total_pos'] ?? 0 }}</p>
                    </div>
                    <div class="text-blue-500 text-3xl">
                        <i class="fas fa-shopping-cart"></i>
                    </div>
                </div>
            </div>

            <!-- Completed Card -->
            <div class="bg-white rounded-xl shadow-lg p-6 border-l-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 mt-2">{{ $data['completed_pos'] ?? 0 }}</p>
                        @if($data['total_pos'] > 0)
                            <p class="text-green-600 text-xs mt-2">{{ round(($data['completed_pos'] / $data['total_pos']) * 100, 2) }}%</p>
                        @endif
                    </div>
                    <div class="text-green-500 text-3xl">
                        <i class="fas fa-check-circle"></i>
                    </div>
                </div>
            </div>

            <!-- Pending Card -->
            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-yellow-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 mt-2">{{ $data['pending_pos'] ?? 0 }}</p>
                        @if($data['total_pos'] > 0)
                            <p class="text-yellow-600 text-xs mt-2">{{ round(($data['pending_pos'] / $data['total_pos']) * 100, 2) }}%</p>
                        @endif
                    </div>
                    <div class="text-yellow-500 text-3xl">
                        <i class="fas fa-clock"></i>
                    </div>
                </div>
            </div>

            <!-- Overdue Card -->
            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-red-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 mt-2">{{ $data['overdue_pos'] ?? 0 }}</p>
                        @if($data['total_pos'] > 0)
                            <p class="text-red-600 text-xs mt-2">{{ round(($data['overdue_pos'] / $data['total_pos']) * 100, 2) }}%</p>
                        @endif
                    </div>
                    <div class="text-red-500 text-3xl">
                        <i class="fas fa-exclamation-triangle"></i>
                    </div>
                </div>
            </div>

            <!-- Compliance Score Card -->
            <div class="bg-white rounded-xl shadow-lg p-6 border-l-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-3xl font-bold text-gray-900 mt-2">{{ $data['compliance_score'] ?? 0 }}%</p>
                        <p class="text-purple-600 text-xs mt-2">
                            @if($data['compliance_score'] >= 80)
                                <i class="fas fa-star text-yellow-400 ml-1"></i>ممتاز
                            @elseif($data['compliance_score'] >= 60)
                                <i class="fas fa-smile text-blue-400 ml-1"></i>جيد
                            @else
                                <i class="fas fa-frown text-red-400 ml-1"></i>يحتاج تحسين
                            @endif
                        </p>
                    </div>
                    <div class="text-purple-500 text-3xl">
                        <i class="fas fa-chart-pie"></i>
                    </div>
                </div>
            </div>
        </div>

        <!-- Charts Section -->
        <div class="grid grid-cols-1 lg:grid-cols-2 gap-8 mb-8">
            <!-- Status Distribution Chart -->
            <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                <h3 class="text-lg font-semibold text-gray-900 mb-6">
                    <i class="fas fa-chart-pie text-blue-600 ml-2"></i>توزيع الحالات
                </h3>
                <canvas id="statusChart" height="300"></canvas>
            </div>

            <!-- Compliance Trend Chart -->
            <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                <h3 class="text-lg font-semibold text-gray-900 mb-6">
                    <i class="fas fa-chart-line text-green-600 ml-2"></i>معدل الالتزام بالمواعيد
                </h3>
                <div class="flex items-center justify-center h-80">
                    <div class="text-center">
                        <div class="inline-flex items-center justify-center w-40 h-40 rounded-full border-8 border-blue-200">
                            <div class="text-center">
                                <div class="text-4xl font-bold text-blue-600">{{ $data['on_time_rate'] ?? 0 }}%</div>
                                <p class="text-gray-600 text-sm mt-2">في الموعد</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Details Table -->
        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200 mb-8">
            <div class="px-6 py-4 bg-gradient-to-r from-gray-800 to-gray-900">
                <h3 class="text-lg font-semibold text-white">
                    <i class="fas fa-list ml-2"></i>تفاصيل أوامر الشراء
                </h3>
            </div>

            @if(!empty($data['details']) && count($data['details']) > 0)
                <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-4 text-right font-semibold text-sm text-gray-700">#</th>
                                <th class="px-6 py-4 text-right font-semibold text-sm text-gray-700">رقم الأمر</th>
                                <th class="px-6 py-4 text-right font-semibold text-sm text-gray-700">المورد</th>
                                <th class="px-6 py-4 text-right font-semibold text-sm text-gray-700">تاريخ الطلب</th>
                                <th class="px-6 py-4 text-right font-semibold text-sm text-gray-700">تاريخ التسليم</th>
                                <th class="px-6 py-4 text-center font-semibold text-sm text-gray-700">الحالة</th>
                                <th class="px-6 py-4 text-center font-semibold text-sm text-gray-700">التقدم</th>
                                <th class="px-6 py-4 text-center font-semibold text-sm text-gray-700">الامتثال</th>
                                <th class="px-6 py-4 text-center font-semibold text-sm text-gray-700">المبلغ</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-200">
                            @foreach($data['details'] as $index => $detail)
                                <tr class="hover:bg-blue-50 transition-colors duration-150">
                                    <td class="px-6 py-4 text-gray-600 font-medium">{{ $index + 1 }}</td>
                                    <td class="px-6 py-4 font-semibold text-blue-600">{{ $detail['po_number'] }}</td>
                                    <td class="px-6 py-4 text-gray-900">{{ $detail['supplier'] }}</td>
                                    <td class="px-6 py-4 text-gray-700">{{ $detail['order_date'] }}</td>
                                    <td class="px-6 py-4 text-gray-700">{{ $detail['delivery_date'] }}</td>
                                    <td class="px-6 py-4 text-center">
                                        @if($detail['status'] === 'completed')
                                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-green-100 text-green-700">
                                                <span class="w-2 h-2 bg-green-500 rounded-full ml-2"></span>
                                                مكتملة
                                            </span>
                                        @elseif($detail['status'] === 'pending')
                                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-yellow-100 text-yellow-700">
                                                <span class="w-2 h-2 bg-yellow-500 rounded-full ml-2"></span>
                                                معلقة
                                            </span>
                                        @else
                                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-gray-100 text-gray-700">
                                                {{ $detail['status'] }}
                                            </span>
                                        @endif
                                    </td>
                                    <td class="px-6 py-4">
                                        <div class="flex items-center gap-2">
                                            <div class="flex-1 bg-gray-200 rounded-full h-2">
                                                <div class="bg-blue-600 h-2 rounded-full" style="width: {{ $detail['completion_percentage'] }}%"></div>
                                            </div>
                                            <span class="text-xs font-medium text-gray-700 w-10 text-center">{{ $detail['completion_percentage'] }}%</span>
                                        </div>
                                    </td>
                                    <td class="px-6 py-4 text-center">
                                        @if($detail['compliance_status'] === 'مكتملة')
                                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-green-100 text-green-700">
                                                ✓ {{ $detail['compliance_status'] }}
                                            </span>
                                        @elseif($detail['compliance_status'] === 'متأخرة')
                                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-red-100 text-red-700">
                                                ⚠ {{ $detail['compliance_status'] }} ({{ $detail['days_late'] }} يوم)
                                            </span>
                                        @else
                                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-blue-100 text-blue-700">
                                                ✓ {{ $detail['compliance_status'] }}
                                            </span>
                                        @endif
                                    </td>
                                    <td class="px-6 py-4 text-center font-semibold text-gray-900">
                                        {{ number_format($detail['total_amount'], 2) }} ريال
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            @else
                <div class="px-6 py-12 text-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>
            @endif
        </div>

        <!-- Export Options -->
        <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
            <h3 class="text-lg font-semibold text-gray-900 mb-4">
                <i class="fas fa-download text-blue-600 ml-2"></i>خيارات التصدير
            </h3>
            <div class="flex flex-wrap gap-3">
                <a href="{{ route('reports.purchase.contract-compliance', array_merge(request()->all(), ['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-all duration-200">
                    <i class="fas fa-file-excel ml-2"></i>تصدير إلى Excel
                </a>
                <a href="{{ route('reports.purchase.contract-compliance', array_merge(request()->all(), ['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-all duration-200">
                    <i class="fas fa-file-pdf ml-2"></i>تصدير إلى PDF
                </a>
                <a href="{{ route('reports.purchase.contract-compliance', array_merge(request()->all(), ['export' => 'true', 'format' => 'csv'])) }}" 
                   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-file-csv ml-2"></i>تصدير إلى CSV
                </a>
                <button onclick="window.print()" 
                   class="inline-flex items-center px-4 py-2 bg-gray-600 hover:bg-gray-700 text-white font-semibold rounded-lg transition-all duration-200">
                    <i class="fas fa-print ml-2"></i>طباعة
                </button>
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.9.1/dist/chart.min.js"></script>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // Status Distribution Chart
        const ctx = document.getElementById('statusChart');
        if (ctx) {
            new Chart(ctx, {
                type: 'doughnut',
                data: {
                    labels: ['مكتملة', 'معلقة', 'متأخرة'],
                    datasets: [{
                        data: [
                            {{ $data['completed_pos'] ?? 0 }},
                            {{ $data['pending_pos'] ?? 0 }},
                            {{ $data['overdue_pos'] ?? 0 }}
                        ],
                        backgroundColor: ['#10b981', '#f59e0b', '#ef4444'],
                        borderColor: ['#059669', '#d97706', '#dc2626'],
                        borderWidth: 2
                    }]
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: true,
                    plugins: {
                        legend: {
                            position: 'bottom',
                            labels: {
                                font: { size: 12 },
                                padding: 15
                            }
                        }
                    }
                }
            });
        }
    });
</script>
@endpush