@extends('layouts.app')
@section('title', $supplier->name)
@section('content')
<div class="d-flex justify-content-between align-items-center mb-3">
    <h4>{{ $supplier->name }}</h4>
    <div>
        <a href="{{ route('suppliers.pay', $supplier) }}" class="btn btn-success btn-sm">Make Payment</a>
        <a href="{{ route('suppliers.index') }}" class="btn btn-secondary btn-sm">Back</a>
    </div>
</div>

<div class="row g-3 mb-4">
    <div class="col-md-4">
        <div class="card">
            <div class="card-body">
                <div class="text-muted small">Total Purchases</div>
                <div class="fs-5 fw-bold">Rs. {{ number_format($supplier->purchases->sum('total_amount'), 2) }}</div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card">
            <div class="card-body">
                <div class="text-muted small">Total Paid</div>
                <div class="fs-5 fw-bold text-success">Rs. {{ number_format($supplier->payments->sum('amount'), 2) }}</div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card">
            <div class="card-body">
                <div class="text-muted small">Balance Due</div>
                <div class="fs-5 fw-bold text-danger">Rs. {{ number_format($supplier->balance, 2) }}</div>
            </div>
        </div>
    </div>
</div>

<div class="card mb-4">
    <div class="card-header">Purchase History</div>
    <div class="card-body p-0">
        <table class="table table-sm mb-0">
            <thead><tr><th>#</th><th>Date</th><th>Items</th><th>Total</th><th>Paid</th><th></th></tr></thead>
            <tbody>
            @forelse($supplier->purchases as $purchase)
                <tr>
                    <td>{{ $purchase->id }}</td>
                    <td>{{ $purchase->date->format('d M Y') }}</td>
                    <td>
                        @foreach($purchase->items as $item)
                            {{ $item->product->category->name ?? '' }} {{ $item->product->weight->label ?? '' }} ({{ $item->bags }} bags){{ !$loop->last ? ', ' : '' }}
                        @endforeach
                    </td>
                    <td>Rs. {{ number_format($purchase->total_amount, 2) }}</td>
                    <td>Rs. {{ number_format($purchase->paid_amount, 2) }}</td>
                    <td><a href="{{ route('purchases.show', $purchase) }}" class="btn btn-sm btn-outline-info">View</a></td>
                </tr>
            @empty
                <tr><td colspan="6" class="text-center text-muted">No purchases yet</td></tr>
            @endforelse
            </tbody>
        </table>
    </div>
</div>

<div class="card">
    <div class="card-header">Payment History</div>
    <div class="card-body p-0">
        <table class="table table-sm mb-0">
            <thead><tr><th>#</th><th>Date</th><th>Amount</th><th>Notes</th></tr></thead>
            <tbody>
            @forelse($supplier->payments as $payment)
                <tr>
                    <td>{{ $payment->id }}</td>
                    <td>{{ $payment->date->format('d M Y') }}</td>
                    <td>Rs. {{ number_format($payment->amount, 2) }}</td>
                    <td>{{ $payment->notes ?: '-' }}</td>
                </tr>
            @empty
                <tr><td colspan="4" class="text-center text-muted">No payments yet</td></tr>
            @endforelse
            </tbody>
        </table>
    </div>
</div>
@endsection
