@extends('layouts.app')

@section('title', 'Checkout')

@section('content')

<div class="hhd-breadcrumb">
    <div class="container small">
        <a href="{{ route('home') }}" class="text-muted-soft">Home</a> /
        <a href="{{ route('cart.index') }}" class="text-muted-soft">Cart</a> /
        <span class="text-charcoal">Checkout</span>
    </div>
</div>

<div class="container py-5">
    <h1 class="font-serif mb-4" style="font-size:1.8rem;">Checkout</h1>

    <form action="{{ route('checkout.store') }}" method="POST">
        @csrf
        <div class="row g-5">
            <div class="col-lg-7">
                <h5 class="font-serif mb-3">Contact &amp; Shipping Details</h5>

                <div class="row g-3">
                    <div class="col-md-6">
                        <label class="form-label small">Full Name *</label>
                        <input type="text" name="full_name" class="form-control @error('full_name') is-invalid @enderror" value="{{ old('full_name', $user->name ?? '') }}" required>
                        @error('full_name')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-md-6">
                        <label class="form-label small">Phone *</label>
                        <input type="text" name="phone" class="form-control @error('phone') is-invalid @enderror" value="{{ old('phone', $user->phone ?? '') }}" required>
                        @error('phone')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-12">
                        <label class="form-label small">Email (optional)</label>
                        <input type="email" name="email" class="form-control @error('email') is-invalid @enderror" value="{{ old('email', $user->email ?? '') }}">
                        @error('email')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-12">
                        <label class="form-label small">Address Line 1 *</label>
                        <input type="text" name="address_line1" class="form-control @error('address_line1') is-invalid @enderror" value="{{ old('address_line1') }}" required>
                        @error('address_line1')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-12">
                        <label class="form-label small">Address Line 2</label>
                        <input type="text" name="address_line2" class="form-control" value="{{ old('address_line2') }}">
                    </div>
                    <div class="col-md-4">
                        <label class="form-label small">City *</label>
                        <input type="text" name="city" class="form-control @error('city') is-invalid @enderror" value="{{ old('city') }}" required>
                        @error('city')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <div class="col-md-4">
                        <label class="form-label small">State / Province</label>
                        <input type="text" name="state" class="form-control" value="{{ old('state') }}">
                    </div>
                    <div class="col-md-4">
                        <label class="form-label small">Postal Code</label>
                        <input type="text" name="postal_code" class="form-control" value="{{ old('postal_code') }}">
                    </div>
                    <div class="col-12">
                        <label class="form-label small">Order Notes (optional)</label>
                        <textarea name="notes" rows="3" class="form-control">{{ old('notes') }}</textarea>
                    </div>
                </div>

                <h5 class="font-serif mt-4 mb-3">Payment Method</h5>
                <div class="border rounded p-3 bg-white">
                    <div class="form-check">
                        <input class="form-check-input" type="radio" checked disabled>
                        <label class="form-check-label">
                            <i class="bi bi-cash-coin me-1"></i> Cash on Delivery (COD)
                        </label>
                    </div>
                    <p class="small text-muted-soft mb-0 mt-2">Pay in cash when your order is delivered to your doorstep. We currently do not support online payments.</p>
                </div>
            </div>

            <div class="col-lg-5">
                <div class="border rounded p-4 bg-white">
                    <h5 class="font-serif mb-3">Order Summary</h5>
                    @foreach($cart->items as $item)
                        <div class="d-flex justify-content-between align-items-center mb-2 small">
                            <span>{{ $item->product->name }}@if($item->variantLabel()) <span class="text-muted-soft">({{ $item->variantLabel() }})</span>@endif &times; {{ $item->quantity }}</span>
                            <span>Rs. {{ number_format($item->quantity * $item->unitPrice()) }}</span>
                        </div>
                    @endforeach
                    <hr>
                    <div class="d-flex justify-content-between mb-2">
                        <span class="text-muted-soft">Subtotal</span>
                        <span>Rs. {{ number_format($cart->total) }}</span>
                    </div>
                    <div class="d-flex justify-content-between mb-2">
                        <span class="text-muted-soft">Shipping</span>
                        @if($shippingFee > 0)
                            <span>Rs. {{ number_format($shippingFee) }}</span>
                        @else
                            <span class="text-sage fw-semibold">FREE</span>
                        @endif
                    </div>
                    @if($shippingFee > 0 && $freeShippingThreshold > 0)
                        <p class="small text-muted-soft mb-0">Add Rs. {{ number_format($freeShippingThreshold - $cart->total) }} more to get free delivery.</p>
                    @endif
                    <hr>
                    <div class="d-flex justify-content-between mb-3">
                        <span class="fw-semibold">Total</span>
                        <span class="fw-semibold text-terracotta fs-5">Rs. {{ number_format($cart->total + $shippingFee) }}</span>
                    </div>
                    <button type="submit" class="btn btn-terracotta w-100">Place Order</button>
                </div>
            </div>
        </div>
    </form>
</div>

@endsection
