<?php
session_start();
include("dbConfig.php");

if(isset($_SESSION['username']) && isset($_SESSION['name']))
{
    
    $username=$_SESSION['username'];
    $name=$_SESSION['name'];
    
    
 $billno=$_GET['Billno'];
  $sl=$_GET['sl'];
$i=1;
$qu2=mysqli_query($db,"SELECT * FROM billparticulars WHERE SlNo=$sl");
while($row=$qu2->fetch_assoc()){

    $part=$row['Particulars'];
    $quan=$row['Quantity'];
    $uprice= $row['UnitPrice'];
    $amou= $row['Amount'];
    $Consultant= $row['consultant'];
    $pa_id = $row['PatId']; 
    $BillNo = $row['BillNo'];
}
?>

<?php
$Consultants=$_POST['cons'];
$particulars=$_POST['particulars'];
$quantity=$_POST['quantity'];
$unitprice=$_POST['unitprice'];
$amount=$_POST['amount'];


if(isset($_POST['submit']))
{
    $pa_id2=$_POST['pa_id'];
    $BillNo2=$_POST['BillNo'];
    $oldEntry ="edited Entry";

    date_default_timezone_set('Asia/Kolkata');
    
    $edited_by = $_POST['preparedby'];
    $date = date("d-m-Y h:i:sa", time ());
     
    $to ="vidhyaranya hospital.com";
    
    $subject ="Edited Bill Particular item from VH index page";
    
    for($i=0;$i<$size;$i++){
         
         $ParticularItems = "(Particulars,Quantity,UnitPrice,Amount)('$particulars','$quantity','$unitprice','$amount')";
          
          
        }
    $txt = "patient id:  ".$pa_id2."\r\nedited by:  ".$edited_by."\r\nsubject: ".$subject."\r\nParticularItems: ".$ParticularItems."\r\nDate&time:  ".$date."\r\nType:  ".$oldEntry;
    $header = "from : noreplay@yourself.com"."\r\n"."CC: somebody@example.com";
    // $mail = mail($to, $subject,$txt, $header);
    
    
    $sql = "UPDATE billparticulars SET Particulars='$particulars',Quantity='$quantity',UnitPrice='$unitprice',Amount='$amount',consultant='$Consultants'  WHERE  SlNo=$sl";
    
    mysqli_query($db,"INSERT INTO edited_billparticulars(BillNo,type,PatId,Particulars,Quantity,UnitPrice,Amount,created_by) VALUES
    ('$BillNo2','$oldEntry','$pa_id2','$particulars','$quantity','$unitprice','$amount','$username')");


if ($db->query($sql) === TRUE)
{
header("Location: /Billing/Editable_bill.php?Billno=$billno");
}
else 
{
  echo "Error updating record: " . $conn->error;
}

$conn->close();
}
?>



<!DOCTYPE html>
<html>
<style>
input[type=text], select {
  width: 100%;
  padding: 12px 20px;
  margin: 8px 0;
  display: inline-block;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
}

input[type=submit] {
  width: 100%;
  background-color: #4CAF50;
  color: white;
  padding: 14px 20px;
  margin: 8px 0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

input[type=submit]:hover {
  background-color: #45a049;
}

div {
  border-radius: 5px;
  background-color: #f2f2f2;
  padding: 20px;
}
</style>
<body>

<h3>Edit Bill</h3>

<div>
  <form method="post">
    <label for="fname">Particulars</label>
    <input type="text" id="fname" name="particulars" value="<?php echo $part; ?>">

    <label for="lname">Quantity</label>
    <input type="text" id="lname" name="quantity" value="<?php echo $quan; ?>">

   
   <label for="lname">Unit Pricce</label>
    <input type="text" id="lname" name="unitprice" value="<?php echo $uprice; ?>">
    
     <label for="lname">Amount</label>
    <input type="text" id="lname" name="amount" value="<?php echo $amou; ?>">
    
    <label for="lname">Consultant</label>
    <input type="text" id="lname" name="cons" value="<?php echo $Consultant; ?>">
    <input type="hidden" id="pa_id" name="pa_id" value="<?php echo $pa_id; ?>">
    
    <input type="hidden" id="BillNo" name="BillNo" value="<?php echo $BillNo; ?>">
    
    <input type="submit" name='submit' value="Submit">
  </form>
</div>

</body>
</html>

<?php 
}
else
{
     echo '<META http-equiv="refresh" content="0;logout.php">';
}
    
?>
