<?php
// hps_sale_customer.php
session_start();
require "db.php";

if (isset($_GET['err']) && $_GET['err']=='has_sales'): ?>
<div class="alert alert-danger small">
⚠ Customer cannot be deleted because sales exist.
</div>
<?php endif; 

/* -----------------------------
   ADD CUSTOMER
------------------------------*/
if ($_SERVER['REQUEST_METHOD']==='POST' && isset($_POST['add_customer'])) {

    $name   = mysqli_real_escape_string($conn,$_POST['name']);
    $mobile = mysqli_real_escape_string($conn,$_POST['mobile']);
    $cnic   = mysqli_real_escape_string($conn,$_POST['cnic']);

    mysqli_query($conn,"
        INSERT INTO hps_customers (name,mobile,cnic)
        VALUES ('$name','$mobile','$cnic')
    ");

    header("Location: hps_sale_customer.php");
    exit;
}

/* -----------------------------
   DELETE CUSTOMER
------------------------------*/
if (isset($_GET['del'])) {
    $id=(int)$_GET['del'];
if (isset($_GET['del'])) {
    $id = (int)$_GET['del'];

    // Check if customer has any sale
    $chk = mysqli_query($conn,"
        SELECT id FROM hps_sales WHERE customer_id=$id LIMIT 1
    ");

    if (mysqli_num_rows($chk) > 0) {
        header("Location: hps_sale_customer.php?err=has_sales");
        exit;
    }

    mysqli_query($conn,"DELETE FROM hps_customers WHERE id=$id");
    header("Location: hps_sale_customer.php");
    exit;
}
    header("Location: hps_sale_customer.php");
    exit;
}

/* -----------------------------
   SELECT CUSTOMER (START SALE)
------------------------------*/
if (isset($_GET['select'])) {

    $cid=(int)$_GET['select'];

    $_SESSION['hps_customer_id']=$cid;

    // create new sale (do NOT touch old sales)
    mysqli_query($conn,"
        INSERT INTO hps_sales (customer_id,status)
        VALUES ($cid,'Draft')
    ");

    $_SESSION['hps_sale_id']=mysqli_insert_id($conn);

    header("Location: hps_sale_products.php");
    exit;
}

/* -----------------------------
   FETCH CUSTOMERS
------------------------------*/
$customers=mysqli_query($conn,"SELECT * FROM hps_customers ORDER BY id DESC");
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Step 1 — Customer</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>

<div class="container py-3">

<div class="d-flex justify-content-between align-items-center mb-2">
  <h4>Step 1 — Select Customer</h4>
  <button class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#addModal">
    + Add New
  </button>
</div>

<!-- PROGRESS -->
<div class="progress mb-3">
  <div class="progress-bar" style="width:14%">Step 1 of 7</div>
</div>

<!-- SEARCH -->
<input type="text" id="search" class="form-control form-control-sm mb-2" placeholder="Search customer...">

<!-- CUSTOMER TABLE -->
<table class="table table-bordered table-sm" id="custTable">
<thead class="table-dark">
<tr>
  <th>Name</th>
  <th>Mobile</th>
  <th>CNIC</th>
  <th width="160">Action</th>
</tr>
</thead>
<tbody>
<?php while($c=mysqli_fetch_assoc($customers)): ?>
<tr>
  <td><?=$c['name']?></td>
  <td><?=$c['mobile']?></td>
  <td><?=$c['cnic']?></td>
  <td>
    <a href="?select=<?=$c['id']?>" class="btn btn-success btn-sm">Select</a>
    <a href="?del=<?=$c['id']?>" class="btn btn-danger btn-sm"
       onclick="return confirm('Delete customer?')">Del</a>
  </td>
</tr>
<?php endwhile; ?>
</tbody>
</table>

</div>

<!-- ADD CUSTOMER MODAL -->
<div class="modal fade" id="addModal">
<div class="modal-dialog">
<form method="post" class="modal-content">

<div class="modal-header">
<h6>Add Customer</h6>
<button class="btn-close" data-bs-dismiss="modal"></button>
</div>

<div class="modal-body">

<input type="text" name="name" class="form-control mb-2" placeholder="Customer Name" required>
<input type="text" name="mobile" class="form-control mb-2" placeholder="Mobile">
<input type="text" name="cnic" class="form-control mb-2" placeholder="CNIC">

</div>

<div class="modal-footer">
<button class="btn btn-secondary btn-sm" data-bs-dismiss="modal">Close</button>
<button name="add_customer" class="btn btn-primary btn-sm">Save</button>
</div>

</form>
</div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>

<script>
// live search
document.getElementById('search').addEventListener('keyup', function(){
    let val=this.value.toLowerCase();
    document.querySelectorAll('#custTable tbody tr').forEach(tr=>{
        tr.style.display = tr.innerText.toLowerCase().includes(val) ? '' : 'none';
    });
});
</script>

</body>
</html>
