<?php
include "config.php";

if(isset($_POST['save'])){

$pid=$_POST['pid'];

mysqli_query($conn,"INSERT INTO bills(patient_id,total)
VALUES('$pid',0)");

$bill_id=mysqli_insert_id($conn);
$total=0;

for($i=0;$i<count($_POST['item']);$i++){
$item=$_POST['item'][$i];
$amt=$_POST['amount'][$i];

$total += $amt;

mysqli_query($conn,"INSERT INTO bill_items(bill_id,item,amount)
VALUES('$bill_id','$item','$amt')");
}

mysqli_query($conn,"UPDATE bills SET total='$total' WHERE id='$bill_id'");

header("Location:print_bill.php?id=$bill_id");
}
?>

<form method="post">

Patient ID:
<input name="pid" required>

<div id="items">
<input name="item[]" placeholder="Service">
<input name="amount[]" placeholder="Amount">
</div>

<button type="button" onclick="add()">Add More</button>

<br><br>
<button name="save">Generate Bill</button>

</form>

<script>
function add(){
document.getElementById("items").innerHTML +=
"<br><input name='item[]'><input name='amount[]'>";
}
</script>
