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

if(!empty($_POST['class_name']) && !empty($_POST['teacher_name'])
    && !empty($_POST['pass_mark'])){
    $subject_code= $_POST['subject_code'];
    $subject_author = $_POST['subject_author'];
    $subject_name = $_POST['subject_name'];
    $pass_mark= $_POST['pass_mark'];
    $final_mark = $_POST['final_mark'];
    $teacher_name = $_POST['teacher_name'];
    $class_id = $_POST['class_name'];
    $subject_id = $_POST['subject_id'];
    $old_subject_name = $_POST['old_subject_name'];

    //fetch class name
    $sql = 'SELECT name FROM classes WHERE id = :class_id';
    $conn->query($sql);
    $conn->bind(':class_id', $class_id);
    $class_name = $conn->fetchColumn();

    //check if subject code already exists
    $sql = "SELECT * FROM subjects WHERE subject_code=:code AND class_id != :class_id";
    $conn->query($sql);
    $conn->bind(':code', $subject_code);
    $conn->bind(':class_id', $class_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>
                Subject code $subject_code already exists for another class.
                  <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 subjects SET class_id=:class_id,  teacher_name=:teacher_name,  pass_mark =:pass_mark, final_mark=:final_mark,  subject_name= :subject_name,  subject_author=:subject_author,  subject_code=:subject_code WHERE id = :subject_id";

        $conn->query($sql);
        $conn->bind(":class_id", $class_id);
        $conn->bind(":teacher_name", $teacher_name);
        $conn->bind(":pass_mark", $pass_mark);
        $conn->bind(":final_mark", $final_mark);
        $conn->bind(":subject_name", $subject_name);
        $conn->bind(":subject_author", $subject_author);
        $conn->bind(":subject_code", $subject_code);
        $conn->bind(":subject_id", $subject_id);

        $send = $conn->execute();

        //check if subject name also exists in the results table
        $sql = "SELECT subject FROM results WHERE subject =:old_subject_name AND class =:class_name";
        $conn->query($sql);
        $conn->bind(":old_subject_name", $old_subject_name);
        $conn->bind(":class_name", $class_name);
        $rowCount = $conn->rowCount();
        if ($rowCount > 0) {
            //update results table
            $sql = "UPDATE results SET subject=:subject_name WHERE subject =:old_subject_name AND class =:class_name";
            $conn->query($sql);
            $conn->bind(":subject_name", $subject_name);
            $conn->bind(":old_subject_name", $old_subject_name);
            $conn->bind(":class_name", $class_name);
            $conn->execute();
        }

     //update subject positions

        $sql = "SELECT subject FROM subject_positions WHERE subject =:old_subject_name AND class =:class_name";
        $conn->query($sql);
        $conn->bind(":old_subject_name", $old_subject_name);
        $conn->bind(":class_name", $class_name);
        $rowCount = $conn->rowCount();
        if ($rowCount > 0) {
            //update results table
            $sql = "UPDATE subject_positions SET subject=:subject_name WHERE subject =:old_subject_name AND class =:class_name";
            $conn->query($sql);
            $conn->bind(":subject_name", $subject_name);
            $conn->bind(":old_subject_name", $old_subject_name);
            $conn->bind(":class_name", $class_name);
            $conn->execute();
        }


        if ($send) {
            echo "<p class='alert alert-success alert-dismissible fade show' role='alert'>
                    <i class='fas fa-check-circle'></i> Subject 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; subject'>
           </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; subject'>
           </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>";
}



