<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Invoice #{{ order.invoice_no or order.id }}</title>
  <style>
    {% if print_size == "thermal80" %}.page{width:80mm!important;min-height:140mm!important;}{% elif print_size == "thermal58" %}.page{width:58mm!important;min-height:120mm!important;font-size:90%;}{% elif print_size == "a5" %}.page{width:148mm!important;height:210mm!important;}{% else %}.page{width:105mm!important;height:148mm!important;}{% endif %}
    *{box-sizing:border-box}body{font-family:Arial,sans-serif;margin:0;padding:0;color:#000}.page{padding:4mm;margin:0 auto;background:#fff;position:relative}.watermark{position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);font-size:70px;font-weight:900;color:rgba(0,0,0,.04);z-index:0}.page>*:not(.watermark){position:relative;z-index:1}@media screen{body{background:#ddd;padding:20px 0}.page{box-shadow:0 0 10px rgba(0,0,0,.25);margin-bottom:20px}}@media print{body{margin:0}.page{page-break-after:always;margin:0}.page:last-child{page-break-after:auto}.no-print{display:none!important}}
    .actions{display:flex;gap:10px;justify-content:center;margin:18px 0}.actions button,.actions a{padding:10px 16px;border:0;border-radius:8px;background:#0f766e;color:#fff;text-decoration:none;cursor:pointer}.actions a{background:#334155}.company{text-align:center;border-bottom:1px solid #000;padding-bottom:2mm;margin-bottom:2mm}.company h1{font-size:11px;margin:0}.company p{font-size:7px;line-height:1.25;margin:1mm 0 0}.title{text-align:center;font-size:15px;font-weight:800;letter-spacing:.5px;margin:2mm 0}.info{font-size:8px;line-height:1.45;margin:2mm 0;display:grid;grid-template-columns:1fr 1fr;gap:1mm 3mm}.info strong{display:inline-block;min-width:21mm}table{width:100%;border-collapse:collapse;margin:2mm 0;font-size:7.5px}th,td{border:.5px solid #000;padding:1mm;vertical-align:top}th{background:#f0f0f0;text-align:left}.center{text-align:center}.right{text-align:right}.badge{display:inline-block;padding:.5mm 1mm;border-radius:2mm;font-size:6px;background:#e5e7eb}.service{background:#dbeafe}.product{background:#dcfce7}.pricing{font-size:9px;margin-top:2mm;border-top:1px solid #000;padding-top:1.5mm}.row{display:flex;justify-content:space-between;margin:1mm 0}.total{font-weight:800;font-size:10px;border-top:.5px solid #000;padding-top:1mm}.warranty{font-size:7px;margin-top:2mm;border-top:.5px solid #ccc;padding-top:1.5mm}.footer{text-align:center;font-size:7px;margin-top:3mm}.terms-page{font-size:6px}.terms-page h2{text-align:center;font-size:9px;border-bottom:1px solid #000;padding-bottom:1mm}.terms-page h3{font-size:7px;margin:1.5mm 0 .8mm}.terms-page li{margin-bottom:.8mm;text-align:justify}.notice{border:.5px solid #000;background:#f9f9f9;padding:1.5mm;text-align:center;margin-top:2mm}
  </style>
</head>
<body>
<div class="actions no-print">
  <button onclick="window.print()">🖨️ Print</button>
  <button onclick="downloadText()">📱 WhatsApp Text</button>
  <a href="{{ url_for('invoice_options', order_id=order.id) }}">Print Size</a>
</div>

<div class="page invoice-page">
  <div class="watermark">3ES</div>
  <div class="company"><h1>360 EDGE SOLUTIONS (PRIVATE) LIMITED</h1><p>29, E. A. Paulas, MW, Kandewatta, Galle<br>Tel: 070 202 2001 | 091 422 5424<br>info@360edgesolutions.lk</p></div>
  <div class="title">INVOICE</div>
  <div class="info">
    <div><strong>Invoice No:</strong> {{ order.invoice_no or order.id }}</div><div><strong>Date:</strong> {{ order.order_date }}</div>
    <div><strong>Order ID:</strong> {{ order.order_id or '-' }}</div><div><strong>Contact:</strong> {{ order.contact1 or '-' }}</div>
    <div><strong>Alt Contact:</strong> {{ order.contact2 or '-' }}</div><div><strong>Order Via:</strong> {{ order.order_via or '-' }}</div>
    <div><strong>Store:</strong> {{ order.store or '-' }}</div><div><strong>Items:</strong> {{ totals.product_count }} products / {{ totals.service_count }} services</div>
  </div>
  <table>
    <thead><tr><th>Item / Service</th><th class="center">Type</th><th class="center">IMEI / Serial</th><th class="center">Qty</th><th class="right">Unit</th><th class="right">Total</th><th>Warranty</th></tr></thead>
    <tbody>
      {% for item in items %}
      <tr>
        <td>{{ item.item_name }}{% if item.notes %}<br><small>{{ item.notes }}</small>{% endif %}</td>
        <td class="center"><span class="badge {{ item.item_type }}">{{ item.item_type|title }}</span></td>
        <td class="center">{% if item.imei %}IMEI: {{ item.imei }}{% endif %}{% if item.serial %}<br>SN: {{ item.serial }}{% endif %}</td>
        <td class="center">{{ item.qty }}</td>
        <td class="right">{{ "{:,.2f}".format(item.unit_price or 0) }}</td>
        <td class="right">{{ "{:,.2f}".format(item.total_price or 0) }}</td>
        <td>{% if item.item_type == 'service' %}Service warranty: {{ item.product_warranty_days or 0 }} days{% else %}Product: {{ item.product_warranty_days or 0 }} days<br>Battery: {{ item.battery_warranty_days or 0 }} days{% endif %}</td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
  <div class="pricing"><div class="row"><span>Subtotal:</span><span>LKR {{ "{:,.2f}".format(totals.total) }}</span></div><div class="row total"><span>Total:</span><span>LKR {{ "{:,.2f}".format(totals.total) }}</span></div><div class="row"><span>Cash Received:</span><span>LKR {{ "{:,.2f}".format(order.cash_received or 0) }}</span></div><div class="row"><span>Balance:</span><span>LKR {{ "{:,.2f}".format((totals.total or 0) - (order.cash_received or 0)) }}</span></div></div>
  <div class="warranty"><strong>Warranty Note:</strong> Each product/service warranty is shown per invoice line. Service warranty is optional. Network speed/signal depends on service provider and is not guaranteed by 360 EDGE SOLUTIONS (PVT) LTD.</div>
  <div class="footer"><strong>This is a system-generated invoice.</strong><br>No signature or seal required.<br>Thank you for your business!</div>
</div>

<div class="page terms-page">
  <div class="watermark">3ES</div>
  <div class="company"><h1>360 EDGE SOLUTIONS (PRIVATE) LIMITED</h1><p>Tel: 070 202 2001 | 091 422 5424</p></div>
  <h2>WARRANTY & RETURN TERMS</h2>
  <p><strong>Invoice:</strong> {{ order.invoice_no or order.id }} | <strong>Date:</strong> {{ order.order_date }}</p>
  <h3>Warranty Coverage</h3><ul><li>Warranty period is calculated separately for each invoice item.</li><li>Products cover manufacturing defects under normal use only.</li><li>Battery warranty applies only where a battery warranty is mentioned.</li><li>Services may have no warranty unless a service warranty is shown.</li></ul>
  <h3>Warranty Exclusions</h3><ul><li>Physical damage, liquid damage, wrong voltage, power surge, unauthorized repair, misuse, software issues, normal wear and tear.</li><li>Network provider limitations, coverage, speed, signal strength, SIM/package issues are not covered.</li></ul>
  <h3>Returns / Exchange</h3><ul><li>Faulty products can be returned/exchanged within the stated return period subject to inspection.</li><li>After return period, use warranty claim process.</li><li>Courier/transport charges are customer responsibility unless agreed otherwise.</li></ul>
  <div class="notice"><strong>Important:</strong> Keep this invoice for warranty claims. Warranty starts from invoice date.</div>
</div>

<script>
function downloadText(){
const msg = `Invoice {{ order.invoice_no or order.id }}\nDate: {{ order.order_date }}\nTotal: LKR {{ "{:,.2f}".format(totals.total) }}\nContact: 070 202 2001\nThank you - 360 EDGE SOLUTIONS (PVT) LTD`;
window.open('https://wa.me/?text=' + encodeURIComponent(msg), '_blank');
}
</script>
</body>
</html>
