@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>
            <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>

        <!-- 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-5 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">
                    <button type="submit" class="w-full 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">
                    <button type="button" onclick="window.print()" class="w-full px-4 py-2 bg-gray-600 text-white rounded-lg hover:bg-gray-700 transition-colors font-medium">
                        طباعة
                    </button>
                </div>
                <div class="flex items-end">
                    <a href="{{ route('reports.sales.by-customer') }}" 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>

        <!-- Quick Stats -->
        <div class="grid grid-cols-1 md: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-blue-600">{{ $sales->count() }}</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($sales->sum('total_amount'), 0) }}</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-emerald-600">{{ number_format($sales->sum('paid_amount'), 0) }}</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-amber-600">{{ number_format($sales->sum('remaining_amount'), 0) }}</p>
            </div>
        </div>

        <!-- Table -->
        <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-6 py-4 text-right font-semibold text-sm">#</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">كود العميل</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">اسم العميل</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">عدد الفواتير</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">إجمالي المبيعات</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">المدفوع</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">المعلق</th>
                            <th class="px-6 py-4 text-center font-semibold text-sm">النسبة</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($sales as $customer)
                        <tr class="hover:bg-blue-50 transition-colors">
                            <td class="px-6 py-4 text-gray-600 font-medium">{{ $loop->iteration }}</td>
                            <td class="px-6 py-4 text-gray-700 font-mono text-sm">{{ $customer['customer_code'] }}</td>
                            <td class="px-6 py-4 font-semibold text-gray-900">{{ $customer['customer_name'] }}</td>
                            <td class="px-6 py-4 text-gray-700 text-center">{{ $customer['invoice_count'] }}</td>
                            <td class="px-6 py-4 text-green-600 font-semibold">{{ number_format($customer['total_amount'], 0) }}</td>
                            <td class="px-6 py-4 text-emerald-600 font-semibold">{{ number_format($customer['paid_amount'], 0) }}</td>
                            <td class="px-6 py-4 text-amber-600 font-semibold">{{ number_format($customer['remaining_amount'], 0) }}</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 bg-blue-100 text-blue-700">
                                    {{ number_format(($customer['total_amount'] / max(1, $sales->sum('total_amount'))) * 100, 1) }}%
                                </span>
                            </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-inbox text-gray-400 text-4xl mb-3"></i>
                                    <p class="text-gray-600 font-medium">لا توجد بيانات</p>
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
@endsection
