@extends('admin.layouts.app')

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

@section('content')
<div class="page-shell">
    <div class="page-head">
        <div>
            <h1 class="page-title">Advanced Purchase System</h1>
            <div class="page-subtitle">Multi-item purchase, supplier payment and stock-in.</div>
        </div>
        <a href="{{ route('admin.purchases.create') }}" class="btn primary">Add Purchase</a>
    </div>

    <form class="filter-card" method="GET" action="{{ route('admin.purchases.index') }}">
        <div class="grid-4">
            <div>
                <label for="q">Search</label>
                <input type="text" id="q" name="q" value="{{ request('q') }}" placeholder="invoice / supplier / note">
            </div>

            <div class="form-actions" style="align-items:end;">
                <button class="btn primary" type="submit">Search</button>
                <a href="{{ route('admin.purchases.index') }}" class="btn btn-light">Reset</a>
            </div>
        </div>
    </form>

    <div class="card">
        <div class="table-wrap">
            <table class="table">
                <thead>
                    <tr>
                        <th>Invoice</th>
                        <th>Date</th>
                        <th>Supplier</th>
                        <th>Subtotal</th>
                        <th>Grand Total</th>
                        <th>Paid</th>
                        <th>Due</th>
                        <th class="text-end">Action</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($purchases as $purchase)
                        <tr>
                            <td>{{ $purchase->invoice_no }}</td>
                            <td>{{ $purchase->purchase_date }}</td>
                            <td>{{ $purchase->supplier_name }}</td>
                            <td>৳ {{ number_format((float) $purchase->subtotal, 2) }}</td>
                            <td>৳ {{ number_format((float) $purchase->grand_total, 2) }}</td>
                            <td>৳ {{ number_format((float) $purchase->paid_amount, 2) }}</td>
                            <td>
                                @if(($purchase->due_amount ?? 0) > 0)
                                    <span class="badge" style="background:#fee2e2;color:#991b1b;">৳ {{ number_format((float) $purchase->due_amount, 2) }}</span>
                                @else
                                    <span class="badge" style="background:#dcfce7;color:#166534;">৳ {{ number_format((float) $purchase->due_amount, 2) }}</span>
                                @endif
                            </td>
                            <td class="text-end" style="white-space:nowrap;">
                                @if(Route::has('admin.purchases.show'))
                                    <a class="btn btn-light" href="{{ route('admin.purchases.show', $purchase) }}">View</a>
                                @endif
                                <a class="btn btn-light" href="{{ route('admin.purchases.edit', $purchase) }}">Edit</a>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="8">No purchase found.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <div style="margin-top:14px;">
            {{ $purchases->links() }}
        </div>
    </div>
</div>
@endsection