<?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'];
$New_Password = $array['New_Password'];
$Re_Password = $array['Re_Password'];
$Status = $array['Status'];
$date_create = date("Y-m-d");
$Access_Level = $array['Access_Level'];

// menghitung jumlah data yang ditemukan base on UserID    
$data = mysqli_query($conn,"SELECT * FROM user WHERE user_id ='$userID' AND status IN('A','I','O')");
$cek = mysqli_num_rows($data);

//var_dump($New_Password);
//print_r($New_Password);


if($cek > 0){	        	        
        $users_duplicate = "duplicate";    
        $res = array("result" => $users_duplicate);
        echo json_encode($res);				
}
elseif($New_Password != $Re_Password){    
        $users_pwd = "PwdNotSame";    
        $res = array("result" => $users_pwd);
        echo json_encode($res);
}
else{
        // Attempt update query execution    
        $sql  = ("INSERT INTO user (user_id, user_pwd, status, date_create, create_by, usr_level)
        VALUES ('$userID',sha1('$New_Password'),'$Status','$date_create','$cek_uid','$Access_Level')");
        $query = mysqli_query($conn, $sql);              

        $user_sukses = "sukses";    
        $res = array("result" => $user_sukses);
        echo json_encode($res);    
}

?>
