@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-3xl font-bold text-gray-900 mb-2">تقرير تدفق النقد الخارج</h1>
                    <p class="text-gray-600">تحليل شامل لدفعات الموردين والمصروفات النقدية</p>
                </div>
                <div class="flex gap-2">
                    <a href="{{ route('reports.purchase.index') }}" class="inline-flex items-center px-4 py-2 bg-gray-200 hover:bg-gray-300 text-gray-800 font-semibold rounded-lg transition-all duration-150">
                        <i class="fas fa-arrow-right ml-2"></i>
                        <span>العودة</span>
                    </a>
                </div>
            </div>
        </div>

        <!-- Alert Messages -->
        @if(session('success'))
        <div class="bg-green-50 border-l-4 border-green-500 p-4 mb-6 rounded-r-lg">
            <div class="flex">
                <i class="fas fa-check-circle text-green-500 text-xl ml-3"></i>
                <p class="text-green-700 font-medium">{{ session('success') }}</p>
            </div>
        </div>
        @endif

        @if(session('error'))
        <div class="bg-red-50 border-l-4 border-red-500 p-4 mb-6 rounded-r-lg">
            <div class="flex">
                <i class="fas fa-exclamation-circle text-red-500 text-xl ml-3"></i>
                <p class="text-red-700 font-medium">{{ session('error') }}</p>
            </div>
        </div>
        @endif

        <!-- Filter Section -->
        <div class="bg-white rounded-xl shadow-lg p-6 mb-6 border border-gray-200">
            <h3 class="text-lg font-semibold text-gray-900 mb-4">
                <i class="fas fa-filter ml-2"></i>تصفية البيانات
            </h3>
            <form method="GET" class="grid grid-cols-1 md:grid-cols-4 gap-4">
                <div>
                    <label for="start_date" class="block text-sm font-medium text-gray-700 mb-2">تاريخ البداية</label>
                    <input type="date" id="start_date" name="start_date" value="{{ request('start_date', now()->subMonth()->toDateString()) }}" 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 for="end_date" class="block text-sm font-medium text-gray-700 mb-2">تاريخ النهاية</label>
                    <input type="date" id="end_date" name="end_date" value="{{ request('end_date', now()->toDateString()) }}" 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 bg-blue-600 hover:bg-blue-700 text-white font-semibold py-2 px-4 rounded-lg transition-all duration-200">
                        <i class="fas fa-search ml-1"></i>بحث
                    </button>
                </div>
                <div class="flex items-end gap-2">
                    <a href="{{ route('reports.purchase.cash-outflow', ['export' => true, 'format' => 'pdf']) }}" class="flex-1 bg-red-600 hover:bg-red-700 text-white font-semibold py-2 px-4 rounded-lg text-center transition-all duration-200">
                        <i class="fas fa-file-pdf ml-1"></i>PDF
                    </a>
                    <a href="{{ route('reports.purchase.cash-outflow', ['export' => true, 'format' => 'excel']) }}" class="flex-1 bg-green-600 hover:bg-green-700 text-white font-semibold py-2 px-4 rounded-lg text-center transition-all duration-200">
                        <i class="fas fa-file-excel ml-1"></i>Excel
                    </a>
                </div>
            </form>
        </div>

        <!-- Summary Statistics -->
        <div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-6">
            <!-- Total Payments -->
            <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">{{ number_format($data['total_payments'] ?? 0, 2) }}</p>
                    </div>
                    <div class="bg-blue-100 rounded-full p-3">
                        <i class="fas fa-money-bill-wave text-blue-600 text-2xl"></i>
                    </div>
                </div>
            </div>

            <!-- Payment Count -->
            <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['payment_count'] ?? 0 }}</p>
                    </div>
                    <div class="bg-green-100 rounded-full p-3">
                        <i class="fas fa-list-ol text-green-600 text-2xl"></i>
                    </div>
                </div>
            </div>

            <!-- Average Payment -->
            <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">{{ number_format($data['average_payment'] ?? 0, 2) }}</p>
                    </div>
                    <div class="bg-yellow-100 rounded-full p-3">
                        <i class="fas fa-chart-line text-yellow-600 text-2xl"></i>
                    </div>
                </div>
            </div>

            <!-- Period -->
            <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-sm font-bold text-gray-900 mt-2">
                            {{ \Carbon\Carbon::parse($data['period_start'] ?? now())->format('Y-m-d') }}<br>
                            {{ \Carbon\Carbon::parse($data['period_end'] ?? now())->format('Y-m-d') }}
                        </p>
                    </div>
                    <div class="bg-purple-100 rounded-full p-3">
                        <i class="fas fa-calendar text-purple-600 text-2xl"></i>
                    </div>
                </div>
            </div>
        </div>

        <!-- Payments by Method -->
        @if(!empty($data['payments_by_method']))
        <div class="bg-white rounded-xl shadow-lg mb-6 border border-gray-200">
            <div class="bg-gradient-to-r from-gray-800 to-gray-900 text-white p-6 rounded-t-xl">
                <h3 class="text-xl font-semibold">
                    <i class="fas fa-credit-card ml-2"></i>الدفعات حسب الطريقة
                </h3>
            </div>
            <div class="p-6">
                <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                    @foreach($data['payments_by_method'] as $method => $amount)
                    <div class="bg-gradient-to-br from-blue-50 to-blue-100 rounded-lg p-4 border border-blue-200">
                        <div class="flex items-center justify-between">
                            <div>
                                <p class="text-gray-600 text-sm font-medium">
                                    @switch($method)
                                        @case('cash')
                                            💰 نقداً
                                            @break
                                        @case('bank_transfer')
                                            🏦 تحويل بنكي
                                            @break
                                        @case('check')
                                            🏷️ شيك
                                            @break
                                        @case('credit_card')
                                            💳 بطاقة ائتمان
                                            @break
                                        @default
                                            {{ $method }}
                                    @endswitch
                                </p>
                                <p class="text-2xl font-bold text-blue-900 mt-2">{{ number_format($amount, 2) }}</p>
                            </div>
                        </div>
                    </div>
                    @endforeach
                </div>
            </div>
        </div>
        @endif

        <!-- Payments by Supplier -->
        @if(!empty($data['payments_by_supplier']))
        <div class="bg-white rounded-xl shadow-lg mb-6 border border-gray-200">
            <div class="bg-gradient-to-r from-gray-800 to-gray-900 text-white p-6 rounded-t-xl">
                <h3 class="text-xl font-semibold">
                    <i class="fas fa-building ml-2"></i>الدفعات حسب المورد
                </h3>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gray-100">
                        <tr>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-900">المورد</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-900">الإجمالي</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-900">عدد الدفعات</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-900">النسبة المئوية</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($data['payments_by_supplier'] as $supplier)
                        <tr class="hover:bg-blue-50 transition-colors duration-150">
                            <td class="px-6 py-4 font-medium text-gray-900">{{ $supplier['supplier_name'] ?? 'غير محدد' }}</td>
                            <td class="px-6 py-4 text-gray-900 font-semibold">{{ number_format($supplier['total'], 2) }}</td>
                            <td class="px-6 py-4 text-gray-600">{{ $supplier['count'] }}</td>
                            <td class="px-6 py-4">
                                <div class="w-full bg-gray-200 rounded-full h-2">
                                    <div class="bg-blue-600 h-2 rounded-full" style="width: {{ ($supplier['total'] / ($data['total_payments'] ?? 1)) * 100 }}%"></div>
                                </div>
                                <p class="text-sm text-gray-600 mt-1">{{ round(($supplier['total'] / ($data['total_payments'] ?? 1)) * 100, 2) }}%</p>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="4" 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>
                                    <p class="text-gray-500 text-sm mt-1">لم نجد أي دفعات للموردين في هذه الفترة</p>
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        @endif

        <!-- Details Table -->
        @if(!empty($data['details']) && $data['details']->count() > 0)
        <div class="bg-white rounded-xl shadow-lg border border-gray-200">
            <div class="bg-gradient-to-r from-gray-800 to-gray-900 text-white p-6 rounded-t-xl">
                <h3 class="text-xl font-semibold">
                    <i class="fas fa-list ml-2"></i>تفاصيل الدفعات
                </h3>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gray-100">
                        <tr>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-900">#</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-900">تاريخ الدفع</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-900">المورد</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-900">المبلغ</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-900">طريقة الدفع</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-900">المرجع</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-900">ملاحظات</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @foreach($data['details'] as $payment)
                        <tr class="hover:bg-blue-50 transition-colors duration-150">
                            <td class="px-6 py-4 text-gray-600 font-medium">{{ $loop->iteration }}</td>
                            <td class="px-6 py-4 text-gray-900">
                                {{ $payment->payment_date ? \Carbon\Carbon::parse($payment->payment_date)->format('Y-m-d') : 'N/A' }}
                            </td>
                            <td class="px-6 py-4 font-medium text-gray-900">{{ $payment->supplier->name ?? 'غير محدد' }}</td>
                            <td class="px-6 py-4 font-semibold text-gray-900">{{ number_format($payment->amount, 2) }}</td>
                            <td class="px-6 py-4">
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium 
                                    @switch($payment->payment_method)
                                        @case('cash')
                                            bg-green-100 text-green-700
                                            @break
                                        @case('bank_transfer')
                                            bg-blue-100 text-blue-700
                                            @break
                                        @case('check')
                                            bg-yellow-100 text-yellow-700
                                            @break
                                        @case('credit_card')
                                            bg-purple-100 text-purple-700
                                            @break
                                        @default
                                            bg-gray-100 text-gray-700
                                    @endswitch
                                ">
                                    @switch($payment->payment_method)
                                        @case('cash')
                                            💰 نقداً
                                            @break
                                        @case('bank_transfer')
                                            🏦 تحويل بنكي
                                            @break
                                        @case('check')
                                            🏷️ شيك
                                            @break
                                        @case('credit_card')
                                            💳 بطاقة ائتمان
                                            @break
                                        @default
                                            {{ $payment->payment_method }}
                                    @endswitch
                                </span>
                            </td>
                            <td class="px-6 py-4 text-gray-600">{{ $payment->reference_number ?? '-' }}</td>
                            <td class="px-6 py-4 text-gray-600 text-sm">{{ $payment->notes ?? '-' }}</td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
        @else
        <!-- Empty State -->
        <div class="bg-white rounded-xl shadow-lg p-12 text-center border border-gray-200">
            <i class="fas fa-inbox text-gray-400 text-6xl mb-4"></i>
            <p class="text-gray-600 font-medium text-lg mb-2">لا توجد بيانات دفعات</p>
            <p class="text-gray-500">لم نجد أي دفعات للموردين في الفترة المحددة ({{ $data['period_start'] ?? 'N/A' }} إلى {{ $data['period_end'] ?? 'N/A' }})</p>
            <a href="{{ route('reports.purchase.index') }}" class="inline-block mt-6 bg-blue-600 hover:bg-blue-700 text-white font-semibold py-2 px-6 rounded-lg transition-all duration-200">
                <i class="fas fa-arrow-right ml-2"></i>العودة إلى التقارير
            </a>
        </div>
        @endif
    </div>
</div>

@push('scripts')
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // إضافة تأثيرات التفاعل
        const rows = document.querySelectorAll('tbody tr');
        rows.forEach(row => {
            row.addEventListener('mouseenter', function() {
                this.style.backgroundColor = '#EFF6FF';
            });
            row.addEventListener('mouseleave', function() {
                this.style.backgroundColor = '';
            });
        });

        // إضافة طباعة سهلة
        window.printReport = function() {
            window.print();
        };
    });
</script>
@endpush

@endsection