<?php
include("result.php");
include("../common/config.php");
include("../common/session.php");

// Set the default time zone to India Standard Time (IST)
date_default_timezone_set('Asia/Kolkata');

// Capture the batch name from the request
$batchname = $_REQUEST['batchname'];

if (isset($_POST['attendance'])) {
    $regno = $_REQUEST['regno'];

    $i = 0;
    foreach ($regno as $val) {
        $attn = $_REQUEST['atten' . $i];

        // Fetch the student's details from the database
        $sql1 = "SELECT * FROM admission WHERE admission_regno='$val'";
        $result11 = mysqli_query($con, $sql1);
        $row = mysqli_fetch_array($result11);
        $fname = $row['admission_fname'];

        // Get the current date and time (now) in IST
        $attendance_date = date('Y-m-d H:i:s'); // Format as 'YYYY-MM-DD HH:MM:SS'

        // Check if attendance already exists for this student, batch, and date
        $sql_check = "SELECT * FROM attendance WHERE attendance_stuid = '$val' AND attendance_batch = '$batchname' AND DATE(attendance_date) = DATE('$attendance_date')";
        $result_check = mysqli_query($con, $sql_check);

        // If no attendance record exists for this student on this date, insert a new one
        if (mysqli_num_rows($result_check) == 0) {
            if ($attn == "Present") {
                // Insert attendance for Present students
                $sql = "INSERT INTO attendance (attendance_stuid, attendance_stuname, attendance_batch, attendance_status, attendance_date) 
                        VALUES ('$val', '$fname', '$batchname', 'Present', '$attendance_date')";
                $result11 = mysqli_query($con, $sql);
            } else {
                // Insert attendance for Absent students
                $Phno = $row['admission_phone1'];
                $sql = "INSERT INTO attendance (attendance_stuid, attendance_stuname, attendance_batch, attendance_status, attendance_date) 
                        VALUES ('$val', '$fname', '$batchname', 'Absent', '$attendance_date')";
                $result11 = mysqli_query($con, $sql);

                // Send SMS if student is Absent
                $xml_data = '<?xml version="1.0"?>
<smslist>
    <sms>
        <user>vedantg</user>
        <password>1fe596eb75XXD</password>
        <mobiles>9140920656</mobiles>
        <message>Dear Student/Parent, ' . $fname . ' is Absent Today.
            If Any Query feel free to contact Mentor or 9911753333
            Vedanta IAS Academy</message>
        <senderid>VIASPL</senderid>
    </sms>
</smslist>';

$URL ='http://smsc.vianett.no/v3/send.ashx?data=' . urlencode($xml_data); //api here 

$i++;
}
}
?>


<!DOCTYPE HTML>
<html>

<head>
    <title>Vedanta Batch all Students list</title>
    <?php include 'header.php'; ?>
</head>

<body class="cbp-spmenu-push">
    <div class="main-content">
        <?php include 'header-leftside.php'; ?>
        <div id="page-wrapper">
            <div class="graphs">
                <div class="xs">
                    <br>
                    <h3>Mark Attendance</h3>
                    <div class="tab-content">
                        <div class="tab-pane active" id="horizontal-form">
                            <form class="form-horizontal" method="post" action="">
                                <div class="col-md-12">
                                    <div class="bs-example4" data-example-id="simple-responsive-table">
                                        <?php
                                        // $batchnam = $_REQUEST['batchname'];
                                        ?>

                                        <div class="table-responsive">
                                            <table class="table table-borded">
                                                <thead style="background-color: #06f; color: #FFF;">
                                                    <tr>
                                                        <th> Student ID</th>
                                                        <th> Student Name</th>
                                                        <th colspan="3" style="text-align:center;">Mark Attendance
                                                        </th>
                                                    </tr>
                                                </thead>
                                                </tbody>
                                                <?php
                                                $sqle = "SELECT * FROM addbatch WHERE add_batchname ='$batchname'";
                                                $resulte = mysqli_query($con, $sqle);
                                                $i = 0;
                                                while ($row = mysqli_fetch_array($resulte)) {
                                                    $batchno = $row['add_batchname'];
                                                    $reg = $row['add_regno'];

                                                    $sql = "SELECT * FROM admission WHERE admission_regno='$reg'";
                                                    $result = mysqli_query($con, $sql);

                                                    while ($row = mysqli_fetch_array($result)) {
                                                        $id = $row['admission_id'];
                                                        $regno = $row['admission_regno'];
                                                        $fname = $row['admission_fname'];
                                                        ?>
                                                <tr>
                                                    <th><input type="hidden" name="regno[]"
                                                            value="<?php echo $regno; ?>"><?php echo $regno; ?></th>
                                                    <th><?php echo $fname; ?></th>
                                                    <th><input type="checkbox" name="atten<?php echo $i; ?>"
                                                            value="Present"> Present</th>
                                                    <th><a target="_blank"
                                                            href="view-student-attendance.php?stuid=<?php echo $regno; ?>">View
                                                        </a> </th>
                                                    <?php
                                                    $i++;
                                                    }
                                                }
                                                ?>
                                                </tr>
                                                </tbody>
                                            </table>
                                            <button type="submit" name="attendance" class="btn-success">Mark
                                                Present</button>
                                        </div>
                                    </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>

            <div class="clearfix"> </div>
            <div class="footer">
                <p>Copyright 2025 Vedanta. All Rights Reserved. </p>
            </div>
        </div>
    </div>
</body>

</html>
