<x-app-layout>
    <x-slot name="header">
        <div class="flex justify-between">
            <h2 class="font-semibold text-l text-gray-800 leading-tight">
                {{ \App\Helpers\TranslationHelper::transFromDatabase('Checks', __('Checks')) }}
            </h2>
            <div class="flex space-y-2 space-y-reverse">
                @permission('create', App\Models\Banking\Check::class)
                    <a href="{{ route('banking.checks.create') }}" class="inline-flex items-center p-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-s text-white uppercase tracking-widest hover:bg-gray-700 active:bg-gray-900 focus:outline-none focus:border-gray-900 focus:ring ring-gray-300 disabled:opacity-25 transition ease-in-out duration-150">
                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Create New Check', __('Create New Check')) }}
                    </a>
                @endpermission
                <a href="{{ route('banking.checks.incoming') }}" class="inline-flex items-center p-4 py-2 bg-blue-600 border border-transparent rounded-md font-semibold text-s text-white uppercase tracking-widest hover:bg-blue-700 active:bg-blue-800 focus:outline-none focus:border-blue-800 focus:ring ring-blue-300 disabled:opacity-25 transition ease-in-out duration-150">
                    {{ \App\Helpers\TranslationHelper::transFromDatabase('Incoming Checks', __('Incoming Checks')) }}
                </a>
                <a href="{{ route('banking.checks.outgoing') }}" class="inline-flex items-center p-4 py-2 bg-green-600 border border-transparent rounded-md font-semibold text-s text-white uppercase tracking-widest hover:bg-green-700 active:bg-green-800 focus:outline-none focus:border-green-800 focus:ring ring-green-300 disabled:opacity-25 transition ease-in-out duration-150">
                    {{ \App\Helpers\TranslationHelper::transFromDatabase('Outgoing Checks', __('Outgoing Checks')) }}
                </a>
            </div>
        </div>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7l m-auto sm:p-6 lg:p-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 bg-white border-b border-gray-200">
                    <!-- فلاتر البحث -->
                    <div class="mb-6">
                        <form method="GET" action="{{ route('banking.checks.index') }}" class="grid grid-cols-1 md:grid-cols-4 gap-4">
                            <div>
                                <x-input-label for="bank_account_id" :value="\App\Helpers\TranslationHelper::transFromDatabase('Bank Account', __('Bank Account'))" />
                                <select id="bank_account_id" name="bank_account_id" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500">
                                    <option value="">{{ \App\Helpers\TranslationHelper::transFromDatabase('All Accounts', __('All Accounts')) }}</option>
                                    @foreach($bankAccounts as $account)
                                        <option value="{{ $account->id }}" {{ request('bank_account_id') == $account->id ? 'selected' : '' }}>
                                            {{ $account->account_number }} - {{ $account->bank->name }}
                                        </option>
                                    @endforeach
                                </select>
                            </div>

                            <div>
                                <x-input-label for="type" :value="\App\Helpers\TranslationHelper::transFromDatabase('Type', __('Type'))" />
                                <select id="type" name="type" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500">
                                    <option value="">{{ \App\Helpers\TranslationHelper::transFromDatabase('All Types', __('All Types')) }}</option>
                                    <option value="incoming" {{ request('type') == 'incoming' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Incoming', __('Incoming')) }}</option>
                                    <option value="outgoing" {{ request('type') == 'outgoing' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Outgoing', __('Outgoing')) }}</option>
                                </select>
                            </div>

                            <div>
                                <x-input-label for="status" :value="\App\Helpers\TranslationHelper::transFromDatabase('Status', __('Status'))" />
                                <select id="status" name="status" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500">
                                    <option value="">{{ \App\Helpers\TranslationHelper::transFromDatabase('All Statuses', __('All Statuses')) }}</option>
                                    <option value="pending" {{ request('status') == 'pending' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Pending', __('Pending')) }}</option>
                                    <option value="deposited" {{ request('status') == 'deposited' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Deposited', __('Deposited')) }}</option>
                                    <option value="cashed" {{ request('status') == 'cashed' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Cashed', __('Cashed')) }}</option>
                                    <option value="returned" {{ request('status') == 'returned' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Returned', __('Returned')) }}</option>
                                    <option value="cancelled" {{ request('status') == 'cancelled' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Cancelled', __('Cancelled')) }}</option>
                                </select>
                            </div>

                            <div>
                                <x-input-label for="due_date" :value="\App\Helpers\TranslationHelper::transFromDatabase('Due Date', __('Due Date'))" />
                                <x-text-input id="due_date" class="block mt-1 w-full" type="date" name="due_date" :value="request('due_date')" />
                            </div>

                            <div class="md:col-span-4 flex justify-end">
                                <x-primary-button class="ms-3">
                                    {{ \App\Helpers\TranslationHelper::transFromDatabase('Search', __('Search')) }}
                                </x-primary-button>
                                <a href="{{ route('banking.checks.index') }}" class="inline-flex items-center p-4 py-2 bg-gray-300 border border-transparent rounded-md font-semibold text-s text-gray-800 uppercase tracking-widest hover:bg-gray-400 active:bg-gray-500 focus:outline-none focus:border-gray-500 focus:ring ring-gray-300 disabled:opacity-25 transition ease-in-out duration-150">
                                    {{ \App\Helpers\TranslationHelper::transFromDatabase('Reset', __('Reset')) }}
                                </a>
                            </div>
                        </form>
                    </div>

                    <!-- جدول الشيكات -->
                    <div class="overflow--auto">
                        <table class="min-w-full divide-y divide-gray-200">
                            <thead class="bg-gray-50">
                                <tr>
                                    <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Check Number', __('Check Number')) }}
                                    </th>
                                    <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Type', __('Type')) }}
                                    </th>
                                    <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Bank Account', __('Bank Account')) }}
                                    </th>
                                    <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Amount', __('Amount')) }}
                                    </th>
                                    <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Due Date', __('Due Date')) }}
                                    </th>
                                    <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Beneficiary/Issuer', __('Beneficiary/Issuer')) }}
                                    </th>
                                    <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Status', __('Status')) }}
                                    </th>
                                    <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Actions', __('Actions')) }}
                                    </th>
                                </tr>
                            </thead>
                            <tbody class="bg-white divide-y divide-gray-200">
                                @forelse($checks as $check)
                                    <tr>
                                        <td class="p-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                                            {{ $check->check_number }}
                                        </td>
                                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                            <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full 
                                                @if($check->type == 'incoming') bg-blue-100 text-blue-800
                                                @elseif($check->type == 'outgoing') bg-green-100 text-green-800
                                                @endif">
                                                @if($check->type == 'incoming') {{ \App\Helpers\TranslationHelper::transFromDatabase('Incoming', __('Incoming')) }}
                                                @elseif($check->type == 'outgoing') {{ \App\Helpers\TranslationHelper::transFromDatabase('Outgoing', __('Outgoing')) }}
                                                @endif
                                            </span>
                                        </td>
                                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                            {{ $check->bankAccount->account_number }} - {{ $check->bankAccount->bank->name }}
                                        </td>
                                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                            {{ number_format($check->amount, 2) }}
                                        </td>
                                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                            {{ $check->due_date->format('Y-m-d') }}
                                        </td>
                                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                            {{ $check->type == 'incoming' ? $check->issuer : $check->beneficiary }}
                                        </td>
                                        <td class="p-6 py-4 whitespace-nowrap">
                                            <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full 
                                                @if($check->status == 'pending') bg-yellow-100 text-yellow-800
                                                @elseif($check->status == 'deposited') bg-blue-100 text-blue-800
                                                @elseif($check->status == 'cashed') bg-green-100 text-green-800
                                                @elseif($check->status == 'returned') bg-red-100 text-red-800
                                                @elseif($check->status == 'cancelled') bg-gray-100 text-gray-800
                                                @endif">
                                                @if($check->status == 'pending') {{ \App\Helpers\TranslationHelper::transFromDatabase('Pending', __('Pending')) }}
                                                @elseif($check->status == 'deposited') {{ \App\Helpers\TranslationHelper::transFromDatabase('Deposited', __('Deposited')) }}
                                                @elseif($check->status == 'cashed') {{ \App\Helpers\TranslationHelper::transFromDatabase('Cashed', __('Cashed')) }}
                                                @elseif($check->status == 'returned') {{ \App\Helpers\TranslationHelper::transFromDatabase('Returned', __('Returned')) }}
                                                @elseif($check->status == 'cancelled') {{ \App\Helpers\TranslationHelper::transFromDatabase('Cancelled', __('Cancelled')) }}
                                                @endif
                                            </span>
                                        </td>
                                        <td class="p-6 py-4 whitespace-nowrap text-sm font-medium">
                                            <div class="flex space-y-2 space-y-reverse">
                                                <a href="{{ route('banking.checks.show', $check) }}" class="text-indigo-600 hover:text-indigo-900">
                                                    {{ \App\Helpers\TranslationHelper::transFromDatabase('View', __('View')) }}
                                                </a>
                                                
                                                @permission('update', $check)
                                                    <a href="{{ route('banking.checks.edit', $check) }}" class="text-yellow-600 hover:text-yellow-900">
                                                        {{ \App\Helpers\TranslationHelper::transFromDatabase('Edit', __('Edit')) }}
                                                    </a>
                                                @endpermission
                                                
                                                @if($check->status == 'pending')
                                                    @if($check->type == 'incoming')
                                                        @permission('deposit', $check)
                                                            <form action="{{ route('banking.checks.deposit', $check) }}" method="POST" class="inline">
                                                                @csrf
                                                                <button type="submit" class="text-blue-600 hover:text-blue-900">
                                                                    {{ \App\Helpers\TranslationHelper::transFromDatabase('Deposit', __('Deposit')) }}
                                                                </button>
                                                            </form>
                                                        @endpermission
                                                        
                                                        @permission('cash', $check)
                                                            <form action="{{ route('banking.checks.cash', $check) }}" method="POST" class="inline">
                                                                @csrf
                                                                <button type="submit" class="text-green-600 hover:text-green-900">
                                                                    {{ \App\Helpers\TranslationHelper::transFromDatabase('Cash', __('Cash')) }}
                                                                </button>
                                                            </form>
                                                        @endpermission
                                                    @endif
                                                    
                                                    @permission('return', $check)
                                                        <form action="{{ route('banking.checks.return', $check) }}" method="POST" class="inline">
                                                            @csrf
                                                            <button type="submit" class="text-red-600 hover:text-red-900">
                                                                {{ \App\Helpers\TranslationHelper::transFromDatabase('Return', __('Return')) }}
                                                            </button>
                                                        </form>
                                                    @endpermission
                                                    
                                                    @permission('cancel', $check)
                                                        <form action="{{ route('banking.checks.cancel', $check) }}" method="POST" class="inline">
                                                            @csrf
                                                            <button type="submit" class="text-gray-600 hover:text-gray-900">
                                                                {{ \App\Helpers\TranslationHelper::transFromDatabase('Cancel', __('Cancel')) }}
                                                            </button>
                                                        </form>
                                                    @endpermission
                                                @endif
                                                
                                                @permission('delete', $check)
                                                    <form action="{{ route('banking.checks.destroy', $check) }}" method="POST" class="inline" onsubmit="return confirm('{{ \App\Helpers\TranslationHelper::transFromDatabase('Are you sure you want to delete this check?', __('Are you sure you want to delete this check?')) }}')">
                                                        @csrf
                                                        @method('DELETE')
                                                        <button type="submit" class="text-red-600 hover:text-red-900">
                                                            {{ \App\Helpers\TranslationHelper::transFromDatabase('Delete', __('Delete')) }}
                                                        </button>
                                                    </form>
                                                @endpermission
                                            </div>
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="8" class="p-6 py-4 text-center text-gray-500">
                                            {{ \App\Helpers\TranslationHelper::transFromDatabase('No checks found', __('No checks found')) }}
                                        </td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>

                    <!-- pagination -->
                    @if($checks->hasPages())
                        <div class="p-6 border-t border-gray-100 flex items-center justify-center">
                            {{ $checks->links() }}
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
