{% extends "base.html" %}
{% block title %}Customer Details{% endblock %}
{% block page_heading %}Customer Profile{% endblock %}
{% block content %}
<div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3">
  <h2 class="mb-0">👤 {{ customer.customer_name }}</h2>
  <a class="btn btn-outline-secondary" href="{{ url_for('manage_customers') }}">← Customers</a>
</div>
<div class="row g-3 mb-4">
  <div class="col-md-3"><div class="card p-3"><small>Orders</small><h3>{{ totals.order_count or 0 }}</h3></div></div>
  <div class="col-md-3"><div class="card p-3"><small>Total Sales</small><h3>{{ '%.2f'|format(totals.total_sales or 0) }}</h3></div></div>
  <div class="col-md-3"><div class="card p-3"><small>Cash Received</small><h3>{{ '%.2f'|format(totals.received or 0) }}</h3></div></div>
  <div class="col-md-3"><div class="card p-3"><small>Outstanding</small><h3 class="text-danger">{{ '%.2f'|format(totals.outstanding or 0) }}</h3></div></div>
</div>
<div class="row g-3">
  <div class="col-lg-5">
    <div class="card p-3 mb-3">
      <h4>Edit Customer</h4>
      <form method="post">
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><input type="hidden" name="action" value="update_customer">
        <input class="form-control mb-2" name="customer_name" value="{{ customer.customer_name }}" required>
        <input class="form-control mb-2" name="phone1" value="{{ customer.phone1 or '' }}" placeholder="Phone 1">
        <input class="form-control mb-2" name="phone2" value="{{ customer.phone2 or '' }}" placeholder="Phone 2">
        <input class="form-control mb-2" name="email" value="{{ customer.email or '' }}" placeholder="Email">
        <div class="row g-2"><div class="col-md-6"><input class="form-control mb-2" name="city" value="{{ customer.city or '' }}" placeholder="City"></div><div class="col-md-6"><select class="form-select mb-2" name="customer_type">{% for t in ['Retail','Wholesale','Service','Other'] %}<option {% if customer.customer_type==t %}selected{% endif %}>{{ t }}</option>{% endfor %}</select></div></div>
        <textarea class="form-control mb-2" name="address" placeholder="Address">{{ customer.address or '' }}</textarea>
        <textarea class="form-control mb-2" name="notes" placeholder="Notes">{{ customer.notes or '' }}</textarea>
        <select class="form-select mb-2" name="is_active"><option value="1" {% if customer.is_active %}selected{% endif %}>Active</option><option value="0" {% if not customer.is_active %}selected{% endif %}>Inactive</option></select>
        <button class="btn btn-primary">Update</button>
      </form>
    </div>
    <div class="card p-3 mb-3">
      <h4>Add Payment / Refund Note</h4>
      <form method="post">
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><input type="hidden" name="action" value="add_transaction">
        <div class="row g-2"><div class="col-md-6"><input type="date" class="form-control" name="trans_date"></div><div class="col-md-6"><select class="form-select" name="trans_type"><option>Payment</option><option>Refund</option><option>Credit Note</option><option>Adjustment</option></select></div></div>
        <input type="number" step="0.01" class="form-control my-2" name="amount" placeholder="Amount">
        <input type="number" class="form-control mb-2" name="order_id" placeholder="Order ID optional">
        <input class="form-control mb-2" name="reason" placeholder="Reason">
        <textarea class="form-control mb-2" name="notes" placeholder="Notes"></textarea>
        <button class="btn btn-success">Save Transaction</button>
      </form>
    </div>
    <div class="card p-3">
      <h4>Link Existing Order</h4>
      <form method="post" class="d-flex gap-2"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><input type="hidden" name="action" value="link_order"><input type="number" class="form-control" name="order_id" placeholder="Order ID"><button class="btn btn-outline-primary">Link</button></form>
    </div>
  </div>
  <div class="col-lg-7">
    <div class="card p-3 mb-3"><h4>Orders</h4><div class="table-responsive"><table class="table table-sm table-striped"><thead><tr><th>ID</th><th>Date</th><th>Invoice</th><th>Item</th><th>Total</th><th>Received</th></tr></thead><tbody>{% for o in orders %}<tr><td>{{ o.id }}</td><td>{{ o.order_date }}</td><td>{{ o.invoice_no }}</td><td>{{ o.item_name }}</td><td>{{ '%.2f'|format(o.total_price or 0) }}</td><td>{{ '%.2f'|format(o.cash_received or 0) }}</td></tr>{% else %}<tr><td colspan="6" class="text-center">No orders.</td></tr>{% endfor %}</tbody></table></div></div>
    <div class="card p-3 mb-3"><h4>Warranty / IMEI Devices</h4><div class="table-responsive"><table class="table table-sm table-striped"><thead><tr><th>IMEI</th><th>Serial</th><th>Status</th><th>Product Warranty</th><th>Battery Warranty</th></tr></thead><tbody>{% for d in devices %}<tr><td>{{ d.imei }}</td><td>{{ d.serial }}</td><td>{{ d.status }}</td><td>{{ d.product_warranty_expiry }}</td><td>{{ d.battery_warranty_expiry }}</td></tr>{% else %}<tr><td colspan="5" class="text-center">No devices.</td></tr>{% endfor %}</tbody></table></div></div>
    <div class="card p-3"><h4>Transactions</h4><div class="table-responsive"><table class="table table-sm table-striped"><thead><tr><th>Date</th><th>Type</th><th>Amount</th><th>Reason</th><th>User</th></tr></thead><tbody>{% for t in transactions %}<tr><td>{{ t.trans_date }}</td><td>{{ t.trans_type }}</td><td>{{ '%.2f'|format(t.amount or 0) }}</td><td>{{ t.reason }}</td><td>{{ t.created_by }}</td></tr>{% else %}<tr><td colspan="5" class="text-center">No transactions.</td></tr>{% endfor %}</tbody></table></div></div>
  </div>
</div>
{% endblock %}
