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

// Enable error reporting for debugging (optional)
error_reporting(E_ALL);
ini_set('display_errors', 1);



// Retrieve data from the examination table
$patid = $_GET['PatId']; // Patient ID passed via URL or session
$query = "SELECT * FROM patients WHERE PatientId = '$patid'";
$result = mysqli_query($conn, $query);
$patient = mysqli_fetch_assoc($result);

// Patient data variables
$patid = $patient['PatientId'];
$title = $patient['Title'];
$Pname = $patient['PatientName'];
$contact1 = $patient['Contact1'];
$email = $patient['Email'];
$Address = $patient['Address'];
$gender = $patient['Gender'];
$age = $patient['Age'];
$blood = $patient['BloodGroup'];
$Consultant = $patient['Consultant'];
$PatientType = $patient['PatientType'];
$AdmitDate = $patient['AdmitDate'];
$AdmitTime = $patient['AdmitTime'];

// Fetch examination details
$examQuery = "SELECT * FROM examinations WHERE PatientId = '$patid'";
$examResult = mysqli_query($conn, $examQuery);

$totalAmount = 0;
?>
<!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>
    .finaltable {
      border-top: 1px solid black;
      border: 1px solid black;
    }

    .finaltable th {
      border-top: 1px solid black;
      border: 1px solid black;
    }

    .finaltable td {
      border-top: 1px solid black;
      border: 1px solid black;
    }

    .finaltable thead tr {
      border-bottom: 3px solid black;

    }

    @media print {
      #printPageButton {
        display: none;
      }
    }

    .spanfont {
      font-size: 20px;
    }

  </style>
</head>

<body>

  <section>

<!--    <div class="row" id="printPageButton" style="margin:5%">-->
<!--     <a href="examination_previe.php?billNo=$abillid&PatId=$patid&amount=$amount"> <button type="button" class="btn btn-primary" >save</button></a> &nbsp;&nbsp;&nbsp;-->
<!--    <a href="edit_examination.php?billNo=<?php echo $row['BillNo']; ?>&PatId=<?php echo $row['PatId']; ?>&amount=<?php echo $row['Amount']; ?>">-->
<!--    <button type="button" class="btn btn-primary">Edit</button>-->
<!--</a>-->

<!--    </div>-->

    <div style="margin:4%">
      <div class="row">
        <div class="col-md-6 col-sm-6">
          <img src="assets/img/logo.png">
          <span class="spanfont"><b>Vidhyaranya Hospital PVT LTD.</b></span>
        </div>

        <div class="col-md-6 col-sm-6">
          <table>
            <tr>
              <td valign="top">Address</td>
              <td>:2876, 4th Cross, NS Road, Chamundipuram, Mysuru, Karnataka 570004</td>
            </tr>
          
            <tr>
              <td valign="top">Contact</td>
              <td>:+91-821 233 1655, +91-821 233 0555, +91 9035933963</td>
            </tr>
            <tr>
              <td>Unit</td>
              <td>:Mysore</td>
            </tr>

          </table>
        </div>
      </div>


      <div class="row text-center mt-3">
        <div class="col-md-12">
          <label style="font-size:15pt"><b> PATIENT EXAMINATION BILL</b></label>
        </div>
      </div>
      <hr>


<div class="row d-flex justify-content-between">
    <div class="col-md-6 col-sm-6 row1 text-left">
        <hr />
      <table>
            <tr>
                <td valign="top">Patient Id</td>
                <td valign="top">: <?php echo $patid; ?></td>
            </tr>
            <tr>
                <td valign="top">Name</td>
                <td valign="top">: <?php echo $title . ' ' . $Pname; ?></td>
            </tr>
            <tr>
                <td valign="top">Contact</td>
                <td valign="top">: <?php echo $contact1; ?></td>
            </tr>
            <?php if ($email != "") { ?>
                <tr>
                    <td valign="top">Email Id</td>
                    <td valign="top">: <?php echo $email; ?></td>
                </tr>
            <?php } ?>
            <tr>
                <td valign="top">Address </td>
                <td valign="top">: <?php echo $Address; ?></td>
            </tr>
        </table>
    </div>

    <!-- Patient Additional Info -->
    <div class="col-md-6 col-sm-6 row2">
        <table>
            <hr />
            <tr>
                <td valign="top">Gender/Age/Blood Group</td>
                <td valign="top">: <?php echo $gender . " / " . $age . " / " . $blood; ?></td>
            </tr>
            <tr>
                <td valign="top">Consultant</td>
                <td valign="top">: <?php echo $Consultant; ?></td>
            </tr>
            <tr>
                <td valign="top">Patient Type</td>
                <td valign="top">: <?php echo $PatientType; ?></td>
            </tr>
            <tr>
                <td valign="top">Admit Date/Time</td>
                <td valign="top">: <?php echo $AdmitDate . ", " . $AdmitTime; ?></td>
            </tr>
        </table>
    </div>
