<?php
    session_start();
    include "../db.php";
    date_default_timezone_set('Asia/Kolkata');
    $currentDate = date('Y-m-d');

    if ( !isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) && !isset($_SESSION['uname']) ){
        header("Location: logout.php");
    }

    $admin_2_run = "SELECT admin_ref FROM users WHERE uref = '$_SESSION[uref]'";
    $admin_2_row2 = mysqli_query($con, $admin_2_run);
    $admin_2_row = mysqli_fetch_assoc($admin_2_row2);
    $admin_2 = $admin_2_row["admin_ref"];
?>

<!DOCTYPE html>
<html lang="en">
<head>
 <?php include "includes/meta.php"; ?>        
    <?php include "includes/css.php"; ?>     
  

</head>
<body id="page-top" class="mt-55">

    <div style="margin-top: 80px;"></div>

      <?php include "includes/header2.php"; ?>
      
<div class="container">
            <div class="row mx-0 just ify-content-center">
                <div class="col-12 col-sm-12 col-md-12">
                <div cl ass="card-body">
                <h1 class="h3 mb-2 text-gray-800 text-center">Student Attendance</h1>
                <hr>
                <form method="POST">
                    <div class="row pb-2 justify-content-center"> 
                        <div class="col-md-3 mt-3">
                            <label class="badge badge-warning">Class</label>
                             <select class="form-control" name="sclass" id="category-dropdown" required>
                                    <option value="" selected disabled>Select Class...</option>
                                    <?php
                                    $result = mysqli_query($con,"SELECT * FROM class WHERE uploadedby = '$admin_2'");
                                    while($row = mysqli_fetch_array($result)) {
                                    ?>
                                    <option value="<?php echo $row['sid'];?>"><?php echo $row["sclass"];?></option>
                                    <?php
                                    }
                                    ?>
                             </select>
                        </div>
                        <div class="col-md-3 mt-3">
                            <label class="badge badge-warning">Subject</label>
                            <!--<select name="period" id="period" class="form-control" required>-->
                            <select class="form-control" name="subject" id="sub-category-dropdown">
                                 <option value="" id="classMsg" selected disabled >Select Class First</option>
                             </select>
                        </div>
                        <div class="col-md-3 mt-3">
                            <label class="badge badge-warning">Class Time</label>
                            <input type="time" name="class_time" id="class_time" class="form-control" placeholder="Enter Class Time" required>
                        </div>
                        <div class="form-group col-md-3" style="margin-top: 28px;">
                            <button type="submit" name="start_attendance" class="btn btn-primary btn-sm w-100" style="font-size: 20px;"> Go <i class="fa fa-chevron-right"></i> </button>
                        </div>
                    </div>
                </form>

                <br>

                <div class="w-100 text-center">
                                    <a href="view-attendance-class.php" class="btn btn-primary"><i class="fa fa-eye-slash" aria-hidden="true"></i> View Attendance</a>
                </div>
            </div>
        </div>
    </div>
</div>
<div style="margin-top: 140px;"></div>
      <?php include "includes/ft.php"; ?>
       <?php include "includes/js.php"; ?>

<script>
$(document).ready(function() {

    $('#category-dropdown').on('change', function() {
        var category_id = this.value;
        var admin_2 = "<?php echo $admin_2; ?>";
        $("#classMsg").hide();
        $.ajax({
            url: "notes-upload-CatSubcat-Ajax.php",
            type: "POST",
            data: {
            admin_2: admin_2,
            category_id: category_id
            },
            cache: false,
            success: function(result){
            $("#sub-category-dropdown").html(result);
            }
        });
    });

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

<?php 
    if(isset($_POST['start_attendance'])){
        

        $sclassId = mysqli_real_escape_string($con, strip_tags($_POST['sclass']));
        $periodSid = mysqli_real_escape_string($con, strip_tags($_POST['subject']));
        $class_time = mysqli_real_escape_string($con, strip_tags($_POST['class_time']));

        $teacher_id = $_SESSION["uid"];
        $currentTime = date('M j, Y g:i a');
        $ref = mt_rand().'_'.time();

        $ins_sql = "INSERT INTO attendance (teacher_id, admin_2_ref, stu_class, class_period, class_time, class_date,class_date2, ref) VALUES ('$teacher_id', '$admin_2', '$sclassId', '$periodSid', '$class_time', '$currentTime','$currentDate', '$ref')";
        

        // echo '<pre>';print_r($ins_sql);exit();

        $stu_run = mysqli_query($con, "SELECT uid FROM users WHERE admin_ref = '$admin_2' AND sclass = '$sclassId'");
        while ( $stu_rows = mysqli_fetch_assoc($stu_run) ) {
            $ins_sql2 = "INSERT INTO attendance_2 (attendance_ref, stu_id, stu_attendance, admin_2_ref) VALUES ('$ref', '$stu_rows[uid]', 'A', '$admin_2')";

            // echo '<pre>';print_r($ins_sql2);exit();

            mysqli_query($con, $ins_sql2);
        }

        if (mysqli_query($con, $ins_sql)) {
            ?>
            <script>
                window.location = "student-attendance.php?ref=<?php echo $ref ?>";
            </script>
            <?php
        } else {
            ?>
            <script>
                window.location = "attendance.php";
            </script>
            <?php
        }
    }
?>


