<?php
session_start();
error_reporting(0);
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE);
// header("Access-Control-Allow-Origin: *");
// error_reporting(E_ALL);
// ini_set('display_errors', '1');
$servername = "localhost";
$username = "schoolro_maker";
$password = "JesusChristisLord";
$dbname = "schoolro_schoolrobot_schools_account";
// Create connection
$school_conn = mysqli_connect($servername, $username, $password, $dbname); ?>
<!doctype html>
<html class="no-js h-100" lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>MISS BALLERINA GHANA</title>
    <meta content="" name="descriptison">
    <meta content="" name="keywords">
    <meta property="og:site_name" content="MISS BALLERINA GHANA">
    <meta property="og:title" content="MISS BALLERINA GHANA" />
    <meta property="og:description" content="MISS BALLERINA GHANA" />
    <meta property="og:image" itemprop="image" content="https://library.gov.gh/isns/assets/ghlaWebFavIcon.ico/favicon.png">
    <meta property="og:type" content="website" />

    <!-- Favicons -->
    <link href="https://library.gov.gh/isns/assets/ghlaWebFavIcon.ico/favicon.png" rel="icon">
    <link href="https://library.gov.gh/isns/assets/ghlaWebFavIcon.ico/apple-touch-icon.png" rel="apple-touch-icon">

    <link href="https://use.fontawesome.com/releases/v5.0.6/css/all.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <link rel="stylesheet" id="main-stylesheet" data-version="1.1.0" href="styles/shards-dashboards.1.1.0.min.css">
    <link rel="stylesheet" href="styles/extras.1.1.0.min.css">
    <script async defer src="https://buttons.github.io/buttons.js"></script>
</head>

