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

    if($_SERVER['REQUEST_METHOD'] == "GET") {
        $m_id = $_GET['m_id'];
        $row = get_team($con, $m_id)->fetch();
    }

    if($_SERVER['REQUEST_METHOD'] == "POST") {
        if(isset($_POST['save'])) {
            $m_id = sanitize_input($_POST['m_id']);
            $team_name = sanitize_input($_POST['team_name']);
            $fire_department = sanitize_input($_POST['fire_department']);
            $row = get_team($con, $m_id)->fetch();
            
            if($_POST['team_name'] != $row['name']) {
                update_team_name($con, $m_id, $team_name);
            }
        
            if($_POST['fire_department'] != $row['feuerwehr']) {
                update_team_fire_department($con, $m_id, $fire_department);
            }
        }
        header("Location: mannschaft.php");
        die;
    }

    include("header_footer/header.php");
?>
<body>
    <div class="center">
        <div class="headline">
            <h2>Mannschaft bearbeiten</h2>
        </div>
        <div class="form_div">
            <form method="post">
                <div class="txt_field">
                    <input type="text" name="team_name" <?php echo "value=\"" . $row['name'] . "\""?> />
                    <span></span>
                    <label for="team_name">Mannschaftsname</label>
                </div>
                <div class="txt_field">
                    <input type="text" name="fire_department" list="fire_departments" <?php echo "value=\"" . $row['feuerwehr'] . "\"" ?>/>
                    <datalist id="fire_departments">
                        <?php
                            load_fire_departments($con);
                            ?>
                    </datalist>
                    <span></span>
                    <label for="fire_department">Feuerwehr</label>
                </div>
                <input type="hidden" name="m_id" <?php echo "value=\"" . $row['m_id'] . "\""?> />
                <input type="submit" value="Speichern" class="btn-confirm" name="save"/>
                <input type="submit" nam="close" value="Schließen" class="btn-confirm"/>
            </form>
        </div>
    </div>
</body>
<?php
    $con = null;
    echo file_get_contents("header_footer/footer.html");
?>