<?php
session_start();
//error_reporting('0');
if(isset($_SESSION['teacher'])){
$username = $_SESSION['teacher'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <style>
        .card-title a {
            font-size: 16px;
            border-bottom: 1px solid #707478;
            color: #707478;
        }
        .card-title a:hover{
            border-bottom: 1px solid #1A2229;
            color: #1A2229;
        }
        .btn-app:first-child{
            margin-left:0 !important;
        }
    </style>


    <?php include '../admin/includes/styles.php'; ?>
    <title>Sections |  <?php echo $school_name; ?> </title>


</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">

    <!-- Navbar -->
    <?php include 'includes/notifications.php'; ?>
    <!-- /.navbar -->

    <!-- Main Sidebar Container -->
    <?php include "includes/sidebar.php";?>

    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <div class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1 class="m-0">Sections</h1>
                    </div><!-- /.col -->
                    <div class="col-sm-6">
                        <ol class="breadcrumb float-sm-right">
                            <li class="breadcrumb-item"><a href="<?php echo $conn->teacher_url(); ?>">Home</a></li>
                            <li class="breadcrumb-item active">Sections</li>
                        </ol>
                    </div><!-- /.col -->
                </div><!-- /.row -->
            </div><!-- /.container-fluid -->
        </div>
        <!-- /.content-header -->

        <!-- Main content -->
        <section class="content">
            <div class="container-fluid">

                <div class="row">
                    <!--          <section class="col-lg-12 connectedSortable">-->
                    <!--           <a href="add-class"><button class="btn btn-outline-success">Add a class</button></a>-->
                    <!--          </section>-->
                    <section class="col-lg-12 connectedSortable">
                        <div class="card">
                            <div class="card-header" style="background: #17a2b8; color: white;">
                                <h3 class="card-title" style="font-size: 15px;"> <i class="fa fa-star"></i> Manage Sections </h3>
                            </div>
                            <!-- /.card-header -->
                            <div class="card-body">
                                <h3 class="card-title text-center" style="font-size: 15px;"> Please select a class to view the section <i class="fa fa-info-circle" data-toggle="tooltip" title="N.B: Only classes with sections added are clickable."></i></h3>
                                <br><br>
                                <div>
                                    <?php
                                    $sql = "SELECT * FROM teachers WHERE username =:username";
                                    $conn->query($sql);
                                    $conn->bind(":username", $username);

                                    $result = $conn->fetchSingle();
                                    $teacher_name = $result->name;

                                    //first fetch teacher's section
                                    $sql = "SELECT * FROM sections WHERE teacher_name =:teacher_name";
                                    $conn->query($sql);
                                    $conn->bind(":teacher_name", $teacher_name);
                                    $rowCount = $conn->rowCount();

                                    if($rowCount > 0){
                                        $result = $conn->fetchSingle();
                                        //teacher is a section teacher
                                        $sectionName = $result->name;
                                        //go ahead and fetch teachers class ID
                                        $classID = $result->class;
                                        // fetch class name
                                        $sql = "SELECT name FROM classes WHERE id =:classID";
                                        $conn->query($sql);
                                        $conn->bind(":classID", $classID);
                                        $className  = strtolower($conn->slugify($conn->fetchColumn()));

                                        //fetch section ID
                                        $sql = "SELECT id FROM sections WHERE name =:section_name AND class =:class_id";
                                        $conn->query($sql);
                                        $conn->bind(":section_name", $sectionName);
                                        $conn->bind(":class_id", $classID);
                                        $sectionId = $conn->fetchColumn();

                                        //fetch available students in the selected class and section
                                        $sql = "SELECT COUNT(studentID) FROM student WHERE classesID =:class_id AND sectionid =:section_id";
                                        $conn->query($sql);
                                        $conn->bind(":class_id", $classID);
                                        $conn->bind(":section_id", $sectionId);
                                        $students_available = $conn->fetchColumn();
                                        //enable or disable link on number of available sections  href="section/<?php echo $class_id;
                                        $newSectionName = $conn->slugify($sectionName);
                                        $url = $conn->teacher_url()."section/$classID/$newSectionName";

                                        //fetch available section in class
                                        $sql = "SELECT COUNT(id) FROM sections WHERE class =:class_name AND name =:sectionName AND teacher_name =:teacherName";
                                        $conn->query($sql);
                                        $conn->bind(":class_name", $classID);
                                        $conn->bind(":sectionName", $sectionName);
                                        $conn->bind(":teacherName", $teacher_name);
                                        $section_available = $conn->fetchColumn();
                                        //enable or disable link on number of available sections  href="section/<?php echo $class_id;

                                        ?>
                                        <a class="btn btn-app" style="min-width: 120px; height: 90px;" <?php if($section_available != 0){echo "href='$url'";}?>>
                                            <span class="badge bg-success" data-toggle="tooltip" title="<?php echo $section_available ?> section(s) available in class <?php echo $className; ?>"><?php echo $section_available ?></span>
                                            <i class="fas fa-users" style="font-size: 50px;"></i> <?php echo ucwords(str_replace('-', ' ', $className). ' '. $sectionName); ?>
                                        </a>
                                        <?php

                                    }else{
                                        //teacher is a subject teacher
                                        ?> <p class="text-center text-info">You are not section teacher. Only section teachers can view sections.</p> <?php
                                    }
                                    ?>



                                </div>
                                <?php
                                //display selected section below
                                if(isset($_GET['section']) AND isset($_GET['class'])){
                                    $class= $_GET['class'];
                                    $section = str_replace('-', ' ', $_GET['section']);

                                    $sql = "SELECT *  FROM sections WHERE name=:section AND class = :class_id AND teacher_name =:teacherName";
                                    $conn->query($sql);
                                    $conn->bind(":section", $section);
                                    $conn->bind(":class_id", $class);
                                    $conn->bind(":teacherName", $teacher_name);
                                    $result = $conn->fetchSingle();
                                    $sectionName = $result->name;

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


                                    ?>
                                    <div class="card-header" style="background: #c3570f; color: white; margin: 20px 0 2px;">
                                        <h3 class="card-title" style="font-size: 15px;"> <i class="fa fa-star"></i> Viewing Section details for class <?php echo $className . ' '. $sectionName; ?> </h3>
                                    </div>
                                    <table id="teacher" class="table table-bordered table-striped">
                                        <thead>
                                        <tr>
                                            <th>Section</th>
                                            <th>Capacity</th>
                                            <th>Category</th>
                                            <th>Teacher Name</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <?php
                                        if($conn->rowCount()){
                                            $capacity= $result->capacity;
                                            $category = $result->category;
                                            ?>
                                            <tr>
                                                <td><?php echo $sectionName;?></td>
                                                <td><?php echo $capacity;?></td>
                                                <td><?php echo $category; ?></td>
                                                <td><?php echo $teacher_name;?></td>
                                            </tr>


                                            <?php
                                        }else{
                                            ?> <tr>
                                                <td colspan="5" class="text-center">No Data in table</td>
                                            </tr><?php
                                        }

                                        ?>
                                        </tbody>
                                        <tfoot>
                                        <tr>
                                            <th>Section</th>
                                            <th>Capacity</th>
                                            <th>Category</th>
                                            <th>Teacher Name</th>
                                        </tr>
                                        </tfoot>
                                    </table>
                                    <?php


                                }



                                ?>

                            </div>
                            <!-- /.card-body -->
                        </div>


                    </section>
                </div>

                <div id="show-msg">

                </div>
                <!-- /.row (main row) -->
            </div><!-- /.container-fluid -->
        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
    <?php include "../admin/includes/footer.php";?>

    <script>
        $(document).ready(function(){
            $('[data-toggle="tooltip"]').tooltip();
        });
    </script>

    <script>
        $(function () {
            $("#teacher").DataTable({
                "responsive": true, "lengthChange": false, "autoWidth": false,
                "buttons": ["copy", "csv", "pdf", "print"]
            }).buttons().container().appendTo('#teacher_wrapper .col-md-6:eq(0)');
            $('#example2').DataTable({
                "paging": true,
                "lengthChange": false,
                "searching": true,
                "ordering": true,
                "info": true,
                "autoWidth": false,
                "responsive": true
            });
        });
    </script>

    <?php
    }else{
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, shrink-to-fit=no">

        <?php include '../admin/includes/styles.php'; ?>
        <title> Login to continue |  <?php echo $school_name; ?> </title>

        <!-- END PAGE LEVEL PLUGINS/CUSTOM STYLES -->

    </head>
    <body>
    <div class="container" style="margin-top: 20%;">
        <div class="row">
            <div class="col-lg-12 col-md-12">
                <div class="section-title text-center">
                    <h6> You need to login to continue </h6>
                    <div class="spinner-border text-info" role="status">
                        <span class="sr-only">Loading...</span>
                        <meta http-equiv='refresh' content='3; <?php echo $teacher_login_link; ?>'>
                    </div>
                    <p>Redirecting to the login  page...</p>
                </div>
            </div>
        </div>
    </div>

    <?php
    }

    ?>
