<?php
    require_once 'db.connect.php';
    require_once 'helpers/func/helper.php';
    require_once 'helpers/classes/Answer.php';

    $errors = array('answer' => '');

    if(isset($_POST['submitAnswer'])){
        $answer = $_POST['answer'];

        if($answer === ''){
            $errors['answer'] = 'Please add an Answer before submiting';
        }
        
        $answerClass = new Answer($conn);
        $addedAnswer = $answerClass->addAnswer($answer, $_SESSION['id'], $questionid);
        if($addedAnswer){
            header("Refresh:0");
        }else{
            $errors['answer'] = 'Unsuccessful please try again later';
        }
    }

?>

<div class="row">
    <form class="col s12" method="post" action="">
        <div class="row">
            <span><?php print $errors['answer']?></span>
            <div class="input-field col s12">
                <textarea id="answerField" name="answer" class="materialize-textarea"></textarea>
                <label for="answerField">Your Answer</label>
            </div>
        </div>
        <div class="row">
            <div class="input-field col s12">
                <input class="btn" name="submitAnswer" type="submit" />
            </div>
        </div>
    </form>
</div>