<H3>
	  <P align="center"><b> KONSULTASI GEJALA </b></P></H3>

<!doctype html>
<html>
<head>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1"> <!--meta untuk tampilan pas-->
<title>Hasil dan Proses Bayes</title>
	<link rel="stylesheet" href="css/themes/default/jquery.mobile-1.4.3.min.css">
	<link rel="shortcut icon" href="img/icon.ico">
	<script src="js/jquery.js"></script>
	<script src="js/jquery.mobile-1.4.3.min.js"></script>
</head>

<body>
<div id="index" data-role="page" data-theme="a">
	<div data-role="header" data-position="fixed" data-theme="a"> <!-- /fixed agar header tidak hilang saat scrol diturunkan -->
   
		
      <h3>
     PROSES DAN HASIL DIAGNOSA BAYES
      </h3>
	</div>
	<!-- /header -->

	<div role="main" class="ui-content" data-role="listview" data-filter="true" data-filter-placeholder="Cari Gejala..." data-inset="true">
    
	  <?php 
include_once "library/inc.connection.php";
include_once "library/inc.library.php";

# ID = no IP komputer, atau No HP, User ID
$ID		= str_replace(".","",$_SERVER['REMOTE_ADDR']);
$HiHasil = array();

 
 
// echo $jum;
 //echo $HiHasil;
 //exit();

