<?php
session_start();
include 'includes/database.php';
include 'includes/functions.php';
$conn= new Functions();

if(!empty($_POST['percentage_from']) AND !empty($_POST['percentage_upto'])
   AND !empty($_POST['teachers_comment'])  AND !empty($_POST['principals_comment'])
  AND !empty($_POST['comment_id'])){
    $percent_from= $_POST['percentage_from'];
    $percent_upto = $_POST['percentage_upto'];
    $teacher_comment = $_POST['teachers_comment'];
    $principal_comment = $_POST['principals_comment'];
    $comment_id = $_POST['comment_id'];

    //check if details already exists

    $sql = "SELECT * FROM comments WHERE percent_from =:percent_from AND percent_upto =:percent_upto
            AND class_teacher_comment =:teacher_comment AND principal_comment =:principal_comment AND id =:comment_id";
    $conn->query($sql);
    $conn->bind(':percent_from', $percent_from);
    $conn->bind(':percent_upto', $percent_upto);
    $conn->bind(':teacher_comment', $teacher_comment);
    $conn->bind(':principal_comment', $teacher_comment);
    $conn->bind(':comment_id', $comment_id);
    $rowCount = $conn->rowCount();

    if($rowCount > 0){

        echo "<p class='alert alert-danger alert-dismissible fade show text-center' role='alert'>
              <i class='fas fa-ban'></i>
                Comment has already been added.
                  <button type='button' class='close' data-dismiss='alert' aria-label='Close'>
                             <span aria-hidden='true'>×</span>
                  </button>
           </p>";
           return false;
    }else{
        //process  updating  of data into database
        $sql = "UPDATE comments SET percent_from =:percent_from, percent_upto =:percent_upto, class_teacher_comment = :class_teacher_comment,
                principal_comment = :principal_comment WHERE id =:comment_id";
        $conn->query($sql);
        $conn->bind(":percent_from", $percent_from);
        $conn->bind(":percent_upto", $percent_upto);
        $conn->bind(":class_teacher_comment", $teacher_comment);
        $conn->bind(":principal_comment", $principal_comment);
        $conn->bind(":comment_id", $comment_id);
        $send = $conn->execute();
        if ($send) {
            echo "<p class='alert alert-success alert-dismissible fade show' role='alert'>
                    <i class='fas fa-check-circle'></i> Comment was updated successfully.
                  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\">
                             <span aria-hidden=\"true\">×</span>
                  </button>
                  <meta http-equiv='refresh' content='3; add-comment'>
           </p>";
        } else {
            echo "<p class='alert alert-danger alert-dismissible fade show' role='alert'>
                    <i class='fas fa-ban'></i> An error occurred while updating data.
                  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\">
                             <span aria-hidden=\"true\">×</span>
                  </button>
                  <meta http-equiv='refresh' content='4; add-comment'>
           </p>";
        }
    }

}else{
    echo "<p class='alert alert-danger alert-dismissible fade show text-center' role='alert'>
              <i class='fas fa-ban'></i>
               Fields marked (*) are required.
                  <button type='button' class='close' data-dismiss='alert' aria-label='Close'>
                             <span aria-hidden='true'>×</span>
                  </button>
           </p>";
}

