<?php
include "koneksi.php";
$response = array();
if(isset($_POST['aksi'])){
	if(isset($_POST['nama_pengguna']) && isset($_POST['password']) && isset($_POST['handphone'])) {
		$password = $_POST['password'];
		$nama_pengguna = $_POST['nama_pengguna'];
		$handphone = $_POST['handphone'];

		if($_POST['aksi']=="tambah"){
			$query=mysqli_query($koneksi, "SELECT * FROM pengguna WHERE handphone='".$handphone."'");
      $rows = mysqli_num_rows($query);
			if($rows==0){
				$sql = "INSERT INTO pengguna(nama_pengguna, handphone, password)
				VALUES('".$nama_pengguna."', '".$handphone."', '".$password."')";
				$result = mysqli_query($koneksi, $sql);
				if($result){
					$response["success"] = mysqli_insert_id($koneksi);
					$response["message"] = "Pendaftaran berhasil, silahkan login...";
					echo json_encode($response);
				} else {
					$response["success"] = 0;
					$response["message"] = "Pendaftaran gagal, ulangi proses daftar";
					echo json_encode($response);
				}
			} else {
				$response["success"] = 0;
				$response["message"] = "Nomor handphone sudah terdaftar gunakan nomor handphone lain...";
				echo json_encode($response);
			}
		} else {
			if(isset($_POST['id_pengguna'])){
				$id_pengguna = $_POST['id_pengguna'];
				$sql = "UPDATE pengguna SET nama_pengguna='".$nama_pengguna."', password='".$password."'
				WHERE id_pengguna='".$id_pengguna."'";
				$result = mysqli_query($koneksi, $sql);
				if($result){
					$response["success"] = 1;
					$response["id_pengguna"] = $id_pengguna;
					$response["message"] = "Edit Profil berhasil, data anda telah tersimpan...";
					echo json_encode($response);
				} else {
					$response["success"] = 0;
					$response["message"] = "Edit profil gagal, coba lagi nanti :(";
					echo json_encode($response);
				}
			} else {
				$response["success"] = 0;
				$response["message"] = "Edit profil gagal, coba lagi nanti :(";
				echo json_encode($response);
			}
		}
	} else {
		$response["success"] = 0;
		$response["message"] = "Aksi gagal, parameter tidak lengkap";
		echo json_encode($response);
	}
} else {
	$response["success"] = 0;
	$response["message"] = "Aksi gagal, parameter tidak lengkap";
	echo json_encode($response);
}
?>
