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

    if($_SERVER['REQUEST_METHOD'] == "POST") {
        try {
            $stmt = $con->prepare("DELETE FROM Punkte WHERE m_id = ? AND s_id = ?");
            $stmt->bindParam(1, $_POST["m_id"], PDO::PARAM_INT);
            $stmt->bindParam(2, $_POST["s_id"], PDO::PARAM_INT);
            $stmt->execute();
            if(get_direct_points($con, $_POST['s_id'])->fetch()['direkte_punkte'] == 0) {
                $stmt = $con->prepare("DELETE FROM Ergebnisse WHERE m_id = ? AND s_id =?");
                $stmt->bindParam(1, $_POST["m_id"], PDO::PARAM_INT);
                $stmt->bindParam(2, $_POST["s_id"], PDO::PARAM_INT);
                $stmt->execute();
                update_points($con, $_POST["s_id"]);
            } 
        } catch(PDOException $e) {
            handle_pdo_exception($e);
        }
    }

    $con = null;
    header("Location: statistik.php");
    die;
?>