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

if(!empty($_POST['name']) && !empty($_POST['category'])
    && !empty($_POST['capacity'])){
    $name = $_POST['name'];
    $category= $_POST['category'];
    $capacity= $_POST['capacity'];
    $class= $_POST['class'];
    $teacher_name= $_POST['teacher_name'];

    $sql = "SELECT name FROM classes WHERE id =:id";
    $conn->query($sql);
    $conn->bind(":id", $class);
    $className = $conn->fetchColumn();

    //check if details already exists

    $sql = "SELECT * FROM sections WHERE name=:name AND class =:class_name";
    $conn->query($sql);
    $conn->bind(':name', $name);
    $conn->bind(':class_name', $class);

    $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>
                Section name already exists for class $className.
                  <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 sections (name, category, capacity, class, teacher_name) 
              VALUES(:name, :category, :capacity, :class_name, :teacher_name)";
        $conn->query($sql);
        $conn->bind(":name", $name);
        $conn->bind(":category", $category);
        $conn->bind(":capacity", $capacity);
        $conn->bind(":class_name", $class);
        $conn->bind(":teacher_name", $teacher_name);

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



