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

    if($_SERVER['REQUEST_METHOD'] == "GET") {
        $m_id = intval(sanitize_input($_GET['m_id']));
        $s_id = intval(sanitize_input($_GET['s_id']));
        $row = get_result($con, $m_id, $s_id)->fetch();
        include("header_footer/header.php");
    }

    if($_SERVER['REQUEST_METHOD'] == 'POST') {
        $s_id = intval(sanitize_input($_POST['s_id']));
        $m_id = intval(sanitize_input($_POST['m_id']));
        if(isset($_POST['save']) && get_direct_points($con, $s_id)->fetch()['direkte_punkte'] == 1) {
            $points = intval(sanitize_input($_POST['points']));
            $minutes = intval(sanitize_input($_POST['minutes']));
            $seconds = intval(sanitize_input($_POST['seconds']));
            $millis = intval(sanitize_input($_POST['millis']));
            if (get_points($con, $m_id, $s_id)->fetch()['punkte'] != $points) {
                change_points($con, $m_id, $s_id, $points);
            }
        
            if (get_minutes($con, $m_id, $s_id)->fetch()['minutes'] != $minutes || get_seconds($con, $m_id, $s_id)->fetch()['seconds'] != $seconds || get_millis($con, $m_id, $s_id)->fetch()['millis'] != $millis) {
                if ($millis < 10) {
                    $millis = "0" . $millis;
                }
            
                if ($minutes < 10) {
                    $minutes = "0" . $minutes;
                }
            
                if ($seconds < 10) {
                    $seconds = "0" . $seconds;
                }
                $time = "00:" . $minutes . ":" . $seconds . "." . $millis;
                change_time($con, $m_id, $s_id, $time);
            }
        } elseif(isset($_POST['save'])) {
            $result = intval(sanitize_input($_POST['result']));
            update_result($con, $s_id, $m_id, $result);
        }
        header("Location: statistik.php");
        die;
    }
?>
<body>
    <div class="center">
        <div class="headline">
            <h2>Ergebnis bearbeiten</h2>
        </div>
        <div class="form_div">
            <form method="post">
                <label class="information">Mannschaftsname: <?php echo $row['name']?></label><br>
                <label for="fire_department" class="information">Feuerwehr: <?php echo $row["feuerwehr"]?></label><br>
                <?php
                if (get_direct_points($con, $s_id)->fetch()['direkte_punkte'] == 1){
                echo "<div class=\"number_field\">
                    <label for=\"points\">Punkte:</label>
                    <input type=\"number\" min=\"0\" max=\"15\" name=\"points\" value=\"" . $row['punkte'] . "\"/>
                </div>
                <div class=\"headline\">
                    <h3>Zeit</h3>
                </div>
                <div id=\"time\">";
                    $time_set = check_time($con, $m_id, $s_id);
                    echo "<div class=\"number_field\">
                        <label for=\"minutes\">Minuten:</label>
                        <input name=\"minutes\" type=\"number\" min=\"0\" max=\"60\" value="; 
                        if(!$time_set){
                            echo "\"0\"";
                        } else {
                            echo "\"" . get_minutes($con, $m_id, $s_id)->fetch()['minutes'] . "\""; 
                        }
                    echo "/>
                    </div>
                    <div class=\"number_field\">
                        <label for=\"seconds\">Sekunden</label>
                        <input name=\"seconds\" type=\"number\" min=\"0\" max=\"60\" value=";
                        if(!$time_set){
                            echo"\"0\"";
                        } else { 
                            echo "\"" . get_seconds($con, $m_id, $s_id)->fetch()['seconds'] . "\""; }
                    echo"/>
                    </div>
                    <div class=\"number_field\">
                        <label for=\"millis\">Millisekunden</label>
                        <input name=\"millis\" type=\"number\" min=\"0\" max=\"99\" value=";
                        if(!$time_set) {
                            echo"\"0\"";
                        } else { 
                            $millis = get_millis($con, $m_id, $s_id)->fetch()['millis'] / 1e4;
                            echo "\"" . $millis . "\""; 
                        }
                        echo"/>
                    </div>
                </div>";
                } else {
                    echo "<div class=\"number_field\">
                            <label for=\"result\">Ergebnis: </label>
                            <input name=\"result\" type=\"number\" min=\"0\" max=\"65535\" value=";
                    echo "\"" . get_result_team_station($con, $s_id, $m_id) . "\"";
                    echo "</div>";
                }?>
                <input type="hidden" name="m_id" value=<?php echo $m_id ?>/>
                <input type="hidden" name="s_id" value=<?php echo $s_id ?>/>
                <input name="save" type="submit" value="Speichern" class="btn-confirm"/>
                <input type="submit" name="close" value="Schließen" class="btn-confirm"/>
            </form>
        </div>
    </div>
</body>
<?php
    $con = null;
    echo file_get_contents("header_footer/footer.html");
?>