@extends('frontend.app')

@section('content')
<div class="max-w-3xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
    
    @if(session('success'))
    <div class="bg-green-50 border border-green-200 text-green-700 px-4 py-3 rounded-xl mb-6 flex items-center gap-3">
        <svg class="w-6 h-6 text-green-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
        <span class="font-medium text-sm">{{ session('success') }}</span>
    </div>
    @endif

    @php
        // LOGIKA STATUS TERPUSAT
        $isDeal = $transaction->survey_status === 'deal';
        $isProses = $transaction->survey_status === 'proses';
        $isPending = $transaction->payment_status === 'pending';
        
        $isGagalBayar = $transaction->payment_status === 'failed' || $transaction->survey_status === 'batal_gagal_bayar';
        $isBatalTenant = $transaction->survey_status === 'batal_tenant';
        $isBatalOwner = $transaction->survey_status === 'batal_owner';
        $isBatalNoShow = $transaction->survey_status === 'batal_no_show';
        $isBatal = $isGagalBayar || $isBatalTenant || $isBatalOwner || $isBatalNoShow;

        // DESAIN HEADER DINAMIS
        if ($isDeal) {
            $headerColor = 'bg-emerald-600';
            $headerIcon = '<svg class="w-12 h-12 text-white mb-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z"></path></svg>';
            $headerTitle = 'Transaksi Selesai (DEAL)';
            $headerDesc = 'Selamat! Properti ini telah resmi Anda sewa.';
        } elseif ($isGagalBayar) {
            $headerColor = 'bg-gray-600';
            $headerIcon = '<svg class="w-12 h-12 text-white mb-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>';
            $headerTitle = 'Booking Kedaluwarsa';
            $headerDesc = 'Batas waktu pembayaran 1 Jam telah habis.';
        } elseif ($isBatal) {
            $headerColor = 'bg-red-500';
            $headerIcon = '<svg class="w-12 h-12 text-white mb-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>';
            $headerTitle = 'Survei Dibatalkan';
            if ($isBatalTenant) $headerDesc = 'Anda membatalkan sewa karena tidak cocok (Berhak Refund 50%).';
            elseif ($isBatalOwner) $headerDesc = 'Dibatalkan oleh sistem/Owner (Berhak Refund 100%).';
            elseif ($isBatalNoShow) $headerDesc = 'Dibatalkan karena Anda tidak datang survei (Dana Hangus).';
            else $headerDesc = 'Transaksi ini telah dibatalkan.';
        } elseif ($isPending) {
            $headerColor = 'bg-amber-500';
            $headerIcon = '<svg class="w-12 h-12 text-white mb-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>';
            $headerTitle = 'Selesaikan Pembayaran';
            $headerDesc = 'Segera transfer agar tiket survei Anda aktif.';
        } else {
            // DEFAULT PROSES SURVEI
            $headerColor = 'bg-green-500';
            $headerIcon = '<svg class="w-12 h-12 text-white mb-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>';
            $headerTitle = 'Tiket Survei Aktif';
            $headerDesc = 'Pembayaran Lunas | Tunjukkan tiket ini saat survei.';
        }
    @endphp

    <div class="bg-white rounded-3xl shadow-xl border border-gray-100 overflow-hidden max-w-2xl mx-auto">
        
        <!-- HEADER DINAMIS -->
        <div class="{{ $headerColor }} p-8 flex flex-col items-center justify-center text-center transition-colors duration-500">
            {!! $headerIcon !!}
            <h2 class="text-2xl font-extrabold text-white tracking-tight">{{ $headerTitle }}</h2>
            <p class="text-white/90 text-sm mt-2 font-medium">{{ $headerDesc }}</p>
            
            <div class="mt-4 bg-white/20 backdrop-blur-md px-4 py-1.5 rounded-full text-white text-xs font-bold tracking-widest border border-white/30">
                KODE: {{ $transaction->booking_code }}
            </div>
        </div>

        <!-- INSTRUKSI PEMBAYARAN JIKA PENDING -->
        @if($isPending)
            <div class="p-6 sm:p-8 border-b border-gray-100 bg-amber-50/30">
                <div class="bg-red-50 border border-red-100 rounded-2xl p-6 text-center mb-6 shadow-sm">
                    <p class="text-red-500 text-sm font-bold uppercase tracking-wider mb-2">Batas Waktu Pembayaran</p>
                    
                    <div x-data="{ 
                        expiry: new Date('{{ $transaction->payment_expired_at }}').getTime(),
                        remaining: 'Menghitung...',
                        updateTimer() {
                            let now = new Date().getTime();
                            let distance = this.expiry - now;
                            if (distance < 0) {
                                this.remaining = 'WAKTU HABIS';
                                return;
                            }
                            let m = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
                            let s = Math.floor((distance % (1000 * 60)) / 1000);
                            this.remaining = m + ' Menit ' + s + ' Detik';
                        }
                    }" x-init="updateTimer(); setInterval(() => updateTimer(), 1000);">
                        <span x-text="remaining" class="text-3xl sm:text-4xl font-extrabold text-red-600 mb-4 block tracking-tight"></span>
                    </div>

                    <p class="text-gray-500 text-sm mb-4">Transfer tepat sebesar nominal di bawah ini untuk mengunci properti:</p>
                    <div class="text-4xl font-black text-gray-900">
                        Rp {{ number_format((float)$transaction->booking_fee, 0, ',', '.') }}
                    </div>
                </div>

                <div class="mb-6 border border-gray-200 rounded-xl p-5 bg-white">
                    <h3 class="font-bold text-gray-900 mb-4 border-b border-gray-100 pb-2">Transfer ke Rekening Berikut:</h3>
                    <div class="flex items-center gap-4">
                        <div class="bg-blue-100 text-blue-800 font-bold px-3 py-1.5 rounded-lg border border-blue-200">BCA</div>
                        <div>
                            <p class="font-mono text-lg font-bold text-gray-900">123 456 7890</p>
                            <p class="text-xs text-gray-500 uppercase">a.n PT Ngekos Yuk Indonesia</p>
                        </div>
                    </div>
                </div>
                
                <a href="https://wa.me/6281234567890?text=Halo%20Admin%20NgekosYuk,%20saya%20sudah%20transfer%20untuk%20kode%20booking%20{{ $transaction->booking_code }}" target="_blank" class="w-full flex items-center justify-center gap-2 bg-green-500 hover:bg-green-600 text-white font-bold py-3.5 px-4 rounded-xl transition shadow-sm">
                    <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 24 24"><path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51a12.8 12.8 0 00-.57-.015c-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 00-3.48-8.413z"/></svg>
                    Konfirmasi via WhatsApp
                </a>
            </div>
        @endif

        <!-- FORM KEPUTUSAN SURVEI (Hanya jika masih proses) -->
        @if($isProses)
            <div class="p-6 sm:p-8 border-b border-gray-100 bg-blue-50/30 text-center">
                <h3 class="font-extrabold text-gray-900 mb-2">Bagaimana Hasil Survei Anda?</h3>
                <p class="text-sm text-gray-500 mb-6">Jika Anda sudah mensurvei lokasi secara langsung, silakan konfirmasi keputusan Anda di bawah ini.</p>
                
                <form action="{{ route('kos.confirm', $transaction->booking_code) }}" method="POST" class="flex flex-col sm:flex-row gap-3 justify-center">
                    @csrf
                    <button type="submit" name="status" value="deal" class="bg-gray-900 hover:bg-black text-white px-6 py-3.5 rounded-xl text-sm font-bold transition shadow-md w-full sm:w-auto">
                        Kamar Cocok & DEAL
                    </button>
                    <!-- Mengubah value yang dikirim disesuaikan dengan kode database terbaru: batal_tenant -->
                    <button type="submit" name="status" value="batal_tenant" class="bg-white border-2 border-red-200 hover:bg-red-50 text-red-600 px-6 py-3.5 rounded-xl text-sm font-bold transition shadow-sm w-full sm:w-auto">
                        Tidak Cocok (Batalkan)
                    </button>
                </form>
            </div>
        @endif

        <!-- INFORMASI UMUM TIKET (Selalu Muncul) -->
        <div class="p-6 sm:p-8 bg-white">
            <h3 class="font-extrabold text-gray-900 text-lg mb-6 border-b border-gray-100 pb-3">Informasi Detail</h3>
            
            <div class="space-y-4">
                <div class="flex justify-between items-center border-b border-gray-50 pb-4">
                    <span class="text-gray-500 text-sm">Penyewa</span>
                    <span class="font-bold text-gray-900 text-sm text-right">{{ $transaction->tenant_name ?? auth()->user()->name }}</span>
                </div>
                <div class="flex justify-between items-center border-b border-gray-50 pb-4">
                    <span class="text-gray-500 text-sm">Jadwal Survei</span>
                    <span class="font-bold text-gray-900 text-sm text-right">{{ \Carbon\Carbon::parse($transaction->survey_date)->translatedFormat('d F Y') }}</span>
                </div>
                <div class="flex justify-between items-center border-b border-gray-50 pb-4">
                    <span class="text-gray-500 text-sm">Properti</span>
                    <span class="font-bold text-gray-900 text-sm text-right">{{ $transaction->boardingHouse->name }}</span>
                </div>
                
                @if($transaction->boardingHouse->category === 'kos' && $transaction->room)
                    <div class="flex justify-between items-center border-b border-gray-50 pb-4">
                        <span class="text-gray-500 text-sm">Tipe Kamar</span>
                        <span class="font-bold text-gray-900 text-sm text-right">{{ $transaction->room->room_type }}</span>
                    </div>
                    <div class="flex justify-between items-center bg-gray-50 p-4 rounded-xl mt-4 border border-gray-100">
                        <span class="text-gray-500 text-xs font-medium uppercase tracking-wider">Estimasi Sewa Bulanan</span>
                        <span class="font-black text-gray-900 text-lg">Rp {{ number_format((float)$transaction->room->price_per_month, 0, ',', '.') }}</span>
                    </div>
                @elseif($transaction->boardingHouse->category === 'kontrakan')
                    <div class="flex justify-between items-center border-b border-gray-50 pb-4">
                        <span class="text-gray-500 text-sm">Tipe Sewa</span>
                        <span class="font-bold text-gray-900 text-sm text-right">Satu Rumah Utuh</span>
                    </div>
                    <div class="flex justify-between items-center bg-gray-50 p-4 rounded-xl mt-4 border border-gray-100">
                        <span class="text-gray-500 text-xs font-medium uppercase tracking-wider">Estimasi Harga Sewa</span>
                        <span class="font-black text-gray-900 text-lg">
                            Rp {{ number_format((float)$transaction->boardingHouse->price, 0, ',', '.') }}<span class="text-sm font-medium text-gray-500">/{{ $transaction->boardingHouse->payment_type }}</span>
                        </span>
                    </div>
                @endif
            </div>

            <!-- INFO REFUND (MUNCUL JIKA BATAL & ADA HAK REFUND) -->
            @if(($isBatalTenant || $isBatalOwner) && !empty($transaction->refund_status))
                <div class="mt-8 bg-blue-50 border border-blue-200 rounded-2xl p-5 shadow-sm">
                    <h4 class="font-extrabold text-blue-900 text-sm mb-3 flex items-center gap-2">
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                        Informasi Pengembalian Dana (Refund)
                    </h4>
                    
                    <div class="flex justify-between items-center mt-3 bg-white p-3 rounded-xl border border-blue-100">
                        <span class="text-blue-800 text-xs font-bold uppercase tracking-wider">Status Proses:</span>
                        @if($transaction->refund_status === 'selesai')
                            <span class="bg-green-100 text-green-700 text-xs font-bold px-3 py-1.5 rounded-md border border-green-200">Selesai Ditransfer</span>
                        @else
                            <span class="bg-amber-100 text-amber-700 text-xs font-bold px-3 py-1.5 rounded-md border border-amber-200 animate-pulse">Menunggu Admin</span>
                        @endif
                    </div>

                    <!-- Kalimat SOP ini sekarang diletakkan di luar if/else agar SELALU MUNCUL -->
                    <p class="text-xs text-blue-700 mt-4 leading-relaxed font-medium">
                        Dana akan dikembalikan ke rekening/e-wallet yang Anda gunakan saat pembayaran (maksimal 1x24 jam). Hubungi bantuan jika ada kendala.
                    </p>

                    <!-- LOGIKA BARU: TAMPILKAN BUKTI JIKA SELESAI -->
                    @if($transaction->refund_status === 'selesai' && $transaction->refund_proof)
                        <div class="mt-4 pt-4 border-t border-blue-100/50">
                            <p class="text-xs font-bold text-blue-800 mb-2 uppercase tracking-wider">Bukti Transfer:</p>
                            
                            <a href="{{ asset('storage/' . $transaction->refund_proof) }}" data-fancybox="bukti-refund" class="block w-full sm:w-48 overflow-hidden rounded-xl border-2 border-blue-100 hover:border-blue-300 transition relative group cursor-pointer shadow-sm">
                                <img src="{{ asset('storage/' . $transaction->refund_proof) }}" alt="Bukti Refund" class="w-full h-32 object-cover group-hover:scale-105 transition duration-300">
                                <div class="absolute inset-0 bg-black/0 group-hover:bg-black/10 flex items-center justify-center transition duration-300">
                                    <svg class="w-6 h-6 text-white opacity-0 group-hover:opacity-100 transition" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0zM10 7v3m0 0v3m0-3h3m-3 0H7"></path></svg>
                                </div>
                            </a>
                        </div>
                    @endif
                </div>
            @endif

            <div class="pt-8 mt-8 border-t border-gray-100 text-center">
                <a href="{{ route('dashboard') }}" class="text-gray-500 hover:text-gray-900 font-bold text-sm transition flex items-center justify-center gap-2">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"></path></svg>
                    Kembali ke Dasbor Utama
                </a>
            </div>
        </div>

    </div>
</div>
@endsection