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

if(!empty($_POST['class']) && !empty($_POST['term'])
    && !empty($_POST['1st_ca'])){
    $class = $_POST['class'];
    $term= $_POST['term'];
    $first_ca= $_POST['1st_ca'];
    $second_ca= $_POST['2nd_ca'];
    $third_ca= $_POST['3rd_ca'];
    $exam= $_POST['exam'];

    //check if first_ca, second_ca and exam are more than 100 or less than 100

    $total = $first_ca + $second_ca + $third_ca + $exam;

    if($total > 100){
        echo "<script>
                   toastr['error']('1st CA, 2nd CA, 3rd CA and Exam total can\'t exceed 100');
                 </script>";
        return false;
    }elseif($total < 100){
        echo "<script>
               toastr['error']('1st CA, 2nd CA, 3rd CA and Exam total must add up to 100');
             </script>";
        return false;
    }


    //check if details already exists

    $sql = "SELECT * FROM mark_setting WHERE class=:class AND exam =:exam AND term =:term AND first_ca =:first_ca
            AND second_ca =:second_ca AND third_ca =:third_ca AND exam =:exam";
    $conn->query($sql);
    $conn->bind(':class', $class);
    $conn->bind(':exam', $exam);
    $conn->bind(':term', $term);
    $conn->bind(':first_ca', $first_ca);
    $conn->bind(':second_ca', $second_ca);
    $conn->bind(':third_ca', $third_ca);

    $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>
                 This marks setting 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 ="INSERT INTO mark_setting (class, term, first_ca, second_ca,third_ca, exam) 
               VALUES (:class, :term, :first_ca, :second_ca, :third_ca, :exam)";
        $conn->query($sql);
        $conn->bind(':class', $class);
        $conn->bind(':exam', $exam);
        $conn->bind(':term', $term);
        $conn->bind(':first_ca', $first_ca);
        $conn->bind(':second_ca', $second_ca);
        $conn->bind(':third_ca', $second_ca);

        $send = $conn->execute();
        if ($send) {
            echo "<p class='alert alert-success alert-dismissible fade show' role='alert'>
                    <i class='fas fa-check-circle'></i> Mark Setting for $term $class 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; mark-setting'>
           </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; mark-setting'>
           </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>";
}


