<?php
$page_title = 'Order Booking';
require_once __DIR__ . '/header.php';

// Handle Bulk Add to Cart
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['bulk_order'])) {
    $qtys = $_POST['qtys'] ?? [];
    foreach ($qtys as $pid => $qty) {
        $q = (int)$qty;
        if ($q > 0) {
            cartAdd($pid, $q);
        }
    }
    // Set customer session automatically so they can checkout seamlessly
    $_SESSION['customer_id'] = $dealer['id'];
    $_SESSION['customer_name'] = $dealer['company_name'];
    $_SESSION['customer_email'] = $dealer['email'];
    redirect(SITE_URL . '/pages/cart.php');
}

$products = $db->query("SELECT * FROM products WHERE status='active' ORDER BY name ASC")->fetchAll();
?>

<div class="card">
  <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px;">
    <div>
      <h3 style="margin:0;color:#0f172a;">Wholesale Order Form</h3>
      <p style="margin:4px 0 0;font-size:.85rem;color:#64748b;">Enter quantities for the products you need and click "Add to Cart" at the bottom.</p>
    </div>
    <a href="price_list.php" class="btn-primary" style="background:#e2e8f0;color:#0f172a;"><i class="fas fa-download"></i> Download Price List</a>
  </div>
  
  <form method="POST">
    <input type="hidden" name="bulk_order" value="1">
    <div class="table-wrap">
      <table>
        <thead>
          <tr>
            <th>Image</th>
            <th>Product Details</th>
            <th>Dimensions</th>
            <th>Dealer Price</th>
            <th style="width:120px;">Quantity</th>
          </tr>
        </thead>
        <tbody>
          <?php foreach ($products as $p): ?>
          <!-- Assuming Dealer price is 20% less than MRP -->
          <?php $dealer_price = $p['price'] * 0.80; ?>
          <tr>
            <td>
              <img src="<?= SITE_URL ?>/assets/images/<?= h($p['image']) ?>" alt="" style="width:50px;height:50px;border-radius:6px;object-fit:cover;background:#f1f5f9;">
            </td>
            <td>
              <div style="font-weight:600;color:#0f172a;"><?= h($p['name']) ?></div>
              <div style="font-size:.8rem;color:#64748b;">Material: <?= h($p['material']) ?> | Finish: <?= h($p['finish']) ?></div>
            </td>
            <td style="font-size:.85rem;color:#475569;"><?= h($p['dimensions']) ?></td>
            <td>
              <strong style="color:#059669;font-size:1.05rem;">₹<?= number_format($dealer_price, 2) ?></strong>
              <span style="text-decoration:line-through;color:#94a3b8;font-size:.85rem;margin-left:8px;">₹<?= number_format($p['price'], 2) ?></span>
            </td>
            <td>
              <input type="number" name="qtys[<?= $p['id'] ?>]" min="0" placeholder="0" style="width:80px;padding:8px;border:1.5px solid #cbd5e1;border-radius:6px;text-align:center;font-weight:600;outline:none;">
            </td>
          </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    </div>
    <div style="text-align:right;margin-top:20px;padding-top:20px;border-top:1px solid #e2e8f0;">
      <button type="submit" class="btn-primary" style="font-size:1.1rem;padding:12px 24px;"><i class="fas fa-shopping-cart"></i> Add Selected to Cart</button>
    </div>
  </form>
</div>

<?php require_once __DIR__ . '/footer.php'; ?>
