<?php
include 'db.php';
include 'layout.php';

$id = $_GET['id'];
$res = $conn->query("SELECT * FROM customers WHERE id = $id");
$customer = $res->fetch_assoc();
?>

<div class="main-wrapper">
  <div class="page-wrapper">
    <div class="content">
      <div class="page-header">
        <div class="page-title">
          <h4>Edit Customer</h4>
        </div>
      </div>

      <div class="card">
        <div class="card-body">
          <form method="POST" action="update_customer.php">
            <input type="hidden" name="id" value="<?= $customer['id'] ?>">
            <div class="mb-2"><label>Name</label><input type="text" name="name" value="<?= $customer['name'] ?>" class="form-control" required></div>
            <div class="mb-2"><label>Phone</label><input type="text" name="phone" value="<?= $customer['phone'] ?>" class="form-control"></div>
            <div class="mb-2"><label>Email</label><input type="email" name="email" value="<?= $customer['email'] ?>" class="form-control"></div>
            <div class="mb-2"><label>Address</label><textarea name="address" class="form-control"><?= $customer['address'] ?></textarea></div>
            <div class="mb-2"><label>Province</label><input type="text" name="province" value="<?= $customer['province'] ?>" class="form-control"></div>
            <div class="mb-2"><label>Category</label>
              <select name="category_id" class="form-control">
                <option value="">Select</option>
                <?php
                $cats = $conn->query("SELECT * FROM customer_categories");
                while ($cat = $cats->fetch_assoc()) {
                  $selected = $cat['id'] == $customer['category_id'] ? 'selected' : '';
                  echo "<option value='{$cat['id']}' $selected>{$cat['category_name']}</option>";
                }
                ?>
              </select>
            </div>
            <div class="form-check mb-2">
              <input class="form-check-input" type="checkbox" name="active" <?= $customer['active'] ? 'checked' : '' ?>>
              <label class="form-check-label">Active</label>
            </div>
            <div class="form-check mb-2">
              <input class="form-check-input" type="checkbox" name="is_walk_in" <?= $customer['is_walk_in'] ? 'checked' : '' ?>>
              <label class="form-check-label">Is Walk-in</label>
            </div>
            <button type="submit" class="btn btn-primary">Update Customer</button>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>
