<?php

class QuizSegment extends Core {

    function save() {

        $data = $_POST;
        $quiz_id = $data['quizId'];
        $filter_id = $data['filterId'];

        if(empty($quiz_id) || empty($filter_id)) {
            throw new Exception("Parâmetros inválidos");
        }

        $sql_delete = "DELETE FROM quiz_segments WHERE quiz_id = :quiz_id";
        $stmt = parent::$conn->prepare($sql_delete);
        $stmt->execute([':quiz_id' => $quiz_id]);

        $sql_insert = "INSERT INTO quiz_segments (quiz_id, quiz_filter_id) VALUES (:quiz_id, :filter_id)";
        $stmt = parent::$conn->prepare($sql_insert);
        $stmt->execute([
            ':quiz_id' => $quiz_id,
            ':filter_id' => $filter_id
        ]);

        return ['type' => 'OK'];

    }

    function getSegment() {

        $quiz_id = $_GET['quizId'];        
        $sql_select = "SELECT * FROM quiz_segments
            WHERE quiz_id = :quiz_id";

        $stmt = parent::$conn->prepare($sql_select);
        $stmt->execute([':quiz_id' => $quiz_id]);
        $result = $stmt->fetch(PDO::FETCH_ASSOC);

        return ['type' => 'OK', 'data' => $result];

    }

}