@extends('layouts.app')

@section('hide_nav_bar', true)

@section('content')
<main class="main-content">
    
    {{-- 1. HEADER (Tombol Kembali & Judul) --}}
    <header class="header mobile-header">
        <div class="user-greeting">
            <a href="{{ route('invoices.index') }}"><i class="fas fa-arrow-left"></i></a>
            <h3>
                @if($invoice->status == 'unpaid')
                    Detail Tagihan
                @else
                    Resi Pembayaran
                @endif
            </h3>
        </div>
    </header>

    {{-- 2. KONTEN (Sutradara Memilih Tampilan) --}}
    @if($invoice->status == 'unpaid')
        
        {{-- Jika BELUM LUNAS, panggil Form Pembayaran --}}
        {{-- (Memanggil file _payment_form.blade.php dari folder 'invoices') --}}
        @include('invoices._payment_form')

    @else

        {{-- Jika SUDAH LUNAS, panggil Tampilan Resi --}}
        {{-- (Memanggil file _receipt_view.blade.php dari folder 'invoices') --}}
        @include('invoices._receipt_view')

    @endif

</main>

{{-- 3. SCRIPT (Hanya dimuat jika perlu bayar) --}}
@if($invoice->status == 'unpaid')
    @push('scripts')
    {{-- Load library Snap Midtrans --}}
    <script src="https://app.sandbox.midtrans.com/snap/snap.js" data-client-key="{{ config('midtrans.client_key') }}"></script>
    
    <script type="text/javascript">
      document.getElementById('pay-button').addEventListener('click', function () {
        window.snap.pay('{{ $snapToken }}', {
          onSuccess: function(result){
            window.location.href = '{{ route('invoices.show', $invoice->id) }}';
          },
          onPending: function(result){
            alert("Menunggu pembayaran Anda!");
          },
          onError: function(result){
            alert("Pembayaran gagal!");
          },
          onClose: function(){
            window.location.href = '{{ route('invoices.show', ['invoice' => $invoice->id, 'reset' => 'true']) }}';
          }
        });
      });
    </script>
    @endpush
@endif

@endsection