<?php 

ob_start();

include "../../db.php";

date_default_timezone_set('Asia/Kolkata');

session_start();



$currentCoreDate = date("Y-m-d");

$currentDT = date('M j, Y g:i a');



if (isset($_POST['action']) && $_POST['action'] == 'insertRemark') {



    $newRemark = mysqli_real_escape_string($con, $_POST["newRemark"]);

    $fdate = isset($_POST["fdate"]) ? mysqli_real_escape_string($con, $_POST["fdate"]) : '';

    $ftime = isset($_POST["ftime"]) ? mysqli_real_escape_string($con, $_POST["ftime"]) : '';

    $sid = mysqli_real_escape_string($con, $_POST["sid"]);

    $uid = isset($_SESSION['uid']) ? mysqli_real_escape_string($con, $_SESSION['uid']) : 'Unknown';



    // Check if required values are empty

    if (empty($newRemark) || empty($sid)) {

        echo "Error: Missing required fields.";

        exit();

    }



    // Insert remark

    $sql = "INSERT INTO remark (r_sid, remark, followup_date, followup_time, uploadDate, uploadCoreDate, r_by) 

            VALUES ('$sid', '$newRemark', '$fdate', '$ftime', '$currentDT', '$currentCoreDate', '$uid')";



    if (mysqli_query($con, $sql)) {

        // Update student_details table

        if ($fdate == '' && $ftime == '') {

            $up_sql = "UPDATE student_details SET st_lastRemarkDate = '$currentCoreDate' WHERE st_no = '$sid'";

        } else {

            $up_sql = "UPDATE student_details 

                        SET st_lastRemarkDate = '$currentCoreDate', 

                            last_followup_date = '$fdate', 

                            last_followup_time = '$ftime' 

                        WHERE st_no = '$sid'";

        }



        if (mysqli_query($con, $up_sql)) {

            echo "successful";

        } else {

            echo "Error in updating student_details: " . mysqli_error($con);

        }

    } else {

        echo "Error in inserting remark: " . mysqli_error($con);

    }

}

?>