<?php
    session_start();
    
    include("../scripts/connection.php");
    include("../scripts/functions.php");
    $user_data = check_login($con);
    
    if(isset($_GET['stationen'])) {
        $_SESSION['select-statistics'] = $_GET['stationen'];
    }

    if((!isset($_SESSION['select-statistics']) || $_SESSION['select-statistics'] == "total-score") && $user_data['user_group'] != 'station') {
        $session = "total-score";
    } else if ($user_data['user_group'] == 'station') {
        $session = $user_data['s_id'];
    } else {
        $session = $_SESSION['select-statistics'];
    }

    include("header_footer/header.php"); 
?>

<body>
    <div class="center">
        <div class="headline">
            <?php
                if($session == "total-score") {
                    echo "<h2>Gesamtpunkte</h2>\n";
                } else {
                    echo "<h2>" . get_station_name($con, $session)['name'] . "</h2>\n";
                }
                ?>
        </div>
        <div class="input-table">
            <div class="input">
                <?php
                    if($user_data['user_group'] != "station") {
                        echo "<div class=\"select-div\">
                                <form name=\"switch-statistics\" method=\"get\">
                                <select name=\"stationen\" id=\"station\" onchange=\"this.form.submit()\">";
                        load_options_stations($con, $session, true);
                        echo "</select>
                            </form>
                            </div>";
                    }
                    
                    if ($session != "total-score" && $user_data['user_group'] != 'statistics') {
                        //add delete button
                        echo "<div class=\"btn-div\">
                        <form action=\"delete_statistics.php\" method=\"post\">
                        <span class=\"gg-trash icon edit disabled\" id=\"icon-delete\"></span>
                        <input type=\"button\" id=\"delete_statistic\" value=\"\" class=\"edit\" disabled=\"true\"/>
                        <input type=\"hidden\" id=\"m_id_delete\" name=\"m_id\"/>
                        <input type=\"hidden\" id=\"s_id_delete\" name=\"s_id\" value=\"" . $session . "\"/>
                        </form>
                        </div>";
                        //add edit button
                        echo "<div class=\"btn-div\">
                        <form action=\"edit_statistics.php\" method=\"get\">
                        <span class=\"gg-pen icon edit disabled\" id=\"icon-edit\"></span>
                        <input type=\"button\" id=\"edit_statistic\" value=\"\" class=\"edit\" disabled=true/>
                        <input type=\"hidden\" id=\"team_id\" name=\"m_id\"/>
                        <input type=\"hidden\" id=\"station_id\" name=\"s_id\" value=\"". $session . "\"/>
                        </form>
                        </div>";
                    } 

                    if($session != "total-score" && get_teams_no_points($con, $session)->rowCount() > 0 && $user_data['user_group'] != 'statistics') {
                        echo "<div class=\"btn-div\">
                        <form action=\"add_entry.php\" method=\"get\">
                        <span class=\"gg-add-r icon\" id=\"icon-add\"></span>
                        <input type=\"hidden\" name=\"station\" value=\"$session\"/> 
                        <input type=\"submit\" value=\"\"/>
                        </form>
                        </div>\n";
                    }
                ?>
            </div>
            <div class="table-div">
                <table id="table">
                    <?php
                        if($session == "total-score") {
                            load_total_score($con);
                        } else {
                            load_station_table($con, $session);
                        }
                    ?>
                </table>
            </div>
        </div>
    </div>
</body>

<?php 
    echo file_get_contents("header_footer/footer.html"); 
    $con=null;
?>