{% extends "base.html" %}
{% block title %}Suppliers & Purchases{% endblock %}
{% block page_heading %}Supplier & Purchase Management{% endblock %}
{% block content %}
<div class="row g-3">
  <div class="col-lg-4">
    <div class="card p-3 h-100">
      <h4>➕ Add Supplier</h4>
      <form method="post">
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
        <input type="hidden" name="action" value="add_supplier">
        <input class="form-control mb-2" name="supplier_name" placeholder="Supplier name" required>
        <input class="form-control mb-2" name="contact_person" placeholder="Contact person">
        <input class="form-control mb-2" name="phone" placeholder="Phone">
        <input class="form-control mb-2" name="email" placeholder="Email">
        <textarea class="form-control mb-2" name="address" placeholder="Address"></textarea>
        <textarea class="form-control mb-2" name="notes" placeholder="Notes"></textarea>
        <button class="btn btn-success w-100">Save Supplier</button>
      </form>
    </div>
  </div>
  <div class="col-lg-8">
    <div class="card p-3 h-100">
      <h4>🧾 Add Purchase Order</h4>
      <form method="post">
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
        <input type="hidden" name="action" value="add_purchase">
        <div class="row g-2">
          <div class="col-md-6"><select class="form-select" name="supplier_id"><option value="">Select supplier</option>{% for s in suppliers %}<option value="{{ s.id }}">{{ s.supplier_name }}</option>{% endfor %}</select></div>
          <div class="col-md-6"><input type="date" class="form-control" name="po_date"></div>
          <div class="col-md-6"><input class="form-control" name="item_name" placeholder="Item name" required></div>
          <div class="col-md-2"><input type="number" class="form-control" name="qty" placeholder="Qty"></div>
          <div class="col-md-4"><input type="number" step="0.01" class="form-control" name="product_cost" placeholder="Product cost"></div>
          <div class="col-md-4"><input type="number" step="0.01" class="form-control" name="shipping_cost" placeholder="Shipping"></div>
          <div class="col-md-4"><input type="number" step="0.01" class="form-control" name="customs_cost" placeholder="Customs"></div>
          <div class="col-md-4"><input type="number" step="0.01" class="form-control" name="other_cost" placeholder="Other"></div>
          <div class="col-md-4"><input type="number" step="0.01" class="form-control" name="amount_paid" placeholder="Amount paid"></div>
          <div class="col-md-4"><select class="form-select" name="status"><option>Pending</option><option>Partially Paid</option><option>Paid</option><option>Received</option><option>Cancelled</option></select></div>
          <div class="col-12"><textarea class="form-control" name="notes" placeholder="Notes"></textarea></div>
        </div>
        <button class="btn btn-primary mt-2">Save Purchase</button>
      </form>
    </div>
  </div>
</div>

<div class="card p-3 mt-4">
  <h4>🏭 Suppliers</h4>
  <div class="table-responsive"><table class="table table-striped table-hover align-middle"><thead><tr><th>Name</th><th>Contact</th><th>Phone</th><th>Email</th><th>Status</th><th></th></tr></thead><tbody>{% for s in suppliers %}<tr><td><strong>{{ s.supplier_name }}</strong></td><td>{{ s.contact_person or '-' }}</td><td>{{ s.phone or '-' }}</td><td>{{ s.email or '-' }}</td><td>{% if s.is_active is defined and not s.is_active %}<span class="badge bg-secondary">Inactive</span>{% else %}<span class="badge bg-success">Active</span>{% endif %}</td><td><a class="btn btn-sm btn-outline-primary" href="{{ url_for('supplier_detail', supplier_id=s.id) }}">Open</a></td></tr>{% else %}<tr><td colspan="6" class="text-center">No suppliers.</td></tr>{% endfor %}</tbody></table></div>
</div>

<div class="card p-3 mt-4">
  <h4>📦 Purchase Orders</h4>
  <div class="table-responsive"><table class="table table-striped table-bordered"><thead><tr><th>Date</th><th>Supplier</th><th>Item</th><th>Qty</th><th>Total</th><th>Unit</th><th>Paid</th><th>Outstanding</th><th>Status</th></tr></thead><tbody>{% for p in purchases %}<tr><td>{{ p.po_date }}</td><td>{% if p.supplier_id %}<a href="{{ url_for('supplier_detail', supplier_id=p.supplier_id) }}">{{ p.supplier_name or '-' }}</a>{% else %}-{% endif %}</td><td>{{ p.item_name }}</td><td>{{ p.qty }}</td><td>{{ '%.2f'|format(p.total_cost or 0) }}</td><td>{{ '%.2f'|format(p.unit_cost or 0) }}</td><td>{{ '%.2f'|format(p.amount_paid or 0) }}</td><td class="text-danger fw-bold">{{ '%.2f'|format((p.total_cost or 0) - (p.amount_paid or 0)) }}</td><td>{{ p.status }}</td></tr>{% else %}<tr><td colspan="9" class="text-center">No purchases.</td></tr>{% endfor %}</tbody></table></div>
</div>
{% endblock %}
