@extends('layouts.frontend')

@section('title', 'Checkout')

@section('content')
<div class="container py-5">
    <div class="row">
        <div class="col-md-8 mx-auto">
            <div class="card border-0 shadow-sm rounded">
                <div class="card-header bg-white py-3">
                    <h4 class="mb-0 text-primary fw-bold">Konfirmasi Pembelian</h4>
                </div>
                <div class="card-body p-4">
                    {{-- Form ini akan mengirim data ke TransaksiController --}}
                    <form action="{{ route('checkout.proses') }}" method="POST">
                        @csrf
                        <input type="hidden" name="produk_id" value="{{ $produk->id }}">
                        
                        <div class="row mb-4">
                            <div class="col-md-4">
                                <img src="{{ $produk->foto_produk ? asset('storage/'.$produk->foto_produk) : asset('template/frontend/img/fruite-item-5.jpg') }}" 
                                     class="img-fluid rounded" alt="Produk">
                            </div>
                            <div class="col-md-8">
                                <h5>{{ $produk->nama_produk }}</h5>
                                <p class="text-muted small mb-2">Penjual: {{ $produk->petani->nama_lengkap }}</p>
                                <h4 class="text-success fw-bold">Rp {{ number_format($produk->harga, 0, ',', '.') }} / unit</h4>
                                <p class="text-secondary mt-3">{{ $produk->deskripsi }}</p>
                            </div>
                        </div>

                        <hr>

                        <div class="mb-3">
                            <label class="form-label fw-bold">Jumlah Pembelian</label>
                            <input type="number" name="jumlah" class="form-control w-25" value="1" min="1" max="{{ $produk->stok }}" id="jumlah" onchange="hitungTotal()">
                            <small class="text-muted">Stok tersedia: {{ $produk->stok }}</small>
                        </div>

                        <div class="mb-3">
                            <label class="form-label fw-bold">Alamat Pengiriman</label>
                            <textarea name="alamat_pengiriman" class="form-control" rows="3" required>{{ Auth::guard('pembeli')->user()->alamat }}</textarea>
                            <small class="text-muted">Pastikan alamat lengkap untuk memudahkan kurir.</small>
                        </div>

                        <div class="mb-4">
                            <label class="form-label fw-bold">Metode Pembayaran</label>
                            <select name="metode_pembayaran" class="form-select">
                                <option value="cod" selected>Bayar di Tempat (COD)</option>
                            </select>
                        </div>

                        <div class="d-flex justify-content-between align-items-center bg-light p-3 rounded mb-4">
                            <h5 class="mb-0">Total Bayar:</h5>
                            <h3 class="fw-bold text-primary" id="total_bayar">Rp {{ number_format($produk->harga, 0, ',', '.') }}</h3>
                        </div>

                        <div class="d-grid gap-2">
                            {{-- PERBAIKAN: Gunakan button type="submit" agar form terkirim --}}
                            <button type="submit" class="btn btn-primary rounded-pill px-4 py-3 mb-2 font-weight-bold">
                                <i class="fa fa-paper-plane me-2"></i> Buat Pesanan Sekarang
                            </button>
                            
                            <a href="{{ route('shop') }}" class="btn btn-outline-secondary py-2 rounded-pill">Batal</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('js')
<script>
    function hitungTotal() {
        let harga = {{ $produk->harga }};
        let jumlah = document.getElementById('jumlah').value;
        
        // Validasi agar tidak minus
        if(jumlah < 1) {
            jumlah = 1;
            document.getElementById('jumlah').value = 1;
        }
        
        let total = harga * jumlah;
        
        document.getElementById('total_bayar').innerText = 'Rp ' + new Intl.NumberFormat('id-ID').format(total);
    }
</script>
@endsection