<?php 

ob_start();

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

date_default_timezone_set('Asia/Kolkata');

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

session_start();



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

  

    $status_id=$_POST["status_id"];

    $sid =$_POST["sid"];



    $s_sql = "UPDATE student_details SET st_status='$status_id' WHERE st_no='$sid'";

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

        $output = "<div class='alert alert-success'> Update Successful</div>";

    }else { 

        $output = "<div class='alert alert-danger'> Not Working</div>";

    }

    echo $output ;

}



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

	

    $remark_for=$_POST["remark_for"];

    $output = "

        <form class='user' id='submitRemark' method='post' enctype='multipart/form-data'>

                    <input type='hidden' id='sid' value='".$remark_for."'>

                    <textarea rows='4' id='newRemark' placeholder='Enter New  Remarks' class='form-control mb-2'></textarea>

                    <input type='date' id='fdate' class='form-control mb-2'>

                    <input type='time' id='ftime' class='form-control mb-2'>

                    <button type='submit' class='btn btn-success btn-user mt-2'>Update</button>

        </form>



        <hr>

    ";



   // print_r($remark_by);

    $sql = "SELECT * FROM remark WHERE r_sid = '$remark_for' ORDER BY r_id DESC";

      $run = mysqli_query($con,$sql);



    while ( $row = mysqli_fetch_assoc($run) ) 

    {

        $output .= "<br><b>".$row['uploadDate']."</b> -  ".$row['remark'] ;

    }

   



    echo $output ;

}



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



  $remark_for=$_POST["remark_for"];

  $output = "";



 // print_r($remark_by);

  $sql = "SELECT * FROM admin_remark WHERE r_sid = '$remark_for' ORDER BY r_id DESC";

   $run = mysqli_query($con,$sql);



    while ( $row = mysqli_fetch_assoc($run) ) 

    {

        $output .= "<br><b>".$row['uploadDate']."</b> -  ".$row['remark'] ;

    }

   



    echo $output ;

}





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

	

    $sid =$_POST["sid"];



    $st_sql = "SELECT `s_id` FROM `status` WHERE `s_name`='Pending'";

    $st_run = mysqli_query($con, $st_sql);

    $st_rows = mysqli_fetch_assoc($st_run);



    $count_sql = "SELECT `st_no` FROM `student_details` 

                   WHERE `st_by` = '$_SESSION[uid]' 

                   AND `st_admin`='$_SESSION[uadmin]' 

                   AND `st_status` = '$st_rows[s_id]'

                   ";

    $count_run = mysqli_query($con, $count_sql);

    $count_rows = mysqli_num_rows($count_run);



    if( $count_rows <= 25){



         $s_sql = "UPDATE student_details SET st_sub_by='$_SESSION[uid]', st_status = '$st_rows[s_id]' WHERE st_no='$sid'";



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



              $output = "<div class='alert alert-success'> Update Successful</div>";

         }

         else {

              

              $output = "<div class='alert alert-danger'> Not Working</div>";

         }

    }else{

         $output = "<div class='alert alert-warning'>First end your pending leads.</div>";

    }



   echo $output ;

}



 ?>





<script>

$('#submitRemark').on('submit', function(event) {

    event.preventDefault();



    var newRemark = $('#newRemark').val().trim();

    var fdate = $('#fdate').val();

    var ftime = $('#ftime').val();

    var sid = $('#sid').val();



    if (newRemark !== '' && sid !== '') {

        $.ajax({

            url: "action-underAction-Ajax.php",

            method: "POST",

            data: {

                action: 'insertRemark',

                newRemark: newRemark,

                fdate: fdate,

                ftime: ftime,

                sid: sid

            },

            success: function(result) {

                console.log("Server Response:", result);

                if ($.trim(result) === 'successful') {

                    alert("Remark added successfully!");

                    location.reload(); // Refresh page after successful submission

                } else {

                    alert("Error: " + result);

                }

            },

            error: function(xhr, status, error) {

                console.error("AJAX Error: ", error);

            }

        });

    } else {

        alert("Please write a Remark.");

    }

});

</script>