<?php
include "functions.php";
if (!isset($_SESSION['hash'])) { header("Location: ./login.php"); exit; }
if ($rPermissions["is_admin"]) { exit; }
$rStatusArray = Array(0 => "CLOSED", 1 => "OPEN", 2 => "RESPONDED", 3 => "READ");
if ($rSettings["sidebar"]) {
    include "header_sidebar.php";
} else {
    include "header.php";
}
        if ($rSettings["sidebar"]) { ?>
        <div class="content-page"><div class="content"><div class="container-fluid">
        <?php } else { ?>
        <div class="wrapper"><div class="container-fluid">
        <?php } ?>
                <!-- start page title -->
                <div class="row">
                    <div class="col-12">
                        <div class="page-title-box">
                            <h4 class="page-title">Dashboard</h4>
                        </div>
                    </div>
                </div>     
                <!-- end page title --> 

                <div class="row">
                    <div class="col-md-6 col-xl-3">
                        <div class="card-box active-connections">
                            <div class="row">
                                <div class="col-6">
                                    <div class="avatar-sm bg-soft-purple rounded">
                                        <i class="fe-zap avatar-title font-22 text-purple"></i>
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="text-right">
                                        <h3 class="text-dark my-1"><span data-plugin="counterup" class="entry">0</span></h3>
                                        <p class="text-muted mb-1 text-truncate">Open Connections</p>
                                    </div>
                                </div>
                            </div>
                        </div> <!-- end card-box-->
                    </div> <!-- end col -->

                    <div class="col-md-6 col-xl-3">
                        <div class="card-box online-users">
                            <div class="row">
                                <div class="col-6">
                                    <div class="avatar-sm bg-soft-success rounded">
                                        <i class="fe-users avatar-title font-22 text-success"></i>
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="text-right">
                                        <h3 class="text-dark my-1"><span data-plugin="counterup" class="entry">0</span></h3>
                                        <p class="text-muted mb-1 text-truncate">Online Users</p>
                                    </div>
                                </div>
                            </div>
                        </div> <!-- end card-box-->
                    </div> <!-- end col -->

                    <div class="col-md-6 col-xl-3">
                        <div class="card-box active-accounts">
                            <div class="row">
                                <div class="col-6">
                                    <div class="avatar-sm bg-soft-primary rounded">
                                        <i class="fe-check-circle avatar-title font-22 text-primary"></i>
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="text-right">
                                        <h3 class="text-dark my-1"><span data-plugin="counterup" class="entry">0</span></h3>
                                        <p class="text-muted mb-1 text-truncate">Active Accounts</p>
                                    </div>
                                </div>
                            </div>
                        </div> <!-- end card-box-->
                    </div> <!-- end col -->

                    <div class="col-md-6 col-xl-3">
                        <div class="card-box credits">
                            <div class="row">
                                <div class="col-6">
                                    <div class="avatar-sm bg-soft-info rounded">
                                        <i class="fe-dollar-sign avatar-title font-22 text-info"></i>
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="text-right">
                                        <h3 class="text-dark my-1"><span data-plugin="counterup" class="entry">0</span></h3>
                                        <p class="text-muted mb-1 text-truncate">Credits Remaining</p>
                                    </div>
                                </div>
                            </div>
                        </div> <!-- end card-box-->
                    </div> <!-- end col -->
                </div>
                <div class="row">
                    <div class="col-xl-6">
                        <div class="card">
                            <div class="card-body">
                                <h4 class="header-title mb-0">Recent Activity</h4>
                                <div id="cardActivity" class="pt-3">
                                    <div class="slimscroll" style="height:350px;">
                                        <div class="timeline-alt">
                                            <?php 
                                            $rResult = $db->query("SELECT `u`.`username`, `r`.`owner`, `r`.`date`, `r`.`type` FROM `reg_userlog` AS `r` INNER JOIN `reg_users` AS `u` ON `r`.`owner` = `u`.`id` WHERE `r`.`owner` IN (".join(",", array_keys(getRegisteredUsers($rUserInfo["id"]))).") ORDER BY `r`.`date` DESC LIMIT 100;");
                                            if (($rResult) && ($rResult->num_rows > 0)) {
                                                while ($rRow = $rResult->fetch_assoc()) { ?>
                                                <div class="timeline-item">
                                                    <i class="timeline-icon"></i>
                                                    <div class="timeline-item-info">
                                                        <a href="#" class="text-body font-weight-semibold mb-1 d-block"><?=$rRow["username"]?></a>
                                                        <small><?=$rRow["type"]?></small>
                                                        <p>
                                                            <small class="text-muted"><?=date("Y-m-d H:i:s", $rRow["date"])?></small>
                                                        </p>
                                                    </div>
                                                </div>
                                                <?php }
                                            } ?>
                                        </div>
                                        <!-- end timeline -->
                                    </div> <!-- end slimscroll -->
                                </div> <!-- collapsed end -->
                            </div> <!-- end card-body -->
                        </div> <!-- end card-->
                    </div> <!-- end col-->
                    <div class="col-xl-6">
                        <div class="card">
                            <div class="card-body">
                                <h4 class="header-title mb-0">Recent Tickets</h4>
                                <div id="cardActivity" class="pt-3">
                                    <div class="slimscroll" style="height: 350px;">
                                        <table class="table table-hover m-0 table-centered dt-responsive nowrap w-100" id="tickets-table">
                                            <thead>
                                                <tr>
                                                    <th>ID</th>
                                                    <th>Reseller</th>
                                                    <th>Subject</th>
                                                    <th class="text-center">Status</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                $i = 0;
                                                foreach (getTickets($rUserInfo["id"]) as $rTicket) { $i ++;
                                                if ($i <= 10) { ?>
                                                <tr id="ticket-<?=$rTicket["id"]?>">
                                                    <td><a href="./ticket_view.php?id=<?=$rTicket["id"]?>"><?=$rTicket["id"]?></a></td>
                                                    <td><?=$rTicket["username"]?></td>
                                                    <td><?=$rTicket["title"]?></td>
                                                    <td class="text-center"><span class="badge badge-<?=Array(0 => "secondary", 1 => "warning", 2 => "success", 3 => "warning")[$rTicket["status"]]?>"><?=$rStatusArray[$rTicket["status"]]?></span></td>
                                                </tr>
                                                <?php }
                                                } ?>
                                            </tbody>
                                        </table>
                                    </div> <!-- end slimscroll -->
                                </div> <!-- collapsed end -->
                            </div> <!-- end card-body -->
                        </div> <!-- end card-->
                    </div> <!-- end col-->
                </div>
                <!-- end row -->
               
            </div> <!-- end container -->
        </div>
        <!-- end wrapper -->
        <?php if ($rSettings["sidebar"]) { echo "</div>"; } ?>
        <!-- Footer Start -->
        <footer class="footer">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-md-12 copyright text-center"><?=getFooter()?></div>
                </div>
            </div>
        </footer>
        <!-- end Footer -->

        <!-- Vendor js -->
        <script src="assets/js/vendor.min.js"></script>

        <!-- Third Party js-->
        <script src="assets/libs/jquery-knob/jquery.knob.min.js"></script>
        <script src="assets/libs/peity/jquery.peity.min.js"></script>
        <script src="assets/libs/apexcharts/apexcharts.min.js"></script>
        <script src="assets/libs/datatables/jquery.dataTables.min.js"></script>
        <script src="assets/libs/jquery-number/jquery.number.js"></script>
        <script src="assets/libs/datatables/dataTables.bootstrap4.js"></script>
        <script src="assets/libs/datatables/dataTables.responsive.min.js"></script>
        <script src="assets/libs/datatables/responsive.bootstrap4.min.js"></script>
        <!-- third party js ends -->

        <!-- Dashboard init -->
        <script src="assets/js/pages/dashboard.init.js"></script>

        <!-- App js-->
        <script src="assets/js/app.min.js"></script>
        
        <script>
        function getStats() {
            var rStart = Date.now();
            $.getJSON("./api.php?action=reseller_dashboard", function(data) {
                $(".active-connections .entry").html($.number(data.open_connections, 0));
                $(".online-users .entry").html($.number(data.online_users, 0));
                $(".active-accounts .entry").html($.number(data.active_accounts, 0));
                <?php if (floor($rUserInfo["credits"]) == $rUserInfo["credits"]) { ?>
                $(".credits .entry").html($.number(data.credits, 0));
                <?php } else { ?>
                $(".credits .entry").html($.number(data.credits, 2));
                <?php } ?>
                if (Date.now() - rStart < 1000) {
                    setTimeout(getStats, 1000 - (Date.now() - rStart));
                } else {
                    getStats();
                }
            }).fail(function() {
                setTimeout(getStats, 1000);
            });
        }
        
        $(document).ready(function() {
            getStats();
        });
        </script>
    </body>
</html>