<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ("includes/function.php");

$zdb = $_SESSION["zdbyear_$dbname"];
mysqli_select_db($conn, $zdb);
$zroot = $_SESSION["zdir"];
$usertype = $_SESSION["usertype"];

if ($usertype != 1) {
    include("includes/menu_rights.php");
}
$vouchno = $_POST["vouchno"];
$acttype = $_POST["acttype"];
if ($acttype == "P") {
    $result = mysqli_query($conn, "SELECT *, date_format(`vouchdt`,'%d-%m-%Y') as mydt FROM `consultmain` WHERE vouchno='$vouchno'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);

    $fbalamt = 0;
    $getpay = mysqli_query($conn, "SELECT sum(amount) as tamt FROM `actran` WHERE slvouch='$vouchno' and accode = '$get[accode]'") or die(mysqli_error($conn));
    if (!empty($getpay)) {
        $mpay = mysqli_fetch_array($getpay);
        $paidamt = $mpay["tamt"];
        $fbalamt = $get['billamt'] - $paidamt;
    }

    if ($get['trpaytype'] == 'I') {
        $trval = "Installment";
    } else if ($get['trpaytype'] == 'F') {
        $trval = "Full Payment";
    } else if ($get['trpaytype'] == 'E') {
        $trval = "EMI";
    } else {
        $trval = "";
    }
    ?>
    <script type="text/javascript">
        $(document).ready(function () {
            $(".form").attr("disabled", true);
        })
    </script>
    <button type='button' class='btn btn-primary'><a href='consult_ent.php?vouchno=<?= $get['vouchno'] ?>&mode=edit' target='_blank' class='clickable-row2' style='color:white !important'>View / Edit</a></button>


    <div class="row">
        <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
            <label class="label-control marg">Concerns</label>
        </div>
        <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
            <input type="text" class="form-control form concern1" name="concern1" value="<?php echo $get["concern1"]; ?>" maxlength="40">
        </div>

        <div class="col-lg-4 col-md-4 col-sm-3 col-xs-3  text-center">
            <label class='label-control marg' style="color:red">Payment Type - <?= $trval ?></label>
        </div>
    </div>
    <div class="row">
        <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
            <label class="label-control marg">Diagnosis</label>
        </div>
        <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
            <input type="text" class="form-control form diag1" name="diag1" value="<?php echo $get["diag1"]; ?>" maxlength="40">
            <input type="text" class="form-control form diag2" name="diag2" value="<?php echo $get["diag2"]; ?>" maxlength="40">
        </div>

    </div>
    <div class="row">
        <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
            <label class="label-control marg">Investigation</label>
        </div>
        <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
            <input type="text" class="form-control form invs" name="invs" value="<?php echo $get["invs"]; ?>">
        </div>

    </div>
    <br>
    <div class="row">
        <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
            <label class="label-control marg">Symptoms</label>
        </div>
        <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad sympbody">
            <?php
            $sctr = 0;
            $gtsmtran = mysqli_query($conn, "Select * from `consultsymp` where vouchno='$get[vouchno]' order by srno");
            if (mysqli_num_rows($gtsmtran) > 0) {
                while ($rowsmt = mysqli_fetch_array($gtsmtran)) {
                    echo "<input type='text' class='form-control form symptoms' name='symptoms_$sctr' value='$rowsmt[symptoms]' maxlength='40'  autocomplete='on'>";
                    $sctr++;
                }
            }
            ?>
        </div>

    </div>
    <div class="row table-responsive" style="padding-left:2%">
        <h4>Prescription</h4>

        <div class="col-lg-6">
            <?php
            $tctr = 0;
            $gtsmtrant = mysqli_query($conn, "Select a.*,b.name from `consulttreat` a left join treatment b
                                                                                             on a.treatment = b.code where a.vouchno='$get[vouchno]' order by a.srno");
            if (mysqli_num_rows($gtsmtrant) > 0) {
                ?>
                <table class="table table-bordered" style="width: 80%">
                    <thead>
                        <tr class="mySearchTr">
                            <th style="width:3%">Sr.</th>
                            <th style="width:30%">Treatment</th>
                            <th style="width:5%" class='text-right'>Nos.</th>
                            <th style="width:5%" class='text-right'>Days</th>
                        </tr>

                    </thead>

                    <!-- Laundry section starts from here -->
                    <tbody class="treatbody">
                        <?php
                        while ($rowsmtt = mysqli_fetch_array($gtsmtrant)) {
                            $chkd = "";
                            if ($rowsmtt['aprov'] == 1) {
                                $chkd = "checked";
                            }

                            echo "<tr>";
                            echo "<td>$rowsmtt[srno]";
                            echo "<td>";
                            echo "$rowsmtt[name]";
                            echo "<td>$rowsmtt[qty]</td>";
                            echo "<td>$rowsmtt[duration]</td>";
                            echo "</tr>";
                            $tctr++;
                        }
                        ?>
                </table>
            <?php }
            ?>
        </div>
        <!--                </div>
        
                    <div class="row table-responsive">-->
        <div class="col-lg-6">
            <?php
            $mdctr = 0;
            $gtmdtran = mysqli_query($conn, "Select a.*,b.name from `consultmed` a left join product b
                                                                                             on a.item = b.code where a.vouchno='$get[vouchno]' order by a.srno");
            if (mysqli_num_rows($gtmdtran) > 0) {
                ?>

                <table class="table table-bordered" style="width: 90%">
                    <thead>
                        <tr class="mySearchTr">
                            <th style="width:3%">Sr.</th>
                            <th style="width:40%">Medicine</th>
                            <th style="width:15%">Dosage</th>
                            <th style="width:5%" class='text-right'>No. Days</th>
                            <th style="width:5%" class='text-center'>Morning / Night</th>
                        </tr>

                    </thead>

                    <!-- Laundry section starts from here -->
                    <tbody class="medbody">
                        <?php
                        while ($rowsmd = mysqli_fetch_array($gtmdtran)) {
                            $altime = $altime1 = $altt = $altime2 = "";
                            if ($rowsmd['daytake'] == 1) {
                                $altime1 = "M";
                            }
                            if ($rowsmd['ngttake'] == 1) {
                                $altime2 = "N";
                            }
                            if ($rowsmd['daytake'] == 1 && $rowsmd['ngttake'] == 1) {
                                $altt = " - ";
                            }
                            $altime = $altime1 . $altt . $altime2;


                            echo "<tr>";

                            echo "<td>$rowsmd[srno]</td>";
                            echo "<td>";
                            echo "$rowsmd[name]";
                            echo "</td>";
                            echo "<td>";
                            echo "$rowsmd[dosage]";
                            echo "</td>";
                            echo "<td>$rowsmd[duration]</td>";
                            echo "<td>";
                            echo "$altime";
                            echo "</td>";
                            echo "</tr>";
                            $mdctr++;
                        }
                        ?>
                        </div>
                </table>
            <?php }
            ?>
        </div>
    </div>
    <?php
    if ($get['ttamount'] + $get['tmedamount'] > 0) {
        ?>
        <div class="row" style="padding-top:1%;padding-left:2%">
            <h4>Billing</h4>

            <div class="col-lg-6 ">

                <table class="table table-bordered" style="width: 90%">
                    <thead>
                        <tr class="mySearchTr">
                            <th style="width:3%">Sr.</th>
                            <th style="width:30%">Treatment</th>
                            <th style="width:5%" class='text-right'>Nos.</th>
                            <th style="width:5%" class='text-right'>Days</th>
                            <th style="width:7%" class='text-right'>Rate</th>
                            <th style="width:10%" class='text-right'>Amount</th>
                            <th style="width:5%" class=''>First Date</th>
                        </tr>

                    </thead>

                    <!-- Laundry section starts from here -->
                    <tbody class="treatbillbody">
                        <?php
                        $btctr = 0;
                        $bmctr = 0;
                        $gtsmtrant2 = mysqli_query($conn, "Select a.*,date_format(a.firstdate,'%d-%m-%Y') as firstdate, b.name from `treatbill` a left join treatment b
                                                                                             on a.treatment = b.code where a.vouchno='$get[vouchno]' order by a.srno");
                        if (mysqli_num_rows($gtsmtrant2) > 0) {

                            while ($rowsmtt2 = mysqli_fetch_array($gtsmtrant2)) {
                                $bmctr++;
                                $chkd = "";
                                if ($rowsmtt2['aprov'] == 1) {
                                    $chkd = "checked";
                                }

                                echo "<tr class='caltreat'>";
                                echo "<td>$bmctr</td>";
                                echo "<td>";
                                echo "$rowsmtt2[name]";
                                echo "</td>";
                                echo "<td>$rowsmtt2[qty]</td>";
                                echo "<td>$rowsmtt2[duration]</td>";
                                echo "<td>$rowsmtt2[rate]</td>";
                                echo "<td>$rowsmtt2[amt]</td>";
                                echo "<td >$rowsmtt2[firstdate]</td>";
                                echo "</tr>";
                                $btctr++;
                            }
                        }
                        ?>

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

            <div class="col-lg-6">

                <table class="table table-bordered" style="width: 90%">
                    <thead>
                        <tr class="mySearchTr">
                            <th style="width:3%">Sr.</th>
                            <th style="width:40%">Medicine</th>
                            <th style="width:5%" class='text-right'>Qty.</th>
                            <th style="width:5%" class='text-right'>Rate</th>
                            <th style="width:5%" class='text-right'>Amount</th>
                        </tr>

                    </thead>

                    <tbody class="">
                        <?php
                        $bmctr = 0;
                        $bmdctr = 0;
                        $gtmdtran = mysqli_query($conn, "Select a.*,b.name from `medbill` a left join product b
                                                                                             on a.item = b.code where a.vouchno='$get[vouchno]' order by a.srno");
                        if (mysqli_num_rows($gtmdtran) > 0) {

                            while ($rowsmd = mysqli_fetch_array($gtmdtran)) {
                                $bmctr++;
                                echo "<tr>";
                                echo "<td>$bmctr</td>";
                                echo "<td>";
                                echo "$rowsmd[name]";
                                echo "</td>";
                                echo "<td>$rowsmd[qty]</td>";
                                echo "<td>$rowsmd[rate]</td>";
                                echo "<td>$rowsmd[amount]</td>";
                                echo "</tr>";
                                $bmdctr++;
                            }
                        }
                        ?>

                    </tbody>


                </table>
            </div>
        </div>
        <?php
    }

    $sdctr = 0;
    $rctr = 0;
    $gtsched = mysqli_query($conn, "SELECT a.*,DATE_FORMAT(a.`scheddate`, '%d-%m-%Y') AS scheddt,DATE_FORMAT(a.`compltdt`, '%d-%m-%Y') AS compltdate,
                                                    b.name from `schedtreat` a left join treatment b
                                                    on a.treatment = b.code where a.vouchno='$get[vouchno]' and a.treatment > 0 order by a.scheddate ASC");
    if (mysqli_num_rows($gtsched) > 0) {
        ?>
        <div class="row" style="padding-top:1%;padding-left:2%">
            <h4>Schedule</h4>

            <div class="col-lg-6 ">

                <table class="table table-bordered" style="width: 90%">

                    <div class="modal fade detTreatModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
                        <div class="modal-dialog modal-lg">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                                    <h4 class="modal-title" id="myLargeModalLabel"><? echo "&nbsp;&nbsp;"; ?>My Second Modal</h4>
                                </div>
                                <div class="modal-body dettreatment">

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


                    <thead>
                        <tr class="mySearchTr">
                            <th style="width:3%">Sr.</th>
                            <th style="width:30%">Treatment</th>
                            <th style="width:15%">Schedule Date</th>
                            <th style="width:30%">Remarks</th>
                            <th style="width:15%">Complete Date</th>
                            <th style="width:15%">Details</th>
                        </tr>

                    </thead>

                    <!-- Laundry section starts from here -->
                    <tbody class="treatbillbody">
                        <?php
                        while ($rowsched = mysqli_fetch_array($gtsched)) {
                            $rctr++;
                            $rd = "";
                            $vis = "display:none";

                            if ($rowsched['compltdt'] != "0000-00-00") {
                                $rd = "readonly";
                                $vis = "";
                            }
                            echo "<tr>";
                            echo "<td>$rctr</td>";
                            echo "<td>$rowsched[name]</td>";
                            echo "<td>$rowsched[scheddt]</td>";
                            echo "<td>$rowsched[remarks]</td>";
                            echo "<td>$rowsched[compltdt]"
                            . "<input type='hidden' class='scheddate' value='$rowsched[scheddate]'>"
                            . "<input type='hidden' class='vouchno' value='$rowsched[vouchno]'>"
                            . "<input type='hidden' class='accode' value='$rowsched[accode]'>"
                            . "<input type='hidden' class='treatment' value='$rowsched[treatment]'>"
                            . "</td>";
                            echo "<td><button type='button' class='treatdet form-control btn' style='font-size:9px !important; height: 25px; !important; width: 50px !important;$vis' ><b>Details</b></button></td>";
                            echo "</tr>";
                            $sdctr++;
                        }
                        ?>

                    </tbody>
                </table>
            </div>
            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 dettreatment">

            </div>
        </div>
    <?php } ?>
    <div <?= $billing; ?> class="row" style="padding-top:1%">
        <div class="col-lg-4 col-md-4 col-sm-6 col-xs-6  text-right">
            <label class='label-control marg'>Consulting Fees</label>
        </div>
        <div class="col-lg-2 col-md-2 col-sm-4 col-xs-5">
            <input type="text" class="form-control text-right form consultfees" name="consultfees" value="<?php echo $get['consultfees']; ?>">
        </div>
        <div class="col-lg-6">

        </div>
    </div>
    <?php
    if ($get['ttamount'] > 0) {
        ?>
        <div <?= $billing; ?> class="row">
            <div class="col-lg-4 col-md-4 col-sm-6 col-xs-6  text-right">
                <label class='label-control marg'>Treatment Fees</label>
            </div>
            <div class="col-lg-2 col-md-2 col-sm-4 col-xs-5">
                <input type="text" class="form-control text-right form newamount" name="ttamount" value="<?php echo $get['ttamount']; ?>" readonly>
            </div>
        </div><?php
    }
    if ($get['tmedamount'] > 0) {
        ?>
        <div <?= $billing; ?> class="row">
            <div class="col-lg-4 col-md-4 col-sm-6 col-xs-6  text-right">
                <label class='label-control marg'>Medicine Fees</label>
            </div>
            <div class="col-lg-2 col-md-2 col-sm-4 col-xs-5">
                <input type="text" class="form-control text-right form tmedamount" name="tmedamount" value="<?php echo $get['tmedamount']; ?>" readonly>
            </div>
        </div>
    <?php }
    ?>
    <div <?= $billing; ?> class="row">
        <div class="col-lg-4 col-md-4 col-sm-6 col-xs-6  text-right">
            <label class='label-control marg'>Sub Total</label>
        </div>
        <div class="col-lg-2 col-md-2 col-sm-4 col-xs-5">
            <input type="text" class="form-control text-right form subtot" name="subtot" value="<?php echo $get['subtot']; ?>" readonly>
        </div>
    </div>
    <div <?= $billing; ?> class="row">
        <div class="col-lg-4 col-md-4 col-sm-6 col-xs-6  text-right">
            <label class='label-control marg'>Discount</label>
        </div>
        <div class="col-lg-2 col-md-2 col-sm-4 col-xs-5">
            <input type="text" class="form-control text-right form discamt" name="discamt" value="<?php echo $get['discamt']; ?>">
        </div>
    </div>
    <div <?= $billing; ?> class="row">
        <div class="col-lg-4 col-md-4 col-sm-6 col-xs-6  text-right">
            <label class='label-control marg' style="color:green;font-size: 20px">Total Amount</label>
        </div>
        <div class="col-lg-2 col-md-2 col-sm-4 col-xs-5">
            <input type="text" class="form-control text-right form billamt" style="color:green;font-size: 22px" name="billamt" value="<?php echo $get['billamt']; ?>" readonly>
        </div>
    </div>
    <div <?= $billing; ?> class="row">
        <div class="col-lg-4 col-md-4 col-sm-6 col-xs-6  text-right">
            <label class='label-control marg' style="color:blue;font-size: 20px"><a target="_blank" href="ledger_fr.php?view=1&fdt=<?= $get['vouchdt'] ?>&acc=<?= $get['accode'] ?>">Paid Amount</a></label>
        </div>
        <div class="col-lg-2 col-md-2 col-sm-4 col-xs-5">
            <input type="text" class="form-control text-right form paidamt" style="color:blue;font-size: 20px" name="paidamt" value="<?php echo $paidamt; ?>" readonly>
        </div>

    </div>
    <div <?= $billing; ?> class="row">
        <div class="col-lg-4 col-md-4 col-sm-6 col-xs-6  text-right">
            <label class='label-control marg' style="color:red;font-size: 20px">Balance</label>
        </div>
        <div class="col-lg-2 col-md-2 col-sm-4 col-xs-5">
            <input type="text" class="form-control text-right form balamt" style="color:red;font-size: 22px" name="balamt" value="<?php echo $fbalamt; ?>" readonly>
        </div>
    </div>

    <?php
    $gtinst = mysqli_query($conn, "Select * from `instalment` where vouchno='$get[vouchno]' order by srno");
    if (mysqli_num_rows($gtinst) > 0) {
        ?>
        <div <?= $billing; ?> class="row">
            <table class="table table-bordered" style="width: 40%">
                <thead>
                    <tr class="mySearchTr">
                        <th style="width:3%">Sr.</th>
                        <th style="width:60%">Amount</th>
                        <th style="width:35%" class='text-right'>Paid Date</th>
                    </tr>

                </thead>

                <!-- Laundry section starts from here -->
                <tbody class="">
                    <?php
                    $ictr = 0;
                    $hctr = 0;


                    while ($rowisnt = mysqli_fetch_array($gtinst)) {
                        $colr = "";
                        $bgcolr = "";
                        if ($rowisnt['paiddate'] == "0000-00-00" && $rowisnt['amount'] > 0) {
                            $colr = "color:red";
                            $bgcolr = "style='background-color:#fff2cc'";
                        }
                        $hctr++;
                        echo "<tr $bgcolr>";
                        echo "<td><input type='text' class='form-control form srno' value='$hctr' name='isrno_$ictr' style='$colr'></td>";

                        echo "<td><input type='text' class='form-control form text-right mamt' value='$rowisnt[amount]' name='iamt_$ictr'  style='$colr'></td>";
                        echo "<td><input type='date' class='form-control form text-right paiddate' value='$rowisnt[paiddate]' name='paiddate_$ictr'  style='$colr'></td>";

                        echo "</tr>";
                        $ictr++;
                    }
                    ?>
                </tbody>

            </table>
        </div>

        <?php
    }
}
?>
