<?php
	session_start();
	include 'prijava/connect.php';
    setlocale(LC_TIME,"hr_HR");
?>
<!DOCTYPE html>
<head>
    <?php
        define('meta', TRUE);
        include 'inc/head.php';
    ?>
    <script src="js/download-pdf.js"></script>
    <script src="js/confirm-pdf.js"></script>
    <script src="js/custom-pdf.js"></script>
    <title>Statistika - KrikStudio</title>
</head>
<body>
    <?php
        if(!isset($_SESSION['username']) || $_SESSION['admin'] == 0) {
            echo '<script>';
            echo 'window.location.href="./"';
            echo '</script>';
        } else {
            $day = date('w');

            $month_this = date("m");
            $year_this = date("Y");

            $month_last = date("m", strtotime("first day of previous month"));
            if($month_last == "12") {
                $year_last = date("Y",strtotime("-1 year"));
            } else {
                $year_last = $year_this;
            }

            $all_users = array();
            $number_users = 0;
            $query = "SELECT * FROM user";
            $result = mysqli_query($dbc, $query);
            while($row = mysqli_fetch_array($result)) {
                array_push($all_users,$row['name']);
                $number_users = $number_users + 1;
            }

            define('nav', TRUE);
            include 'inc/nav.php';
        }
    ?>

    <section class="stats">
        <div class="wrapper">
            <h2>MJESEČNA STATISTIKA</h2>
            <p>Za <?php echo "".$month_this."/".$year_this.""; ?></p>
            <?php include('inc/month-table.php'); ?>

            <!-- MOBILNI PRIKAZ STATISTIKE -->

            <div class="mob-stats">
                <table>
                    <thead>
                        <tr>
                            <th></th>
                            <th>UKUPNO</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                    $i = 0;
                    for ($i=0; $i<$number_users; $i++) {
                    ?>
                        <tr>
                            <td><?php echo $all_users[$i] ?></td>
                            <?php
                                $sum = 0;
                                $query = "SELECT * FROM orders WHERE MONTH(date) = $month_this AND completed = '1' AND user = '$all_users[$i]'";
                                $result = mysqli_query($dbc, $query);
                                while($row = mysqli_fetch_array($result)) {
                                    $sum += 1;
                                }
                                echo "<td>".$sum."</td>";
                            ?>
                        </tr>
                    <?php } ?>
                        <tr>
                            <td><b>UKUPNO</b></td>
                            <?php
                                $month_sum = 0;
                                $query = "SELECT * FROM orders WHERE MONTH(date) = $month_this AND completed = '1'";
                                $result = mysqli_query($dbc, $query);
                                while($row = mysqli_fetch_array($result)) {
                                    $month_sum += 1;
                                }
                                echo "<td><b>".$month_sum."</b></td>";
                            ?>
                        </tr>
                    </tbody>
                </table>
            </div>

            <div class="stats-btns">
                <div><h2>PRETHODNI MJESEC</h2></div>
                <?php if($_SESSION['dir'] == 1) { ?>
                <div class="stats-btns_box">
                    <!--<button class="pdf-send" onclick="confirmPDF()"><i data-feather="send"></i>Pošalji</button>-->
                    <button class="pdf-download" onclick="downloadPDF()"><i data-feather="download"></i>Preuzmi</button>
                </div>
                <?php } ?>
            </div>
            <p>Za <?php echo "".$month_last."/".$year_last.""; ?></p>
            <?php include('inc/month-table-last.php'); ?>

            <!-- MOBILNI PRIKAZ STATISTIKE -->

            <div class="mob-stats">
                <table>
                    <thead>
                        <tr>
                            <th></th>
                            <th>UKUPNO</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                    $i = 0;
                    for ($i=0; $i<$number_users; $i++) {
                    ?>
                        <tr>
                            <td><?php echo $all_users[$i] ?></td>
                            <?php
                                $sum = 0;
                                $query = "SELECT * FROM orders WHERE MONTH(date) = $month_last AND completed = '1' AND user = '$all_users[$i]'";
                                $result = mysqli_query($dbc, $query);
                                while($row = mysqli_fetch_array($result)) {
                                    $sum += 1;
                                }
                                echo "<td>".$sum."</td>";
                            ?>
                        </tr>
                    <?php } ?>
                        <tr>
                            <td><b>UKUPNO</b></td>
                            <?php
                                $month_sum = 0;
                                $query = "SELECT * FROM orders WHERE MONTH(date) = $month_last AND completed = '1'";
                                $result = mysqli_query($dbc, $query);
                                while($row = mysqli_fetch_array($result)) {
                                    $month_sum += 1;
                                }
                                echo "<td><b>".$month_sum."</b></td>";
                            ?>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </section>

    <section class="stats-modal">
        <span onclick="customPDF()">X</span>
        <h3>Sva statistika</h3>
        <a target="_blank" href="inc/mjesecna_statistika_custom?year=2023&month=01"><i data-feather="file-text"></i> Statistika za 01/2023</a>
        <a target="_blank" href="inc/mjesecna_statistika_custom?year=2022&month=12"><i data-feather="file-text"></i> Statistika za 12/2022</a>
        <a target="_blank" href="inc/mjesecna_statistika_custom?year=2022&month=11"><i data-feather="file-text"></i> Statistika za 11/2022</a>
        <a target="_blank" href="inc/mjesecna_statistika_custom?year=2022&month=10"><i data-feather="file-text"></i> Statistika za 10/2022</a>
    </section>

    <?php define('footer', TRUE);include 'inc/footer.php'; ?>

    <script>
        feather.replace();
    </script>
</body>