
<?php
	 include('../../koneksi.php');
if (isset ($_POST ['simpan_user']))
{
	 	$nama  = $_POST['nama'];
    	$username  = $_POST['Username'];
    	$password  = $_POST['Password'];
    	$email = $_POST['Email'];
    	$jkel  = $_POST['Sex'];
		$level  = $_POST['level'];
		$status  = $_POST['status'];
   		 $bobot = $_POST['bobot'];

     $query = "INSERT INTO user (nama_lengkap,username,password,Email,Jkel,level,status_admin) VALUES('$nama','$username','$password','$email','$jkel','$level','$status')";
    $hasil = mysql_query($query);
   
   if($hasil){
		header('Location: tabel_user.php');
	}
	else{
		echo "gagal menginput";
		}
mysql_close();
}
elseif (isset ($_POST ['simpan_wilayah']))
{
	 	$id_wilayah  = $_POST['id_wilayah'];
		$nama_wilayah  = $_POST['nama_wilayah'];
		$alamat_wilayah = $_POST['alamat_wilayah'];
  		$tanggal = $_POST['tanggal'];
		$query = "INSERT INTO wilayah (id_wilayah,nama_wilayah,alamat_wilayah,tanggal) VALUES('$id_wilayah','$nama_wilayah','$alamat_wilayah','$tanggal')";
    $hasil = mysql_query($query);
   
   if($hasil){
		header('Location: wilayah/tabel_wilayah.php');
	}
	else{
		echo "gagal menginput";
		}
mysql_close();
}
elseif (isset ($_POST ['simpan_kriteria']))
{
	 	$id_kriteria  = $_POST['id_kriteria'];
		$nama_kriteria  = $_POST['nama_kriteria'];
  		$bobot = $_POST['bobot'];
		$type = $_POST['type'];

	
	 $sql1 = mysql_query("SELECT * FROM nilai group by id_wilayah");
	     while ($dt1 = mysql_fetch_array($sql1)) {

	      $id_wilayah=$dt1['id_wilayah'];	  
		  $query = "INSERT INTO nilai (id_kriteria,id_wilayah,id_sub,nilai) VALUES('$id_kriteria','$id_wilayah','0','0')";
   
		  $hasil2 = mysql_query($query);
		  }
   
     $query = "INSERT INTO kriteria (id_kriteria,nama_kriteria,bobot,type) VALUES('$id_kriteria','$nama_kriteria','$bobot','$type')";
    $hasil = mysql_query($query);
   if($hasil)
     {
        
		  header('Location: kriteria/tabel_kriteria.php');
		  
	   }
	else
	    {
		echo "gagal menginput";
		}
mysql_close();
}
elseif (isset ($_POST ['simpan_kriteria2']))
{
	 	// Loop through the friend array
     $id_wilayah  = $_POST['nama'];
	$tampilt=mysql_query("SELECT * FROM  nilai  where id_wilayah='$id_wilayah'");
      $wt=mysql_fetch_array($tampilt);
					if (!empty($wt))
					{

	
	 foreach ($_POST['xsub'] as $id_sub) {
	 
    // Do something with each valid friend entry ...
    if ($id_sub) {
      
		
		
	$tampil=mysql_query("SELECT * FROM  sub_kriteria  where id_sub='$id_sub'");
      while($w=mysql_fetch_array($tampil))
					{
			    $id_kriteria= "$w[id_kriteria]"; 
				$bobot="$w[bobot]";
				$id_sub2="$w[id_sub]";
				 $query = "update nilai set id_sub='$id_sub2',nilai='$bobot' where id_kriteria= '$id_kriteria' and id_wilayah= '$id_wilayah'";
				$hasil = mysql_query($query);
				}	
		
   }
   }
   }
   else
   {
    foreach ($_POST['xsub'] as $id_sub) {
	 
    // Do something with each valid friend entry ...
    if ($id_sub) {
        echo $id_sub."<br />";
		
		
	$tampil=mysql_query("SELECT * FROM  sub_kriteria  where id_sub='$id_sub'");
      while($w=mysql_fetch_array($tampil))
					{
			    $id_kriteria= "$w[id_kriteria]"; 
				$bobot="$w[bobot]";
				 $query = "INSERT INTO nilai (id_wilayah,id_sub,id_kriteria,nilai) VALUES('$id_wilayah','$id_sub','$id_kriteria','$bobot')";
				$hasil = mysql_query($query);
				}	
		
   }
   }
   }
   
   if($hasil){
		
		header('Location: topsis/function.php');
	}
	else{
		header('Location: topsis/function.php');
		}
		
    

		
	mysql_close();
}
elseif (isset ($_POST ['simpan_pilihan_kriteria']))
{
$nama_kriteria  = $_POST['nama_kriteria'];
$tampil=mysql_query("SELECT * FROM kriteria  where nama_kriteria='$nama_kriteria'");
while($w=mysql_fetch_array($tampil))
					{
			    $id_kriteria= "$w[id_kriteria]"; 
				}
				$id_sub = $_POST['id_sub'];
  		 $nama_sub = $_POST['nama_sub'];
		  $bobot = $_POST['bobot'];

		
				
$query = "INSERT INTO sub_kriteria (id_kriteria,id_sub,nama_kriteria,nama_sub,bobot) VALUES('$id_kriteria','$id_sub','$nama_kriteria','$nama_sub','$bobot')";
    $hasil = mysql_query($query);
$query = "update nilai set id_sub='$id_sub' where id_sub=0 and id_kriteria= '$id_kriteria'";
				$hasil = mysql_query($query);
   
   if($hasil){
              
		header('Location: sub_kriteria/tabel_sub.php');
	}
	else{
		echo "gagal menginput";
		}
mysql_close();
}
elseif (isset ($_POST ['simpan_rumah']))
{
$dir_gambar = 'C:\xampp\htdocs\website\SPK-Perumahan\images\rumah\\';
$url_folder_gambar = 'http://localhost/website/SPK-Perumahan/images/rumah/';
if(empty($_FILES['userfile']['name'])){
//echo "Gambar kosong, harap pilih file terlebih dahulu.";
$filename2 = 'default.jpg';
}
else
{
$filename2 = basename($_FILES['userfile']['name']);
$uploadfile = $dir_gambar . $filename2;
echo $_FILES['userfile']['tmp_name'];
echo '<br />' . $uploadfile;
$uploadedfile = $_FILES['userfile']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);

// Create an Image from it so we can do the resize
//$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);

// For our purposes, I have resized the image to be
// 600 pixels wide, and maintain the original aspect
// ratio. This prevents the image from being “stretched”
// or “squashed”. If you prefer some max width other than
// 600, simply change the $newwidth variable
$newwidth=960;
$newheight=($height/$width)*600;
$tmp=imagecreatetruecolor($newwidth,$newheight);

// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

// now write the resized image to disk. I have assumed that you want the
// resized, uploaded image file to reside in the ./images subdirectory.
$filename = $dir_gambar. $_FILES['userfile']['name'];
//$filename = basename($_FILES['userfile']['name']);
imagejpeg($tmp,$filename,100);

imagedestroy($src);
imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
// has completed.

	//move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);
}	
	 	$id  = $_POST['id'];
		$tipe  = $_POST['tipe'];
    	$jumlah_kamar = $_POST['jumlah_kamar'];
    	$harga  = $_POST['harga'];
		$cicilan  = $_POST['cicilan'];

     $query = "INSERT INTO data_rumah (id,tipe_rumah,jumlah_kamar,harga,cicilan,gambar) VALUES('$id','$tipe','$jumlah_kamar','$harga','$cicilan','$filename2')";
    $hasil = mysql_query($query);
   
   if($hasil){
		header('Location: tabel_rumah.php');
	}
	else{
		echo "gagal menginput";
		}
mysql_close();
}

?>