<?php
if($_POST['t']=='create'){
	$password=Password($_POST['password']);
	if($_POST['foto']){
		$foto = simpanGambar($_POST['foto'],'assets/images/user/',75);
	}else{
		$foto = NULL;
	}
	$q=mysqli_query($connect, "INSERT INTO `users` (`ID_bidang`,`password`, `user_nama`, `user_email`,`level`,`user_tgl`, `user_status`,`foto`)
								 VALUES ('$_POST[bidang]','$password', '$_POST[user_nama]', '$_POST[user_email]','$_POST[level]', '$now', '1','$foto');");
	if(!$q){
		$data = array('status'=>0,'msg'=>mysqli_error($connect));
	}else{
		$data = array('status'=>1);
	}
	echo json_encode($data);
}elseif($_POST['t']=='update'){
	$q=mysqli_query($connect, "UPDATE `users` set `user_nama`='$_POST[user_nama]', `user_hp`='$_POST[user_hp]' where ID='$_POST[id]';");
	if($_POST['password']){
		$password=Password($_POST['password']);
		$q=mysqli_query($connect, "UPDATE `users` set password='$password' where ID='$_POST[id]'");
	}
	if(!$_POST['old_img']){
		$foto = simpanGambar($_POST['foto'],'assets/images/user/',75);
		$q=mysqli_query($connect, "UPDATE `users` set `user_foto`='$foto' where ID='$_POST[id]'");
	}
	if($_SESSION['id_user']==$_POST['id']){
		$_SESSION['nama_user']=$_POST['user_nama'];
		$_SESSION['email_user']=$_POST['user_email'];
	}
	if(!$q){
		$data = array('status'=>0,'msg'=>mysqli_error($connect));
	}else{
		$data = array('status'=>1,'reloadPage'=>1);
	}
	echo json_encode($data);
}elseif($_POST['t']=='delete'){
	$q=mysqli_query($connect, "DELETE FROM `users` WHERE `ID`='$_POST[id]';");
	if(!$q){
		$data = array('status'=>0,'msg'=>mysqli_error($connect));
	}else{
		$data = array('status'=>1);
	}
	echo json_encode($data);
}