{% extends 'base.html' %}
{% block content %}
  <h2>Supplier Accounts</h2>
  <div class="card" style="margin-top:8px">
    <table class="table supplier-accounts-table">
      <thead>
        <tr>
          <th>Supplier Name</th>
          <th>Email</th>
          <th>Company</th>
          <th>Phone</th>
          <th class="highlight-balance">Payable Amount</th>
          <th>Manager</th>
          <th>Actions</th>
        </tr>
      </thead>
      <tbody>
        {% for s in suppliers %}
          <tr>
            <td>{{ s.name }}</td>
            <td>{{ s.email }}</td>
            <td>{{ s.company_name or '-' }}</td>
            <td>{{ s.phone or '-' }}</td>
            <td class="highlight-balance">{{ (balances[s.id] if balances and s.id in balances else 0)|currency }}</td>
            <td>
              {% if s.parent_id %}
                {% set mgr = (suppliers | selectattr('id', 'equalto', s.parent_id) | list | first) %}
                {{ mgr.name if mgr else 'Admin' }}
              {% else %}
                -
              {% endif %}
            </td>
            <td class="actions">
              <a class="btn-outline" href="{{ url_for('supplier_ledger', supplier_id=s.id) }}">Ledger</a>
              <a class="btn-primary" href="{{ url_for('supplier_payment', supplier_id=s.id) }}">Pay</a>
            </td>
          </tr>
        {% else %}
          <tr><td colspan="7" class="muted">No suppliers found.</td></tr>
        {% endfor %}
      </tbody>
      <tfoot>
        <tr class="total-row">
          <th colspan="4">Total Payable</th>
          <th class="highlight-balance grand-total">{{ (balances.values() | sum)|currency }}</th>
          <th colspan="2"></th>
        </tr>
      </tfoot>
    </table>
  </div>
{% endblock %}
