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

			$sql = "INSERT INTO dimensi(nama_dimensi) VALUES('" . $nama_dimensi . "')";
			$result = mysqli_query($koneksi, $sql);

			if ($result) {
				$response["success"] = mysqli_insert_id($koneksi);
				$response["message"] = "Atribut berhasil ditambah";
				echo json_encode($response);
			} else {
				$response["success"] = 0;
				$response["message"] = "Atribut gagal ditambah";
				echo json_encode($response);
			}
		} else {
			$response["success"] = 0;
			$response["message"] = "Atribut gagal ditambah";
			echo json_encode($response);
		}
	} else if ($_POST['aksi'] == "edit") {
		if (isset($_POST['id_dimensi'], $_POST['nama_dimensi'])) {
			$id_dimensi = $_POST['id_dimensi'];
			$nama_dimensi = $_POST['nama_dimensi'];

			$sql = "UPDATE dimensi SET nama_dimensi='" . $nama_dimensi . "' WHERE id_dimensi='" . $id_dimensi . "'";
			$result = mysqli_query($koneksi, $sql);

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