<?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'></textarea>
                        <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 = "
        <form class='user' id='submitAdminRemark' method='post' enctype='multipart/form-data'>
                        <input type=hidden id='sid' value='".$remark_for."'>
                        <textarea rows='4' id='newRemark' placeholder='Enter New  Admin Remarks' class='form-control'></textarea>
                        <button type='submit' class='btn btn-success btn-user mt-2'>Update</button>
            </form>

            <hr>
      ";

     // 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'] == 'subagentChange') {
	
     $agentId=$_POST["agentId"];
     $sid =$_POST["sid"];

    $st_sql = "SELECT `s_id` FROM `status` WHERE `s_by`='$_SESSION[uref]' AND `s_name`='Pending'";
    $st_run = mysqli_query($con, $st_sql);
    $st_rows = mysqli_fetch_assoc($st_run);

    $andWhere = "";
    if($st_rows){
        $andWhere = ", st_status = '$st_rows[s_id]'";
    }

    $s_sql = "UPDATE student_details SET st_sub_by='$agentId' $andWhere 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'] == 'MoveToRecycleBin') {

     $sid =$_POST["sid"];

     $s_sql = "UPDATE student_details SET `flag_status` ='D' WHERE st_no='$sid'";

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

          $output = "<div class='alert alert-danger'> Delete Successfully</div>";
     }
     else {
          
          $output = "<div class='alert alert-danger'> Not Working</div>";
     }


    echo $output ;
}

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

     $sid =$_POST["sid"];

     $s_sql = "UPDATE student_details SET `flag_status` ='A' WHERE st_no='$sid'";

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

          $output = "<div class='alert alert-success'> Reload Successfully</div>";
     }
     else {
          
          $output = "<div class='alert alert-danger'> Not Working</div>";
     }


    echo $output ;
}

 ?>


<script>
$('#submitRemark').on('submit', function(event) {
    event.preventDefault();

    var newRemark = $('#newRemark').val();
    var sid = $('#sid').val();
    //console.log('enter write review');  
    if (newRemark != '' && sid != '') {
        $.ajax({
            url: "action-underAction-Ajax.php",
            method: "POST",
            data: {
                action: 'insertRemark',
                newRemark: newRemark,
                sid: sid
            },
            success: function(result) {
                //alert(data);  
                if (result == 'successful') {
                    alert("successful");
                } else {
                    alert("error");
                    // location.reload();  
                }
            }
        });
    } else {
        alert("Write a Remark.");
    }
});





$('#submitAdminRemark').on('submit', function(event) {
    event.preventDefault();

    var newRemark = $('#newRemark').val();
    var sid = $('#sid').val();
    //console.log('enter write review');  
    if (newRemark != '' && sid != '') {
        $.ajax({
            url: "action-underAction-Ajax.php",
            method: "POST",
            data: {
                action: 'insertAdminRemark',
                newRemark: newRemark,
                sid: sid
            },
            success: function(result) {
                //alert(data);  
                if (result == 'successful') {
                    alert("successful");
                } else {
                    alert("error");
                    // location.reload();  
                }
            }
        });
    } else {
        alert("Write a Remark.");
    }
});
</script>