{% extends "base.html" %}
{% block title %}Supplier Details{% endblock %}
{% block page_heading %}Supplier Profile{% endblock %}
{% block content %}
<div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3"><h2 class="mb-0">🏭 {{ supplier.supplier_name }}</h2><a class="btn btn-outline-secondary" href="{{ url_for('suppliers') }}">← Suppliers</a></div>
{% set extra_paid = pay_total.total or 0 %}
{% set outstanding = (totals.purchase_total or 0) - (totals.po_paid or 0) - extra_paid %}
<div class="row g-3 mb-4">
  <div class="col-md-3"><div class="card p-3"><small>Purchase Total</small><h3>{{ '%.2f'|format(totals.purchase_total or 0) }}</h3></div></div>
  <div class="col-md-3"><div class="card p-3"><small>PO Paid</small><h3>{{ '%.2f'|format(totals.po_paid or 0) }}</h3></div></div>
  <div class="col-md-3"><div class="card p-3"><small>Extra Payments</small><h3>{{ '%.2f'|format(extra_paid) }}</h3></div></div>
  <div class="col-md-3"><div class="card p-3"><small>Outstanding</small><h3 class="text-danger">{{ '%.2f'|format(outstanding) }}</h3></div></div>
</div>
<div class="row g-3">
  <div class="col-lg-5">
    <div class="card p-3 mb-3"><h4>Edit Supplier</h4><form method="post"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><input type="hidden" name="action" value="update_supplier"><input class="form-control mb-2" name="supplier_name" value="{{ supplier.supplier_name }}" required><input class="form-control mb-2" name="contact_person" value="{{ supplier.contact_person or '' }}" placeholder="Contact person"><input class="form-control mb-2" name="phone" value="{{ supplier.phone or '' }}" placeholder="Phone"><input class="form-control mb-2" name="email" value="{{ supplier.email or '' }}" placeholder="Email"><textarea class="form-control mb-2" name="address" placeholder="Address">{{ supplier.address or '' }}</textarea><textarea class="form-control mb-2" name="notes" placeholder="Notes">{{ supplier.notes or '' }}</textarea><select class="form-select mb-2" name="is_active"><option value="1" {% if supplier.is_active %}selected{% endif %}>Active</option><option value="0" {% if not supplier.is_active %}selected{% endif %}>Inactive</option></select><button class="btn btn-primary">Update Supplier</button></form></div>
    <div class="card p-3"><h4>Add Supplier Payment</h4><form method="post"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><input type="hidden" name="action" value="add_payment"><input type="date" class="form-control mb-2" name="payment_date"><input type="number" step="0.01" class="form-control mb-2" name="amount" placeholder="Amount"><select class="form-select mb-2" name="payment_method"><option>Cash</option><option>Bank Transfer</option><option>Card</option><option>Other</option></select><input class="form-control mb-2" name="reference_no" placeholder="Reference no"><textarea class="form-control mb-2" name="notes" placeholder="Notes"></textarea><button class="btn btn-success">Save Payment</button></form></div>
  </div>
  <div class="col-lg-7">
    <div class="card p-3 mb-3"><h4>Purchase Orders</h4><div class="table-responsive"><table class="table table-sm table-striped"><thead><tr><th>Date</th><th>Item</th><th>Qty</th><th>Total</th><th>Paid</th><th>Outstanding</th><th>Status</th></tr></thead><tbody>{% for p in purchases %}<tr><td>{{ p.po_date }}</td><td>{{ p.item_name }}</td><td>{{ p.qty }}</td><td>{{ '%.2f'|format(p.total_cost or 0) }}</td><td>{{ '%.2f'|format(p.amount_paid or 0) }}</td><td>{{ '%.2f'|format((p.total_cost or 0)-(p.amount_paid or 0)) }}</td><td>{{ p.status }}</td></tr>{% else %}<tr><td colspan="7" class="text-center">No purchases.</td></tr>{% endfor %}</tbody></table></div></div>
    <div class="card p-3"><h4>Payments</h4><div class="table-responsive"><table class="table table-sm table-striped"><thead><tr><th>Date</th><th>Amount</th><th>Method</th><th>Reference</th><th>User</th></tr></thead><tbody>{% for p in payments %}<tr><td>{{ p.payment_date }}</td><td>{{ '%.2f'|format(p.amount or 0) }}</td><td>{{ p.payment_method }}</td><td>{{ p.reference_no }}</td><td>{{ p.created_by }}</td></tr>{% else %}<tr><td colspan="5" class="text-center">No payments.</td></tr>{% endfor %}</tbody></table></div></div>
  </div>
</div>
{% endblock %}
