<?php
    session_start();
    include("../scripts/connection.php");
    include("../scripts/functions.php");
    $user_data = check_login($con);

    if($_SERVER['REQUEST_METHOD'] == "GET") {
        $s_id = sanitize_input($_GET['station']);
    }

    if($_SERVER['REQUEST_METHOD'] == "POST") {
        $s_id = sanitize_input($_GET['station']);
        if(isset($_POST['save']) && get_direct_points($con, $s_id)->fetch()['direkte_punkte'] == 1){
            $points = sanitize_input($_POST['points']);
            $minutes = sanitize_input($_POST['minutes']);
            $seconds = sanitize_input($_POST['seconds']);
            $miliseconds = sanitize_input($_POST['miliseconds']);
            $m_id = sanitize_input($_POST['team']);

            if($minutes == 0 && $seconds == 0 && $miliseconds == 0){
                $time = null;
            } else {
                if ($minutes < 10) {
                    $minutes = "0" . $minutes;
                }

                if ($seconds < 10) {
                    $seconds = "0" . $seconds;
                }

                if ($miliseconds < 10) {
                    $miliseconds = "0" . $miliseconds;
                }
                $time = "00:" . $minutes . ":" . $seconds . "." . $miliseconds;
            }
            write_points($con, $s_id, $m_id, $points, $time);
        } elseif (isset($_POST['save'])) {
            $result = sanitize_input($_POST['result']);
            $m_id = sanitize_input($_POST['team']);
            write_result($con, $s_id, $m_id, $result);
        }
        header("Location: statistik.php");
        die;
    }

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

<body>
    <div class="center">
        <div class="headline">
            <h2>Ergebnis eintragen</h2>
        </div>
        <div class="form_div">
            <form action="" method="post">
                <div id="team-div" class="dropdown">
                    <label for="team">Mannschaft</label>
                    <select name="team" id="team">
                        <?php load_teams_no_points($con, sanitize_input($_GET['station']))?>
                    </select>
                </div>
                <?php
                if(get_direct_points($con, $s_id)->fetch()['direkte_punkte'] == 1) {
                echo "<div class=\"number_field\">
                    <label for=\"points\">Punkte: </label>
                    <input name=\"points\" id=\"points\" type=\"number\" max=\"15\" min=\"0\" value=\"0\" required/>
                </div>
                <div class=\"headline\">
                    <h3>Zeit</h3>
                </div>
                <div id=\"zeit\">
                    <div class=\"number_field\">
                        <label for=\"minutes\">Minuten</label>
                        <input name=\"minutes\" id=\"minutes\" type=\"number\" max=\"60\" min=\"0\" value=\"0\"/>
                    </div>
                    <div class=\"number_field\">
                        <label for=\"seconds\">Sekunden</label>
                        <input name=\"seconds\" id=\"seconds\" type=\"number\" max=\"60\" min=\"0\" value=\"0\"/>
                    </div>
                    <div class=\"number_field\">
                        <label for=\"miliseconds\">Millisekunden</label>
                        <input name=\"miliseconds\" id=\"miliseconds\" type=\"number\" max=\"99\" min=\"0\" value=\"0\"/>
                    </div>
                </div>";} else {
                    echo "<div class=\"number_field\">
                            <label for=\"result\">Ergebnis: </label>
                            <input name=\"result\" id=\"result\" type=\"number\" max=\"65535\" min=\"0\" value=\"0\" required/>
                        </div>";
                }?>
                <div>
                    <input id="button" type="submit" value="Eintragen" class="btn-confirm" name="save"/>
                    <input id="close" type="submit" value="Schließen" class="btn-confirm" name="close">
                </div>
            </form>
        </div>
    </div>
</body>
<?php
    $con = null;
    echo file_get_contents("header_footer/footer.html");
?>