@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">
        <!-- رأس الصفحة -->
        <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>
                <a href="{{ route('reports.sales.index') }}" class="inline-flex items-center px-6 py-3 bg-gradient-to-r from-gray-600 to-gray-700 text-white font-semibold rounded-lg hover:from-gray-700 hover:to-gray-800 transition-all duration-200 shadow-lg">
                    <i class="fas fa-arrow-right ml-2"></i>
                    <span>العودة</span>
                </a>
            </div>
        </div>

        <!-- الفلاتر -->
        <div class="bg-white rounded-xl shadow-lg border border-gray-200 p-6 mb-8">
            <h3 class="text-lg font-semibold text-gray-900 mb-4">🔍 الفلاتر</h3>
            <form method="GET" class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div>
                    <label class="block text-gray-700 font-medium mb-2">حتى التاريخ</label>
                    <input type="date" name="as_of_date" value="{{ $asOfDate }}" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                </div>
                <div class="flex items-end">
                    <button type="submit" class="w-full px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white font-semibold rounded-lg transition-all duration-200 shadow-lg">
                        <i class="fas fa-search ml-2"></i>تحديث
                    </button>
                </div>
            </form>
        </div>

        <!-- الإحصائيات السريعة -->
        <div class="grid grid-cols-1 md:grid-cols-3 lg:grid-cols-6 gap-4 mb-8">
            <div class="bg-white rounded-lg shadow-md p-4 border-l-4 border-gray-800">
                <div class="text-gray-600 text-xs font-medium">إجمالي المديونية</div>
                <div class="text-lg font-bold text-gray-900 mt-1">{{ number_format($data['summary']['total_balance'], 2) }}</div>
            </div>
            <div class="bg-white rounded-lg shadow-md p-4 border-l-4 border-blue-500">
                <div class="text-gray-600 text-xs font-medium">جارية (لم تستحق)</div>
                <div class="text-lg font-bold text-gray-900 mt-1">{{ number_format($data['summary']['total_current'], 2) }}</div>
            </div>
            <div class="bg-white rounded-lg shadow-md p-4 border-l-4 border-green-500">
                <div class="text-gray-600 text-xs font-medium">1 - 30 يوم</div>
                <div class="text-lg font-bold text-gray-900 mt-1">{{ number_format($data['summary']['total_1_30'], 2) }}</div>
            </div>
            <div class="bg-white rounded-lg shadow-md p-4 border-l-4 border-yellow-500">
                <div class="text-gray-600 text-xs font-medium">31 - 60 يوم</div>
                <div class="text-lg font-bold text-gray-900 mt-1">{{ number_format($data['summary']['total_31_60'], 2) }}</div>
            </div>
            <div class="bg-white rounded-lg shadow-md p-4 border-l-4 border-orange-500">
                <div class="text-gray-600 text-xs font-medium">61 - 90 يوم</div>
                <div class="text-lg font-bold text-gray-900 mt-1">{{ number_format($data['summary']['total_61_90'], 2) }}</div>
            </div>
            <div class="bg-white rounded-lg shadow-md p-4 border-l-4 border-red-500">
                <div class="text-gray-600 text-xs font-medium">أكثر من 90 يوم</div>
                <div class="text-lg font-bold text-gray-900 mt-1">{{ number_format($data['summary']['total_over_90'], 2) }}</div>
            </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-4 py-4 text-right font-semibold text-xs">كود العميل</th>
                            <th class="px-4 py-4 text-right font-semibold text-xs">اسم العميل</th>
                            <th class="px-4 py-4 text-center font-semibold text-xs">إجمالي الرصيد</th>
                            <th class="px-4 py-4 text-center font-semibold text-xs">جارية</th>
                            <th class="px-4 py-4 text-center font-semibold text-xs">1 - 30</th>
                            <th class="px-4 py-4 text-center font-semibold text-xs">31 - 60</th>
                            <th class="px-4 py-4 text-center font-semibold text-xs">61 - 90</th>
                            <th class="px-4 py-4 text-center font-semibold text-xs">أكثر من 90</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200 text-sm">
                        @forelse($data['customers'] as $customer)
                        <tr class="hover:bg-blue-50 transition-colors duration-150">
                            <td class="px-4 py-4 text-gray-600 font-medium">{{ $customer['customer_code'] }}</td>
                            <td class="px-4 py-4 font-semibold text-gray-900">{{ $customer['customer_name'] }}</td>
                            <td class="px-4 py-4 text-center font-bold text-gray-900">{{ number_format($customer['total_balance'], 2) }}</td>
                            <td class="px-4 py-4 text-center text-blue-600">{{ number_format($customer['current'], 2) }}</td>
                            <td class="px-4 py-4 text-center text-green-600">{{ number_format($customer['1_30'], 2) }}</td>
                            <td class="px-4 py-4 text-center text-yellow-600 font-medium">{{ number_format($customer['31_60'], 2) }}</td>
                            <td class="px-4 py-4 text-center text-orange-600 font-semibold">{{ number_format($customer['61_90'], 2) }}</td>
                            <td class="px-4 py-4 text-center text-red-600 font-bold bg-red-50">{{ number_format($customer['over_90'], 2) }}</td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="8" class="px-6 py-12 text-center">
                                <div class="flex flex-col items-center justify-center">
                                    <i class="fas fa-check-circle text-green-400 text-4xl mb-3"></i>
                                    <p class="text-gray-600 font-medium">لا توجد مديونيات مستحقة</p>
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                    <tfoot class="bg-gray-100 font-bold">
                        <tr>
                            <td colspan="2" class="px-4 py-4 text-right text-gray-900">الإجماليات</td>
                            <td class="px-4 py-4 text-center text-gray-900">{{ number_format($data['summary']['total_balance'], 2) }}</td>
                            <td class="px-4 py-4 text-center text-blue-600">{{ number_format($data['summary']['total_current'], 2) }}</td>
                            <td class="px-4 py-4 text-center text-green-600">{{ number_format($data['summary']['total_1_30'], 2) }}</td>
                            <td class="px-4 py-4 text-center text-yellow-600">{{ number_format($data['summary']['total_31_60'], 2) }}</td>
                            <td class="px-4 py-4 text-center text-orange-600">{{ number_format($data['summary']['total_61_90'], 2) }}</td>
                            <td class="px-4 py-4 text-center text-red-600 bg-red-100">{{ number_format($data['summary']['total_over_90'], 2) }}</td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>

        <!-- أزرار التصدير -->
        <div class="mt-8 flex gap-4 justify-end">
            <button onclick="window.print()" class="inline-flex items-center px-6 py-3 bg-blue-600 hover:bg-blue-700 text-white font-semibold rounded-lg transition-all duration-200 shadow-lg">
                <i class="fas fa-print ml-2"></i>
                <span>طباعة التقرير</span>
            </button>
        </div>
    </div>
</div>
@endsection
