{% extends "base.html" %}
{% block title %}IMEI & Warranty{% endblock %}
{% block content %}
<style>.page-card{background:white;border-radius:16px;padding:1.5rem;margin-bottom:1.5rem;box-shadow:0 4px 10px rgba(0,0,0,.08)}.status{font-weight:700}</style>
<div class="page-card"><h2>📱 IMEI / Serial / Warranty / TRCSL Tracking</h2><p class="text-muted">Use TRCSL only for telecom products. For services or non-network items, keep TRCSL as Not Required.</p></div>
<div class="page-card">
<form method="post" class="row g-3">
  <input type="hidden" name="device_id" value="{{ edit_row.id if edit_row else '' }}">
  <div class="col-md-3"><label class="form-label">IMEI *</label><input name="imei" class="form-control" required value="{{ edit_row.imei if edit_row else '' }}"></div>
  <div class="col-md-3"><label class="form-label">Serial</label><input name="serial" class="form-control" value="{{ edit_row.serial if edit_row else '' }}"></div>
  <div class="col-md-3"><label class="form-label">Stock Item</label><select name="stock_id" class="form-select"><option value="">Manual / Service</option>{% for s in stocks %}<option value="{{ s.id }}" {% if edit_row and edit_row.stock_id==s.id %}selected{% endif %}>{{ s.stock_id }} - {{ s.item_name }}</option>{% endfor %}</select></div>
  <div class="col-md-3"><label class="form-label">Item Name</label><input name="item_name" class="form-control" value="{{ edit_row.item_name if edit_row else '' }}"></div>
  <div class="col-md-2"><label class="form-label">Status</label><select name="status" class="form-select">{% for st in statuses %}<option {% if edit_row and edit_row.status==st %}selected{% endif %}>{{ st }}</option>{% endfor %}</select></div>
  <div class="col-md-2"><label class="form-label">Product Warranty Expiry</label><input type="date" name="product_warranty_expiry" class="form-control" value="{{ edit_row.product_warranty_expiry if edit_row else '' }}"></div>
  <div class="col-md-2"><label class="form-label">Battery Warranty Expiry</label><input type="date" name="battery_warranty_expiry" class="form-control" value="{{ edit_row.battery_warranty_expiry if edit_row else '' }}"></div>
  <div class="col-md-2"><label class="form-label">Location</label><input name="location" class="form-control" value="{{ edit_row.location if edit_row else 'Shop' }}"></div>
  <div class="col-md-2"><label class="form-label">TRCSL Status</label><select name="trcsl_status" class="form-select">{% for st in trcsl_statuses %}<option {% if edit_row and edit_row.trcsl_status==st %}selected{% endif %}>{{ st }}</option>{% endfor %}</select></div>
  <div class="col-md-2 d-flex align-items-end"><div class="form-check"><input class="form-check-input" type="checkbox" name="trcsl_required" {% if edit_row and edit_row.trcsl_required %}checked{% endif %}><label class="form-check-label">TRCSL Required</label></div></div>
  <div class="col-md-3"><label class="form-label">TRCSL Ref</label><input name="trcsl_reference" class="form-control" value="{{ edit_row.trcsl_reference if edit_row else '' }}"></div>
  <div class="col-md-3"><label class="form-label">Import Batch</label><input name="import_batch" class="form-control" value="{{ edit_row.import_batch if edit_row else '' }}"></div>
  <div class="col-md-12"><button class="btn btn-success">{{ 'Update Device' if edit_row else 'Add Device' }}</button>{% if edit_row %}<a href="{{ url_for('manage_imei') }}" class="btn btn-secondary">Cancel</a>{% endif %}</div>
</form>
</div>
<div class="page-card"><form method="get" class="row g-2"><div class="col-md-4"><input name="q" class="form-control" value="{{ q }}" placeholder="Search IMEI / serial / item / batch"></div><div class="col-md-2"><select name="status" class="form-select"><option>All</option>{% for st in statuses %}<option {% if status_filter==st %}selected{% endif %}>{{ st }}</option>{% endfor %}</select></div><div class="col-md-2"><select name="trcsl_status" class="form-select"><option>All</option>{% for st in trcsl_statuses %}<option {% if trcsl_filter==st %}selected{% endif %}>{{ st }}</option>{% endfor %}</select></div><div class="col-md-2 form-check d-flex align-items-center"><input class="form-check-input me-2" type="checkbox" name="sellable_only" {% if sellable_only %}checked{% endif %}> Sellable only</div><div class="col-md-2"><button class="btn btn-primary w-100">Filter</button></div></form></div>
<div class="page-card"><div class="table-responsive"><table class="table table-hover"><thead><tr><th>IMEI</th><th>Serial</th><th>Item</th><th>Status</th><th>Product W.</th><th>Battery W.</th><th>TRCSL</th><th>Batch</th><th>Location</th><th>Action</th></tr></thead><tbody>{% for d in devices %}<tr><td>{{ d.imei }}</td><td>{{ d.serial }}</td><td>{{ d.item_name }}</td><td class="status">{{ d.status }}</td><td>{{ d.product_warranty_expiry }}</td><td>{{ d.battery_warranty_expiry }}</td><td>{{ d.trcsl_status }}</td><td>{{ d.import_batch }}</td><td>{{ d.location }}</td><td><a class="btn btn-sm btn-warning" href="{{ url_for('manage_imei', edit=d.id) }}">Edit</a> <a class="btn btn-sm btn-danger" onclick="return confirm('Delete?')" href="{{ url_for('manage_imei', delete=d.id) }}">Delete</a></td></tr>{% else %}<tr><td colspan="10" class="text-center text-muted">No devices</td></tr>{% endfor %}</tbody></table></div></div>
{% endblock %}
