<?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 (is_array($_POST['Particulars'])) {
        $part = $_POST['Particulars'];
        $consultant = $_POST['consultant'];
        $qty = $_POST['Quantity'];
        $optionlab = $_POST['optionlab'];
        $size = sizeof($part);
        $Total = 0;
        $Amount[] = 0;
        $disamount = 0;
        for ($i = 0; $i < $size; $i++) {
        // Use a prepared statement to safely handle special characters
        $stmt = $db->prepare("SELECT * FROM particulars WHERE Description = ?");
        $stmt->bind_param("s", $part[$i]); // 's' indicates a string parameter
        $stmt->execute();
        $result = $stmt->get_result();

        while ($row = $result->fetch_assoc()) {
            $price[$i] = $row['Price'];
            $Amount[$i] = $price[$i] * $qty[$i];
        }
        $Total += $Amount[$i];

        $stmt->close(); // Close the statement
    }

        $patid = $_POST['patid'];
        $billtype = $_POST['billtype'];
        $InsuranceName = $_POST['InsuranceName'];
        $InsuranceNo = $_POST['InsuranceNo'];
        $bank = $_POST['bank'] ?? '';
    $cardno = $_POST['cardno'] ?? '';
    $bank_a = $_POST['bank-a'] ?? '';
    $cardno_a = $_POST['cardno-a'] ?? '';
    $cash_a = $_POST['cash-a'] ?? '';
    $card_a = $_POST['card-a'] ?? '';
        $upi = $_POST['upi'];
        $cash = $_POST['cash'];
        $bdate = $_POST['bdate'];
        $btime = $_POST['btime'];
        $payable = $Total - $disamount;
    } else if (is_array($_POST['Epart'])) {
        $part = $_POST['Epart'];
        $consultant = $_POST['consultant'];
        $qty = $_POST['qty'];
        $optionlab = $_POST['optionlab'];
        $price = $_POST['price'];
        $size = $_POST['size'];
        $patid = $_POST['patid'];
        $billtype = $_POST['billtype'];
        $InsuranceName = $_POST['InsuranceName'];
        $InsuranceNo = $_POST['InsuranceNo'];
        $bank = $_POST['bank'] ?? '';
    $cardno = $_POST['cardno'] ?? '';
    $bank_a = $_POST['bank-a'] ?? '';
    $cardno_a = $_POST['cardno-a'] ?? '';
    $cash_a = $_POST['cash-a'] ?? '';
    $card_a = $_POST['card-a'] ?? '';
        $upi = $_POST['upi'];
        $cash = $_POST['cash'];
        $bdate = $_POST['bdate'];
        $btime = $_POST['btime'];
        $Total = 0;
        for ($i = 0; $i < $size; $i++) {
            $Amount[$i] = $price[$i] * $qty[$i];
            $Total += $Amount[$i];
        }
        $payable = $Total - $disamount;
    }



    $advanceAmount = 0;
    $advance = mysqli_query($db, "SELECT `SlNo`, `BillNo`, `PatId`, `Amount`, `AdvanceDate`, `advanceTime` 
          FROM `advance` WHERE PatId='$patid'");
    while ($row = $advance->fetch_assoc()) {
        $advanceAmount += $row['Amount'];
    }


    $qu4 = mysqli_query($db, "SELECT * FROM patientdetails WHERE PatId='$patid'");
    if (mysqli_num_rows($qu4) != 0) {
        while ($row = $qu4->fetch_assoc()) {
            $title = $row['title'];
            $Pname = $row['Name'];
            $contact1 = $row['Contact'];
            $email = $row['Email'];
            $dob = $row['Dob'];
            $age = $row['Age'];
            $gender = $row['Gender'];
            $blood = $row['BloodGroup'];
            $Address = $row['Address'];
            $Consultant = $row['Consultant'];
            $PatientType = "IPD";
            $AdmitDate = $row['AdmitDate'];
            $AdmitTime = $row['AdmitTime'];
            $ipd = "IN";
        }
    } else {
        $qu5 = mysqli_query($db, "SELECT * FROM opdpatientregister WHERE PatientId='$patid'");
        while ($row = $qu5->fetch_assoc()) {
            $title = $row['title'];
            $Pname = $row['Name'];
            $contact1 = $row['Contact'];
            $email = $row['Email'];
            $age = $row['Age'];
            $gender = $row['Gender'];
            $Address = $row['Address'];
            $Consultant = $row['Consultant'];
            $PatientType = "OPD";
        }
    }
    ?>
<!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>
        .table td,
        .table th {
            padding: 2px;
        }
    </style>

</head>

<body>

    <section>
        <div class="row">
            <div class="col-md-1"></div>
            <div class="col-md-10">


                <div class="shadow p-3 mb-5 bg-light rounded">
                    <div class="row">
                        <div class="col-md-3">
                            <img src="assets/img/logo.png" style="width:196px">
                        </div>

                        <div class="col-md-6">
                            <p style="text-align:center;margin:0px">
                                <label style="font-size: 19pt;"> <b>Vidyaranya Hospital PVt LTD</b> </label><br />
                                <label>2876, 4th Cross, NS Road, Chamundipuram, Mysuru, Karnataka 570004</label><br />
                                <label><b>Ph no:</b> +91-821 233 1655, +91-821 233 0555, +91 9035933963</label><br />
                                <label style="font-size:15pt"><b><?php if ($ipd == 'IN') {
                                                                        echo $ipd;
                                                                    } else {
                                                                        echo "OUT";
                                                                    } ?> PATIENT BILL</b></label>
                            </p>

                          
                        </div>
                        <div class="col-md-3"></div>
                    </div>
                    <hr style="margin-top:0px;margin-bottom:0px">
                    <div class="row">
                        <div class="col-md-6">
                            <table>
                                <tr>
                                    <td valign="top"><b>Patient Id</b></td>
                                    <td valign="top">: <?php echo $patid; ?></td>
                                </tr>
                                <tr>
                                    <td>Patient Name</td>
                                    <td>: <strong><?php echo $title . ' ' . $Pname; ?></strong></td>


                                </tr>
                                <tr>
                                    <td valign="top"><b>Contact No.</b></td>
                                    <td valign="top">: <?php echo $contact1; ?></td>
                                </tr>
                                <!--<tr>
		<td valign="top"><b>Email Id</b></td>
		<td valign="top">: <?php echo $email; ?></td>
	</tr>-->
                                <tr>
                                    <td valign="top"><b>Address</b></td>
                                    <td valign="top">: <?php echo $Address; ?></td>
                                </tr>

                            </table>
                        </div><?php
                                if ($PatientType == "IPD") { ?>
                            <div class="col-md-6">
                                <table>
                                    <tr>
                                        <td valign="top"><b>Gender/Age/Blood Group</b></td>
                                        <td valign="top">: <?php echo $gender . "/ " . $age . " / " . $blood; ?></td>
                                    </tr>

                                    <tr>
                                        <td valign="top"><b>Consultant</b></td>
                                        <td valign="top">: <?php echo $Consultant; ?></td>
                                    </tr>
                                    <tr>
                                        <td valign="top"><b>Patient Type</b></td>
                                        <td valign="top">: <?php echo $PatientType; ?></td>
                                    </tr>
                                    <tr>
                                        <td valign="top"><b>Admit Date/Time</b></td>
                                        <td valign="top">: <?php echo $AdmitDate . ", " . date('H:i A', strtotime($AdmitTime)); ?></td>
                                    </tr>
                                    <tr>
                                        <td valign="top"><b>Bill Type/Bill Date-Time</b></td>
                                        <td valign="top">: <?php echo $billtype . "/ " . $bdate . ", " . date('H:i A', strtotime($btime)); ?></td>
                                    </tr>
                                    <?php
                                    if ($billtype == "Insurance") {
                                        echo "<tr><td>Insurance Name / Number</td>";
                                        echo "<td>:$InsuranceName, $InsuranceNo</td></tr>";
                                    }
                                    if ($billtype == "Card") {
                                        echo "<tr><td>Bank Name / Card Number</td>";
                                        echo "<td>:$bank, $cardno</td></tr>";
                                    }
                                    if ($billtype == "Cash and UPI") {
                                        echo "<tr><td><b>Cash Amount / UPI Amount</b></td>";
                                        echo "<td>:$cash, $upi</td></tr>";
                                    }
                                    ?>
                                </table>
                            </div>
                        <?php } else { ?>
                            <div class="col-md-6">
                                <table>
                                    <tr>
                                        <td valign="top"><b>Gender/Age</b></td>
                                        <td valign="top">: <?php echo $gender . "/ " . $age; ?></td>
                                    </tr>

                                    <tr>
                                        <td valign="top"><b>Consultant</b></td>
                                        <td valign="top">: <?php echo $Consultant; ?></td>
                                    </tr>
                                    <tr>
                                        <td valign="top"><b>Patient Type</b></td>
                                        <td valign="top">: <?php echo $PatientType; ?></td>
                                    </tr>
                                    <tr>
                                        <td valign="top"><b>Bill Type/Bill Date-Time</b></td>
                                        <td valign="top">: <?php echo $billtype . "/ " . $bdate . ", " . $btime; ?></td>
                                    </tr>
                                </table>
                            </div>
                        <?php } ?>




                    </div>

                    <table class="table table-hover">
                        <thead>
                            <th scope="col">Sl No.</th>
                            <th scope="col">Particulars</th>
                            <th scope="col">Department</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 for ($i = 0; $i < $size; $i++) : ?>
                                <tr>
                                    <td><?= $i + 1; ?></td>
                                    <td><?= $part[$i]; ?></td>
                                    <td><?= !empty($optionlab[$i]) ? $optionlab[$i] : 'N/A'; ?></td>
                                    <td><?= $consultant[$i]; ?></td>
                                    <td><?= $qty[$i]; ?></td>
                                    <td><?= $price[$i]; ?></td>
                                    <td><?= $Amount[$i]; ?></td>
                                </tr>
                            <?php endfor; ?>
                            <tr>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td>Sub Total</td>
                                <td colspan=""><?= $Total; ?></td>
                            </tr>
                        </tbody>
                    </table>

                    <div class="row">
                        <!--<div class="col-md-6">-->
                        <!--           <table class="table">-->
                              

                        <!--        <tr>-->
                        <!--        	<td><b>Advance &nbsp;&nbsp;&nbsp;&nbsp;:</b></td>-->
                        <!--        	<td><b>-->
                        <!--        <?php echo $advanceAmount; ?>-->
                        <!--        	    </b></td>-->
                        <!--        </tr>-->
                        <!--        </table>-->
                        <!--</div>-->
                        <div class="col-md-6">
                            <table class="table">
                                <tr>
                                    <td><b>Total Bill Amount &nbsp;&nbsp;&nbsp;&nbsp;:</b></td>
                                    <td><b><?php echo $Total; ?></b></td>
                                </tr>

                                <!--<tr>-->
                                <!--	<td><b>Advance &nbsp;&nbsp;&nbsp;&nbsp;:</b></td>-->
                                <!--	<td><b>-->
                                <!--<?php echo $advanceAmount; ?>-->
                                <!--	    </b></td>-->
                                <!--</tr>-->
                                <?php

                                function getIndianCurrency(float $number)
                                {
                                    $decimal = round($number - ($no = floor($number)), 2) * 100;
                                    $hundred = null;
                                    $digits_length = strlen($no);
                                    $i = 0;
                                    $str = array();
                                    $words = array(
                                        0 => '',
                                        1 => 'one',
                                        2 => 'two',
                                        3 => 'three',
                                        4 => 'four',
                                        5 => 'five',
                                        6 => 'six',
                                        7 => 'seven',
                                        8 => 'eight',
                                        9 => 'nine',
                                        10 => 'ten',
                                        11 => 'eleven',
                                        12 => 'twelve',
                                        13 => 'thirteen',
                                        14 => 'fourteen',
                                        15 => 'fifteen',
                                        16 => 'sixteen',
                                        17 => 'seventeen',
                                        18 => 'eighteen',
                                        19 => 'nineteen',
                                        20 => 'twenty',
                                        30 => 'thirty',
                                        40 => 'forty',
                                        50 => 'fifty',
                                        60 => 'sixty',
                                        70 => 'seventy',
                                        80 => 'eighty',
                                        90 => 'ninety'
                                    );
                                    $digits = array('', 'hundred', 'thousand', 'lakh', 'crore');
                                    while ($i < $digits_length) {
                                        $divider = ($i == 2) ? 10 : 100;
                                        $number = floor($no % $divider);
                                        $no = floor($no / $divider);
                                        $i += $divider == 10 ? 1 : 2;
                                        if ($number) {
                                            $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
                                            $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
                                            $str[] = ($number < 21) ? $words[$number] . ' ' . $digits[$counter] . $plural . ' ' . $hundred : $words[floor($number / 10) * 10] . ' ' . $words[$number % 10] . ' ' . $digits[$counter] . $plural . ' ' . $hundred;
                                        } else $str[] = null;
                                    }
                                    $Rupees = implode('', array_reverse($str));
                                    $paise = ($decimal > 0) ? "." . ($words[$decimal / 10] . " " . $words[$decimal % 10]) . ' Paise' : '';
                                    return ($Rupees ? $Rupees . 'Rupees ' : '') . $paise;
                                }

                                ?>

                                <tr>
                                    <td><b>Patient Payable Amount &nbsp;&nbsp;&nbsp;&nbsp;:</b></td>
                                    <td><b><?php echo $payable; ?></b></td>
                                </tr>
                            </table>


                        </div>
                        
                        <div>
                            <h6 style="font-weight:bold; margin-left:15px"><?php echo getIndianCurrency($Total); ?></h6>
                        </div>
                    </div>
                    <br><br><br>
                    <div class="row">
                        <div class="col-md-1"></div>
                        <div class="col-md-7">



                        </div>
                        <div class="col-md-4">

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

                </div>
                <div class="col-md-1"></div>

            </div>
        </div>
        <div class="row">


            <form action="store.php" method="POST">
                <?php
                foreach ($part as $value) {
                    echo '<input type="hidden" name="part[]" value="' . htmlspecialchars($value) . '">';
                }
                foreach ($consultant as $value) {
                    echo '<input type="hidden" name="consultant[]" value="' . htmlspecialchars($value) . '">';
                }
                foreach ($qty as $value) {
                    echo '<input type="hidden" name="qty[]" value="' . htmlspecialchars($value) . '">';
                }
                foreach ($optionlab as $value) {
                    echo '<input type="hidden" name="optionlab[]" value="' . htmlspecialchars($value) . '">';
                }
                foreach ($Amount as $value) {
                    echo '<input type="hidden" name="Amount[]" value="' . htmlspecialchars($value) . '">';
                }
                foreach ($price as $value) {
                    echo '<input type="hidden" name="price[]" value="' . htmlspecialchars($value) . '">';
                }
                ?>
                <input type="hidden" name="size" value="<?php echo htmlspecialchars($size); ?>">
                <input type="hidden" name="Total" value="<?php echo htmlspecialchars($Total); ?>">
                <input type="hidden" name="patid" value="<?php echo htmlspecialchars($patid); ?>">
                <input type="hidden" name="billtype" value="<?php echo htmlspecialchars($billtype); ?>">
                <input type="hidden" name="bdate" value="<?php echo htmlspecialchars($bdate); ?>">
                <input type="hidden" name="btime" value="<?php echo htmlspecialchars($btime); ?>">
                <input type="hidden" name="payable" value="<?php echo htmlspecialchars($payable); ?>">
                <input type="hidden" name="patienttype" value="<?php echo htmlspecialchars($PatientType); ?>">
                <input type="hidden" name="preparedby" value="<?php echo htmlspecialchars($name); ?>">
                <input type="hidden" name="InsuranceName" value="<?php echo htmlspecialchars($InsuranceName); ?>">
                <input type="hidden" name="InsuranceNo" value="<?php echo htmlspecialchars($InsuranceNo); ?>">
                <input type="hidden" name="bank" value="<?php echo htmlspecialchars($bank); ?>">
                <input type="hidden" name="cardno" value="<?php echo htmlspecialchars($cardno); ?>">
                <input type="hidden" name="upi" value="<?php echo htmlspecialchars($upi); ?>">
                <input type="hidden" name="cash" value="<?php echo htmlspecialchars($cash); ?>">
                <input type="hidden" name="bank-a" value="<?php echo $bank_a; ?>">
                <input type="hidden" name="cardno-a" value="<?php echo $cardno_a; ?>">
                <input type="hidden" name="cash-a" value="<?php echo $cash_a; ?>">
                <input type="hidden" name="card-a" value="<?php echo $card_a; ?>">
                <div style="text-align: center;">
                    <button type="submit" class="btn btn-primary">Confirm and Save</button>
                </div>
            </form>

            <form action="EditBill.php" method="POST">
                <?php
                foreach ($part as $value) {
                    echo '<input type="hidden" name="part[]" value="' . htmlspecialchars($value) . '">';
                }
                foreach ($consultant as $value) {
                    echo '<input type="hidden" name="consultant[]" value="' . htmlspecialchars($value) . '">';
                }
                foreach ($qty as $value) {
                    echo '<input type="hidden" name="qty[]" value="' . htmlspecialchars($value) . '">';
                }
                foreach ($optionlab as $value) {
                    echo '<input type="hidden" name="optionlab[]" value="' . htmlspecialchars($value) . '">';
                }
                foreach ($Amount as $value) {
                    echo '<input type="hidden" name="Amount[]" value="' . htmlspecialchars($value) . '">';
                }
                foreach ($price as $value) {
                    echo '<input type="hidden" name="price[]" value="' . htmlspecialchars($value) . '">';
                }
                ?>
                <input type="hidden" name="size" value="<?php echo htmlspecialchars($size); ?>">
                <input type="hidden" name="Total" value="<?php echo htmlspecialchars($Total); ?>">
                <input type="hidden" name="patid" value="<?php echo htmlspecialchars($patid); ?>">
                <input type="hidden" name="billtype" value="<?php echo htmlspecialchars($billtype); ?>">
                <input type="hidden" name="bdate" value="<?php echo htmlspecialchars($bdate); ?>">
                <input type="hidden" name="btime" value="<?php echo htmlspecialchars($btime); ?>">
                <input type="hidden" name="payable" value="<?php echo htmlspecialchars($payable); ?>">
                <input type="hidden" name="patienttype" value="<?php echo htmlspecialchars($PatientType); ?>">
                <input type="hidden" name="preparedby" value="<?php echo htmlspecialchars($name); ?>">
                <input type="hidden" name="InsuranceName" value="<?php echo htmlspecialchars($InsuranceName); ?>">
                <input type="hidden" name="InsuranceNo" value="<?php echo htmlspecialchars($InsuranceNo); ?>">
                <input type="hidden" name="bank" value="<?php echo htmlspecialchars($bank); ?>">
                <input type="hidden" name="cardno" value="<?php echo htmlspecialchars($cardno); ?>">
                <input type="hidden" name="upi" value="<?php echo htmlspecialchars($upi); ?>">
                <input type="hidden" name="cash" value="<?php echo htmlspecialchars($cash); ?>">
                <input type="hidden" name="bank-a" value="<?php echo $bank; ?>">
                <input type="hidden" name="cardno-a" value="<?php echo $cardno; ?>">
                <input type="hidden" name="cash-a" value="<?php echo $cash_a; ?>">
                <input type="hidden" name="card-a" value="<?php echo $card_a; ?>">
                <div style="text-align: center;">
                    <button type="submit" class="btn btn-danger">Edit</button>
                </div>
            </form>


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