<?php
session_start();
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">


    <?php include '../admin/includes/styles.php'; ?>
    <title>Classes |  <?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">Classes</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">Classes</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">
                        <?php
                        $sql = "SELECT * FROM teachers WHERE username =:username";
                        $conn->query($sql);
                        $conn->bind(":username", $username);
                        $details = $conn->fetchSingle();
                        $teacher_name = $details->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 * FROM classes WHERE id =:classID";
                            $conn->query($sql);
                            $conn->bind(":classID", $classID);
                            $resultSet = $conn->fetchSingle();
                            $className  = ucfirst($resultSet->name);
                            ?>
                            <div class="card">
                                <div class="card-header">
                                    <h3 class="card-title"> Manage Section </h3>
                                </div>
                                <!-- /.card-header -->
                                <div class="card-body">
                                    <table id="teacher" class="table table-bordered table-striped">
                                        <thead>
                                        <tr>
                                            <th>#</th>
                                            <th>Class</th>
                                            <th>Section</th>
                                            <th>Class numeric</th>
                                            <th>Teacher Name</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <?php
                                        $i = 1;
                                        if($conn->rowCount()){
                                            $class_numeric = $resultSet->class_numeric;
                                            ?>
                                            <tr>
                                                <td><?php echo $i++; ?></td>
                                                <td><?php echo $className;?></td>
                                                <td><?php echo $sectionName;?></td>
                                                <td><?php echo $class_numeric;?></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>#</th>
                                            <th>Class</th>
                                            <th>Section</th>
                                            <th>Class numeric</th>
                                            <th>Teacher Name</th>
                                        </tr>
                                        </tfoot>
                                    </table>
                                </div>
                                <!-- /.card-body -->
                            </div>

                        <?php else: ?>
                            <p class="text-center text-info">You are not a section teacher. Only section teachers can view section and class.</p>
                        <?php endif; ?>


                    </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>
        $(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">
        <title>Login to continue |  <?php echo $school_name; ?> </title>

        <?php include '../admin/includes/styles.php'; ?>

        <!-- 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
    }

    ?>
