<?php
session_start();
include("dbConfig.php");
if(isset($_SESSION['username']) && isset($_SESSION['name']))
{
    $username=$_SESSION['username'];
    $name=$_SESSION['name'];
}
else
{
     echo '<META http-equiv="refresh" content="0;logout.php">';
}

?><?php
include("dbConfig.php");
if (isset($_GET['PatientID'])) {
    $patientId = $_GET['PatientID'];

    // 1. Fetch patient details from `opdpatientregister`
    $opdQuery = "SELECT `SlNo`, `PatientId`, `title`, `Name`, `FHN`, `Contact`, `Email`, `Age`, `Gender`, `Address`, `Consultant`, `admitDate`, `Createdat` 
                 FROM `opdpatientregister` WHERE `PatientId` = '$patientId'";
    $opdResult = mysqli_query($db, $opdQuery);
    $opdData = mysqli_fetch_assoc($opdResult);

    // 2. Fetch bill details from `bill`
    $billQuery = "SELECT `BillId`, `BillNo`, `PatientId`, `PatientType`, `TotAmount`, `PaymentMode`, `BDate`, `BTime`, `PayableAmount`, `InsuranceName`, `InsuranceNo`, `bank`, `cardno`, `upi`, `cash`, `PreparedBy` 
                  FROM `bill` WHERE `PatientId` = '$patientId'";
    $billResult = mysqli_query($db, $billQuery);
    $billData = mysqli_fetch_assoc($billResult);

    // 3. Fetch bill particulars from `billparticulars`
    $particularsQuery = "SELECT `SlNo`, `BillNo`, `PatId`, `Particulars`, `Quantity`, `UnitPrice`, `Amount`, `optionlab`, `consultant`, `created_at`, `created_by`, `modified_at`, `modified_by` 
                         FROM `billparticulars` WHERE `PatId` = '$patientId'";
    $particularsResult = mysqli_query($db, $particularsQuery);

    // 4. Fetch final bill details from `finalbill`
    $finalBillQuery = "SELECT `SlNo`, `FinalBillNo`, `PatientId`, `AdmitDate`, `AdmitTime`, `DischargeDate`, `DischargeTime`, `TotalAmount`, `insuranceAmt`, `DiscountDisp`, `DiscountAmount`, `AdvanceAmount`, `PayableAmount`, `PreparedBy`, `PaymentMode`, `InsuranceName`, `InsuranceNo`, `bank`, `cardno`, `upi`, `cash` 
                       FROM `finalbill` WHERE `PatientId` = '$patientId'";
    $finalBillResult = mysqli_query($db, $finalBillQuery);
    $finalBillData = mysqli_fetch_assoc($finalBillResult);

?>
<!DOCTYPE html>
<html>
<head>
  <title>
    
  </title>
  <meta charset="UTF-8">
  <link href="assets/img/favicon.png" rel="icon">
  <link href="assets/img/apple-touch-icon.png" rel="apple-touch-icon">
  <script src='https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
<link href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.css'>
<link href='https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css'>
  <!-- Google Fonts -->
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,700,700i|Raleway:300,400,500,700,800|Montserrat:300,400,700" rel="stylesheet">
<link href='https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css'>
<link href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.css'>

  <!-- Vendor CSS Files -->
  <link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  <link href="assets/vendor/ionicons/css/ionicons.min.css" rel="stylesheet">
  <link href="assets/vendor/animate.css/animate.min.css" rel="stylesheet">
  <link href="assets/vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet">
  <link href="assets/vendor/venobox/venobox.css" rel="stylesheet">
  <link href="assets/vendor/owl.carousel/assets/owl.carousel.min.css" rel="stylesheet">
  <link href="assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
  <script src="https://code.jquery.com/jquery.min.js"></script>
  <script src="jquery.dependent.fields.js"></script>
  <!-- Template Main CSS File -->
  <link href="assets/css/style.css" rel="stylesheet">
  <script src="https://code.jquery.com/jquery.min.js"></script>
<script src="jquery.dependent.fields.js"></script>
<style>
    @media print {
  #printPageButton {
    display: none;
  }
}

