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

//include("includes/menu_rights.php");
$admin = $_SESSION["admin_" . $dbname];
$usertype = $_SESSION["usertype_" . $dbname];
$complogo = $_SESSION["complogo"];
$menutype = $_SESSION["menutype"];

$zroot = $_SESSION["zdir"];
$tdate = date("Y-m-d");
$zdb = $_SESSION["zdbyear_" . $dbname];
//$zdb = "raj_eva_101";
// browse($zdb);
mysqli_select_db($conn, $zdb);

?>
<!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" />
  <!-- CSS Files -->
  <link id="pagestyle" href="../assets/css/material-dashboard.css?v=3.2.0" rel="stylesheet" />
</head>

<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">
      <div class="row">
        <div class="col-xl-12 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;"> -->

                  <!-- code by obaid selot for date picker on top nav -->
                  <div id="reportrange"
                    style="background: #fff; color:black; cursor: pointer; width: 100%;margin-top:10px">
                    <i class="fa fa-calendar"></i>&nbsp;
                    <span></span> <i class="fa fa-caret-down"></i>
                  </div>

                  <script type="text/javascript">
                    $(function () {

                      var start = moment().subtract(29, 'days');
                      var end = moment();

                      function cb(start, end) {
                        let date_start = start;
                        let date_end = end;
                        // alert(date_start);
                        // alert(date_end);

                        $(".date_start").val(date_start);
                        $(".date_end").val(date_end);

                        $('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));

                        $.post(".getDashboard.php", { startdate: date_start, enddate: date_end }, function (data) {

                        })
                      }

                      $('#reportrange').daterangepicker({
                        startDate: start,
                        endDate: end,
                        ranges: {
                          'Today': [moment(), moment()],
                          'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                          'Last 7 Days': [moment().subtract(6, 'days'), moment()],
                          'Last 30 Days': [moment().subtract(29, 'days'), moment()],
                          'This Month': [moment().startOf('month'), moment().endOf('month')],
                          'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
                        }
                      }, cb);

                      cb(start, end);
                    });
                  </script>
                  <!-- date picker ends here -->

                  <!-- </p> -->
                </div>
              </div>
            </div>
            <hr class="dark horizontal my-0">
          </div>
        </div>

        <!-- section of date end here -->
      </div>
      <!-- <br> -->

      <div 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 ") or die(mysqli_error($conn));
                  $billamt = mysqli_fetch_array($getbillamt);

                  $getnetamt = mysqli_query($conn, "SELECT SUM(netamt) AS netamt FROM salemain WHERE netamt > 0 ") 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'>₹ $billing</h4>";

                  ?>
                </div>

                <div class="icon icon-md icon-shape bg-gradient-dark shadow-dark shadow text-center border-radius-lg">
                  <i class="fa fa-database" aria-hidden="true"></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 ") or die(mysqli_error($conn));
                  $fetpurch = mysqli_fetch_array($getpurch);

                  $fetpurch['netamt'];

                  if ($fetpurch['netamt'] > 0) {
                    echo "<h4 class='mb-0' style='color:green'>₹ $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="fa fa-money" aria-hidden="true"></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 ") or die(mysqli_error($conn));
                  $cashpay = mysqli_fetch_array($getcashpay);

                  $getbankpay = mysqli_query($conn, "SELECT SUM(amount) AS amount FROM bankpay WHERE amount > 0 ") 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'>₹ $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-database" aria-hidden="true"></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") or die(mysqli_error($conn));
                  $cash = mysqli_fetch_array($getcash);

                  $getbank = mysqli_query($conn, "SELECT SUM(amount) AS amount FROM bankrecp") 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'>₹ $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>
                </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") or die(mysqli_error($conn));
                  $invoice = mysqli_num_rows($getinvoice);
                  echo "<h4 class='mb-0'>$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>
                </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 Clients</p>

                  <?php
                  $getclient = mysqli_query($conn, "SELECT * FROM `accmast` WHERE (`grpcode`='010' or grpcode ='022')") or die(mysqli_error($conn));
                  $client = mysqli_num_rows($getclient);
                  echo "<h4 class='mb-0'>$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>
                </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 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">Reffered By</th>
                      <th class='font-weight-bolder text-left'
                        style="color:white !important; font-size: 16px !important">Edit</th>
                    </tr>
                  </thead>
                  <tbody>
                    <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'") or die(mysqli_error($conn));
                    while ($scedule = mysqli_fetch_array($getscedule)) {
                      $ctr++;
                      echo "<tr class='ts-entry'>";
                      echo "<td class='font-weight-bolder text-center' style='color:black; font-size: 14px;'>$ctr</td>";
                      echo "<td class='font-weight-bolder'style='color:black;font-size: 14px'>$scedule[name]</td>";
                      echo "<td class='font-weight-bolder'style='color:black;font-size: 14px'>$scedule[apttime]</td>";
                      echo "<td class='font-weight-bolder'style='color:black;font-size: 14px'>$scedule[purpvisit]</td>";
                      echo "<td class='font-weight-bolder'style='color:black;font-size: 14px'>$scedule[refby]</td>";
                      echo "<td class='font-weight-bolder'><a href='treatment.php'><button type='button' class='btn btn-secondary' style='color:white'>Edit</a></td>";
                      echo "</tr>";
                    }
                    ?>
                    <script>
                      document.querySelectorAll('.ts-entry').forEach(entry => {
                        entry.addEventListener('click', () => {
                          entry.classList.toggle('line-through');
                        });
                      });
                    </script>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>
        <div class="col-lg-4 col-md-6">

          <!-- DONT DELETE THIS CODE  -->

          <div class="card" style="height: 14rem;">
            <div class="d-flex justify-content-between">
              <div>
                <h6 style="margin: 10px;" style="margin:10px;border-bottom:1px solid black;">Top Selling Treatment</h6>
                <?php
                $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
                      GROUP BY a.treatment 
                      LIMIT 5") or die(mysqli_error($conn));
                while ($treatment = mysqli_fetch_array($gettreatment)) {

                  echo "<h6 class='mb-0' style='margin:10px;color:#F87A53 !important'>$treatment[name] - $treatment[qty]</h6>";
                }
                ?>
              </div>
              <div class="icon icon-md icon-shape bg-gradient-dark shadow-dark shadow text-center border-radius-lg"
                style="margin:10px">
                <i class="fa fa-stethoscope" aria-hidden="true"></i>
              </div>
            </div>
          </div>

          <div class="card" style="height: 14rem;">
            <div class="d-flex justify-content-between">
              <div>
                <h6 style="margin: 10px;" style="margin:10px">Top Selling Medicine</h6>
                <?php
                $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
                GROUP BY a.item 
                LIMIT 5") or die(mysqli_error($conn));
                while ($med = mysqli_fetch_array($getmed)) {
                  echo "<h6 class='mb-0' style='margin:10px;color:#F87A53 !important'>$med[name] - $med[qty]</h6>";
                }
                ?>
              </div>
              <div class="icon icon-md icon-shape bg-gradient-dark shadow-dark shadow text-center border-radius-lg"
                style="margin:10px">
                <i class="fa fa-medkit" aria-hidden="true"></i>
              </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>