if(! $_POST) {
	echo "<meta http-equiv='refresh' content='0; url=konsultasi_pilih_gejala.php'>";
}
else if(isset($_POST['btnKonsultasi'])){
   $jum  = count($dataGejala);
  // echo $jum;
   //exit();
   if ($jum==0) {
		//echo "BELUM ADA GEJALA YANG DIPILIH";
		include_once "gagal.php";
   }
   else {
		echo "<b>Gejala yang dihadapi : </b><br><br>";
		foreach ($dataGejala as $nilai) {
			$gejalaSQL = "SELECT * FROM gejala WHERE id_gejala='$nilai'";
			$gejalaQry = mysql_query($gejalaSQL) or die ("Error gejala".mysql_error());
			$gejalaHsl = mysql_fetch_array($gejalaQry);
			$kodegejala1=$gejalaHsl['id_gejala'];
			echo $gejalaHsl['id_gejala'].": ".$gejalaHsl['nm_gejala']."<br>";
			
		}
		
		
		# TAMPILKAN SEMUA DAFTAR PENYAKIT, Sekaligus membuat Rumus berulang sejumlah Penyakit
		//echo "<b> Hasil Diagnosa : </b><hr>";
		$Hi	= 1; $h = 0; $PH	= 0;
		$penyakitSql = "SELECT * FROM penyakit";
		$penyakitQry = mysql_query($penyakitSql) or die ("Error penyakit".mysql_error());
		while ($penyakitData = mysql_fetch_array($penyakitQry)){
			
			#Buat info aja
			//echo "<br> <b> $penyakitData[id_penyakit] $penyakitData[nm_penyakit]</b><br>";
		
			$np_populasi = $penyakitData['np_populasi'];
			
			// Kode untuk mengurai Gejala yang dipilih
			//$Em = 1;
			$bayes2 = 0;
			foreach ($dataGejala as $id_gejala) {
				$aturanSql  = "SELECT * FROM aturan WHERE id_penyakit='$penyakitData[id_penyakit]' AND id_gejala='$id_gejala'";
				$aturanQry  = mysql_query($aturanSql) or die ("Error program".mysql_error());
				$aturanData = mysql_fetch_array($aturanQry);
				
				
				
				$id_gejala = $aturanData['id_gejala'];
				$nilai_probabilitas = $aturanData['nilai_probabilitas'];
				
				//echo $id_gejala,$nilai_probabilitas;
				
				$bayes1=$np_populasi*$nilai_probabilitas;
				$bayes2=$bayes2+$bayes1;
				
				//echo $bayes2;
				//echo "<br>";
				}
				
				
				
				$aturanok  = "SELECT * FROM aturan WHERE id_penyakit='$penyakitData[id_penyakit]'";
				$aturanoke  = mysql_query($aturanok) or die ("Error program".mysql_error());
				$bayes4=0;
				while($aturanokee = mysql_fetch_array($aturanoke)){
				$id_gejalaok = $aturanokee['id_gejala'];
				$nilai_probabilitasok = $aturanokee['nilai_probabilitas'];
				
				//echo $nilai_probabilitasok;
				//echo "<br>";
				//echo $np_populasi;
				$bayes3=$np_populasi*$nilai_probabilitasok;
				//echo "<br>";
				//echo $bayes3;
				//echo "<br>";
				//echo $bayes4;
				$bayes4=$bayes4+$bayes3;
				//$bayes5=$bayes4-$np_populasi-$nilai_probabilitasok;
				}
				
				//echo "<br>";
				$hasilakhir=($bayes2/$bayes4);
				$hasilok=$hasilakhir*100;
				
				
				//echo "$bayes2 / $bayes5";
				//echo "<br>";
				//echo floor($hasilok)."%";
				
				$HiHasil[$penyakitData['id_penyakit']] = $hasilakhir;
				//$hasilsip[$bayes2]=$mantapok;
				
				//echo "<br>";
				
			//exit();
			
		}
		//echo "<br>";
		
		mysql_query("DELETE FROM tmp_diagnosa WHERE ID='$ID'") or die ("Error delete tmp".mysql_error());
		
		
	//$PHitung = array();
		//var_dump($HiHasil);
		foreach ($HiHasil as $kode => $hasilakhir){
			//$PHitung= calcBagi($bayes2, $bayes4);
			// Simpan konsultasi ke Temporary
			//echo $hasilakhir;	
			
			$inTmpSql = "INSERT INTO tmp_diagnosa (ID,id_penyakit,hasil_hitung)Values ('$ID','$kode','$hasilakhir')";
						 //echo $inTmpSql ;
			
			mysql_query($inTmpSql) or die ("Error inTmpSql".mysql_error());
			//echo $inTmpSql;
			}
		
		
		
		
		
		
		echo "<hr><b>Hasil Diagnosa : </b><hr>";
		$maxSql = "SELECT * FROM tmp_diagnosa WHERE hasil_hitung=(SELECT MAX(hasil_hitung) FROM tmp_diagnosa WHERE ID='$ID') AND 			ID='$ID'";
		$maxQry = mysql_query($maxSql) or die ("Error maxSql".mysql_error());
		$maxHsl = mysql_fetch_array($maxQry);
		$maxHasil = $maxHsl['hasil_hitung'];
		$maxHasilok = $maxHasil*100;
		$penyakit2Sql = "SELECT * FROM penyakit WHERE id_penyakit='$maxHsl[id_penyakit]'";
		$penyakit2Qry = mysql_query($penyakit2Sql) or die ("Error penyakit2Sql".mysql_error());
		$penyakit2Hsl = mysql_fetch_array($penyakit2Qry);
		$nm_penyakit = $penyakit2Hsl['nm_penyakit'];
		$penyebab = $penyakit2Hsl['penyebab'];
		$pengobatan = $penyakit2Hsl['pengobatan'];
		
		echo "<br>";
		# HITUNG : STEP 7 : INTERPRETASI NILAI PROBABILITAS
		# ========================================================
		//echo "<br><b> Step 3 : </b><hr>";	
		if($maxHasil < 0) {
			$hasil = "Tidak diketahui";
		}
		elseif($maxHasil == -1) {
			$hasil = "Pasti Tidak";
		}
		elseif($maxHasil >  -1 && $maxHasil <= -0.8) {
			$hasil = "Hampir Pasti Tidak";
		}
		elseif($maxHasil >  -0.8 && $maxHasil <= -0.6) {
			$hasil = "Kemungkinan Besar Tidak";
		}
		elseif($maxHasil >  -0.6 && $maxHasil <= -0.4) {
			$hasil = "Kemungkinan Besar Tidak";
		}
		elseif($maxHasil >  -0.4 && $maxHasil <= -0.2) {
			$hasil = "Mungkin Tidak";
		}
		elseif($maxHasil >  -0.2 && $maxHasil <= -0.2) {
			$hasil = "Tidak Tahu";
		}
		elseif($maxHasil >  0.2 && $maxHasil <= 0.4) {
			$hasil = "Mungkin";
		}
		elseif($maxHasil >  0.4 && $maxHasil <= 0.7) {
			$hasil = "Kemungkinan Besar";
		}
		else if($maxHasil >  0.6 && $maxHasil <= 0.8) {
			$hasil = "Hampir Pasti";
		}
		else if($maxHasil >  0.8 && $maxHasil <= 1) {
			$hasil = "pasti";
		}
		else if($maxHasil > 1) {
			$hasil = "Tidak diketahui";
		}
		
		//echo round($zxc,2);
		//echo $cv;
		echo "Pasien tersebut <b>$hasil</b>	menderita penyakit <b> $nm_penyakit </b>dengan nilai probabilitas $maxHasilok %";
		echo "<br><br>Penyebab :  $penyebab  ";
		echo "<br><br>Solusi : <br> $pengobatan  ";
	}
}
else {
	echo "<meta http-equiv='refresh' content='0; url=?'>";
}
?>

	  <br>
</div><!-- /content -->

	<div data-role="footer" data-position="fixed"><!-- /fixed agar footer tidak bergerak -->
		<div data-role="navbar" data-iconpos="bottom"> 
			<ul> 
				<li><a href="index.php" data-icon="home" data-transition="slidefade">Home</a></li>
                <li><a href="konsultasi_pilih_gejala.php" data-icon="info" data-transition="slidefade">Mulai Diagnosa</a></li>				
			  <li><a href="tentang.php" data-icon="info" data-transition="slidefade">About</a></li>
			</ul>
		</div>	
	</div><!-- /footer -->
</div><!-- /page -->
</body>
</html>