.table{
    
}
.table td, .table th {
    padding: 2px;
}
.table td, .patient_details{
    font-weight: bold;
    font-size:1em;
}


</style>
</head>
<body>

<section>
  
<div class="row" id="printPageButton" style="margin:5%">
  <button  type="button" class="btn btn-primary" onclick="window.print()" >Print</button> &nbsp;&nbsp;&nbsp;
<button type="button" class="btn btn-primary" onclick="history.back()">Back</button>
</div>      

  <div style="margin:0 5%">
    <div class="row">
      <div class="col-md-3">
        <img src="assets/img/logo.png">
      </div>
      <div class="col-md-6">
	   <p style="text-align:center;margin:0px">
	  <label style="font-size: 19pt;"> <b>VIDHYARANY HOSPITAL</b> </label><br/>
	  <label> 2876, 4th Cross, NS Road, Chamundipuram, Mysuru,<br> Karnataka 570004</label><br/>
	  <label><b>Ph no:</b> 0821- 2330113 & +91-76194 27745</label><br/>
	  <? if($PatientType=="OPD"){
       echo '<label style="font-size:15pt"><b>OUT PATIENT BILL</b></label>';
      
  }
		elseif($PatientType=="IPD"){
		echo '<label style="font-size:15pt"><b>IN PATIENT BILL</b></label>';
		}
		else{
		echo '<label style="font-size:15pt"><b>PATIENT BILL</b></label>';
		}
  ?>
	  <!--<label style="font-size:15pt"><b>OUT PATIENT BILL</b></label>-->
	  </p>
	  
      <!--<table>
      <tr>
        <td valign="top">Address</td>
          <td>:4th Cross Road, 7th Main Road, #13, KRS Rd, Brindavan Extension, Mysuru, Karnataka 570020</td>
      </tr>
      <tr>
        <td valign="top">Website</td>
        <td>:www.newpriyadarshinihospital.com/</td>
      </tr>
      <tr>
        <td valign="top">Contact</td>
        <td>:+91-76194 27745, 0821- 2413976</td>
      </tr>
      <tr>
        <td>Unit</td>
        <td>:Mysore</td>
      </tr>

    </table>-->
      </div>
	  <div class="col-md-3"></div>
    </div>
<hr>


<div class="row">
  <div class="col-md-6">
    <table class="patient_details" id="patient_details">
         <tr>
      <td valign="top"><b>Bill Num</b></td>
      <td valign="top">: <?php echo $billData['BillNo']; ?></td>
    </tr>
    <tr>
      <td valign="top"><b>Patient Id</b></td>
      <td valign="top">: <?php echo $opdData['PatientId']; ?></td>
    </tr>
  <tr>
    <td valign="top"><b>Name</b></td>
    <td valign="top">: <?php echo $opdData['title'] . ' ' . $opdData['Name']; ?></td>
  </tr>
  <tr>
    <td valign="top"><b>Contact No.</b></td>
    <td valign="top">: <?php echo $opdData['Contact']; ?></td>
  </tr>
  <?php if($opdData['Email'] != "") { ?>
      <tr>
    <td valign="top"><b>Email Id</b></td>
    <td valign="top">: <?php echo $opdData['Email']; ?></td>
  </tr>
  <?php } ?>
  
  <tr>
    <td valign="top"><b>Address</b></td>
    <td valign="top">: <?php echo $opdData['Address']; ?></td>
  </tr>

