<?php
include_once "library/inc.connection.php";
include_once "library/inc.library.php";

if (isset ($_POST ['simpan_user']))
{
		$id_user = $_POST['id_user'];
		$nama_lengkap = $_POST['nama_lengkap'];
		
		$username = $_POST ['username'];
		$password = $_POST ['password'];
		$jenis_kelamin = $_POST ['jenis_kelamin'];
		$status = $_POST ['status'];
		$level = $_POST ['level'];
		$query = "INSERT INTO user (id_user,nama_lengkap,username,password,jenis_kelamin,status,level) VALUES
		('$id_user','$nama_lengkap','$username','$password','$jenis_kelamin','$status','$level')";
		$hasil = mysql_query($query);
	if($hasil){
		echo "<script type=\"text/javascript\">
						alert(\"Data Telah Tersimpan\");
						window.location = \"tabel_user.php\"
					</script>";
				}
		else
		{
			echo "<script type=\"text/javascript\">
						alert(\"Data Gagal disimpan\");
						window.location = \"tabel_user.php\"
					<script>";
		}
		mysql_close();
}
if (isset ($_POST ['simpan_registrasi']))
{
		$id_user = $_POST['id_user'];
		$nama_lengkap = $_POST['nama_lengkap'];
		
		$username = $_POST ['username'];
		$password = $_POST ['password'];
		$jenis_kelamin = $_POST ['jenis_kelamin'];
		$status = $_POST ['status'];
		$level = $_POST ['level'];
		$query = "INSERT INTO user (id_user,nama_lengkap,username,password,jenis_kelamin,status,level) VALUES
		('$id_user','$nama_lengkap','$username','$password','$jenis_kelamin','$status','$level')";
		$hasil = mysql_query($query);
	if($hasil){
		echo "<script type=\"text/javascript\">
						alert(\"Data Telah Tersimpan Silahkan Login Untuk Menggunakan Aplikasi\");
						window.location = \"login.php\"
					</script>";
				}
		else
		{
			echo "<script type=\"text/javascript\">
						alert(\"Data Gagal disimpan\");
						window.location = \"tabel_user.php\"
					<script>";
		}
		mysql_close();
}				

if (isset ($_POST ['simpan_gejala']))
{
		$id_gejala = $_POST['id_gejala'];
		$nama_gejala = $_POST['nama_gejala'];
		$query2 = mysql_query("SELECT count(id_gejala) as jumlah FROM gejala");
			while ($row = mysql_fetch_array($query2) ) 
			{
			$jumlah=$row['jumlah'];
			}
		//$np = $_POST['np'];
		 if(isset($_POST["id_penyakit"]))  
        { 
            // Retrieving each selected option 
			$as=1;
			 foreach ($_POST['id_penyakit'] as $id_penyakita)  
               {
				$as=$as+1;
				}
            foreach ($_POST['id_penyakit'] as $id_penyakit)  
               {
			   $np=$as/$jumlah;
			   $queryc = "INSERT INTO aturan (id_penyakit,id_gejala,nilai_probabilitas) VALUES
				('$id_penyakit','$id_gejala','$np')";
				$hasilc = mysql_query($queryc);
			
				}
			
        } 
		$query = "INSERT INTO gejala (id_gejala,nama_gejala) VALUES
		('$id_gejala','$nama_gejala')";
		$hasil = mysql_query($query);
	if($hasil){
		echo "<script type=\"text/javascript\">
						alert(\"Data Telah Tersimpan\");
						window.location = \"tabel_gejala.php\"
					</script>";
				}
		else
		{
			echo "<script type=\"text/javascript\">
						alert(\"Data Gagal disimpan\");
						window.location = \"tabel_gejala.php\"
					<script>";
		}
		mysql_close();
}			

if (isset ($_POST ['simpan_penyakit']))
{
		$id_penyakit = $_POST['id_penyakit'];
		$nama_penyakit = $_POST['nama_penyakit'];
		$keterangan = $_POST['keterangan'];
		
		$query = "INSERT INTO penyakit (id_penyakit,nama_penyakit,keterangan) VALUES
		('$id_penyakit','$nama_penyakit','$keterangan')";
		$hasil = mysql_query($query);
		$query2 = mysql_query("SELECT count(id_penyakit) as jumlah FROM penyakit");
			while ($row = mysql_fetch_array($query2) ) 
			{
			$jumlah=$row['jumlah'];
			$probabiliti=1/$jumlah;
			$update = mysql_query("update penyakit set np_populasi='$probabiliti'");
			}
	if($hasil){
		echo "<script type=\"text/javascript\">
						alert(\"Data Telah Tersimpan\");
						window.location = \"tabel_penyakit.php\"
					</script>";
				}
		else
		{
			echo "<script type=\"text/javascript\">
						alert(\"Data Gagal disimpan\");
						window.location = \"tabel_penyakit.php\"
					<script>";
		}
		
		mysql_close();
}		

if (isset ($_POST ['simpan_solusi']))
{
		$id_solusi = $_POST['id_solusi'];
		$id_penyakit = $_POST['id_penyakit'];
		$keterangan = $_POST['keterangan'];
		
		$query = "INSERT INTO solusi (id_solusi,id_penyakit,solusi) VALUES
		('$id_solusi','$id_penyakit','$keterangan')";
		$hasil = mysql_query($query);
	if($hasil){
		echo "<script type=\"text/javascript\">
						alert(\"Data Telah Tersimpan\");
						window.location = \"tabel_solusi.php\"
					</script>";
				}
		else
		{
			echo "<script type=\"text/javascript\">
						alert(\"Data Gagal disimpan\");
						window.location = \"tabel_solusi.php\"
					<script>";
		}
		mysql_close();
}				
?>