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

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

    if($_SERVER['REQUEST_METHOD'] == 'POST') {
        $m_id = intval($_POST['m_id']);
        $s_id = intval($_POST['s_id']);
        $points = intval($_POST['points']);
        $minutes = intval($_POST['minutes']);
        $seconds = intval($_POST['seconds']);
        $millis = intval($_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) {
                $time = "00:" . $minutes . ":" . $seconds . "." . "0" . $millis;
            } else {
                $time = "00:" . $minutes . ":" . $seconds . "." . $millis;
            }
            change_time($con, $m_id, $s_id, $time);
        }

        header("Location: statistik.php");
        die;
    }
?>
<body>
    <div class="center">
        <div class="headline">
            <h2>Ergebniss 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>
                <div class="number_field">
                    <label for="points">Punkte:</label>
                    <input type="number" min="0" max="15" name="points" value="<?php echo $row['punkte']?>"/>
                </div>
                <div class="headline">
                    <h3>Zeit</h3>
                </div>
                <div id="time">
                    <?php $time_set = check_time($con, $row['m_id'], $row['s_id']);?>
                    <div class="number_field">
                        <label for="minutes">Minuten:</label>
                        <input name="minutes" type="number" min="0" max="60" value=<?php if(!$time_set){echo "\"0\"";} else {echo "\"" . get_minutes($con, $row['m_id'], $row['s_id'])->fetch()['minutes'] . "\""; }?>/>
                    </div>
                    <div class="number_field">
                        <label for="seconds">Sekunden</label>
                        <input name="seconds" type="number" min="0" max="60" value=<?php if(!$time_set){echo"\"0\"";} else { echo "\"" . get_seconds($con, $row['m_id'], $row['s_id'])->fetch()['seconds'] . "\""; }?>/>
                    </div>
                    <div class="number_field">
                        <label for="millis">Millisekunden</label>
                        <input name="millis" type="number" min="0" max="99" value=<?php if(!$time_set){echo"\"0\"";} else { echo "\"" . get_millis($con, $row['m_id'], $row['s_id'])->fetch()['millis'] / 1e4 . "\""; }?>/>
                    </div>
                </div>
                <input type="hidden" name="m_id" value=<?php echo $row['m_id'] ?>/>
                <input type="hidden" name="s_id" value=<?php echo $row['s_id'] ?>/>
                <input name="button" type="submit" value="Speichern" class="btn-confirm"/>
            </form>
        </div>
    </div>
</body>
<?php
    $con = null;
    echo file_get_contents("header_footer/footer.html");
?>