@extends('admin.layouts.app')

@section('title', 'Edit Purchase')
@section('page_title', 'Edit Purchase')

@section('content')
@php
    $suppliers = $suppliers ?? collect();
    $products = $products ?? collect();
@endphp

<div class="page-shell">
    <div class="page-head">
        <div>
            <h1 class="page-title">Edit Purchase</h1>
            <div class="page-subtitle">Update purchase details without breaking supplier and stock records.</div>
        </div>
        <a href="{{ route('admin.purchases.index') }}" class="btn btn-light">Back to List</a>
    </div>

    <form method="POST" action="{{ route('admin.purchases.update', $purchase) }}">
        @csrf
        @method('PUT')

        <div class="grid-3">
            <div class="card">
                <label for="supplier_name">Supplier</label>
                <select id="supplier_name" name="supplier_name">
                    <option value="">Select Supplier</option>
                    @foreach($suppliers as $supplier)
                        <option value="{{ $supplier->name }}" @selected(old('supplier_name', $purchase->supplier_name) == $supplier->name)>{{ $supplier->name }}</option>
                    @endforeach
                </select>
            </div>

            <div class="card">
                <label for="purchase_date">Purchase Date</label>
                <input type="date" id="purchase_date" name="purchase_date" value="{{ old('purchase_date', $purchase->purchase_date) }}">
            </div>

            <div class="card">
                <label for="invoice_no">Invoice No</label>
                <input type="text" id="invoice_no" name="invoice_no" value="{{ old('invoice_no', $purchase->invoice_no) }}">
            </div>

            <div class="card">
                <label for="product_name">Product</label>
                <select id="product_name" name="product_name">
                    <option value="">Select Product</option>
                    @foreach($products as $product)
                        @php $productName = $product->product_name ?? $product->name; @endphp
                        <option value="{{ $productName }}" @selected(old('product_name', $purchase->product_name) == $productName)>{{ $productName }}</option>
                    @endforeach
                </select>
            </div>

            <div class="card">
                <label for="quantity">Quantity</label>
                <input type="number" id="quantity" name="quantity" value="{{ old('quantity', $purchase->quantity) }}" min="1">
            </div>

            <div class="card">
                <label for="unit_price">Unit Price</label>
                <input type="number" step="0.01" id="unit_price" name="unit_price" value="{{ old('unit_price', $purchase->unit_price) }}">
            </div>

            <div class="card">
                <label for="paid_amount">Paid Amount</label>
                <input type="number" step="0.01" id="paid_amount" name="paid_amount" value="{{ old('paid_amount', $purchase->paid_amount ?? 0) }}">
            </div>

            <div class="card">
                <label for="payment_method">Payment Method</label>
                <select id="payment_method" name="payment_method">
                    <option value="Cash" @selected(old('payment_method', $purchase->payment_method) == 'Cash')>Cash</option>
                    <option value="Bank" @selected(old('payment_method', $purchase->payment_method) == 'Bank')>Bank</option>
                    <option value="Mobile Banking" @selected(old('payment_method', $purchase->payment_method) == 'Mobile Banking')>Mobile Banking</option>
                    <option value="Card" @selected(old('payment_method', $purchase->payment_method) == 'Card')>Card</option>
                    <option value="Due" @selected(old('payment_method', $purchase->payment_method) == 'Due')>Due</option>
                </select>
            </div>

            <div class="card">
                <label for="status">Status</label>
                <select id="status" name="status">
                    <option value="Completed" @selected(old('status', $purchase->status ?? 'Completed') == 'Completed')>Completed</option>
                    <option value="Pending" @selected(old('status', $purchase->status) == 'Pending')>Pending</option>
                </select>
            </div>
        </div>

        <div class="grid-2" style="margin-top:12px;">
            <div class="card">
                <label for="notes">Notes</label>
                <textarea id="notes" name="notes" rows="5">{{ old('notes', $purchase->notes) }}</textarea>
            </div>

            <div class="card">
                <div class="page-subtitle" style="margin-bottom:12px;">Purchase Summary</div>
                <div class="stat">
                    <div class="k">Current Invoice</div>
                    <div class="v" style="font-size:16px;">{{ $purchase->invoice_no ?: 'N/A' }}</div>
                </div>

                <div class="form-actions">
                    <button type="submit" class="btn primary">Update Purchase</button>
                    <a href="{{ route('admin.purchases.index') }}" class="btn btn-light">Cancel</a>
                </div>
            </div>
        </div>
    </form>
</div>
@endsection