<?php
include "koneksi.php";
$response = array();
if(isset($_POST['jawaban'])){
	$id_pengguna = $_POST['id_pengguna'];
	$pernyataan = $_POST['jawaban'];

	//$pernyataan = array();

	// $i=0;
	// $query=mysqli_query($koneksi, "SELECT * FROM pernyataan ORDER BY id_pernyataan");
	// while ($row = mysqli_fetch_array($query)){
	// 	$pernyataan[$i] = $row['id_pernyataan'];
	// 	$cf_pernyataan[$i] = $row['cf'];
	// 	if($jawaban[$i]!="Tidak Tahu"){
	// 		$pernyataan_simpan .= $row['nama_pernyataan']." : ".$jawaban[$i]."\n";
	// 	}
	// 	$i++;
	// }

	$pernyataan_simpan = substr($pernyataan_simpan, 0, (strlen($pernyataan_simpan)-1));

	$persentase = array();
	$dimensi = array();
	$j=0;
	$counter = 0;

	$perhitungan = "";
	$response['dimensi'] = array();
	$query=mysqli_query($koneksi, "SELECT * FROM dimensi ORDER BY id_dimensi");
	while ($row = mysqli_fetch_array($query)){
		$id_dimensi = $row['id_dimensi'];
		$cf = 0.0;

		if($perhitungan == ""){
			$perhitungan .= $row['nama_dimensi']."\n";
		} else {
			$perhitungan .= "\n".$row['nama_dimensi']."\n";
		}

		$counter = "";

		$query2 = mysqli_query($koneksi, "SELECT * FROM aturan WHERE id_dimensi='".$id_dimensi."'");
		$total = mysqli_num_rows($query2);
		$count = 0;
		while($row2 = mysqli_fetch_array($query2)){
			$id_pernyataan = $row2['id_pernyataan'];
			for($i=0; $i<count($pernyataan); $i++){
				if($pernyataan[$i] == $id_pernyataan){
					$count++;
					break;
				}
			}
		}

		if($count == $total){
			$output = array();
			$output["id_dimensi"] = $row['id_dimensi'];
			$output["nama_dimensi"] = $row['nama_dimensi'];
			$output["penyebab"] = $row['penyebab'];
			$output["solusi"] = $row['solusi'];
			array_push($response["dimensi"], $output);
		}
		$perhitungan .= $count." : ".$total;
		$j++;
	}

	$perhitungan = "";
	for($i=0; $i<count($pernyataan); $i++){
		$perhitungan .= $pernyataan[$i].", ";
	}

	// if($dimensi==""){
	// 	$dimensi = "Hasil konsultasi : dimensi kamu tidak diketahui...";
	// }

	// $max = 0.0;
	// $hasil = array();
	// $counter = 0;
	// for($i=0; $i<count($dimensi); $i++){
	// 	//$perhitungan .= $dimensi[$i]." : ".$persentase[$i]." - ";
	// 	if((float)$persentase[$i] == $max){
	// 		$hasil[$counter] = $dimensi[$i];
	// 		$counter++;
	// 	} else if((float)$persentase[$i] > $max){
	// 		$counter = 0;
	// 		unset($hasil);
	// 		$hasil = array();
	// 		$max = $persentase[$i];
	// 		$hasil[$counter] = $dimensi[$i];
	// 		$counter++;
	// 	}
	// 	//$perhitungan .= $max." - ".$counter." | ";
	// }

	//echo $hasil." - ".$max;
	//echo $perhitungan;

	// $hasil_simpan = "";
	// for($i=0; $i<count($hasil); $i++){
	// 	$query2 = mysqli_query($koneksi, "SELECT * FROM dimensi WHERE id_dimensi='".$hasil[$i]."'");
	// 	if($row2 = mysqli_fetch_array($query2)){
	// 		$output = array();
	// 		$output["id_dimensi"] = $row2['id_dimensi'];
	// 		$output["nama_dimensi"] = $row2['nama_dimensi'];
	// 		$output["penyebab"] = $row2['penyebab'];
	// 		$output["solusi"] = $row2['solusi'];
	// 		$output["gambar"] = $row2['gambar'];
	//
	// 		$hasil_simpan .= $row2['nama_dimensi']." & ";
	//
	// 		array_push($response["dimensi"], $output);
	// 	}
	// }

	//$hasil_simpan = substr($hasil_simpan, 0, (strlen($hasil_simpan)-3));

	// $sql = "INSERT INTO riwayat(tanggal, id_pengguna, pernyataan, hasil, persentase)
	// VALUES(NOW(), '".$id_pengguna."', '".$pernyataan_simpan."', '".$hasil_simpan."', '".$max."')";
	// $result = mysqli_query($koneksi, $sql);

	$response["perhitungan"] = $perhitungan;
	//$response["perhitungan"] = count($hasil);
	$response["persentase"] = $max;
	echo json_encode($response);
} else {
	$response['persentase'] = 0;
	$response['perhitungan'] = "Sistem gagal menganalisa, coba lagi nanti";
	echo json_encode($response);
}
?>
