<!DOCTYPE html>
<html>
<head>
  <style>
    body {
      font-family: 'Segoe UI', sans-serif;
      margin: 0;
      background: linear-gradient(135deg, #1e3c72, #2a5298);
      color: #333;
    }

    /* HEADER */
    .header {
      background: #0f172a;
      color: #fff;
      padding: 20px;
      text-align: center;
      font-size: 26px;
      font-weight: bold;
      letter-spacing: 1px;
      box-shadow: 0 2px 10px rgba(0,0,0,0.3);
    }

    .container {
      display: flex;
    }

    /* PRODUCTS */
    .products {
      width: 70%;
      padding: 20px;
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      gap: 20px;
    }

    .card {
      background: #ffffff;
      border-radius: 15px;
      padding: 15px;
      text-align: center;
      box-shadow: 0 5px 15px rgba(0,0,0,0.2);
      transition: transform 0.2s;
    }

    .card:hover {
      transform: translateY(-5px);
    }

    .card img {
      width: 100px;
      height: 100px;
      object-fit: cover;
      margin-bottom: 10px;
    }

    .card h4 {
      margin: 5px 0;
      font-size: 18px;
    }

    .price {
      color: #16a34a;
      font-weight: bold;
      margin-bottom: 10px;
    }

    /* ADD BUTTON */
    .add-btn {
      background: linear-gradient(135deg, #22c55e, #16a34a);
      border: none;
      color: white;
      padding: 10px;
      border-radius: 8px;
      cursor: pointer;
      font-weight: bold;
      width: 100%;
      transition: 0.2s;
    }

    .add-btn:hover {
      transform: scale(1.05);
      background: linear-gradient(135deg, #16a34a, #15803d);
    }

    /* CART */
    .cart {
      width: 30%;
      padding: 20px;
      background: #0f172a;
      color: white;
      min-height: 100vh;
    }

    .cart h2 {
      margin-top: 0;
      border-bottom: 1px solid #444;
      padding-bottom: 10px;
    }

    .cart-item {
      background: #1e293b;
      padding: 10px;
      margin-bottom: 10px;
      border-radius: 8px;
      font-size: 14px;
    }

    .total {
      font-size: 18px;
      font-weight: bold;
      margin-top: 10px;
    }

    .checkout {
      margin-top: 20px;
      padding: 12px;
      background: linear-gradient(135deg, #f97316, #ea580c);
      color: white;
      border: none;
      cursor: pointer;
      width: 100%;
      border-radius: 10px;
      font-size: 16px;
      font-weight: bold;
    }


    .cart button {
  padding: 5px 8px;
  margin: 2px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

.cart button:hover {
  opacity: 0.8;
}

    .checkout:hover {
      background: linear-gradient(135deg, #ea580c, #c2410c);
    }
  </style>
</head>

<body>

<div class="header">
  🧾 SMART Point Of Sale Application
</div>

<div class="container">

  <div class="products" id="products"></div>

  <div class="cart">
    <h2>🛒 Cart</h2>
    <div id="cartItems"></div>
    <div class="total">Total: GHS <span id="total">0</span></div>
    <button class="checkout" onclick="checkout()">Checkout</button>
  </div>

</div>

<script>
let cart = [];

function loadProducts() {
  google.script.run.withSuccessHandler(displayProducts).getProducts();
}

function displayProducts(products) {
  const container = document.getElementById("products");
  container.innerHTML = "";

  products.forEach(p => {
    container.innerHTML += `
      <div class="card">
        <img src="${p.image}">
        <h4>${p.name}</h4>
        <div class="price">GHS ${p.price}</div>
        <button class="add-btn" onclick='addToCart(${JSON.stringify(p)})'>
          🛒 Add to Cart
        </button>
      </div>
    `;
  });
}

function addToCart(product) {
  const found = cart.find(i => i.id === product.id);
  if (found) {
    found.qty++;
  } else {
    product.qty = 1;
    cart.push(product);
  }
  renderCart();
}

function renderCart() {
  let html = "";
  let total = 0;

  cart.forEach((item, index) => {
    total += item.qty * item.price;

    html += `
      <div class="cart-item">
        <div>${item.name}</div>
        
        <div style="margin-top:5px;">
          <button onclick="decreaseQty(${index})">➖</button>
          <span style="margin:0 10px;">${item.qty}</span>
          <button onclick="increaseQty(${index})">➕</button>
        </div>

        <div style="margin-top:5px;">
          GHS ${item.qty * item.price}
        </div>

        <button onclick="removeItem(${index})" style="margin-top:5px;color:red;">
          ❌ Remove
        </button>
      </div>
    `;
  });

  document.getElementById("cartItems").innerHTML = html;
  document.getElementById("total").innerText = total;
}

function checkout() {
  if (cart.length === 0) {
    alert("Cart is empty");
    return;
  }

  google.script.run.withSuccessHandler(showReceipt).saveSale(cart);
}

function showReceipt(receiptId) {
  let total = 0;

  let receiptHTML = `
    <style>
      body {
        font-family: monospace;
        background: #f4f4f4;
        display: flex;
        justify-content: center;
        padding: 20px;
      }

      .receipt {
        background: white;
        width: 300px;
        padding: 20px;
        border-radius: 10px;
        box-shadow: 0 0 10px rgba(0,0,0,0.1);
      }

      .center {
        text-align: center;
      }

      .item {
        display: flex;
        justify-content: space-between;
        margin: 5px 0;
      }

      hr {
        border: none;
        border-top: 1px dashed #000;
        margin: 10px 0;
      }

      .total {
        font-weight: bold;
        font-size: 16px;
      }

      .print-btn {
        margin-top: 15px;
        width: 100%;
        padding: 10px;
        background: black;
        color: white;
        border: none;
        cursor: pointer;
      }
    </style>

    <div class="receipt">
      <div class="center">
        <h2>🧾 RECEIPT</h2>
        <p>${receiptId}</p>
      </div>
      <hr>
  `;

  cart.forEach(item => {
    total += item.qty * item.price;
    receiptHTML += `
      <div class="item">
        <span>${item.name} x${item.qty}</span>
        <span>GHS ${item.qty * item.price}</span>
      </div>
    `;
  });

  receiptHTML += `
      <hr>
      <div class="item total">
        <span>Total</span>
        <span>GHS ${total}</span>
      </div>

      <div class="center">
        <p>Thank you!</p>
      </div>

      <button class="print-btn" onclick="window.print()">Print</button>
    </div>
  `;

  document.body.innerHTML = receiptHTML;
}


function increaseQty(index) {
  cart[index].qty++;
  renderCart();
}

function decreaseQty(index) {
  if (cart[index].qty > 1) {
    cart[index].qty--;
  } else {
    cart.splice(index, 1);
  }
  renderCart();
}

function removeItem(index) {
  cart.splice(index, 1);
  renderCart();
}

loadProducts();
</script>

</body>
</html>