@extends('layouts.app')

@section('content')
    <x-common.page-breadcrumb pageTitle="{{ $title ?? 'Rekening Pembayaran' }}" />

    @if(session('success'))
    <div class="mb-4 rounded-lg bg-success-50 p-4 text-sm text-success-700 dark:bg-success-500/15 dark:text-success-500">
        {{ session('success') }}
    </div>
    @endif

    @if(session('error'))
    <div class="mb-4 rounded-lg bg-error-50 p-4 text-sm text-error-700 dark:bg-error-500/15 dark:text-error-500">
        {{ session('error') }}
    </div>
    @endif

    <div x-data class="rounded-2xl border border-gray-200 bg-white dark:border-gray-800 dark:bg-white/[0.03]">
        <div class="border-b border-gray-200 px-5 py-4 dark:border-gray-800">
            <div class="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
                <div>
                    <h3 class="text-lg font-semibold text-gray-800 dark:text-white/90">Daftar Rekening</h3>
                    <p class="text-sm text-gray-500 dark:text-gray-400">Kelola rekening bank dan e-wallet untuk pembayaran</p>
                </div>
                <a href="{{ route('payment-accounts.create') }}" 
                   class="inline-flex items-center gap-2 rounded-lg bg-brand-500 px-4 py-2.5 text-sm font-medium text-white shadow-theme-xs hover:bg-brand-600">
                    <svg class="size-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
                    </svg>
                    Tambah Rekening
                </a>
            </div>
        </div>

        {{-- Filters --}}
        <div class="border-b border-gray-200 px-5 py-4 dark:border-gray-800">
            <form method="GET" class="flex flex-wrap items-end gap-4">
                <div class="flex-1 min-w-[200px]">
                    <x-form.input
                        name="search"
                        placeholder="Cari nama, provider, nomor rekening..."
                        :value="request('search')"
                    />
                </div>
                <div class="w-36">
                    <x-form.select
                        name="type"
                        placeholder="Semua Tipe"
                        :options="['bank' => '🏦 Bank', 'ewallet' => '📱 E-Wallet']"
                        :value="request('type')"
                    />
                </div>
                <div class="w-36">
                    <x-form.select
                        name="status"
                        placeholder="Semua Status"
                        :options="['active' => '✅ Aktif', 'inactive' => '⚪ Nonaktif']"
                        :value="request('status')"
                    />
                </div>
                <button type="submit" class="h-11 rounded-lg bg-gray-100 px-4 text-sm font-medium text-gray-700 hover:bg-gray-200 dark:bg-gray-800 dark:text-gray-300 dark:hover:bg-gray-700">
                    Filter
                </button>
                @if(request()->hasAny(['search', 'type', 'status']))
                <a href="{{ route('payment-accounts.index') }}" class="h-11 flex items-center rounded-lg border border-gray-300 px-4 text-sm font-medium text-gray-700 hover:bg-gray-50 dark:border-gray-700 dark:text-gray-300 dark:hover:bg-gray-800">
                    Reset
                </a>
                @endif
            </form>
        </div>

        <div class="overflow-x-auto">
            <table class="w-full">
                <thead>
                    <tr class="border-b border-gray-200 dark:border-gray-800">
                        <th class="px-5 py-3 text-left text-sm font-medium text-gray-500 dark:text-gray-400">Nama</th>
                        <th class="px-5 py-3 text-left text-sm font-medium text-gray-500 dark:text-gray-400">Tipe</th>
                        <th class="px-5 py-3 text-left text-sm font-medium text-gray-500 dark:text-gray-400">Provider</th>
                        <th class="px-5 py-3 text-left text-sm font-medium text-gray-500 dark:text-gray-400">Nomor Rekening</th>
                        <th class="px-5 py-3 text-left text-sm font-medium text-gray-500 dark:text-gray-400">Pemilik</th>
                        <th class="px-5 py-3 text-center text-sm font-medium text-gray-500 dark:text-gray-400">Status</th>
                        <th class="px-5 py-3 text-center text-sm font-medium text-gray-500 dark:text-gray-400">Aksi</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-200 dark:divide-gray-800">
                    @forelse($accounts as $account)
                    <tr class="hover:bg-gray-50 dark:hover:bg-white/[0.02]">
                        <td class="px-5 py-4">
                            <span class="text-sm font-medium text-gray-800 dark:text-white/90">{{ $account->name }}</span>
                        </td>
                        <td class="px-5 py-4">
                            @if($account->type === 'bank')
                            <span class="inline-flex items-center rounded-full bg-cyan-50 px-2 py-0.5 text-xs font-medium text-cyan-700 dark:bg-cyan-500/15 dark:text-cyan-400">
                                <svg class="mr-1 size-3" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 10h18M7 15h1m4 0h1m-7 4h12a3 3 0 003-3V8a3 3 0 00-3-3H6a3 3 0 00-3 3v8a3 3 0 003 3z"/></svg>
                                Bank
                            </span>
                            @else
                            <span class="inline-flex items-center rounded-full bg-purple-50 px-2 py-0.5 text-xs font-medium text-purple-700 dark:bg-purple-500/15 dark:text-purple-400">
                                <svg class="mr-1 size-3" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 18h.01M8 21h8a2 2 0 002-2V5a2 2 0 00-2-2H8a2 2 0 00-2 2v14a2 2 0 002 2z"/></svg>
                                E-Wallet
                            </span>
                            @endif
                        </td>
                        <td class="px-5 py-4">
                            <span class="text-sm font-semibold text-gray-800 dark:text-white/90">{{ $account->provider }}</span>
                        </td>
                        <td class="px-5 py-4">
                            <span class="text-sm text-gray-600 dark:text-gray-300 font-mono">{{ $account->account_number }}</span>
                        </td>
                        <td class="px-5 py-4">
                            <span class="text-sm text-gray-600 dark:text-gray-300">{{ $account->account_holder }}</span>
                        </td>
                        <td class="px-5 py-4 text-center">
                            <form action="{{ route('payment-accounts.toggle', $account) }}" method="POST" class="inline">
                                @csrf
                                @method('PATCH')
                                <button type="submit" class="inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium transition-colors {{ $account->is_active ? 'bg-success-50 text-success-700 hover:bg-success-100 dark:bg-success-500/15 dark:text-success-400' : 'bg-gray-100 text-gray-500 hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-400' }}" title="Klik untuk toggle status">
                                    {{ $account->is_active ? 'Aktif' : 'Nonaktif' }}
                                </button>
                            </form>
                        </td>
                        <td class="px-5 py-4">
                            <div class="flex items-center justify-center gap-2">
                                <a href="{{ route('payment-accounts.edit', $account) }}" class="text-gray-500 hover:text-brand-500" title="Edit">
                                    <svg class="size-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
                                    </svg>
                                </a>
                                <button @click="$dispatch('open-delete-modal', { action: '{{ route('payment-accounts.destroy', $account) }}' })" class="text-gray-500 hover:text-error-500" title="Hapus">
                                    <svg class="size-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
                                    </svg>
                                </button>
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="7" class="px-5 py-8 text-center text-sm text-gray-500 dark:text-gray-400">
                            Belum ada rekening pembayaran. <a href="{{ route('payment-accounts.create') }}" class="text-brand-500 hover:underline">Tambah sekarang</a>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        @if($accounts->hasPages())
        <div class="border-t border-gray-200 px-5 py-4 dark:border-gray-800">
            {{ $accounts->links() }}
        </div>
        @endif

        {{-- Delete Modal --}}
        <x-ui.delete-confirm />
    </div>
@endsection
