@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 action="{{ route('checkout.proses') }}" method="POST">
                            @csrf
                            @if (count($items) == 1 && request()->has('produk_id'))
                                <input type="hidden" name="produk_id" value="{{ $items->first()->id }}">
                                <input type="hidden" name="jumlah" value="{{ $items->first()->jumlah }}">
                            @endif

                            <h5 class="mb-3">Daftar Barang</h5>
                            <ul class="list-group mb-4">
                                @foreach ($items as $item)
                                    <li class="list-group-item d-flex justify-content-between align-items-center">
                                        <div class="d-flex align-items-center">
                                            <img src="{{ $item->foto ? asset('storage/' . $item->foto) : asset('template/frontend/img/fruite-item-5.jpg') }}"
                                                width="50" class="rounded me-3">
                                            <div>
                                                <h6 class="my-0">{{ $item->nama_produk }}</h6>
                                                <small class="text-muted">{{ $item->jumlah }} x Rp
                                                    {{ number_format($item->harga, 0, ',', '.') }}</small>
                                            </div>
                                        </div>
                                        <span class="text-muted">Rp {{ number_format($item->subtotal, 0, ',', '.') }}</span>
                                    </li>
                                @endforeach
                                <li class="list-group-item d-flex justify-content-between bg-light">
                                    <span>Total (IDR)</span>
                                    <strong>Rp {{ number_format($total_belanja, 0, ',', '.') }}</strong>
                                </li>
                            </ul>

                            <div class="mb-3">
                                <label class="form-label fw-bold">Alamat Pengiriman</label>
                                <textarea name="alamat_pengiriman" class="form-control" rows="3" required
                                    placeholder="Tulis alamat lengkap Anda...">{{ Auth::guard('pembeli')->user()->alamat }}</textarea>
                            </div>

                            <div class="card border-0 shadow-sm rounded-4 mb-4">
                                <div class="card-header bg-white py-3 border-bottom-0">
                                    <h5 class="mb-0 fw-bold text-dark">Metode Pembayaran</h5>
                                </div>
                                <div class="card-body">

                                    <div class="alert alert-success d-flex align-items-center border-0 shadow-sm"
                                        role="alert" style="background-color: #d1e7dd; color: #0f5132;">
                                        <div class="bg-white p-3 rounded-circle me-3 text-success d-flex align-items-center justify-content-center"
                                            style="width: 50px; height: 50px;">
                                            <i class="fas fa-hand-holding-usd fs-4"></i>
                                        </div>
                                        <div>
                                            <h6 class="fw-bold mb-1">Bayar di Tempat (COD)</h6>
                                            <p class="mb-0 small opacity-75">
                                                Pembayaran dilakukan secara tunai langsung kepada petani/kurir saat pesanan
                                                sampai di lokasi Anda.
                                            </p>
                                        </div>
                                    </div>

                                    <input type="hidden" name="metode_pembayaran" value="cod">

                                </div>
                            </div>

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