@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">
                <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.purchase.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>

        <!-- Partner 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-3 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['supplier']->name }}</p>
                    <p class="text-sm text-gray-500">كود: {{ $statement['supplier']->code ?? '-' }}</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']->name ?? 'غير مرتبط' }}</p>
                    <p class="text-sm text-gray-500">كود: {{ $statement['customer']->code ?? '-' }}</p>
                </div>
                <div>
                    <p class="text-gray-600 text-sm font-medium mb-2">صافي الرصيد النهائي</p>
                    <p class="text-2xl font-bold {{ $statement['summary']['closing_balance'] >= 0 ? 'text-red-600' : 'text-green-600' }}">
                        {{ number_format(abs($statement['summary']['closing_balance']), 2) }}
                        <span class="text-sm font-normal">
                            ({{ $statement['summary']['closing_balance'] >= 0 ? 'مستحق لنا' : 'مستحق عليه' }})
                        </span>
                    </p>
                </div>
            </div>
        </div>

        <!-- Summary Stats -->
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 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['summary']['total_debit'], 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-emerald-600">{{ number_format($statement['summary']['total_credit'], 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-purple-600">{{ count($statement['transactions']) }}</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">
                <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="{{ url()->current() }}" 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>
                            <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">{{ $transaction['date'] }}</td>
                                <td class="px-6 py-4">
                                    <span class="inline-block px-3 py-1 rounded-full text-sm font-medium
                                        @if($transaction['type'] == 'customer_invoice') bg-blue-100 text-blue-800
                                        @elseif($transaction['type'] == 'customer_payment') bg-green-100 text-green-800
                                        @elseif($transaction['type'] == 'supplier_invoice') bg-red-100 text-red-800
                                        @elseif($transaction['type'] == 'supplier_payment') bg-purple-100 text-purple-800
                                        @endif
                                    ">
                                        @if($transaction['type'] == 'customer_invoice') فاتورة مبيعات
                                        @elseif($transaction['type'] == 'customer_payment') تحصيل مبيعات
                                        @elseif($transaction['type'] == 'supplier_invoice') فاتورة مشتريات
                                        @elseif($transaction['type'] == 'supplier_payment') سداد مشتريات
                                        @endif
                                    </span>
                                </td>
                                <td class="px-6 py-4 text-gray-700">{{ $transaction['reference'] }}</td>
                                <td class="px-6 py-4 text-gray-700 text-sm">{{ $transaction['description'] }}</td>
                                <td class="px-6 py-4 text-blue-600 font-medium">
                                    {{ $transaction['debit'] > 0 ? number_format($transaction['debit'], 2) : '-' }}
                                </td>
                                <td class="px-6 py-4 text-emerald-600 font-medium">
                                    {{ $transaction['credit'] > 0 ? number_format($transaction['credit'], 2) : '-' }}
                                </td>
                                <td class="px-6 py-4">
                                    <span class="font-semibold {{ $transaction['balance'] >= 0 ? 'text-red-600' : 'text-green-600' }}">
                                        {{ number_format(abs($transaction['balance']), 2) }}
                                        <span class="text-xs font-normal">({{ $transaction['balance'] >= 0 ? 'مدين' : 'دائن' }})</span>
                                    </span>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="px-6 py-8 text-center text-gray-500">
                                    لا توجد حركات في الفترة المحددة
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
@endsection
