<?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 usr_profile SET last_activity = '$str_datetime' WHERE user_id = '$cek_uid'");
//$query = mysqli_query($conn, $sql);

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

//get array value
$json = $data_post;
$array = json_decode($json, true);
$Principal_Code = $array['Principal_Code'];
$Principal_Name = $array['Principal_Name'];
$Address = $array['Address'];
$Email = $array['Email'];
$Phone_Number = $array['Phone_Number'];
$City = $array['City'];
$id_edit = $array['id_edit'];


// menghitung jumlah data department_group yang ditemukan    
    $data = "SELECT * FROM Principal WHERE Principal_Code ='$Principal_Code' AND Status=0 AND ID <> $id_edit";
    $result = mysqli_query($conn, $data);
    $cek = mysqli_num_rows($result);       
    
if($cek > 0){	        	        
        $duplicate = "duplicate";    
        $res = array("result" => $duplicate);
        echo json_encode($res);
}
else{
		$sql  = ("UPDATE principal SET Principal_Code = '$Principal_Code', Principal_Name = '$Principal_Name', Address = '$Address', Email = '$Email', Phone = '$Phone_Number', City = '$City', Date_Updated = '$str_datetime' WHERE ID = '$id_edit'");
        $query = mysqli_query($conn, $sql);

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