<?php
switch ($formData->t) {
	case 'nama':
		if ($formData->l == 1) {
			$q = mysqli_query($connect, "update users set user_nama='" . $formData->val . "' where ID='" . $userData->ID . "'");
		} else {
			$q = mysqli_query($connect, "update karyawan set nama='" . $formData->val . "' where ID='" . $userData->ID . "' and ID_user='" . $userData->ID_user . "'");
		}
		if (!$q) {
			$error .= mysqli_error($connect);
		}
		break;
	case 'email':
		$email_kar = mysqli_num_rows(mysqli_query($connect, "select ID from karyawan where email='" . $formData->val . "'"));
		$email_usr = mysqli_num_rows(mysqli_query($connect, "select ID from users where user_email='" . $formData->val . "'"));
		if (!$email_kar && !$email_usr) {
			if ($formData->l == 1) {
				$q = mysqli_query($connect, "update users set user_email='" . $formData->val . "' where ID='" . $userData->ID . "'");
			} else {
				$q = mysqli_query($connect, "update karyawan set emal='" . $formData->val . "' where ID='" . $userData->ID . "' and ID_user='" . $userData->ID_user . "'");
			}
			if (!$q) {
				$error .= mysqli_error($connect);
			}
		} else {
			$error .= "Gunakan alamat email lain";
		}
		break;
	case 'hp':
		if ($formData->l == 1) {
			$q = mysqli_query($connect, "update users set user_hp='" . $formData->val . "' where ID='" . $userData->ID . "'");
		} else {
			$q = mysqli_query($connect, "update karyawan set hp='" . $formData->val . "' where ID='" . $userData->ID . "' and ID_user='" . $userData->ID_user . "'");
		}
		if (!$q) {
			$error .= mysqli_error($connect);
		}
		break;
	case 'cek-pass':
		$pass = Password($formData->val);
		if ($formData->l == 1) {
			$j = mysqli_num_rows(mysqli_query($connect, "select * from users where ID='" . $userData->ID . "' and password='$pass'"));
		} else {
			$j = mysqli_num_rows(mysqli_query($connect, "select * from karyawan where ID='" . $userData->ID . "' and ID_user='" . $userData->ID_user . "' and password='$pass'"));
		}
		if (!$j) {
			$error .= "Password Salah!";
		}
		break;
	case 'change-pass':
		$pass = Password($formData->val);
		if ($formData->l == 1) {
			$q = mysqli_query($connect, "update users set password='" . $pass . "' where ID='" . $userData->ID . "'");
		} else {
			$q = mysqli_query($connect, "update karyawan set password='" . $pass . "' where ID='" . $userData->ID . "' and ID_user='" . $userData->ID_user . "'");
		}
		if (!$q) {
			$error .= mysqli_error($connect);
		}
		break;
}
if ($error) {
	$data = array('status' => 0, 'msg' => $error);
} else {
	if ($formData->l == 1) {
		$b = mysqli_fetch_array(mysqli_query($connect, "select * from users where ID='" . $userData->ID . "'"));
		$timeout = time() + 1800;
		$auth_data = array('email' => $b['user_email'], 'ua' => $_SERVER['HTTP_USER_AGENT'], 'timeout' => $timeout);
		$auth = encryptString(json_encode($auth_data), ENCRYPTION_KEY);
		setAuth($connect, $b['ID'], $auth);
		$user = array('hp' => $b['user_hp'], 'email' => $b['user_email'], 'nama' => $b['user_nama'], 'foto' => $b['foto'], 't' => 1);
	} else {
		$b = mysqli_fetch_array(mysqli_query($connect, "select * from karyawan where ID='" . $userData->ID . "' and ID_user='" . $userData->ID_user . "'"));
		$timeout = time() + 1800;
		$auth_data = array('email' => $b['email'], 'ua' => $_SERVER['HTTP_USER_AGENT'], 'timeout' => $timeout);
		$auth = encryptString(json_encode($auth_data), ENCRYPTION_KEY);
		setAuth($connect, $b['ID'], $auth);
		$user = array('hp' => $b['no_hp'], 'email' => $b['email'], 'nama' => $b['nama'], 'foto' => $b['foto'], 't' => 2);
	}
	$data = array('status' => 1, 'msg' => "Berhasil disimpan", 'user' => $user, 'auth' => $auth);
}
