<?php
session_start();
if(isset($_SESSION['teacher']))
{
    $teacher_id=$_SESSION['teacher']['id'];
    if(isset($_GET['classId']))
    {
        $class_id=htmlentities($_GET['classId']);
    include '../classes/classroom_class.php';
    include '../classes/subjects_class.php';
    include '../classes/class_subjects_class.php';
    include '../classes/teachers.php';
    include "../manage/function.php";
    include '../classes/attendance_class.php';

    $attendance=new Attendance("../manage/connection.php");
    $classroom=new ClassRoom("../manage/connection.php");
    $subject=new Subject("../manage/connection.php");
    $classSubject=new ClassSubject("../manage/connection.php");
    $teachers=new Teachers("../manage/connection.php");
    if($classroom->getClassRoom($class_id)['teacher_id']==$teacher_id)
    {
        
    ?>
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Malika International Academy Teacher</title>
    <!-- HTML5 Shim and Respond.js IE9 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
      <![endif]-->
    <!-- Meta -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="description" content="CodedThemes">
    <meta name="keywords"
        content=" Admin , Responsive, Landing, Bootstrap, App, Template, Mobile, iOS, Android, apple, creative app">
    <meta name="author" content="CodedThemes">
    <!-- Favicon icon -->
    <link rel="icon" href="assets/images/favicon.ico" type="image/x-icon">
    <!-- Google font-->
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:400,600" rel="stylesheet">
    <!-- Required Fremwork -->
    <link rel="stylesheet" type="text/css" href="assets/css/bootstrap/css/bootstrap.min.css">
    <!-- themify-icons line icon -->
    <link rel="stylesheet" type="text/css" href="assets/icon/themify-icons/themify-icons.css">
    <!-- ico font -->
    <link rel="stylesheet" type="text/css" href="assets/icon/icofont/css/icofont.css">
    <!-- Style.css -->
    <link rel="stylesheet" type="text/css" href="assets/css/style.css">
    <link rel="stylesheet" type="text/css" href="assets/css/jquery.mCustomScrollbar.css">
    <link rel="stylesheet" href="assets/vendors/simple-datatables/style.css">
    <style>
    input[type='radio'][id='present']:after {
        width: 30px;
        height: 30px;
        /* border-radius: 15px;
                                                top: -2px;
                                                left: -1px;
                                                position: relative; */
        background-color: #d1d3d1;
        content: '';
        display: inline-block;
        visibility: visible;
        border: 2px solid white;
    }

    input[type='radio'][id='present']:checked:after {
        width: 30px;
        height: 30px;
        /* border-radius: 15px;
                                                top: -2px;
                                                left: -1px;
                                                position: relative; */
        background-color: green;
        content: '';
        display: inline-block;
        visibility: visible;
        border: 2px solid white;
    }

    input[type='radio'][id='absent']:after {
        width: 30px;
        height: 30px;
        /* border-radius: 15px;
                                                top: -2px;
                                                left: -1px;
                                                position: relative; */
        background-color: #d1d3d1;
        content: '';
        display: inline-block;
        visibility: visible;
        border: 2px solid white;
    }

    input[type='radio'][id='absent']:checked:after {
        width: 30px;
        height: 30px;
        /* border-radius: 15px;
                                                top: -2px;
                                                left: -1px;
                                                position: relative; */
        background-color: red;
        content: '';
        display: inline-block;
        visibility: visible;
        border: 2px solid white;
    }
    </style>
</head>

<body>
    <!-- Pre-loader start -->
    <div class="theme-loader">
        <div class="ball-scale">
            <div class='contain'>
                <div class="ring">
                    <div class="frame"></div>
                </div>
                <div class="ring">
                    <div class="frame"></div>
                </div>
                <div class="ring">

                    <div class="frame"></div>
                </div>
                <div class="ring">
                    <div class="frame"></div>
                </div>
                <div class="ring">
                    <div class="frame"></div>
                </div>
                <div class="ring">
                    <div class="frame"></div>
                </div>
                <div class="ring">
                    <div class="frame"></div>
                </div>
                <div class="ring">
                    <div class="frame"></div>
                </div>
                <div class="ring">
                    <div class="frame"></div>
                </div>
                <div class="ring">
                    <div class="frame"></div>
                </div>
            </div>
        </div>
    </div>
    <!-- Pre-loader end -->
    <div id="pcoded" class="pcoded">
        <div class="pcoded-overlay-box"></div>
        <div class="pcoded-container navbar-wrapper">

            <?php
                include 'topnav.php';
            ?>
            <div class="pcoded-main-container">
                <div class="pcoded-wrapper">
                    <?php
                        include 'sidenav.php';
                    ?>
                    <div class="pcoded-content">
                        <div class="pcoded-inner-content">
                            <div class="main-body">
                                <div class="page-wrapper">
                                    <div>
                                        <h4>
                                            Class Info
                                            <?php echo strtoupper($classroom->getClassRoom($class_id)['section']).' '.$classroom->getClassRoom($class_id)['class_no'];?><br>
                                            Students: <?php echo count($classroom->getAllClassStudents($class_id));?>
                                        </h4>
                                        <?php
                                            $att_date=date('Y-m-d');
                                            if(isset($_GET['date']))
                                            {
                                                $att_date=htmlentities($_GET['date']);
                                            }
                                        ?>
                                    </div>
                                    <div class="page-body">
                                        <section class="people">
                                            <section class="w3l-team-block">
                                                <!-- //people cards style 1 -->
                                                <div class="row">
                                                    <div class="col-sm-12">
                                                        <div class="card card_border mb-5">
                                                            <div class="cards__heading">
                                                                <div id="sstatus" align="center"></div>
                                                                <div class="container-fluid">
                                                                    <div class="row">
                                                                        <div class="col-sm-10">
                                                                            <!-- <h3>Students <span> </span></h3> -->
                                                                            <!-- <div>Current Session <b>First term 2021</b></div> -->
                                                                        </div>
                                                                        <div class="col-sm-2" align="right">
                                                                            <!-- Button -->
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                            <div class="card-body">

                                                                <form method="post" name="attendance">
                                                                    <div class="row">
                                                                        <div class="col-sm-5"></div>
                                                                        <div align="center" clas='col-sm-4'>
                                                                            <h4>Choose Date</h4>
                                                                            <input type="date" name="attendance_date"
                                                                                class="form-control"
                                                                                value="<?php echo $att_date;?>" />
                                                                        </div>
                                                                    </div>
                                                                    <table class='table table-striped' id="table1">
                                                                        <thead>
                                                                            <tr>
                                                                                <th>S/N</th>
                                                                                <th>First Name</th>
                                                                                <th>Last Name</th>
                                                                                <th>Reg No.</th>
                                                                                <th>Present</th>
                                                                                <th>Absent</th>
                                                                            </tr>
                                                                        </thead>
                                                                        <tbody>
                                                                            <?php
                                                                                $n=1;
                                                                                foreach($classroom->getAllClassStudents($class_id) as $cStudent)
                                                                                {
                                                                                    $attendance_status=$attendance->getAttendance($cStudent['id'],$att_date)['attendance'];
                                                                                    // if($attendance_status=="present")
                                                                                    echo '<tr>
                                                                                        <td>'.$n.'</td>
                                                                                        <td>'.$cStudent['firstname'].'</td>
                                                                                        <td>'.$cStudent['lastname'].'</td>
                                                                                        <td>'.$cStudent['reg_no'].'</td> 
                                                                                        <td>
                                                                                            <input type="radio" id="present" name="'.$cStudent['id'].'_attendance" '.($attendance_status=="present"?"checked":"").' value="present"/>
                                                                                        </td>
                                                                                        <td>
                                                                                            <input type="radio" id="absent" data-toggle="modal"
                                                                                            data-target="#assessModal'.$cStudent['id'].'" name="'.$cStudent['id'].'_attendance" '.($attendance_status=="absent"?"checked":"").' style="height: 30px; width: 30px;" value="absent"/>

                                                                                            <!--<button type="button" class="btn btn-primary" data-toggle="modal"
                                                                                                data-target="#assessModal'.$cStudent['id'].'" id="assess">
                                                                                                    Assess
                                                                                                </button>-->

                                                                                            <div class="modal fade" id="assessModal'.$cStudent['id'].'" tabindex="-1" role="dialog"
                                                                                                aria-labelledby="assessModalTitle" aria-hidden="true">
                                                                                                <div class="modal-dialog modal-dialog-centered" role="document">
                                                                                                    <div class="modal-content">
                                                                                                        <div class="modal-header">
                                                                                                            <h5 class="modal-title" id="delModalLongTitle">
                                                                                                                Assessment for <b>'.$cStudent['firstname'].' '.$cStudent['lastname'].'</b></h5>
                                                                                                            <button type="button" class="close" data-dismiss="modal"
                                                                                                                aria-label="Close">
                                                                                                                <span aria-hidden="true">&times;</span>
                                                                                                            </button>
                                                                                                        </div>
                                                                                                        
                                                                                                        <div id="sstatus_" name="'.$n.'" align="center"></div>
                                                                                                        <div class="modal-body" align="center">
                                                                                                            <textarea name="reason_'.$cStudent['id'].'" class="form-control" placeholder="Reason for being absent"></textarea>
                                                                                                        </div>
                                                                                                        <div class="modal-footer">
                                                                                                    <button type="button" class="btn btn-danger"
                                                                                                            data-dismiss="modal">Save & Close</button>
                                                                                                            <!-- <button type="button" class="btn btn-success">Save
                                                                                                            changes</button>-->
                                                                                                        </div>
                                                                                                        </div>
                                                                                                        </div>
                                                                                                    </div>
                                                                                        </td>
                                                                                    </tr>';
                                                                                    $n++;
                                                                                }
                                                                                ?>
                                                                        </tbody>
                                                                    </table>
                                                                    <div align="center">
                                                                        <div id="present_count"></div>
                                                                        <input type="radio" name="class_id"
                                                                            value="<?php echo $class_id;?>"
                                                                            style="display: none;" checked />
                                                                        <button name="submit"
                                                                            class="btn btn-success">Mark
                                                                            Attendance</button>
                                                                    </div>
                                                                </form>
                                                            </div>
                                                        </div>
                                                    </div>

                                                </div>
                                            </section>
                                            <!-- //teams1 -->

                                            <!-- //people cards style 2 -->

                                        </section>
                                    </div>

                                    <div id="styleSelector">

                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>

        <!-- Warning Section Ends -->
        <!-- Required Jquery -->
        <script type="text/javascript" src="assets/js/jquery/jquery.min.js"></script>
        <script type="text/javascript" src="assets/js/jquery-ui/jquery-ui.min.js"></script>
        <script type="text/javascript" src="assets/js/popper.js/popper.min.js"></script>
        <script type="text/javascript" src="assets/js/bootstrap/js/bootstrap.min.js"></script>
        <!-- jquery slimscroll js -->
        <script type="text/javascript" src="assets/js/jquery-slimscroll/jquery.slimscroll.js"></script>
        <!-- modernizr js -->
        <script type="text/javascript" src="assets/js/modernizr/modernizr.js"></script>
        <!-- am chart -->
        <script src="assets/pages/widget/amchart/amcharts.min.js"></script>
        <script src="assets/pages/widget/amchart/serial.min.js"></script>
        <!-- Todo js -->
        <script type="text/javascript " src="assets/pages/todo/todo.js "></script>
        <!-- Custom js -->
        <script type="text/javascript" src="assets/pages/dashboard/custom-dashboard.js"></script>
        <script type="text/javascript" src="assets/js/script.js"></script>
        <script type="text/javascript " src="assets/js/SmoothScroll.js"></script>
        <script src="assets/js/pcoded.min.js"></script>
        <script src="assets/js/demo-12.js"></script>
        <script src="assets/js/jquery.mCustomScrollbar.concat.min.js"></script>

        <script src="assets/js/jquery.nicescroll.js"></script>
        <script src="assets/js/scripts.js"></script>
        <script src="assets/vendors/perfect-scrollbar/perfect-scrollbar.min.js"></script>
        <script src="assets/vendors/simple-datatables/simple-datatables.js"></script>
        <script src="assets/js/vendors.js"></script>
        <script>
        var $window = $(window);
        var nav = $('.fixed-button');
        $window.scroll(function() {
            if ($window.scrollTop() >= 200) {
                nav.addClass('active');
            } else {
                nav.removeClass('active');
            }
        });
        </script>
        <script>
        $(document).on('change','input[name=attendance_date]',function()
        {
            // alert($('input[name=attendance_date]').val());
            window.location="?classId=<?php echo $class_id.'&date=';?>"+$('input[name=attendance_date]').val();
        });
    $(document).on('click', 'input[type="radio"]', function(e) {
        // e.preventDefault();
        //    alert("WORKIN"); 
        var pres = 0;
        var abs = 0;

        $('[id*="present"]').each(function() {
            if ($(this).is(':checked')) {
                // alert($(this).val())
                pres++;
            }
        });
        $('[id*="absent"]').each(function() {
            if ($(this).is(':checked')) {
                // alert($(this).val())
                abs++;
            }
        });
        $('#present_count').html('<h4 class="card-title">' + pres + ' Present and ' + abs + ' Absent</h4>');
        // alert(pres);
    });

    $(document).on('submit', 'form[name=attendance]', function(e) {
        e.preventDefault();
        //    alert("WORKIN");
        var formData = new FormData(this);
        var pres = 0;
        $('[id*="present"]').each(function() {
            if ($(this).is(':checked')) {
                // alert($(this).val())
                pres++;
            }
        });
        $('#sstatus').html('<h4 class="card-title">Adding</h4>');
        // alert(pres);
        $.ajax({
            url: "../control/attendance.php", // Url to which the request is send
            type: "POST", // Type of request to be send, called as method
            data: formData, // Data sent to server, a set of key/value pairs (i.e. form fields and values)
            contentType: false, // The content type used when sending data to the server.
            cache: false, // To unable request pages to be cached
            processData: false, // To send DOMDocument or non processed data file it is set to false
            success: function(data) // A function to be called if request succeeds
            {
                $('#sstatus').html('<h4 class="card-title">' + data + '</h4>');
                alert(data);

            }
        });
    });
    </script>
</body>

</html>
<?php
    }
    else
    {
        //Not appropriate teacher
        header('location:classes.php');
    }
    }
    else
    {
        header("location:classes.php");
    }
}
else
{
    header("location:../index.php");
}
?>