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

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

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

    //check if the same name of class and section already exists for the section
    $sql = "SELECT * FROM sections WHERE name=:name AND class =:class_name AND id != :section_id";
    $conn->query($sql);
    $conn->bind(':name', $name);
    $conn->bind(':class_name', $class);
    $conn->bind(':section_id', $section_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>
                Section name already exists for class $class_name.
                  <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 sections SET name=:name, class =:class_name, teacher_name =:teacher_name,
            capacity =:capacity, category =:category WHERE id =:section_id";
        $conn->query($sql);
        $conn->bind(':name', $name);
        $conn->bind(':class_name', $class);
        $conn->bind(':capacity', $capacity);
        $conn->bind(':category', $category);
        $conn->bind(':teacher_name', $teacher_name);
        $conn->bind(':section_id', $section_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> Section 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; 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 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; 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>";
}



