@extends('layouts.app')

@section('title', 'تحليل المدفوع مقابل المعلق')

@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 flex items-center justify-between">
            <div>
                <h1 class="text-3xl font-bold text-gray-900 mb-2">📊 تحليل المدفوع مقابل المعلق</h1>
                <p class="text-gray-600">توزيع الفواتير حسب حالة الدفع مع تحليل مفصل</p>
            </div>
            <div class="flex gap-3">
                <a href="{{ route('reports.sales.paid-vs-pending-pdf', ['from_date' => $fromDate, 'to_date' => $toDate]) }}" class="inline-flex items-center px-6 py-3 bg-red-600 text-white rounded-lg hover:bg-red-700">
                    <i class="fas fa-file-pdf mr-2"></i>
                    تحميل PDF
                </a>
                <a href="{{ route('reports.sales.index') }}" class="inline-flex items-center px-6 py-3 bg-gray-600 text-white rounded-lg hover:bg-gray-700">
                    <i class="fas fa-arrow-right mr-2"></i>
                    عودة
                </a>
            </div>
        </div>

        <!-- Summary Stats -->
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
            <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                <p class="text-gray-600 text-sm font-medium mb-2">إجمالي الفواتير</p>
                <p class="text-3xl font-bold text-gray-900">{{ number_format($summary['total_invoices']) }}</p>
                <p class="text-xs text-gray-500 mt-2">فاتورة</p>
            </div>
            <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                <p class="text-gray-600 text-sm font-medium mb-2">إجمالي المبلغ</p>
                <p class="text-3xl font-bold text-blue-600">{{ number_format($summary['total_amount'], 0) }}</p>
                <p class="text-xs text-gray-500 mt-2">ريال</p>
            </div>
            <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                <p class="text-gray-600 text-sm font-medium mb-2">المدفوع</p>
                <p class="text-3xl font-bold text-green-600">{{ number_format($summary['total_paid'], 0) }}</p>
                <p class="text-xs text-green-500 mt-2">{{ $summary['paid_percentage'] }}%</p>
            </div>
            <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                <p class="text-gray-600 text-sm font-medium mb-2">المعلق</p>
                <p class="text-3xl font-bold text-orange-600">{{ number_format($summary['total_pending'], 0) }}</p>
                <p class="text-xs text-orange-500 mt-2">{{ $summary['pending_percentage'] }}%</p>
            </div>
        </div>

        <!-- Chart -->
        @if(isset($charts['payment_status_pie']) && $charts['payment_status_pie'])
        <div class="bg-white rounded-xl shadow-lg p-8 mb-8 border border-gray-200">
            <h2 class="text-2xl font-bold text-gray-900 mb-6">توزيع حالات الدفع</h2>
            <div style="max-width: 400px; margin: 0 auto;">
                <canvas id="paymentStatusChart" height="80"></canvas>
            </div>
        </div>
        @endif

        <!-- Filters -->
        <div class="bg-white rounded-xl shadow-lg p-6 mb-8 border border-gray-200">
            <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="{{ $fromDate }}" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none 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="{{ $toDate }}" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                </div>
                <div class="flex items-end gap-2">
                    <button type="submit" class="flex-1 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors font-medium">
                        تحديث
                    </button>
                </div>
                <div class="flex items-end">
                    <a href="{{ route('reports.sales.paid-vs-pending') }}" class="w-full px-4 py-2 bg-gray-300 text-gray-700 rounded-lg hover:bg-gray-400 transition-colors font-medium text-center">
                        إعادة تعيين
                    </a>
                </div>
            </form>
        </div>

        <!-- Payment Status Breakdown -->
        <div class="grid grid-cols-1 lg:grid-cols-2 gap-8 mb-8">
            <!-- Paid Fully -->
            <div class="bg-white rounded-xl shadow-lg p-6 border border-green-200 border-l-4">
                <h3 class="text-lg font-bold text-gray-900 mb-4 flex items-center">
                    <i class="fas fa-check-circle text-green-600 mr-2 text-xl"></i>
                    مدفوع كاملاً
                </h3>
                <div class="space-y-3">
                    <div>
                        <p class="text-gray-600 text-sm mb-1">عدد الفواتير</p>
                        <p class="text-3xl font-bold text-green-600">{{ number_format($summary['paid_fully']['count']) }}</p>
                    </div>
                    <div>
                        <p class="text-gray-600 text-sm mb-1">المبلغ الإجمالي</p>
                        <p class="text-2xl font-semibold text-gray-900">{{ number_format($summary['paid_fully']['amount'], 0) }} ريال</p>
                    </div>
                    <div>
                        <p class="text-gray-600 text-sm mb-1">النسبة من الإجمالي</p>
                        <p class="text-xl font-semibold text-green-600">{{ $summary['paid_fully']['percentage'] }}%</p>
                    </div>
                </div>
            </div>

            <!-- Paid Partially -->
            <div class="bg-white rounded-xl shadow-lg p-6 border border-amber-200 border-l-4">
                <h3 class="text-lg font-bold text-gray-900 mb-4 flex items-center">
                    <i class="fas fa-partial-circle text-amber-600 mr-2 text-xl"></i>
                    مدفوع جزئياً
                </h3>
                <div class="space-y-3">
                    <div>
                        <p class="text-gray-600 text-sm mb-1">عدد الفواتير</p>
                        <p class="text-3xl font-bold text-amber-600">{{ number_format($summary['paid_partially']['count']) }}</p>
                    </div>
                    <div>
                        <p class="text-gray-600 text-sm mb-1">المبلغ الإجمالي</p>
                        <p class="text-2xl font-semibold text-gray-900">{{ number_format($summary['paid_partially']['total_amount'], 0) }} ريال</p>
                    </div>
                    <div>
                        <p class="text-gray-600 text-sm mb-1">النسبة من الإجمالي</p>
                        <p class="text-xl font-semibold text-amber-600">{{ $summary['paid_partially']['percentage'] }}%</p>
                    </div>
                </div>
            </div>

            <!-- Pending -->
            <div class="bg-white rounded-xl shadow-lg p-6 border border-orange-200 border-l-4">
                <h3 class="text-lg font-bold text-gray-900 mb-4 flex items-center">
                    <i class="fas fa-hourglass text-orange-600 mr-2 text-xl"></i>
                    معلق
                </h3>
                <div class="space-y-3">
                    <div>
                        <p class="text-gray-600 text-sm mb-1">عدد الفواتير</p>
                        <p class="text-3xl font-bold text-orange-600">{{ number_format($summary['pending']['count']) }}</p>
                    </div>
                    <div>
                        <p class="text-gray-600 text-sm mb-1">المبلغ الإجمالي</p>
                        <p class="text-2xl font-semibold text-gray-900">{{ number_format($summary['pending']['amount'], 0) }} ريال</p>
                    </div>
                    <div>
                        <p class="text-gray-600 text-sm mb-1">النسبة من الإجمالي</p>
                        <p class="text-xl font-semibold text-orange-600">{{ $summary['pending']['percentage'] }}%</p>
                    </div>
                </div>
            </div>

            <!-- Overdue -->
            <div class="bg-white rounded-xl shadow-lg p-6 border border-red-200 border-l-4">
                <h3 class="text-lg font-bold text-gray-900 mb-4 flex items-center">
                    <i class="fas fa-exclamation-circle text-red-600 mr-2 text-xl"></i>
                    متأخر
                </h3>
                <div class="space-y-3">
                    <div>
                        <p class="text-gray-600 text-sm mb-1">عدد الفواتير</p>
                        <p class="text-3xl font-bold text-red-600">{{ number_format($summary['overdue']['count']) }}</p>
                    </div>
                    <div>
                        <p class="text-gray-600 text-sm mb-1">المبلغ الإجمالي</p>
                        <p class="text-2xl font-semibold text-gray-900">{{ number_format($summary['overdue']['amount'], 0) }} ريال</p>
                    </div>
                    <div>
                        <p class="text-gray-600 text-sm mb-1">النسبة من الإجمالي</p>
                        <p class="text-xl font-semibold text-red-600">{{ $summary['overdue']['percentage'] }}%</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Detailed Tables -->
        @if(count($paidFully) > 0)
        <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-green-100 to-green-50 border-b border-gray-200">
                <h3 class="text-lg font-semibold text-green-900 flex items-center">
                    <i class="fas fa-check-circle text-green-600 mr-2"></i>
                    الفواتير المدفوعة كاملاً ({{ count($paidFully) }})
                </h3>
            </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-sm text-gray-700">رقم الفاتورة</th>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">العميل</th>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">المبلغ الإجمالي</th>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">المبلغ المدفوع</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @foreach($paidFully as $invoice)
                            <tr class="hover:bg-green-50 transition-colors">
                                <td class="px-6 py-3 font-medium text-gray-900">{{ $invoice['invoice_number'] }}</td>
                                <td class="px-6 py-3 text-gray-700">{{ $invoice['customer_name'] }}</td>
                                <td class="px-6 py-3 font-semibold text-gray-900">{{ number_format($invoice['total_amount'], 0) }} ريال</td>
                                <td class="px-6 py-3 font-semibold text-green-600">{{ number_format($invoice['paid_amount'], 0) }} ريال</td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
        @endif

        @if(count($pending) > 0)
        <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-orange-100 to-orange-50 border-b border-gray-200">
                <h3 class="text-lg font-semibold text-orange-900 flex items-center">
                    <i class="fas fa-hourglass text-orange-600 mr-2"></i>
                    الفواتير المعلقة ({{ count($pending) }})
                </h3>
            </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-sm text-gray-700">رقم الفاتورة</th>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">العميل</th>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">المبلغ الإجمالي</th>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">المبلغ المعلق</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @foreach($pending as $invoice)
                            <tr class="hover:bg-orange-50 transition-colors">
                                <td class="px-6 py-3 font-medium text-gray-900">{{ $invoice['invoice_number'] }}</td>
                                <td class="px-6 py-3 text-gray-700">{{ $invoice['customer_name'] }}</td>
                                <td class="px-6 py-3 font-semibold text-gray-900">{{ number_format($invoice['total_amount'], 0) }} ريال</td>
                                <td class="px-6 py-3 font-semibold text-orange-600">{{ number_format($invoice['remaining_amount'], 0) }} ريال</td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
        @endif
    </div>
</div>

@push('scripts')
@if(isset($charts['payment_status_pie']) && $charts['payment_status_pie'])
<script src="https://cdn.jsdelivr.net/npm/chart.js@3/dist/chart.min.js"></script>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        const ctx = document.getElementById('paymentStatusChart').getContext('2d');
        const data = @json($charts['payment_status_pie']);
        new Chart(ctx, {
            type: 'doughnut',
            data: data,
            options: {
                responsive: true,
                maintainAspectRatio: true,
                plugins: {
                    legend: {
                        position: 'bottom',
                        labels: {
                            font: { size: 14 },
                            padding: 20
                        }
                    }
                }
            }
        });
    });
</script>
@endif
@endpush

@endsection
