<x-app-layout>
    <!-- Header -->
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between mb-6">
        <div class="flex items-center">
            <a href="{{ route('transactions.index') }}" class="text-slate-400 hover:text-teal-600 mr-3 sm:mr-4 transition">
                <i class="fas fa-arrow-left text-lg sm:text-xl"></i>
            </a>
            <div>
                <h2 class="text-xl sm:text-2xl font-bold text-slate-800">Detail Transaksi</h2>
            </div>
        </div>
        <div class="mt-3 sm:mt-0 flex items-center space-x-2">
            <a href="{{ route('transactions.edit', $transaction) }}"
               class="inline-flex items-center px-3 sm:px-4 py-2 border border-slate-200 text-slate-700 text-sm rounded-xl hover:bg-slate-50 transition">
                <i class="fas fa-edit mr-1 sm:mr-2"></i><span class="hidden sm:inline">Edit</span>
            </a>
            <form action="{{ route('transactions.duplicate', $transaction) }}" method="POST" class="inline">
                @csrf
                <button type="submit" class="inline-flex items-center px-3 sm:px-4 py-2 border border-slate-200 text-slate-700 text-sm rounded-xl hover:bg-slate-50 transition">
                    <i class="fas fa-copy mr-1 sm:mr-2"></i><span class="hidden sm:inline">Duplikat</span>
                </button>
            </form>
        </div>
    </div>

    <div class="w-full max-w-2xl">
        <div class="bg-white border border-slate-100 rounded-xl sm:rounded-2xl shadow-sm overflow-hidden">
            <!-- Amount Header -->
            <div class="p-4 sm:p-6 {{ $transaction->type === 'expense' ? 'bg-rose-50' : 'bg-emerald-50' }}">
                <div class="flex items-center justify-between">
                    <div class="flex items-center">
                        <div class="w-10 h-10 sm:w-14 sm:h-14 rounded-xl flex items-center justify-center"
                             style="background-color: {{ $transaction->category?->color ?? '#14b8a6' }}15; color: {{ $transaction->category?->color ?? '#14b8a6' }};">
                            <i class="fas fa-{{ $transaction->category?->icon ?? 'exchange-alt' }} text-lg sm:text-2xl"></i>
                        </div>
                        <div class="ml-3 sm:ml-4">
                            <p class="text-xs sm:text-sm text-slate-500">{{ $transaction->category?->name ?? 'Tanpa Kategori' }}</p>
                            <p class="text-xl sm:text-3xl font-bold {{ $transaction->type === 'expense' ? 'text-rose-600' : 'text-emerald-600' }}">
                                {{ $transaction->type === 'expense' ? '-' : '+' }}Rp {{ number_format($transaction->amount, 0, ',', '.') }}
                            </p>
                        </div>
                    </div>
                    <span class="px-2 sm:px-3 py-1 rounded-full text-xs sm:text-sm font-medium {{ $transaction->type === 'expense' ? 'bg-rose-100 text-rose-600' : 'bg-emerald-100 text-emerald-600' }}">
                        {{ $transaction->type === 'expense' ? 'Pengeluaran' : 'Pemasukan' }}
                    </span>
                </div>
            </div>

            <!-- Details -->
            <div class="p-4 sm:p-6 space-y-4 sm:space-y-6">
                <!-- Description -->
                @if($transaction->description)
                    <div>
                        <p class="text-xs sm:text-sm font-medium text-slate-500 mb-1">Deskripsi</p>
                        <p class="text-slate-800">{{ $transaction->description }}</p>
                    </div>
                @endif

                <div class="grid grid-cols-1 sm:grid-cols-2 gap-4 sm:gap-6">
                    <!-- Date & Time -->
                    <div>
                        <p class="text-xs sm:text-sm font-medium text-slate-500 mb-1">Tanggal & Waktu</p>
                        <p class="text-slate-800">
                            <i class="fas fa-calendar mr-2 text-slate-400"></i>
                            {{ ($transaction->date ?? $transaction->transaction_date)?->format('l, d F Y') }}
                        </p>
                        <p class="text-sm text-slate-600 mt-1">
                            <i class="fas fa-clock mr-2 text-slate-400"></i>
                            {{ $transaction->time ? \Carbon\Carbon::parse($transaction->time)->format('H:i:s') : '00:00:00' }}
                        </p>
                    </div>

                    <!-- Account -->
                    <div>
                        <p class="text-xs sm:text-sm font-medium text-slate-500 mb-1">Akun</p>
                        <div class="flex items-center">
                            <i class="fas fa-{{ $transaction->account?->icon ?? 'wallet' }} mr-2 text-slate-400"></i>
                            <span class="text-slate-800">{{ $transaction->account?->name }}</span>
                        </div>
                    </div>
                </div>

                <!-- Metadata -->
                <div class="pt-6 border-t border-slate-100">
                    <div class="grid grid-cols-2 gap-4 text-sm">
                        <div>
                            <p class="text-slate-400">Dibuat</p>
                            <p class="text-slate-600">{{ $transaction->created_at?->format('d M Y, H:i') }}</p>
                        </div>
                        @if($transaction->updated_at && $transaction->updated_at != $transaction->created_at)
                            <div>
                                <p class="text-slate-400">Diperbarui</p>
                                <p class="text-slate-600">{{ $transaction->updated_at?->format('d M Y, H:i') }}</p>
                            </div>
                        @endif
                    </div>
                </div>

                <!-- Attachment -->
                @if($transaction->attachment)
                    <div class="pt-6 border-t border-slate-100">
                        <p class="text-sm font-medium text-slate-500 mb-3">
                            <i class="fas fa-paperclip mr-2"></i>Lampiran
                        </p>
                        <a href="{{ Storage::url($transaction->attachment) }}" target="_blank" class="block">
                            <img src="{{ Storage::url($transaction->attachment) }}"
                                 alt="{{ $transaction->attachment_name ?? 'Lampiran' }}"
                                 class="max-w-full max-h-64 rounded-xl border border-slate-200 hover:shadow-lg transition cursor-pointer">
                        </a>
                        <p class="text-xs text-slate-400 mt-2">{{ $transaction->attachment_name ?? 'Lampiran' }}</p>
                    </div>
                @endif
            </div>

            <!-- Actions -->
            <div class="px-4 sm:px-6 py-4 bg-slate-50 border-t border-slate-100 flex flex-col sm:flex-row sm:justify-between gap-3 sm:gap-0">
                <form action="{{ route('transactions.destroy', $transaction) }}" method="POST"
                      onsubmit="return confirm('Yakin ingin menghapus transaksi ini?')">
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="text-rose-500 hover:text-rose-700 font-medium transition">
                        <i class="fas fa-trash mr-2"></i>Hapus
                    </button>
                </form>
                <a href="{{ route('transactions.edit', $transaction) }}"
                   class="px-5 py-2 bg-gradient-to-r from-teal-500 to-emerald-500 text-white font-medium rounded-xl transition shadow-lg shadow-teal-500/30">
                    <i class="fas fa-edit mr-2"></i>Edit Transaksi
                </a>
            </div>
        </div>
    </div>
</x-app-layout>
