<?php
// hps_suppliers.php
require "db.php";

/* -----------------------------
   CONFIG
------------------------------*/
$DELETE_PASSWORD = "1234";

function esc($v){
    return htmlspecialchars($v ?? '', ENT_QUOTES, 'UTF-8');
}

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

    $id      = (int)($_POST['id'] ?? 0);
    $name    = mysqli_real_escape_string($conn,$_POST['name']);
    $mobile  = mysqli_real_escape_string($conn,$_POST['mobile']);
    $cnic    = mysqli_real_escape_string($conn,$_POST['cnic']);
    $address = mysqli_real_escape_string($conn,$_POST['address']);

    if ($id > 0) {
        mysqli_query($conn,"
            UPDATE hps_suppliers SET
            name='$name',
            mobile='$mobile',
            cnic='$cnic',
            address='$address'
            WHERE id=$id
        ");
    } else {
        mysqli_query($conn,"
            INSERT INTO hps_suppliers (name,mobile,cnic,address)
            VALUES ('$name','$mobile','$cnic','$address')
        ");
    }

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

/* -----------------------------
   DELETE (PASSWORD)
------------------------------*/
if ($_SERVER['REQUEST_METHOD']==='POST' && isset($_POST['delete_supplier'])) {

    if ($_POST['password'] !== $DELETE_PASSWORD) {
        header("Location: hps_suppliers.php?err=pass");
        exit;
    }

    $id = (int)$_POST['delete_id'];
    mysqli_query($conn,"DELETE FROM hps_suppliers WHERE id=$id");

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

/* -----------------------------
   SEARCH
------------------------------*/
$q = mysqli_real_escape_string($conn,$_GET['q'] ?? '');
$where = $q ? "WHERE name LIKE '%$q%' OR mobile LIKE '%$q%'" : "";

/* -----------------------------
   FETCH
------------------------------*/
$res = mysqli_query($conn,"
    SELECT * FROM hps_suppliers
    $where
    ORDER BY id DESC
");
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Suppliers</title>

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

<style>
body{background:#f1f3f5}

/* 3D Card */
.main-card{
    background:rgba(200,200,200,.70);
    border-radius:30px;
    padding:24px;
    margin:30px auto;
    max-width:1000px;
    box-shadow:0 10px 25px rgba(0,0,0,.08),
               0 2px 6px rgba(0,0,0,.06);
}

/* Inputs */
.form-control{
    border:1.5px solid #6c757d;
}
.form-control:focus{
    border-color:#198754;
    box-shadow:0 0 0 .15rem rgba(25,135,84,.25);
}

/* Table hover */
#supplierTable tbody tr:hover{
    background:#eaf5ea;
    cursor:pointer;
}
</style>
</head>
<body>

<div class="container-fluid">
<div class="main-card">

<h4 class="mb-3">Suppliers</h4>

<?php if(isset($_GET['err'])): ?>
<div class="alert alert-danger py-2">❌ Wrong delete password</div>
<?php endif; ?>

<!-- ADD SUPPLIER -->
<div class="card mb-2">
<div class="card-header fw-bold">Add Supplier</div>
<div class="card-body">

<form method="post" class="row g-2">
<input type="hidden" name="id">

<!-- ROW 1 -->
<div class="col-md-4">
<label class="small">Supplier Name</label>
<input name="name" class="form-control form-control-sm" required>
</div>

<div class="col-md-4">
<label class="small">Mobile</label>
<input name="mobile" class="form-control form-control-sm">
</div>

<div class="col-md-4">
<label class="small">CNIC</label>
<input name="cnic" class="form-control form-control-sm">
</div>

<!-- ROW 2 -->
<div class="col-md-8">
<label class="small">Address</label>
<input name="address" class="form-control form-control-sm">
</div>

<div class="col-md-4 d-flex align-items-end">
<button name="save_supplier"
        class="btn btn-primary btn-sm w-100">
    Add
</button>
</div>

</form>


</div>
</div>

<!-- LIVE SEARCH -->
<input id="searchBox" class="form-control form-control-sm mb-2"
placeholder="Live search supplier...">

<!-- LIST -->
<table class="table table-bordered table-sm" id="supplierTable">
<thead class="table-dark">
<tr>
<th>#</th>
<th>Name</th>
<th>Mobile</th>
<th>CNIC</th>
<th>Address</th>
<th width="130">Action</th>
</tr>
</thead>
<tbody>

<?php $i=1; while($r=mysqli_fetch_assoc($res)): ?>
<tr>
<td><?=$i++?></td>
<td><?=esc($r['name'])?></td>
<td><?=esc($r['mobile'])?></td>
<td><?=esc($r['cnic'])?></td>
<td><?=esc($r['address'])?></td>
<td>
<button class="btn btn-sm btn-success"
data-bs-toggle="modal"
data-bs-target="#editModal"
data-id="<?=$r['id']?>"
data-name="<?=esc($r['name'])?>"
data-mobile="<?=$r['mobile']?>"
data-cnic="<?=$r['cnic']?>"
data-address="<?=esc($r['address'])?>">
Edit
</button>

<button class="btn btn-sm btn-danger"
data-bs-toggle="modal"
data-bs-target="#deleteModal"
data-id="<?=$r['id']?>">
Delete
</button>
</td>
</tr>
<?php endwhile; ?>

<?php if(mysqli_num_rows($res)==0): ?>
<tr><td colspan="6" class="text-center text-muted">No records</td></tr>
<?php endif; ?>

</tbody>
</table>

</div>
</div>

<!-- EDIT MODAL -->
<div class="modal fade" id="editModal">
<div class="modal-dialog">
<div class="modal-content">
<form method="post">

<div class="modal-header">
<h5>Edit Supplier</h5>
<button class="btn-close" data-bs-dismiss="modal"></button>
</div>

<div class="modal-body">
<input type="hidden" name="id" id="edit_id">

<input class="form-control mb-2" name="name" id="edit_name" required>
<input class="form-control mb-2" name="mobile" id="edit_mobile">
<input class="form-control mb-2" name="cnic" id="edit_cnic">
<input class="form-control" name="address" id="edit_address">
</div>

<div class="modal-footer">
<button class="btn btn-secondary btn-sm" data-bs-dismiss="modal">Cancel</button>
<button name="save_supplier" class="btn btn-primary btn-sm">Update</button>
</div>

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

<!-- DELETE MODAL -->
<div class="modal fade" id="deleteModal">
<div class="modal-dialog">
<div class="modal-content">
<form method="post">

<div class="modal-header">
<h5 class="text-danger">Confirm Delete</h5>
<button class="btn-close" data-bs-dismiss="modal"></button>
</div>

<div class="modal-body">
<input type="hidden" name="delete_id" id="delete_id">
<input type="password" name="password" class="form-control" placeholder="Password" required>
</div>

<div class="modal-footer">
<button class="btn btn-secondary btn-sm" data-bs-dismiss="modal">Cancel</button>
<button name="delete_supplier" class="btn btn-danger btn-sm">Delete</button>
</div>

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

<script>
// edit modal
document.getElementById('editModal').addEventListener('show.bs.modal',e=>{
let b=e.relatedTarget;
edit_id.value=b.dataset.id;
edit_name.value=b.dataset.name;
edit_mobile.value=b.dataset.mobile;
edit_cnic.value=b.dataset.cnic;
edit_address.value=b.dataset.address;
});

// delete modal
document.getElementById('deleteModal').addEventListener('show.bs.modal',e=>{
delete_id.value=e.relatedTarget.dataset.id;
});

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

</body>
</html>
