<?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">';
}

if (is_array($_POST['part'])) {
    $part = $_POST['part'];
    $consultant = $_POST['consultant'];
    $qty = $_POST['qty'];
    $optionlab = $_POST['optionlab'];
    $Price = $_POST['price'];
    $size = $_POST['size'];
    $patid = $_POST['patid'];
    $billtype = $_POST['billtype'];
    $bdate = $_POST['bdate'];
    $btime = $_POST['btime'];
    $payable = $_POST['payable'];
    $preparedby = $_POST['preparedby'];
    $InsuranceName = $_POST['InsuranceName'];
    $InsuranceNo = $_POST['InsuranceNo'];
    $bank = $_POST['bank'];
    $cardno = $_POST['cardno'];
    $upi = $_POST['upi'];
    $cash = $_POST['cash'];
    $upiid = $_POST['upiid'];

    $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'];
        }
    } 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">

    <!-- 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>
</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">
                        </div>
                        <div class="col-md-3"></div>
                        <div class="col-md-6">
                            <table>
                                <tr>
                                    <td valign="top">Address</td>
                                    <td>:2876, 4th Cross, NS Road, Chamundipuram, Mysuru, Karnataka 570004</td>
                                </tr>
                                <tr>
                                    <td valign="top">Website</td>
                                    <td>:www.vidhyaran.com</td>
                                </tr>
                                <tr>
                                    <td valign="top">Contact</td>
                                    <td>:+91-821 233 1655</td>
                                </tr>
                                <tr>
                                    <td>Unit</td>
                                    <td>:Mysore</td>
                                </tr>

                            </table>
                        </div>
                    </div>
                    <hr>
                    <div class="row">
                        <div class="col-md-6">
                            <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 No.</td>
                                    <td valign="top">: <?php echo $contact1; ?></td>
                                </tr>
                                <tr>
                                    <td valign="top">Email Id</td>
                                    <td valign="top">: <?php echo $email; ?></td>
                                </tr>
                                <tr>
                                    <td valign="top">Address</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">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>
                                    <tr>
                                        <td valign="top">Bill Type/Bill Date-Time</td>
                                        <td valign="top">: <?php echo $billtype . "/ " . $bdate . ", " . $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>Cash Amount / UPI Amount</td>";
                                        echo "<td>:$cash, $upi</td></tr>";
                                    }
                                    ?>
                                </table>
                            </div>
                        <?php } else { ?>
                            <div class="col-md-6">
                                <table>
                                    <tr>
                                        <td valign="top">Gender/Age</td>
                                        <td valign="top">: <?php echo $gender . "/ " . $age; ?></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">Bill Type/Bill Date-Time</td>
                                        <td valign="top">: <?php echo $billtype . "/ " . $bdate . ", " . $btime; ?></td>
                                    </tr>
                                </table>
                            </div>
                        <?php } ?>




                    </div>
                    <br>
                    <form action="editedBillprev.php" method="POST">
                        <table class="table table-hover">
                            <thead>
                                <th><input type="checkbox" id="select-all"></th>
                                <th scope="col">Sl No.</th>
                                <th scope="col">Particulars</th>
                                <th scope="col">Radiology / Lab</th>
                                <th scope="col">Consultant</th>
                                <th scope="col">Quantity</th>
                                <th scope="col">Unit Price</th>
                            </thead>
                            <tbody id="billTable">
                                <?php
                                for ($i = 0; $i < $size; $i++):
                                    $current_particular = $part[$i];
                                    $current_consultant = $consultant[$i];
                                ?>
                                    <tr>
                                        <td><input type="checkbox" class="row-select"></td>
                                        <td class="serial-number"><?= $i + 1; ?></td>
                                        <td>
                                            <select name="part[]" class="form-control particulars-select" required>
                                                <option value="">Select Particular</option>
                                                <?php
                                                $qu2 = mysqli_query($db, "SELECT * FROM particulars ORDER BY Description ASC");
                                                while ($row = $qu2->fetch_assoc()) {
                                                    $desp = $row['Description'];
                                                    $price = $row['Price'];
                                                    $selected = ($desp == $current_particular) ? 'selected' : '';
                                                    echo "<option value=\"$desp\" data-price=\"$price\" $selected>$desp</option>";
                                                }
                                                ?>
                                            </select>
                                        </td>
                                        <td>
                                            <select name="optionlab[]" class="form-control">
                                                <option value="">N/A</option>
                                                <option value="Radiology">Radiology</option>
                                                <option value="Lab Test">Laboratory</option>
                                                <option value="Cardiology">Cardiology</option>
                                            </select>
                                        </td>

                                        <td>
                                            <select name="consultant[]" class="form-control consultant-select" required>
                                                <option value="">Select Consultant</option>
                                                <?php
                                                $qu2 = mysqli_query($db, "SELECT `doc_name` FROM `docotorslist`");
                                                while ($row = $qu2->fetch_assoc()) {
                                                    $doc_name = $row['doc_name'];
                                                    $selected = ($doc_name == $current_consultant) ? 'selected' : '';
                                                    echo "<option value=\"$doc_name\" $selected>$doc_name</option>";
                                                }
                                                ?>
                                            </select>
                                        </td>
                                        <td>
                                            <input type="number" name="qty[]" class="form-control" value="<?= $qty[$i] ?>" min="1" required>
                                        </td>
                                        <td>
                                            <input type="number" name="price[]" class="form-control price-input" value="<?= $Price[$i] ?>">
                                        </td>
                                    </tr>
                                <?php endfor; ?>
                            </tbody>
                        </table>
                        <button type="button" class="btn btn-success" id="add-row">Add Row</button>
                        <button type="button" class="btn btn-danger" id="remove-selected">Remove Selected</button>
                        <br><br>
                        <!--<div class="input-group control-group after-add-more">         -->
                        <!-- <button class="btn btn-success add-more" type="button"><i class="glyphicon glyphicon-plus"></i>Add Particulars</button>-->
                        <!--</div><br>-->

                        <br>
                        <div class="row">
                            <div class="col-md-1"></div>
                            <div class="col-md-7"></div>
                            <div class="col-md-4">
                                <br>
                                <h6>Prepared by <?php echo $name ?></h6><br>
                                <h6>Authorized Signatory</h6>
                            </div>
                        </div>
                        <br><br><br><br>

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

            </div>
        </div>


        <?php
        foreach ($part as $value) {
            echo '<input type="hidden" name="Epart[]" value="' . $value . '">';
        }

        foreach ($consultant as $value) {
            echo '<input type="hidden" name="consultant[]" value="' . $value . '">';
        }
        foreach ($optionlab as $value) {
            echo '<input type="hidden" name="optionlab[]" value="' . $value . '">';
        }
        ?>
        <input type="hidden" name="size" value="<?php echo $size; ?>">
        <input type="hidden" name="patid" value="<?php echo $patid; ?>">
        <input type="hidden" name="billtype" value="<?php echo $billtype; ?>">
        <input type="hidden" name="bdate" value="<?php echo $bdate; ?>">
        <input type="hidden" name="btime" value="<?php echo $btime; ?>">
        <input type="hidden" name="preparedby" value="<?php echo $name; ?>">
        <input type="hidden" name="InsuranceName" value="<?php echo $InsuranceName; ?>">
        <input type="hidden" name="InsuranceNo" value="<?php echo $InsuranceNo; ?>">
        <input type="hidden" name="bank" value="<?php echo $bank; ?>">
        <input type="hidden" name="cardno" value="<?php echo $cardno; ?>">
        <input type="hidden" name="upi" value="<?php echo $upi; ?>">
        <input type="hidden" name="cash" value="<?php echo $cash; ?>">
        <input type="hidden" name="upiid" value="<?php echo $upiid; ?>">
        <div style="text-align: center;">
            <button type="submit" class="btn btn-primary">Preview Bill</button>
        </div>
        </form>
    </section>
    <script>
        $(document).ready(function() {
            // Fetch and store price data
            let particularsData = {};
            <?php
            $qu2 = mysqli_query($db, "SELECT * FROM particulars ORDER BY Description ASC");
            while ($row = $qu2->fetch_assoc()) {
                echo "particularsData['" . addslashes($row['Description']) . "'] = " . $row['Price'] . ";";
            }
            ?>

            // Update price when a particular is selected
            $(document).on('change', '.particulars-select', function() {
                let selectedParticular = $(this).find(':selected').text();
                let price = particularsData[selectedParticular] || 0;
                $(this).closest('tr').find('.price-input').val(price);
            });

            // Add Row Functionality
            $('#add-row').click(function() {
                const tableBody = $('#billTable');
                const rowCount = tableBody.find('tr').length + 1;
                let newRow = `
            <tr>
                <td><input type="checkbox" class="row-select"></td>
                <td class="serial-number">${rowCount}</td>
                <td>
                    <select name="part[]" class="form-control particulars-select" required>
                        <option value="">Select Particular</option>
                        <?php
                        $qu2 = mysqli_query($db, "SELECT * FROM particulars ORDER BY Description ASC");
                        while ($row = $qu2->fetch_assoc()) {
                            echo "<option value=\"" . addslashes($row['Description']) . "\" data-price=\"" . $row['Price'] . "\">" . $row['Description'] . "</option>";
                        }
                        ?>
                    </select>
                </td>
                <td>
                    <select name="optionlab[]" class="form-control">
                        <option value="">N/A</option>
                        <option value="Radiology">Radiology</option>
                        <option value="Lab Test">Laboratory</option>
                        <option value="Cardiology">Cardiology</option>
                    </select>
                </td>
                <td>
                    <select name="consultant[]" class="form-control consultant-select" required>
                        <option value="">Select Consultant</option>
                        <?php
                        $qu2 = mysqli_query($db, "SELECT `doc_name` FROM `docotorslist`");
                        while ($row = $qu2->fetch_assoc()) {
                            echo "<option value=\"" . addslashes($row['doc_name']) . "\">" . $row['doc_name'] . "</option>";
                        }
                        ?>
                    </select>
                </td>
                <td>
                    <input type="number" name="qty[]" class="form-control" value="1" min="1" required>
                </td>
                <td>
                    <input type="number" name="price[]" class="form-control price-input" value="">
                </td>
            </tr>
            `;

                tableBody.append(newRow);
            });

            // Remove Selected Rows
            $('#remove-selected').click(function() {
                $('#billTable .row-select:checked').closest('tr').remove();

                // Recalculate serial numbers
                $('#billTable .serial-number').each(function(index) {
                    $(this).text(index + 1);
                });
            });
        });
    </script>
    <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>