<?php
//open connection
include 'db_connection.php';

/* start the session */
session_start();

if(!isset($_SESSION['username'])){ // Jika session username ada berarti dia sudah login   
  echo "<script>  window.location.href='../../login.php';</script>"; // Kita Redirect ke halaman welcome.php
}

if (isset($_SESSION['username'])) {       
    $data = mysqli_query($conn,"SELECT * FROM user WHERE status IN ('I','D') AND user_id ='".$_SESSION['username']."'");
    $cek_usrBaned = mysqli_num_rows($data);

    if($cek_usrBaned > 0){ // found the banned user       
      session_destroy(); 
      echo "<script>  window.location.href='../../login.php';</script>";
    }

} else {    

}

//update timestamp
$cek_uid =$_SESSION['username'];  
$timezone = "Asia/Jakarta";
date_default_timezone_set($timezone);  
$datetime = new DateTime(date("Y-m-d H:i:s"));
$str_datetime = $datetime->format("Y-m-d H:i:s");
$sql  = ("UPDATE user SET last_activity = '$str_datetime' WHERE user_id = '$cek_uid'");
$query = mysqli_query($conn, $sql);


//get parameters
$data_users = $_POST['data_post'];

//get array value
$json = $data_users;
$array = json_decode($json, true);
$UserID = $array['UserID'];
$Status = $array['Status'];
$Access_Level = $array['Access_Level'];
$id_edit = $array['id_edit'];

// Attempt update query execution    
$sql  = ("UPDATE user SET status = '$Status', usr_level = '$Access_Level', update_by = '$cek_uid', date_update = '$str_datetime' WHERE ID = '$id_edit'");
$query = mysqli_query($conn, $sql);
        
        $group_sukses = "sukses";    
        $res = array("result" => $group_sukses);
        echo json_encode($res);

?>
