{% extends 'core/base.html' %}
{% load currency_filters %}
{% load static %}

{% block title %}Customers — SwiftPOS{% endblock %}
{% block topbar_title %}Customers{% endblock %}

{% block extra_css %}
<style>
.cust-tab-bar { display:flex; gap:0.5rem; border-bottom:2px solid var(--sp-border,#e5e7eb); margin-bottom:1.25rem; }
.cust-tab {
    padding:0.55rem 1.2rem; border-radius:10px 10px 0 0;
    font-size:0.82rem; font-weight:600; cursor:pointer;
    border:2px solid transparent; border-bottom:none;
    color:var(--sp-text-muted,#6b7280); background:transparent;
    transition:all .18s;
}
.cust-tab.active {
    background:var(--sp-primary,#4f46e5); color:#fff;
    border-color:var(--sp-primary,#4f46e5);
}
.cust-stat-card { border-radius:14px; padding:1rem 1.25rem; }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid px-0">

  {% if messages %}
  <div class="mb-3">
    {% for message in messages %}
    <div class="alert alert-{{ message.tags|default:'info' }} alert-dismissible fade show" role="alert">
      {{ message }}
      <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    {% endfor %}
  </div>
  {% endif %}

  <!-- Header -->
    <div class="sp-page-header">
        <div>
            <h1 class="sp-page-title"><i class="fas fa-users"></i>Customers</h1>
            <p class="sp-page-subtitle">Manage registered customers, credit accounts &amp; payment history.</p>
        </div>
        <a href="{% url 'sales:customer_create' %}" class="btn btn-primary btn-sm d-flex align-items-center gap-2">
            <i class="fas fa-user-plus"></i> New Customer
        </a>
    </div>

  <!-- Summary cards -->
  <div class="row g-3 mb-3">
    <div class="col-6 col-md-3">
      <div class="card cust-stat-card shadow-sm sp-animate-card">
        <div class="d-flex justify-content-between align-items-center">
          <div>
            <div class="small text-muted text-uppercase fw-semibold mb-1">Total Customers</div>
            <div class="fw-bold fs-4">{{ customers.count }}</div>
          </div>
          <div class="rounded-circle d-flex align-items-center justify-content-center"
               style="width:42px;height:42px;background:rgba(79,70,229,.1);color:var(--sp-primary);flex-shrink:0;">
            <i class="fas fa-users"></i>
          </div>
        </div>
      </div>
    </div>
    <div class="col-6 col-md-3">
      <div class="card cust-stat-card shadow-sm sp-animate-card">
        <div class="d-flex justify-content-between align-items-center">
          <div>
            <div class="small text-muted text-uppercase fw-semibold mb-1">On Credit</div>
            <div class="fw-bold fs-4 text-danger">{{ total_credit_customers }}</div>
          </div>
          <div class="rounded-circle d-flex align-items-center justify-content-center"
               style="width:42px;height:42px;background:rgba(220,38,38,.1);color:#dc2626;flex-shrink:0;">
            <i class="fas fa-exclamation-circle"></i>
          </div>
        </div>
      </div>
    </div>
    <div class="col-6 col-md-3">
      <div class="card cust-stat-card shadow-sm sp-animate-card">
        <div class="d-flex justify-content-between align-items-center">
          <div>
            <div class="small text-muted text-uppercase fw-semibold mb-1">Total Outstanding</div>
            <div class="fw-bold fs-5 text-danger">{% money total_outstanding %}</div>
          </div>
          <div class="rounded-circle d-flex align-items-center justify-content-center"
               style="width:42px;height:42px;background:rgba(220,38,38,.1);color:#dc2626;flex-shrink:0;">
            <i class="fas fa-hand-holding-usd"></i>
          </div>
        </div>
      </div>
    </div>
    <div class="col-6 col-md-3">
      <div class="card cust-stat-card shadow-sm sp-animate-card">
        <div class="d-flex justify-content-between align-items-center">
          <div>
            <div class="small text-muted text-uppercase fw-semibold mb-1">Paid Recently</div>
            <div class="fw-bold fs-4 text-success">{{ payment_history.count }}</div>
          </div>
          <div class="rounded-circle d-flex align-items-center justify-content-center"
               style="width:42px;height:42px;background:rgba(34,197,94,.12);color:#16a34a;flex-shrink:0;">
            <i class="fas fa-check-circle"></i>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- Search bar -->
  <form method="get" class="mb-3 d-flex gap-2">
    <input type="hidden" name="tab" value="{{ tab }}">
    <input type="text" name="q" class="form-control" placeholder="🔍  Search by name, phone or email…" value="{{ q }}" style="border-radius:10px; max-width:400px;">
    <button class="btn btn-outline-secondary btn-sm" type="submit" style="border-radius:10px;">Search</button>
    {% if q %}<a href="?tab={{ tab }}" class="btn btn-link btn-sm">Clear</a>{% endif %}
  </form>

  <!-- Tab bar -->
  <div class="cust-tab-bar">
    <a href="?tab=all{% if q %}&q={{ q }}{% endif %}" class="cust-tab {% if tab == 'all' %}active{% endif %}">
      <i class="fas fa-users me-1"></i> All Customers
      <span class="badge bg-secondary ms-1 rounded-pill" style="font-size:.65rem;">{{ customers.count }}</span>
    </a>
    <a href="?tab=credit{% if q %}&q={{ q }}{% endif %}" class="cust-tab {% if tab == 'credit' %}active{% endif %}">
      <i class="fas fa-exclamation-circle me-1"></i> Pending Payments
      <span class="badge bg-danger ms-1 rounded-pill" style="font-size:.65rem;">{{ total_credit_customers }}</span>
    </a>
    <a href="?tab=history{% if q %}&q={{ q }}{% endif %}" class="cust-tab {% if tab == 'history' %}active{% endif %}">
      <i class="fas fa-history me-1"></i> Payment History
    </a>
  </div>

  <!-- ══ TAB: All Customers ══ -->
  {% if tab == 'all' %}
  <div class="card">
    <div class="card-body p-0">
      {% if customers %}
      <div class="sp-table-container">
        <table class="table table-hover align-middle mb-0">
          <thead class="table-light">
            <tr>
              <th>Name</th>
              <th>Phone</th>
              <th>Email</th>
              <th>Orders</th>
              <th>Total Spent</th>
              <th>Outstanding</th>
              <th></th>
            </tr>
          </thead>
          <tbody>
            {% for c in customers %}
            <tr>
              <td class="fw-semibold">{{ c.name }}</td>
              <td class="text-muted small">{{ c.phone|default:"—" }}</td>
              <td class="text-muted small">{{ c.email|default:"—" }}</td>
              <td>{{ c.total_orders }}</td>
              <td>{% money c.total_spent %}</td>
              <td>
                {% if c.outstanding_balance > 0 %}
                  <span class="sp-badge-danger">{% money c.outstanding_balance %}</span>
                {% else %}
                  <span class="sp-badge-success">Clear</span>
                {% endif %}
              </td>
              <td>
                <a href="{% url 'sales:customer_detail' c.pk %}" class="btn btn-sm btn-outline-primary" style="border-radius:8px;font-size:.75rem;">View</a>
                <a href="{% url 'sales:customer_edit' c.pk %}" class="btn btn-sm btn-outline-secondary ms-1" style="border-radius:8px;font-size:.75rem;">Edit</a>
              </td>
            </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
      {% else %}
      <div class="text-center py-5 text-muted">
        <i class="fas fa-users fa-2x opacity-25 d-block mb-2"></i>
        <small>{% if q %}No customers match "{{ q }}"{% else %}No customers yet — <a href="{% url 'sales:customer_create' %}">Add one</a>{% endif %}</small>
      </div>
      {% endif %}
    </div>
  </div>
  {% endif %}

  <!-- ══ TAB: Pending Payments (Credit) ══ -->
  {% if tab == 'credit' %}
  <div class="card mb-3">
    <div class="card-header">
      <h6 class="mb-0 fw-bold"><i class="fas fa-exclamation-triangle text-warning me-2"></i>Customers with Outstanding Balances</h6>
    </div>
    <div class="card-body p-0">
      {% if credit_summary %}
      <div class="sp-table-container">
        <table class="table table-hover align-middle mb-0">
          <thead class="table-light">
            <tr>
              <th>Customer</th>
              <th>Phone</th>
              <th>Orders</th>
              <th>Total Owed</th>
              <th>Action</th>
            </tr>
          </thead>
          <tbody>
            {% for c in credit_summary %}
            <tr>
              <td class="fw-semibold">{{ c.customer_name|default:"Anonymous" }}</td>
              <td class="text-muted small">{{ c.customer_phone|default:"—" }}</td>
              <td>{{ c.orders_count }}</td>
              <td><span class="sp-badge-danger">{% money c.total_owed %}</span></td>
              <td>
                <a href="?tab=history&q={{ c.customer_name }}" class="btn btn-sm btn-outline-info" style="border-radius:8px;font-size:.75rem;">
                  <i class="fas fa-history me-1"></i>History
                </a>
              </td>
            </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
      {% else %}
      <div class="text-center py-5 text-muted">
        <i class="fas fa-check-circle fa-2x opacity-25 d-block mb-2 text-success"></i>
        <small>No outstanding balances {% if q %}matching "{{ q }}"{% endif %} 🎉</small>
      </div>
      {% endif %}
    </div>
  </div>

  <!-- Individual credit orders -->
  {% if credit_orders %}
  <div class="card">
    <div class="card-header">
      <h6 class="mb-0 fw-semibold small text-muted">Individual Credit Orders</h6>
    </div>
    <div class="card-body p-0">
      <div class="sp-table-container">
        <table class="table table-sm table-hover align-middle mb-0">
          <thead class="table-light">
            <tr>
              <th>Order #</th>
              <th>Customer</th>
              <th>Date</th>
              <th>Total</th>
              <th>Paid</th>
              <th>Balance</th>
              <th>Status</th>
              <th></th>
            </tr>
          </thead>
          <tbody>
            {% for o in credit_orders %}
            <tr>
              <td class="font-monospace small">{{ o.order_number }}</td>
              <td>{{ o.customer_name|default:"—" }}</td>
              <td class="text-muted small">{{ o.created_at|date:"d M Y" }}</td>
              <td>{% money o.final_amount %}</td>
              <td>{% money o.amount_paid %}</td>
              <td><span class="sp-badge-danger">{% money o.balance_amount %}</span></td>
              <td>
                {% if o.payment_status == 'credit' %}<span class="sp-badge-danger">Credit</span>
                {% else %}<span class="sp-badge-warning">Partial</span>{% endif %}
              </td>
              <td>
                <div class="d-flex align-items-center gap-1">
                  <a href="{% url 'sales:print_receipt' o.pk %}" class="btn btn-sm btn-outline-secondary" style="border-radius:8px;font-size:.72rem;">View</a>
                  {% if o.balance_amount > 0 %}
                  <a href="{% url 'sales:clear_credit' o.pk %}" class="btn btn-sm btn-outline-success" style="border-radius:8px;font-size:.72rem;" onclick="return confirm('Mark order {{ o.order_number }} balance of {% money o.balance_amount %} as paid?');">Clear</a>
                  {% endif %}
                </div>
              </td>
            </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
    </div>
  </div>
  {% endif %}
  {% endif %}

  <!-- ══ TAB: Payment History ══ -->
  {% if tab == 'history' %}
  <div class="card">
    <div class="card-header">
      <h6 class="mb-0 fw-bold"><i class="fas fa-history text-success me-2"></i>Completed Payments</h6>
    </div>
    <div class="card-body p-0">
      {% if payment_history %}
      <div class="sp-table-container">
        <table class="table table-hover align-middle mb-0">
          <thead class="table-light">
            <tr>
              <th>Order #</th>
              <th>Customer</th>
              <th>Phone</th>
              <th>Date Paid</th>
              <th>Amount</th>
              <th>Method</th>
              <th></th>
            </tr>
          </thead>
          <tbody>
            {% for o in payment_history %}
            <tr>
              <td class="font-monospace small">{{ o.order_number }}</td>
              <td class="fw-semibold">{{ o.customer_name }}</td>
              <td class="text-muted small">{{ o.customer_phone|default:"—" }}</td>
              <td class="text-muted small">{{ o.updated_at|date:"d M Y" }}</td>
              <td class="text-success fw-bold">{% money o.final_amount %}</td>
              <td><span class="badge bg-light text-dark border">{{ o.get_payment_method_display }}</span></td>
              <td>
                <a href="{% url 'sales:print_receipt' o.pk %}" class="btn btn-sm btn-outline-secondary" style="border-radius:8px;font-size:.72rem;">View</a>
              </td>
            </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
      {% else %}
      <div class="text-center py-5 text-muted">
        <i class="fas fa-history fa-2x opacity-25 d-block mb-2"></i>
        <small>No payment history{% if q %} for "{{ q }}"{% endif %}</small>
      </div>
      {% endif %}
    </div>
  </div>
  {% endif %}

</div>
{% endblock %}