</div>

<br /><br />

<!-- Examination Table -->
<table class="table table-hover finaltable">
    <thead>
        <th scope="col">Sl No.</th>
        <th scope="col">Particulars</th>
        <th scope="col">Consultant</th>
        <th scope="col">Department</th>
        <th scope="col">Quantity</th>
        <th scope="col">Price</th>
        <th scope="col">Amount</th>
        <th scope="col">Actions</th> <!-- Add Actions column for buttons -->
    </thead>
    <tbody>
        <?php
        $i = 1;
        while ($row = mysqli_fetch_assoc($examResult)) {
            $particulars = $row['Particulars'];
            $consultant = $row['consultant'];
            $department = $row['optionlab'];
            $quantity = $row['Quantity'];
            $price = $row['UnitPrice'];
            $amount = $row['Amount'];
            $totalAmount += $amount;
            echo "<tr><td>$i</td>";
            echo "<td><input type='text' name='particulars[$i]' value='$particulars'></td>";
            echo "<td><input type='text' name='consultant[$i]' value='$consultant'></td>";
            echo "<td><input type='text' name='department[$i]' value='$department'></td>";
            echo "<td><input type='number' name='quantity[$i]' value='$quantity'></td>";
            echo "<td><input type='number' name='price[$i]' value='$price'></td>";
            echo "<td><input type='number' name='amount[$i]' value='$amount' readonly></td>";
            echo "<td>
                    <a href='javascript:void(0)' class='btn btn-primary' onclick='window.print()'>Print</a>
                    <a href='edit.php?billNo=" . $row['BillNo'] . "&PatId=$patid' class='btn btn-warning'>Edit</a>
                  </td>";  // Print and Edit buttons
            echo "</tr>";
            $i++;
        }
        ?>
        <tr>
            <td colspan="6" style="text-align:right;"><strong>Total Amount</strong></td>
            <td><strong><?php echo $totalAmount; ?></strong></td>
        </tr>
    </tbody>
</table>

<br>

<!-- Total Amount in Words -->
<div>
    <h6 style="font-weight:bold"><?php echo getIndianCurrency($totalAmount) . " only/-"; ?></h6>
</div>

<?php
// Function to convert totalAmount to Indian currency format
function getIndianCurrency($amount) {
    $no = round($amount);
    $decimal = round($amount - $no, 2) * 100;
    $digits = array('', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 
                    'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen');
    $tens = array('', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety');
    $hundreds = array('', 'Hundred', 'Thousand', 'Lakh', 'Crore');

    $decimal = ($decimal > 0) ? "and " . $decimal . " Paise" : "";
    $currency = "";

    if ($no > 0) {
        $unit = $no % 10;
        $tenth = floor($no / 10) % 10;
        $hund = floor($no / 100);
        $thousand = floor($no / 1000);
        $lakh = floor($no / 100000);
        $crore = floor($no / 10000000);
        
        if ($crore > 0) {
            $currency .= $digits[$crore] . " Crore ";
        }
        if ($lakh > 0) {
            $currency .= $digits[$lakh] . " Lakh ";
        }
        if ($thousand > 0) {
            $currency .= $digits[$thousand] . " Thousand ";
        }
        if ($hund > 0) {
            $currency .= $digits[$hund] . " Hundred ";
        }

        if ($tenth > 1) {
            $currency .= $tens[$tenth] . " ";
            $currency .= $digits[$unit];
        } else {
            $currency .= $digits[$unit + ($tenth * 10)];
        }

        $currency .= " Rupees";
    }

    return $currency . " " . $decimal . " only/-";
}
?>

<!-- Print Button -->



      <div class="row mt-5">

        <div class="col-md-3">
         
        </div>
        <div class="col-md-6"></div>
        <div class="col-md-3">
          Authorized Signatory</h6>
          <br><br>
           <h6>Prepared by <?php echo $name ?></h6>
        </div>
      </div>


    </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>
   