<body class="h-100">


    <div class="container-fluid" style='display:none;'>
        <div class="row">
            <!-- Main Sidebar -->
            <aside class="main-sidebar col-12 col-md-3 col-lg-2 px-0" style="display:none;">
                <div class="main-navbar">
                    <nav class="navbar align-items-stretch navbar-light bg-white flex-md-nowrap border-bottom p-0">
                        <a class="navbar-brand w-100 mr-0" href="#" style="line-height: 25px;">
                            <div class="d-table m-auto">
                                <!-- <img id="main-logo" class="d-inline-block align-top mr-1" style="max-width: 25px;" src="https://library.gov.gh/isns/assets/logo/ghlaLogo.png" alt="Shards Dashboard"> -->
                                <span class="d-none d-md-inline ml-1">MBG Admin Dashboard</span>
                            </div>
                        </a>
                        <a class="toggle-sidebar d-sm-inline d-md-none d-lg-none">
                            <i class="material-icons">&#xE5C4;</i>
                        </a>
                    </nav>
                </div>
                <form action="#" class="main-sidebar__search w-100 border-right d-sm-flex d-md-none d-lg-none">
                    <div class="input-group input-group-seamless ml-3">
                        <div class="input-group-prepend">
                            <div class="input-group-text">
                                <i class="fas fa-search"></i>
                            </div>
                        </div>
                        <input class="navbar-search form-control" type="text" placeholder="Search for something..." aria-label="Search">
                    </div>
                </form>

            </aside>
            <!-- End Main Sidebar -->
            <!-- <main class="main-content col-lg-10 col-md-9 col-sm-12 p-0 offset-lg-2 offset-md-3"> -->
            <main class="main-content col-lg-12 col-md-12 col-sm-12">
                <div class="main-navbar sticky-top bg-white">
                    <!-- Main Navbar -->
                    <nav class="navbar align-items-stretch navbar-light flex-md-nowrap p-0">


                        <nav class="nav">
                            <a href="#" class="nav-link nav-link-icon toggle-sidebar d-md-inline d-lg-none text-center border-left" data-toggle="collapse" data-target=".header-navbar" aria-expanded="false" aria-controls="header-navbar">
                                <i class="material-icons">&#xE5D2;</i>
                            </a>
                        </nav>
                    </nav>
                </div>
                <!-- / .main-navbar -->
                <div class="main-content-container container-fluid px-4">
                    <!-- Page Header -->
                    <div class="page-header row no-gutters py-4">
                        <div class="col-12 col-sm-4 text-center text-sm-left mb-0">
                            <span class="text-uppercase page-subtitle">MBG Admin Dashboard</span>
                            <h3 class="page-title">Report Analytics</h3>
                        </div>
                    </div>
                    <!-- End Page Header -->
                    <!-- Small Stats Blocks -->

                    <div class="row votes">
                        <div class="col-md-12">
                            <div class="col">
                                <div class="card card-small mb-4">
                                    <div class="card-header border-bottom">
                                        <h6 class="m-0">
                                            Voting Report
                                    </div>
                                    <div class="card-body p-0 pb-3 text-center">
                                        <div class="row">
                                            <div class="col-md-12">
                                                <table class="table mb-0">
                                                    <thead class="bg-light">
                                                        <tr>
                                                            <th scope="col" class="border-0">#</th>
                                                            <th scope="col" class="border-0">Show Code</th>
                                                            <th scope="col" class="border-0">Show Name</th>
                                                            <!-- <th scope="col" class="border-0">Voter</th> -->
                                                            <th scope="col" class="border-0">Number Of Votes</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody class='registration-details'>
                                                        <?php
                                                        error_reporting(E_ALL);
                                                        ini_set('display_errors', '1');

                                                        // Group weeks
                                                        $queryWeeks = mysqli_query($school_conn, "SELECT DISTINCT YEARWEEK(Date, 1) AS week_id, MIN(Date) AS start_date, MAX(Date) AS end_date FROM gateway_dance_v WHERE Status = 'Successful' GROUP BY week_id ORDER BY start_date DESC");
                                                        while ($week = mysqli_fetch_assoc($queryWeeks)) {
                                                            $weekId   = $week['week_id'];
                                                            $start    = date("d M Y", strtotime($week['start_date']));
                                                            $end      = date("d M Y", strtotime($week['end_date']));

                                                            echo "<tr class='table-secondary'>
                    <td colspan='6'><strong>Week of {$start} – {$end}</strong></td>
                    </tr>";

                                                            // Fetch rows for this week ORDERED BY ShowId
                                                            $queryRows = mysqli_query($school_conn, "SELECT ShowId, Name, PhoneNumber, SUM(NumberOfVotes) AS TOTAL, MIN(Date) AS FirstVoteDate, MAX(Date) AS LastVoteDate FROM gateway_dance_v WHERE YEARWEEK(Date, 1) = '$weekId' AND Status = 'Successful' GROUP BY ShowId, Name ORDER BY ShowId ASC");
                                                            $no = 1;
                                                            $weeklyTotal = 0;
                                                            while ($spec1 = mysqli_fetch_assoc($queryRows)) {
                                                                $weeklyTotal += $spec1["TOTAL"];

                                                                echo "<tr>";
                                                                echo "<td style='border: 1px solid #ededed;'>" . $no++ . "</td>";
                                                                echo "<td style='border: 1px solid #ededed;'>" .
                                                                    ($spec1["ShowId"] > 0 ? $spec1["ShowId"] : "------") . "</td>";
                                                                echo "<td style='border: 1px solid #ededed;'>" .
                                                                    ((empty($spec1["Name"]) || preg_match('/^[0-9]/', $spec1["Name"]))
                                                                        ? "------"
                                                                        : $spec1["Name"]) . "</td>";
                                                                // echo "<td style='border: 1px solid #ededed;'>" . $spec1["PhoneNumber"] . "</td>";
                                                                echo "<td style='border: 1px solid #ededed;'>" . $spec1["TOTAL"] . "</td>";
                                                                echo "</tr>";
                                                            }


                                                            // Add total row
                                                            echo "<tr class='table-warning'>
                    <td colspan='3' style='text-align:right;'><strong>Total Votes This Week:</strong></td>
                    <td colspan='2'><strong>{$weeklyTotal}</strong></td>
                </tr>";
                                                        }
                                                        ?>
                                                    </tbody>
                                                </table>
                                            </div>


                                            <div class="col-md-12">
                                                <div class="card card-small mb-4">
                                                    <div class="card-header text-white text-center">
                                                        <h5 class="mb-0">Weekly Votes Summary</h5>
                                                    </div>

                                                    <div class="card-body text-center">
                                                        <canvas id="weeklyVotesChart" height="130"></canvas>
                                                    </div>
                                                </div>

                                                <?php
                                                // Fetch vote totals per ShowId
                                                $queryRows = mysqli_query($school_conn, "SELECT ShowId, Name, SUM(NumberOfVotes) AS TOTAL FROM gateway_dance_v WHERE YEARWEEK(Date, 1) = '$weekId' AND Status = 'Successful' GROUP BY ShowId, Name ORDER BY ShowId ASC");
                                                // Prepare data for the chart
                                                $labels = [];
                                                $totals = [];

                                                while ($spec1 = mysqli_fetch_assoc($queryRows)) {
                                                    $name = !empty($spec1["Name"]) ? $spec1["Name"] : "Show " . $spec1["ShowId"];
                                                    $labels[] = $name;
                                                    $totals[] = (int)$spec1["TOTAL"];
                                                }
                                                ?>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                            </div>
                        </div>


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

                        </div>
                    </div>


                </div>
                <footer class="main-footer d-flex p-2 px-3 bg-white border-top">

                    <span class="copyright ml-auto my-auto mr-2">Copyright © 2025
                        <a href="https://library.gov.gh" rel="nofollow">Aura-rich Consult & Technologies</a>
                    </span>
                </footer>
            </main>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"></script>
    <script src="https://unpkg.com/shards-ui@latest/dist/js/shards.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Sharrre/2.0.1/jquery.sharrre.min.js"></script>
    <script src="scripts/extras.1.1.0.min.js"></script>
    <script src="scripts/shards-dashboards.1.1.0.min.js"></script>
    <script src="scripts/app/app-blog-overview.1.1.0.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

    <script>
        // $(document).ready(function() {
        const ctx = document.getElementById('weeklyVotesChart').getContext('2d');
        const weeklyVotesChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: <?php echo json_encode($labels); ?>,
                datasets: [{
                    label: 'Total Votes',
                    data: <?php echo json_encode($totals); ?>,
                    backgroundColor: 'rgba(13, 110, 253, 0.7)', // Bootstrap primary
                    borderColor: 'rgba(13, 110, 253, 1)',
                    borderWidth: 1,
                    borderRadius: 8,
                }]
            },
            options: {
                responsive: true,
                plugins: {
                    legend: {
                        display: false
                    },
                    title: {
                        display: true,
                        text: 'Votes per Show',
                        color: '#333',
                        font: {
                            size: 16,
                            weight: 'bold'
                        }
                    }
                },
                scales: {
                    x: {
                        ticks: {
                            color: '#333',
                            font: {
                                size: 12
                            }
                        },
                        grid: {
                            display: false
                        }
                    },
                    y: {
                        beginAtZero: true,
                        ticks: {
                            color: '#333',
                            stepSize: 1
                        },
                        grid: {
                            color: '#eee'
                        }
                    }
                }
            }
        });
    </script>
</body>

</html>