@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">{{ $statement['customer_name'] ?? 'بيان الحساب' }}</p>
            </div>
            <div class="flex gap-3">
                <a href="{{ route('reports.sales.customer-statement-pdf', ['customerId' => request('customerId'), '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>
                <button onclick="window.print()" class="inline-flex items-center px-6 py-3 bg-gray-600 text-white rounded-lg hover:bg-gray-700">
                    <i class="fas fa-print mr-2"></i>
                    طباعة
                </button>
                <a href="{{ route('reports.sales.customer-statement') }}" 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>

        <!-- Customer Info -->
        <div class="bg-white rounded-xl shadow-lg p-6 mb-8 border border-gray-200">
            <div class="grid grid-cols-1 md:grid-cols-4 gap-6">
                <div>
                    <p class="text-gray-600 text-sm font-medium mb-2">اسم العميل</p>
                    <p class="text-lg font-semibold text-gray-900">{{ $statement['customer_name'] ?? '-' }}</p>
                </div>
                <div>
                    <p class="text-gray-600 text-sm font-medium mb-2">رقم العميل</p>
                    <p class="text-lg font-semibold text-gray-900">{{ $statement['customer_code'] ?? '-' }}</p>
                </div>
                <div>
                    <p class="text-gray-600 text-sm font-medium mb-2">الرصيد الحالي</p>
                    <p class="text-lg font-semibold {{ $statement['current_balance'] >= 0 ? 'text-green-600' : 'text-red-600' }}">
                        {{ number_format($statement['current_balance'] ?? 0, 2) }}
                    </p>
                </div>
                <div>
                    <p class="text-gray-600 text-sm font-medium mb-2">عدد الحركات</p>
                    <p class="text-lg font-semibold text-gray-900">{{ count($statement['transactions'] ?? []) }}</p>
                </div>
            </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-blue-600">{{ number_format($statement['total_invoiced'] ?? 0, 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($statement['total_paid'] ?? 0, 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-orange-600">{{ number_format($statement['total_pending'] ?? 0, 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-purple-600">{{ $statement['collection_rate'] ?? 0 }}%</p>
            </div>
        </div>

        <!-- Filter Form -->
        <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">
                <input type="hidden" name="customerId" value="{{ request('customerId') }}">
                <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">
                    <a href="{{ route('reports.sales.customer-statement', ['customerId' => request('customerId')]) }}" 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>

        <!-- Transactions Table -->
        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200 mb-8">
            <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>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($statement['transactions'] ?? [] as $transaction)
                            <tr class="hover:bg-blue-50 transition-colors">
                                <td class="px-6 py-4 text-gray-700">{{ \Carbon\Carbon::parse($transaction['date'])->format('Y-m-d') }}</td>
                                <td class="px-6 py-4">
                                    <span class="inline-block px-3 py-1 rounded-full text-sm font-medium
                                        {{ $transaction['type'] == 'invoice' ? 'bg-blue-100 text-blue-800' : '' }}
                                        {{ $transaction['type'] == 'payment' ? 'bg-green-100 text-green-800' : '' }}
                                    ">
                                        {{ $transaction['type'] == 'invoice' ? 'فاتورة' : 'دفعة' }}
                                    </span>
                                </td>
                                <td class="px-6 py-4 text-gray-700">{{ $transaction['description'] ?? '-' }}</td>
                                <td class="px-6 py-4 text-gray-700">{{ $transaction['reference_number'] ?? '-' }}</td>
                                <td class="px-6 py-4">
                                    <span class="font-semibold {{ $transaction['type'] == 'invoice' ? 'text-red-600' : 'text-green-600' }}">
                                        {{ $transaction['type'] == 'invoice' ? '+' : '-' }}{{ number_format(abs($transaction['amount']), 2) }}
                                    </span>
                                </td>
                                <td class="px-6 py-4">
                                    <span class="font-semibold {{ $transaction['running_balance'] >= 0 ? 'text-green-600' : 'text-red-600' }}">
                                        {{ number_format($transaction['running_balance'], 2) }}
                                    </span>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="px-6 py-8 text-center text-gray-500">
                                    <i class="fas fa-inbox text-4xl mb-4 block opacity-50"></i>
                                    لا توجد حركات لهذا العميل في الفترة المحددة
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
@endsection
