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

$admin = $_SESSION["admin_" . $dbname];
$usertype = $_SESSION["usertype"];
$complogo = $_SESSION["complogo"];
$menutype = $_SESSION["menutype"];

$nstartdate = $_REQUEST['startdate'];
$nenddate = $_REQUEST['enddate'];

$startdate = date("Y-m-d", strtotime($nstartdate));
$enddate = date("Y-m-d", strtotime($nenddate));
$actstartDate = date("Y-m-d", strtotime($startdate . ' +1 day'));
$zroot = $_SESSION["zdir"];
$tdate = date("Y-m-d");
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

if (isset($_POST["submit"])) {
    $vouchno = $_POST["vouchno"];
    $apprv = $_POST["apprv"];
    mysqli_query($conn, "UPDATE `consultmain` SET `apprv`='$apprv' WHERE `vouchno`='$vouchno'") or die(mysqli_error($conn));
    mysqli_query($conn, "UPDATE `schedtreat` SET `checkin`='$apprv' WHERE `vouchno`='$vouchno' and scheddate ='$tdate'") or die(mysqli_error($conn));
}

if ($usertype != 1) {
    include("includes/menu_rights.php");
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="apple-touch-icon" sizes="76x76" href="../assets/img/apple-icon.png">
    <title>
        Eva
    </title>
    <!--     Fonts and icons     -->
    <link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Inter:300,400,500,600,700,900" />
    <!-- Nucleo Icons -->
    <link href="../assets/css/nucleo-icons.css" rel="stylesheet" />
    <link href="../assets/css/nucleo-svg.css" rel="stylesheet" />
    <!-- Font Awesome Icons -->


    <!-- <script src="https://kit.fontawesome.com/42d5adcbca.js" crossorigin="anonymous"></script> -->

    <!-- links added by obaid for date picker -->
    <script type="text/javascript" src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />
    <!-- links of date ends here -->

    <!-- Material Icons -->
    <link rel="stylesheet"
        href="https://fonts.googleapis.com/css2?family=Material+Symbols+Rounded:opsz,wght,FILL,GRAD@24,400,0,0" />
    <script src="https://kit.fontawesome.com/4e6ba47525.js" crossorigin="anonymous"></script>

    <!-- BOOTSTRAP LINK FOR BOOTSTRAP ICON BY OBAID -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">

    <!-- CSS Files -->
    <link id="pagestyle" href="../assets/css/material-dashboard.css?v=3.2.0" rel="stylesheet" />
    <script src="MyScript/get_Dashboard.js?A=<?= mt_rand(); ?>" type="text/javascript"></script>

</head>
<style>
    .button {
        background-color: #04AA6D;
        border: none;
        border-radius: 200px;
        color: white;
        padding: 3px 8px;
        text-align: center;
        text-decoration: none;
        display: inline-block;
        margin: 4px 2px;
        cursor: pointer;
    }

    /* .button:hover{
        box-shadow: 2px 2px  #888888 ;
      } */
    .icon {
        color: #FFFFFF !important;
    }
    .boots-icon{
        font-size: larger !important;
    }

    /* th,td{
        border: 1px solid #4F6228 !important;
        padding: 3px !important;
    } */
</style>

<body class="g-sidenav-show  bg-gray-100">
    <main class="main-content position-relative max-height-vh-100 h-100 border-radius-lg ">
        <div class="container-fluid py-2">

            <!-- <br> -->

            <div <?= $revenue; ?> class="row" style="margin-top: 10px !important;">

                <div class="col-xl-2 col-sm-6 mb-xl-0 mb-4">
                    <div class="card">
                        <div class="card-header p-2 ps-3">
                            <div class="d-flex justify-content-between">
                                <div>
                                    <p class="text-sm mb-0 text-capitalize" style="text-align: right;">Total Billing</p>
                                    <?php
                                    $billing = 0;

                                    $getbillamt = mysqli_query($conn, "SELECT SUM(billamt) AS billamt FROM consultmain WHERE billamt > 0 and vouchdt >= '$actstartDate' and vouchdt <='$enddate' ") or die(mysqli_error($conn));
                                    $billamt = mysqli_fetch_array($getbillamt);

                                    $getnetamt = mysqli_query($conn, "SELECT SUM(netamt) AS netamt FROM salemain WHERE netamt > 0  and vouchdt >= '$actstartDate' and vouchdt <='$enddate'  ") or die(mysqli_error($conn));
                                    $netamt = mysqli_fetch_array($getnetamt);

                                    $totbilling = $billamt['billamt'] + $netamt['netamt'];
                                    $billing = number_format($totbilling);

                                    echo "<h4 class='mb-0' style='color:green;font-size: 20px;'>₹ $billing</h4>";
                                    ?>
                                </div>

                                <div class="icon icon-md icon-shape bg-gradient-dark shadow-dark shadow text-center border-radius-lg boots-icon">
                                    <!-- <i class="fa fa-database" aria-hidden="true"></i> -->
                                    <i class="bi bi-database-fill "></i>
                                </div>

                            </div>
                        </div>
                        <hr class="dark horizontal my-0">
                        <div class="card-footer p-2 ps-3">
                            <!-- <p class="mb-0 text-sm"><span class="text-success font-weight-bolder">+55% </span>than last week</p> -->
                        </div>
                    </div>
                </div>

                <div class="col-xl-2 col-sm-6 mb-xl-0 mb-4">
                    <div class="card">
                        <div class="card-header p-2 ps-3">
                            <div class="d-flex justify-content-between">
                                <div>
                                    <p class="text-sm mb-0 text-capitalize" style="text-align: right;">Total Purchase</p>
                                    <?php
                                    $purch = 0;

                                    $getpurch = mysqli_query($conn, "SELECT SUM(netamt) AS netamt FROM purmain WHERE netamt > 0  and vouchdt >= '$actstartDate' and vouchdt <='$enddate'  ") or die(mysqli_error($conn));
                                    $fetpurch = mysqli_fetch_array($getpurch);

                                    $fetpurch['netamt'];

                                    if ($fetpurch['netamt'] > 0) {
                                        echo "<h4 class='mb-0' style='color:green;font-size: 20px;'>₹ $fetpurch[netamt]</h4>";
                                    } else {
                                        echo "<h4 class='mb-0' style='color:green'>₹ 0</h4>";
                                    }
                                    ?>
                                </div>
                                <div class="icon icon-md icon-shape bg-gradient-dark shadow-dark shadow text-center border-radius-lg ">
                                    <!-- <i class="fas fa-shopping-cart"></i> -->
                                    <i class="bi bi-cart-fill boots-icon"></i>
                                </div>

                            </div>
                        </div>
                        <hr class="dark horizontal my-0">
                        <div class="card-footer p-2 ps-3">
                            <!-- <p class="mb-0 text-sm"><span class="text-success font-weight-bolder">+55% </span>than last week</p> -->
                        </div>
                    </div>
                </div>

                <div class="col-xl-2 col-sm-6 mb-xl-0 mb-4">
                    <div class="card">
                        <div class="card-header p-2 ps-3">
                            <div class="d-flex justify-content-between">
                                <div>
                                    <p class="text-sm mb-0 text-capitalize" style="text-align: right;">Total Expense</p>
                                    <?php
                                    $billing = 0;

                                    $getcashpay = mysqli_query($conn, "SELECT SUM(amount) AS amount FROM cashpay WHERE amount > 0   and vouchdt >= '$actstartDate' and vouchdt <='$enddate' ") or die(mysqli_error($conn));
                                    $cashpay = mysqli_fetch_array($getcashpay);

                                    $getbankpay = mysqli_query($conn, "SELECT SUM(amount) AS amount FROM bankpay WHERE amount > 0  and vouchdt >= '$actstartDate' and vouchdt <='$enddate' ") or die(mysqli_error($conn));
                                    $bankpay = mysqli_fetch_array($getbankpay);

                                    $totpay = $cashpay['amount'] + $bankpay['amount'];
                                    $pay = number_format($totpay);

                                    echo "<h4 class='mb-0' style='color:green;font-size: 20px;'>₹ $pay</h4>";
                                    ?>
                                </div>

                                <div class="icon icon-md icon-shape bg-gradient-dark shadow-dark shadow text-center border-radius-lg">
                                    <!-- <i class="fa fa-money" aria-hidden="true"></i> -->
                                    <i class="bi bi-cash boots-icon"></i>
                                </div>

                            </div>
                        </div>
                        <hr class="dark horizontal my-0">
                        <div class="card-footer p-2 ps-3">
                            <!-- <p class="mb-0 text-sm"><span class="text-success font-weight-bolder">+55% </span>than last week</p> -->
                        </div>
                    </div>
                </div>



                <div class="col-xl-2 col-sm-6 mb-xl-0 mb-4">
                    <div class="card">
                        <div class="card-header p-2 ps-3">
                            <div class="d-flex justify-content-between">
                                <div>
                                    <p class="text-sm mb-0 text-capitalize">Total Collection</p>
                                    <?
                                    $collection = 0;

                                    $getcash = mysqli_query($conn, "SELECT SUM(amount) AS amount FROM cashrecp where vouchdt >= '$actstartDate' and vouchdt <='$enddate' ") or die(mysqli_error($conn));
                                    $cash = mysqli_fetch_array($getcash);

                                    $getbank = mysqli_query($conn, "SELECT SUM(amount) AS amount FROM bankrecp where vouchdt >= '$actstartDate' and vouchdt <='$enddate' ") or die(mysqli_error($conn));
                                    $bank = mysqli_fetch_array($getbank);

                                    $totamt = $bank['amount'] + $cash['amount'];

                                    $total = number_format($totamt);

                                    echo "<h4 class='mb-0' style='color:green;font-size: 20px;'>₹ $total</h4>";
                                    ?>

                                </div>

                                <div class="icon icon-md icon-shape bg-gradient-dark shadow-dark shadow text-center border-radius-lg">
                                    <!-- <i class="fas fa-rupee-sign"></i> -->
                                    <i class="bi bi-wallet-fill boots-icon"></i>
                                </div>

                            </div>
                        </div>
                        <hr class="dark horizontal my-0">
                        <div class="card-footer p-2 ps-3">
                            <!-- <p class="mb-0 text-sm"><span class="text-danger font-weight-bolder">-2% </span>than yesterday</p> -->
                        </div>
                    </div>
                </div>


                <div class="col-xl-2 col-sm-6 mb-xl-0 mb-4">
                    <div class="card">
                        <div class="card-header p-2 ps-3">
                            <div class="d-flex justify-content-between">
                                <div>
                                    <p class="text-sm mb-0 text-capitalize">Total Invoices</p>
                                    <?
                                    $invoice = 0;

                                    $getinvoice = mysqli_query($conn, "SELECT `vouchno` FROM salemain where vouchdt >= '$actstartDate' and vouchdt <='$enddate' ") or die(mysqli_error($conn));
                                    $invoice1 = mysqli_num_rows($getinvoice);
                                    $getinvoice2 = mysqli_query($conn, "SELECT `vouchno` FROM consultmain where vouchdt >= '$actstartDate' and vouchdt <='$enddate' ") or die(mysqli_error($conn));
                                    $invoice2 = mysqli_num_rows($getinvoice2);
                                    $invoice = $invoice1 + $invoice2;
                                    echo "<h4 class='mb-0' style='font-size: 20px;'>$invoice</h4>";
                                    ?>
                                </div>
                                <!-- fafa icons -->

                                <div class="icon icon-md icon-shape bg-gradient-dark shadow-dark shadow text-center border-radius-lg">
                                    <!-- <i class="fas fa-file-invoice"></i> -->
                                    <i class="bi bi-receipt-cutoff boots-icon"></i>
                                </div>

                            </div>
                        </div>
                        <hr class="dark horizontal my-0">
                        <div class="card-footer p-2 ps-3">
                            <!-- <p class="mb-0 text-sm"><span class="text-success font-weight-bolder">+3% </span>than last month</p> -->
                        </div>
                    </div>
                </div>

                <div class="col-xl-2 col-sm-6">
                    <div class="card">
                        <div class="card-header p-2 ps-3">
                            <div class="d-flex justify-content-between">
                                <div>
                                    <p class="text-sm mb-0 text-capitalize">Total Patient's</p>

                                    <?php
                                    $getclient = mysqli_query($conn, "SELECT * FROM `accmast` WHERE (`grpcode`='010' or grpcode ='022') and regndt >= '$actstartDate' and regndt <='$enddate'") or die(mysqli_error($conn));
                                    $client = mysqli_num_rows($getclient);
                                    echo "<h4 class='mb-0' style='font-size: 20px;'>$client</h4>";
                                    ?>

                                </div>

                                <!-- fafa icons -->

                                <div class="icon icon-md icon-shape bg-gradient-dark shadow-dark shadow text-center border-radius-lg">
                                    <!-- <i class="fas fa-user"></i> -->
                                    <i class="bi bi-person-fill boots-icon"></i>
                                </div>

                            </div>
                        </div>
                        <hr class="dark horizontal my-0">
                        <div class="card-footer p-2 ps-3">
                            <!-- <p class="mb-0 text-sm"><span class="text-success font-weight-bolder">+5% </span>than yesterday</p> -->
                        </div>
                    </div>
                </div>
            </div>
            <!-- <br> -->
            <div class="row mb-4" style="margin-top: 10px;">
                <div class="col-lg-8 col-md-6 mb-md-0 mb-4">
                    <div class="card">
                        <div class="card-header pb-0">
                            <div class="row">
                                <div class="col-lg-6 col-7">
                                    <h6>Today's Schedule </h6>
                                </div>
                                <div class="col-lg-6 col-7">
                                    <div class="dropdown float-lg-end pe-4">
                                        <?php
                                        $date = date("F j, Y");
                                        echo "<h6>$date</h6>";
                                        ?>
                                    </div>
                                </div>
                                <hr style="border-bottom: 1px solid black;">
                            </div>
                        </div>
                        <div class="card-body px-0 pb-2">
                            <div class="table-responsive ">
                                <table class="table table-hover thtd align-items-center mb-0">
                                    <thead style="background-color: #4A628A; color:white !important;border-radius:50% !important">
                                        <tr>
                                            <th class='font-weight-bolder text-left'
                                                style="color:white !important; font-size: 16px !important;width:50px !important">Sr</th>
                                            <th class='font-weight-bolder text-left'
                                                style="color:white !important; font-size: 16px !important">Name</th>
                                            <th class='font-weight-bolder text-left'
                                                style="color:white !important; font-size: 16px !important">Time</th>
                                            <th class='font-weight-bolder text-left'
                                                style="color:white !important; font-size: 16px !important">Purpose</th>
                                            <th class='font-weight-bolder text-left'
                                                style="color:white !important; font-size: 16px !important; width:20px !important">Reffered By</th>
                                            <th class='font-weight-bolder text-left' style="color:white !important; width:10px !important">Arrived</th>
                                            <th class='font-weight-bolder text-left' style="color:white !important;"></th>
                                            <!-- <th class='font-weight-bolder text-left'
                                                  style="color:white !important; font-size: 16px !important">Edit</th> -->
                                        </tr>
                                    </thead>
                                    <tbody class='schedtr thtd'>
                                        <input type="hidden" class="date_start" value="">
                                        <input type="hidden" class="date_end" value="">
                                        <?php
                                        $ctr = 0;
                                        $date = date("Y-m-d");
                                        $getscedule = mysqli_query($conn, "SELECT a.*, b.name, b.accode FROM `schedtreat`a
                                                                           LEFT JOIN accmast b ON a.accode=b.accode WHERE a.`scheddate` = '$date' GROUP BY a.`vouchno`") or die(mysqli_error($conn));
                                        while ($scedule = mysqli_fetch_array($getscedule)) {
                                            $ctr++;
                                            $vouchno = $scedule['vouchno'];


                                            // browse($vouchno);
                                            echo "<tr class='ts-entry'>";
                                            echo "<td class='text-uppercase font-weight-bolder text-center' style='color:black; font-size: 14px;'>$ctr</td>";
                                            echo "<td class='text-uppercase font-weight-bolder'style='color:black;font-size: 14px'>$scedule[name]</td>";
                                            echo "<td class='text-uppercase font-weight-bolder'style='color:black;font-size: 14px'>$scedule[apttime]</td>";
                                            echo "<td class='text-uppercase font-weight-bolder'style='color:black;font-size: 14px'>$scedule[purpvisit]</td>";
                                            echo "<td class='text-uppercase font-weight-bolder'style='color:black;font-size: 14px'>$scedule[refby]</td>";


                                            $ischk = "";
                                            if ($scedule['checkin'] == 1) {
                                                $ischk = "checked disabled";
                                            }
                                            echo "<td><input type='checkbox' class='apprv' name='apprv' value='$apprv[apprv]' style='width:20px !important;height:20px !important; margin-left:23px !important' $ischk></td>";
                                            if ($scedule['checkin'] != 1) {
                                                echo "<td><button class='button button1' type='submit' name='submit'><i class='fa fa-check icon' aria-hidden='true'></i></button>
                                                      <input type='hidden' class='vouchno' value='$vouchno'> </td>";
                                            }
                                            echo "</tr>";
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>



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

                    <!-- DONT DELETE THIS CODE  -->

                    <div <?= $topselltreat; ?> class="col-lg-12 col-md-6 mb-md-0 mb-4">
                        <div class="card" style="height: 20rem !important;">
                            <div class="card-header pb-0" style="padding:10px !important">
                                <div class="row">
                                    <div class="col-lg-6 col-7">
                                        <h6>Top 5 Selling Treatment</h6>
                                    </div>
                                    <div class="col-lg-6 col-7">
                                        <div class="icon icon-md icon-shape bg-gradient-dark shadow-dark shadow text-center border-radius-lg"
                                            style="margin-left:160px">
                                            <!-- <i class="fa fa-stethoscope" aria-hidden="true"></i> -->
                                            <i class="bi bi-file-earmark-medical-fill"></i>
                                        </div>
                                    </div>
                                    <hr style="border-bottom: 1px solid black; margin-top:10px">
                                </div>
                            </div>
                            <div class="card-body px-0 pb-2" style="margin-top: -30px;">
                                <div class="table-responsive">
                                    <table class="table align-items-center mb-0" style="width: 97% !important; margin-left:1.3% !important">
                                        <thead style="background-color: #4A628A; color:white !important;border-radius:50% !important">
                                            <tr>
                                                <th class='font-weight-bolder text-center'
                                                    style="color:white !important; font-size: 16px !important;width:50px !important">Sr</th>
                                                <th class='font-weight-bolder text-left'
                                                    style="color:white !important; font-size: 16px !important; width: 340px !important;">Treatment</th>
                                                <th class='font-weight-bolder text-right'
                                                    style="color:white !important; font-size: 16px !important">Qty</th>
                                            </tr>
                                        </thead>
                                        <tbody class='schedtr'>
                                            <?php
                                            $treatctr = 0;
                                            $gettreatment = mysqli_query($conn, "SELECT a.treatment, SUM(a.qty) AS qty, b.name, b.code
                                                                        FROM treatbill a
                                                                        LEFT JOIN treatment b ON a.treatment=b.code
                                                                        where b.name !='' AND a.vouchdt >= '$actstartDate' and a.vouchdt <='$enddate'  GROUP BY a.treatment 
                                                                        having sum(a.qty) > 0 ORDER BY SUM(a.qty) DESC LIMIT 5") or die(mysqli_error($conn));
                                            while ($treatment = mysqli_fetch_array($gettreatment)) {
                                                $treatctr++;
                                                echo "<tr class='ts-entry'>";
                                                echo "<td class='font-weight-bolder text-center' style='color:black; font-size: 14px;'>$treatctr</td>";
                                                echo "<td class='font-weight-bolder'style='color:black;font-size: 14px'>$treatment[name]</td>";
                                                echo "<td class='font-weight-bolder'style='color:black;font-size: 14px; text-align:center !important'>$treatment[qty]</td>";
                                                echo "</tr>";
                                            }
                                            ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                    <br>
                    <div <?= $topsellmed; ?> class="col-lg-12 col-md-6 mb-md-0 mb-4">
                        <div class="card" style="height: 20rem !important;">
                            <div class="card-header pb-0" style="padding:10px !important">
                                <div class="row">
                                    <div class="col-lg-6 col-7">
                                        <h6>Top 5 Selling Medicine </h6>
                                    </div>
                                    <div class="col-lg-6 col-7">
                                        <div class="icon icon-md icon-shape bg-gradient-dark shadow-dark shadow text-center border-radius-lg"
                                            style="margin-left:160px">
                                            <!-- <i class="fa fa-medkit" aria-hidden="true"></i> -->
                                            <i class="bi bi-briefcase-fill"></i>
                                        </div>
                                    </div>
                                    <hr style="border-bottom: 1px solid black; margin-top:10px">
                                </div>
                            </div>
                            <div class="card-body px-0 pb-2" style="margin-top: -30px;">
                                <div class="table-responsive">
                                    <table class="table align-items-center mb-0" style="width: 97% !important; margin-left:1.3% !important">
                                        <thead style="background-color: #4A628A; color:white !important;border-radius:50% !important">
                                            <tr>
                                                <th class='font-weight-bolder text-center'
                                                    style="color:white !important; font-size: 16px !important;width:50px !important">Sr</th>
                                                <th class='font-weight-bolder text-left'
                                                    style="color:white !important; font-size: 16px !important">Medicine</th>
                                                <th class='font-weight-bolder text-right'
                                                    style="color:white !important; font-size: 16px !important">Qty</th>
                                            </tr>
                                        </thead>
                                        <tbody class='schedtr'>
                                            <?php
                                            $medctr = 0;
                                            $getmed = mysqli_query($conn, "SELECT a.item, SUM(a.qty) AS qty, b.name, b.code
                                                                    FROM medbill a
                                                                    LEFT JOIN product b ON a.item=b.code
                                                                    where a.vouchdt >= '$actstartDate' and a.vouchdt <='$enddate'  GROUP BY a.item 
                                                                    having sum(a.qty) > 0 ORDER BY SUM(a.qty) DESC LIMIT 5") or die(mysqli_error($conn));
                                            while ($med = mysqli_fetch_array($getmed)) {
                                                $medctr++;
                                                echo "<tr class='ts-entry'>";
                                                echo "<td class='font-weight-bolder text-center' style='color:black; font-size: 14px;'>$medctr</td>";
                                                echo "<td class='font-weight-bolder'style='color:black;font-size: 14px'>$med[name]</td>";
                                                echo "<td class='font-weight-bolder'style='color:black;font-size: 14px; text-align:center !important'>$med[qty]</td>";
                                                echo "</tr>";
                                            }
                                            ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>

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

    <script>
        var win = navigator.platform.indexOf('Win') > -1;
        if (win && document.querySelector('#sidenav-scrollbar')) {
            var options = {
                damping: '0.5'
            }
            Scrollbar.init(document.querySelector('#sidenav-scrollbar'), options);
        }
    </script>
</body>

</html>