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

if(!empty($_POST['percent_from']) AND !empty($_POST['percent_upto'])
   AND !empty($_POST['teacher_comment'])  AND !empty($_POST['principal_comment']) ){
    $percent_from= $_POST['percent_from'];
    $percent_upto = $_POST['percent_upto'];
    $teacher_comment = $_POST['teacher_comment'];
    $principal_comment = $_POST['principal_comment'];

    //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";
    $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);
    $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 have 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 = "INSERT INTO comments(percent_from, percent_upto, class_teacher_comment, principal_comment) 
                VALUES (:percent_from, :percent_upto, :class_teacher_comment, :principal_comment)";
        $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);
        $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 added 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 adding 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>";
}

