<?php
include "koneksi.php";
$response = array();
if (isset($_POST['aksi'])) {
	if ($_POST['aksi'] == "tambah") {
		if (isset($_POST['nama_pernyataan'])) {
			$nama_pernyataan = $_POST['nama_pernyataan'];
			$id_dimensi = $_POST['id_dimensi'];

			$sql = "INSERT INTO pernyataan(nama_pernyataan, id_dimensi) VALUES('" . $nama_pernyataan . "', '" . $id_dimensi . "')";
			$result = mysqli_query($koneksi, $sql);
			if ($result) {
				$response["success"] = mysqli_insert_id($koneksi);
				$response["message"] = "Pernyataan berhasil ditambah";
				echo json_encode($response);
			} else {
				$response["success"] = 0;
				$response["message"] = "Pernyataan gagal ditambah";
				echo json_encode($response);
			}
		} else {
			$response["success"] = 0;
			$response["message"] = "Pernyataan gagal ditambah";
			echo json_encode($response);
		}
	} else if ($_POST['aksi'] == "edit") {
		if (isset($_POST['id_pernyataan'], $_POST['nama_pernyataan'])) {
			$id_pernyataan = $_POST['id_pernyataan'];
			$nama_pernyataan = $_POST['nama_pernyataan'];
			$id_dimensi = $_POST['id_dimensi'];

			$sql = "UPDATE pernyataan SET nama_pernyataan='" . $nama_pernyataan . "', id_dimensi='" . $id_dimensi . "' WHERE id_pernyataan='" . $id_pernyataan . "'";
			$result = mysqli_query($koneksi, $sql);
			if ($result) {
				$response["success"] = $id_pernyataan;
				$response["message"] = "Pernyataan berhasil diedit";
				echo json_encode($response);
			} else {
				$response["success"] = 0;
				$response["message"] = "Pernyataan gagal diedit";
				echo json_encode($response);
			}
		} else {
			$response["success"] = 0;
			$response["message"] = "Pernyataan gagal diedit";
			echo json_encode($response);
		}
	} else {
		if (isset($_POST['id_pernyataan'])) {
			$id_pernyataan = $_POST['id_pernyataan'];
			$sql = "DELETE FROM pernyataan WHERE id_pernyataan='" . $id_pernyataan . "'";
			$result = mysqli_query($koneksi, $sql);
			if ($result) {
				$response["success"] = $id_pernyataan;
				$response["message"] = "Pernyataan berhasil dihapus";
				echo json_encode($response);
			} else {
				$response["success"] = 0;
				$response["message"] = "Pernyataan gagal dihapus";
				echo json_encode($response);
			}
		} else {
			$response["success"] = 0;
			$response["message"] = "Pernyataan gagal dihapus (param=null)";
			echo json_encode($response);
		}
	}
} else {
	$response["success"] = 0;
	$response["message"] = "Olah pernyataan gagal";
	echo json_encode($response);
}
