@extends('layouts.app')

@section('title', \App\Helpers\TranslationHelper::transFromDatabase('Bank Reconciliations', 'التسويات البنكية'))

@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">{{ \App\Helpers\TranslationHelper::transFromDatabase('Bank Reconciliations', __('التسويات البنكية')) }}</h1>
                    <p class="text-gray-600">{{ \App\Helpers\TranslationHelper::transFromDatabase('Manage and monitor bank reconciliations and account matches', __('إدارة ومتابعة التسويات البنكية ومطابقة الحسابات')) }}</p>
                </div>
                @permission('banking.reconciliations.create')
                <a href="{{ route('banking.reconciliations.create') }}" class="inline-flex items-center px-6 py-3 bg-gradient-to-r from-blue-600 to-blue-700 text-white font-semibold rounded-lg hover:from-blue-700 hover:to-blue-800 transition-all duration-200 shadow-lg hover:shadow-xl transform hover:scale-105">
                    <i class="fas fa-plus ml-2"></i>
                    <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Create New Reconciliation', __('إنشاء تسوية جديدة')) }}</span>
                </a>
                @endpermission
            </div>

            @if(session('success'))
            <div class="bg-green-50 border-l-4 border-green-500 p-4 mb-6 rounded-r-lg">
                <div class="flex">
                    <div class="flex-shrink-0">
                        <i class="fas fa-check-circle text-green-500 text-xl"></i>
                    </div>
                    <div class="mr-3">
                        <p class="text-green-700 font-medium">{{ session('success') }}</p>
                    </div>
                </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">
                    <div class="flex-shrink-0">
                        <i class="fas fa-exclamation-circle text-red-500 text-xl"></i>
                    </div>
                    <div class="mr-3">
                        <p class="text-red-700 font-medium">{{ session('error') }}</p>
                    </div>
                </div>
            </div>
            @endif
        </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-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Reconciliation Number', __('رقم التسوية')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Bank', __('البنك')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Account Number', __('رقم الحساب')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Start Date', __('تاريخ البدء')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('End Date', __('تاريخ الانتهاء')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Book Balance', __('الرصيد الدفتري')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Bank Balance', __('الرصيد البنكي')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Status', __('الحالة')) }}</th>
                            <th class="px-6 py-4 text-center font-semibold text-sm">{{ \App\Helpers\TranslationHelper::transFromDatabase('Actions', __('الإجراءات')) }}</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($reconciliations ?? [] as $reconciliation)
                        <tr class="hover:bg-blue-50 transition-colors duration-150 border-b border-gray-100">
                            <td class="px-6 py-4 text-gray-700 font-medium">{{ $reconciliation->reconciliation_number }}</td>
                            <td class="px-6 py-4 text-gray-700">{{ $reconciliation->bankAccount->bank->name }} - {{ $reconciliation->bankAccount->account_name }}</td>
                            <td class="px-6 py-4 text-gray-700">{{ $reconciliation->bankAccount->account_number }}</td>
                            <td class="px-6 py-4 text-gray-700">{{ $reconciliation->start_date }}</td>
                            <td class="px-6 py-4 text-gray-700">{{ $reconciliation->end_date }}</td>
                            <td class="px-6 py-4 text-gray-700 font-semibold">{{ number_format($reconciliation->system_balance, 2) }} {{ $reconciliation->bankAccount->currency }}</td>
                            <td class="px-6 py-4 text-gray-700 font-semibold">{{ number_format($reconciliation->statement_balance, 2) }} {{ $reconciliation->bankAccount->currency }}</td>
                            <td class="px-6 py-4">
                                @if($reconciliation->status == 'in_progress')
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-amber-100 text-amber-700">
                                    <span class="w-2 h-2 bg-amber-500 rounded-full ml-2"></span>
                                    {{ \App\Helpers\TranslationHelper::transFromDatabase('In Progress', __('قيد التنفيذ')) }}
                                </span>
                                @elseif($reconciliation->status == 'completed')
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-green-100 text-green-700">
                                    <span class="w-2 h-2 bg-green-500 rounded-full ml-2"></span>
                                    {{ \App\Helpers\TranslationHelper::transFromDatabase('Completed', __('مكتملة')) }}
                                </span>
                                @endif
                            </td>
                            <td class="px-6 py-4">
                                <div class="flex items-center justify-center gap-1 flex-wrap">
                                    @permission('banking.reconciliations.view')
                                    <a href="{{ route('banking.reconciliations.show', $reconciliation) }}" class="inline-flex items-center px-3 py-2 rounded-lg bg-blue-100 text-blue-600 hover:bg-blue-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('View', __('عرض')) }}">
                                        <i class="fas fa-eye ml-1"></i>
                                        <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('View', __('عرض')) }}</span>
                                    </a>
                                    @endpermission
                                    
                                    @if($reconciliation->status == 'in_progress')
                                        @permission('banking.reconciliations.edit')
                                        <a href="{{ route('banking.reconciliations.edit', $reconciliation) }}" class="inline-flex items-center px-3 py-2 rounded-lg bg-amber-100 text-amber-600 hover:bg-amber-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" title="{{ \App\Helpers\TranslationHelper::transFromDatabase('Edit', __('تعديل')) }}">
                                            <i class="fas fa-edit ml-1"></i>
                                            <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Edit', __('تعديل')) }}</span>
                                        </a>
                                        @endpermission
                                        
                                        @permission('banking.reconciliations.edit')
                                        <a href="{{ route('banking.reconciliations.items', $reconciliation) }}" class="inline-flex items-center px-3 py-2 rounded-lg bg-indigo-100 text-indigo-600 hover:bg-indigo-200 transition-all duration-150 text-sm font-medium whitespace-nowrap">
                                            <i class="fas fa-list mr-1"></i>
                                            <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Items', __('العناصر')) }}</span>
                                        </a>
                                        @endpermission
                                        
                                        @permission('banking.reconciliations.edit')
                                        <form action="{{ route('banking.reconciliations.complete', $reconciliation) }}" method="POST" class="inline">
                                            @csrf
                                            <button type="submit" class="inline-flex items-center px-3 py-2 rounded-lg bg-green-100 text-green-600 hover:bg-green-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" onclick="return confirm('{{ \App\Helpers\TranslationHelper::transFromDatabase('Are you sure you want to complete this reconciliation?', __('هل أنت متأكد من إكمال هذه التسوية؟')) }}')">
                                                <i class="fas fa-check mr-1"></i>
                                                <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Complete', __('إكمال')) }}</span>
                                            </button>
                                        </form>
                                        @endpermission
                                        
                                        @permission('banking.reconciliations.delete')
                                        <form action="{{ route('banking.reconciliations.destroy', $reconciliation) }}" method="POST" class="inline">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="inline-flex items-center px-3 py-2 rounded-lg bg-red-100 text-red-600 hover:bg-red-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" onclick="return confirm('{{ \App\Helpers\TranslationHelper::transFromDatabase('Are you sure you want to delete this reconciliation?', __('هل أنت متأكد من حذف هذه التسوية؟')) }}')">
                                                <i class="fas fa-trash mr-1"></i>
                                                <span>{{ \App\Helpers\TranslationHelper::transFromDatabase('Delete', __('حذف')) }}</span>
                                            </button>
                                        </form>
                                        @endpermission
                                    @endif
                                </div>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="9" 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">{{ \App\Helpers\TranslationHelper::transFromDatabase('No bank reconciliations found', __('لا توجد تسويات بنكية')) }}</p>
                                    <p class="text-gray-500 text-sm mt-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Start by creating a new reconciliation', __('ابدأ بإنشاء تسوية جديدة')) }}</p>
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
@endsection