</table>
  </div>
  <div class="col-md-6">
    <table class="patient_details">   
  <tr>
    <td valign="top"><b>Gender/Age/Blood Group</b></td>
    <td valign="top">: <?php echo $opdData['Gender'] . "/ " . $opdData['Age'] . " Years / " . $opdData['blood']; ?></td>
  </tr>

  <tr>
    <td valign="top"><b>Consultant</b></td>
    <td valign="top">: <?php echo $opdData['Consultant']; ?></td>
  </tr>
  <tr>
    <td valign="top"><b>Patient Type</b></td>
    <td valign="top">: <?php echo $billData['PatientType']; ?></td>
  </tr>
  <?php if($opdData['admitDate'] != "") { ?>
  <tr>
    <td valign="top"><b>Admit Date/Time</b></td>
    <td valign="top">: <?php echo $opdData['admitDate']; ?></td>
  </tr>
  <?php } ?>
  
  <tr>
    <td valign="top"><b>Bill Type/Bill Date-Time</b></td>
    <td valign="top">: <?php echo $billData['PaymentMode'] . "/ " . $billData['BDate'] . ", " . $billData['BTime']; ?></td>
  </tr>  
  <?php if($billData['PaymentMode'] == "Insurance") { ?>
  <tr>
    <td>Insurance Name / Number</td>
    <td>: <?php echo $billData['InsuranceName'] . ", " . $billData['InsuranceNo']; ?></td>
  </tr>
  <?php } ?>
</table>
  </div>
</div>
<br>

<table class="table table-hover">
  <thead>
    <th scope="col">Sl No.</th>
    <th scope="col">Particulars</th>
    <th scope="col">Laboratory / Radiology</th>
    <th scope="col">Consultant</th>
    <th scope="col">Quantity</th>
    <th scope="col">Unit Price</th>
    <th scope="col">Amount</th>
  </thead>
  <tbody>
<?php
$i = 1;
while ($row = mysqli_fetch_assoc($particularsResult)) {
    echo "<tr>";
    echo "<td>$i</td>";
    echo "<td>" . $row['Particulars'] . "</td>";
    echo "<td>" . (!empty($row['optionlab']) ? $row['optionlab'] : 'N/A') . "</td>";
    echo "<td>" . (!empty($row['consultant']) ? $row['consultant'] : 'N/A') . "</td>";
    echo "<td>" . $row['Quantity'] . "</td>";
    echo "<td>" . $row['UnitPrice'] . "</td>";
    echo "<td>" . $row['Amount'] . "</td>";
    echo "</tr>";
    $i++;
}
?>

<tr>
  <td></td><td></td>
  <td></td>
  <td></td>
  <td></td>
  <td>Sub Total</td>
  <td><?php echo $billData['TotAmount']; ?></td>
</tr>

  </tbody>
</table>

<div class="row">
  <div class="col-md-6"></div>
  <div class="col-md-5">
    <table class="table">
  <tr>
    <td><b>Total Bill Amount:</b></td>
    <td><b><?php echo $billData['TotAmount']; ?></b></td>
  </tr>
  <!--<tr>-->
  <!--  <td><b>Discount Amount:</b></td>-->
  <!--  <td><b><?php echo $finalBillData['DiscountAmount']; ?></b></td>-->
  <!--</tr>-->
  <!--<tr>-->
  <!--  <td><b>Patient Payable Amount:</b></td>-->
  <!--  <td><b><?php echo $finalBillData['PayableAmount']; ?></b></td>-->
  <!--</tr>-->
</table>
  </div>
  <div class="col-md-1"></div>
</div>
<br><br>
<div class="row">
  <div class="col-md-1"></div>
  <div class="col-md-7">
    
   
    
  </div>
  <div class="col-md-4">

    Authorized Signatory</h6>
     
<br><h6>Prepared by <b><?echo $name?></b></h6>
  </div>
</div>
<br><br><br><br>

    </div>
  



</section>
 <script src='https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js'></script>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
  <script src="assets/vendor/jquery/jquery.min.js"></script>
  <script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
  <script src="assets/vendor/jquery.easing/jquery.easing.min.js"></script>
  <script src="assets/vendor/php-email-form/validate.js"></script>
  <script src="assets/vendor/wow/wow.min.js"></script>
  <script src="assets/vendor/venobox/venobox.min.js"></script>
  <script src="assets/vendor/owl.carousel/owl.carousel.min.js"></script>
  <script src="assets/vendor/jquery-sticky/jquery.sticky.js"></script>
  <script src="assets/vendor/superfish/superfish.min.js"></script>
  <script src="assets/vendor/hoverIntent/hoverIntent.js"></script>
  <script src="assets/vendor/isotope-layout/isotope.pkgd.min.js"></script>
  <script src="assets/js/main.js"></script>
</body>
</html>


<?
}

?>


