<?php

include "../../../koneksi.php";
$sql9 = mysql_query("TRUNCATE TABLE fungsid");
$sql9 = mysql_query("TRUNCATE TABLE normalisasi_r");


/////////////////////////////



////////////////////////////////////////////////////////////////////////
           $i=0;
		
      $sql1 = mysql_query("SELECT * FROM nilai group by id_wilayah");
	  while ($dt1 = mysql_fetch_array($sql1)) {
	     $id_wilayah[]=$dt1['id_wilayah'];
		
	  $sql3 = mysql_query("SELECT * FROM nilai where id_wilayah='$id_wilayah[$i]' ");
	  while ($dt3 = mysql_fetch_array($sql3)) {
	     $a[]=$dt3['nilai'];
		
	  }
	  $i=$i+1;
	  }
	  /////////////////////////////////////////////////////////////////////////
	  $b=0;
 $sql5 = mysql_query("SELECT * FROM nilai group by id_kriteria");
	  while ($dt5 = mysql_fetch_array($sql5)) {
	  $id_kriteria[]=$dt5['id_kriteria'];
	  
	  // rumus R
$sql4 = mysql_query("SELECT sqrt(sum(Pow(nilai,2))) As Jumlah FROM nilai As Jumlah WHERE id_kriteria='$id_kriteria[$b]'");
	  while ($dt4 = mysql_fetch_array($sql4)) {
	  $jumlahPangkatC[]=$dt4['Jumlah'];
	   
		
	  }
	  $b=$b+1;
	  }
//start rumus cari R
//$jumlahPangkatC[0] = sqrt(($a[0] * $a[0]) + ($a[5] * $a[5]) + ($a[10]*  $a[10]));
//echo $jumlahPangkatC[0];
//$jumlahPangkatC[1] = sqrt(($a[1] * $a[1]) + ($a[6] * $a[6]) + ($a[11] * $a[11]));
//$jumlahPangkatC[2] = sqrt(($a[2] * $a[2]) + ($a[7] * $a[7]) + ($a[12] * $a[12]));
//$jumlahPangkatC[3] = sqrt(($a[3] * $a[3]) + ($a[8] * $a[8]) + ($a[13] * $a[13]));
//$jumlahPangkatC[4] = sqrt(($a[4] * $a[4]) + ($a[9] * $a[9]) + ($a[14] * $a[14]));
//end   rumus cari R
//
$sql3 = mysql_query("SELECT * from kriteria ");
	  while ($dt3 = mysql_fetch_array($sql3)) {
	     $bobot[]=$dt3['bobot'];
		 $type[]=$dt3['type'];
		 //$id_kriteriafgg[]=$dt3['id_kriteria'];
		 	  }
	
        //start fungsi Y min max
		$p=0;
	//$sql7 = mysql_query("SELECT * from nilai group by id_kriteria ");
	// while ($dt7 = mysql_fetch_array($sql7)) {
	 //    $id_kriteria[]=$dt7['id_kriteria
	 
	 $i=0;
	 $sqld = mysql_query("SELECT * from nilai group by id_kriteria ");
	  while ($sqld2 = mysql_fetch_array($sqld)) {
	  $id_kriteriafgg[]=$sqld2['id_kriteria'];
	 //echo $i;
	$q=0;
	$sql6 = mysql_query("SELECT * from nilai where id_kriteria='$id_kriteriafgg[$i]' ");
	  while ($dt6 = mysql_fetch_array($sql6)) {
	     $bobo=$dt6['nilai'];
		 $id_wilayah=$dt6['id_wilayah'];
		 $id_kriteria=$dt6['id_kriteria'];
		 
		 $p1= $bobo/ $jumlahPangkatC[$i] * $bobot[$i];
	$p2[]= $bobo/ $jumlahPangkatC[$i] * $bobot[$i];
	      $normalisasi=$bobo/ $jumlahPangkatC[$i];
		$y1 = max($p2);
	    $y1m = min($p2);
		$query = "INSERT INTO fungsid (sy,y,id_kriteria,normalisasi) VALUES('$p1','$id_wilayah','$id_kriteria','$normalisasi')";
		$hasil = mysql_query($query);
		$q=$q+1;
	 	  }
		$sqlmax = mysql_query("SELECT MAX( sy ) as terbesar,MIN(sy) as terkecil FROM fungsid where id_kriteria='$id_kriteriafgg[$i]' ");
	     while ($dtmax = mysql_fetch_array($sqlmax)) 
		{
			$terbesar=$dtmax['terbesar'];
			$terkecil=$dtmax['terkecil'];
			
			$query = "update fungsid set ymax='$terbesar', ymin='$terkecil' where id_kriteria='$id_kriteriafgg[$i]' ";
			$hasil = mysql_query($query);
	  	}
		  $i=$i+1;
		}
		
		/*
	 
	$q=0;
	$sql6 = mysql_query("SELECT * from nilai where id_kriteria='K01' ");
	  while ($dt6 = mysql_fetch_array($sql6)) {
	     $bobo[]=$dt6['nilai'];
		  $id_wilayah=$dt6['id_wilayah'];
		  $id_kriteria=$dt6['id_kriteria'];
		  
	$p1[]= $bobo[$q] / $jumlahPangkatC[0] * $bobot[0];
	
		$y1 = max($p1);
	    $y1m = min($p1);
		$query = "INSERT INTO fungsid (sy,y,id_kriteria) VALUES('$p1[$q]','$id_wilayah','$id_kriteria')";
		$hasil = mysql_query($query);
		
		$query = "update fungsid set ymax='$y1',ymin='$y1m' where id_kriteria='$id_kriteria' ";
		$hasil = mysql_query($query);
		$q=$q+1;
	 	  }
		$q=0;
	$sql6 = mysql_query("SELECT * from nilai where id_kriteria='K02' ");
	  while ($dt6 = mysql_fetch_array($sql6)) {
	     $bobo2[]=$dt6['nilai'];
		  $id_wilayah=$dt6['id_wilayah'];
		  $id_kriteria=$dt6['id_kriteria'];
		$p2[]= $bobo2[$q] / $jumlahPangkatC[1] * $bobot[1];
		$y2 = max($p2);
	    $y2m = min($p2);
		$query = "INSERT INTO fungsid (sy,y,id_kriteria) VALUES('$p2[$q]','$id_wilayah','$id_kriteria')";
		$hasil = mysql_query($query);
		$query = "update fungsid set ymax='$y2',ymin='$y2m' where id_kriteria='$id_kriteria' ";
		$hasil = mysql_query($query);
		$q=$q+1;
	 	  }
		 
	   $q=0;
	$sql6 = mysql_query("SELECT * from nilai where id_kriteria='K03' ");
	  while ($dt6 = mysql_fetch_array($sql6)) {
	     $bobo3[]=$dt6['nilai'];
		 $id_wilayah=$dt6['id_wilayah'];
		  $id_kriteria=$dt6['id_kriteria'];
		$p3[]= $bobo3[$q] / $jumlahPangkatC[2] * $bobot[2];
		$y3 = max($p3);
	    $y3m = min($p3);
		$query = "INSERT INTO fungsid (sy,y,id_kriteria) VALUES('$p3[$q]','$id_wilayah','$id_kriteria')";
		$hasil = mysql_query($query);
		$query = "update fungsid set ymax='$y3',ymin='$y3m' where id_kriteria='$id_kriteria' ";
		$hasil = mysql_query($query);
		$q=$q+1;
		
	 	  }
		 
		   $q=0;
	$sql6 = mysql_query("SELECT * from nilai where id_kriteria='K04' ");
	  while ($dt6 = mysql_fetch_array($sql6)) {
	     $bobo4[]=$dt6['nilai'];
		  $id_wilayah=$dt6['id_wilayah'];
		   $id_kriteria=$dt6['id_kriteria'];
		$p4[]= $bobo4[$q] / $jumlahPangkatC[3] * $bobot[3];
		$y4 = max($p3);
	    $y4m = min($p3);
		$query = "INSERT INTO fungsid (sy,y,id_kriteria) VALUES('$p4[$q]','$id_wilayah','$id_kriteria')";
		$hasil = mysql_query($query);
		$query = "update fungsid set ymax='$y4',ymin='$y4m' where id_kriteria='$id_kriteria' ";
		$hasil = mysql_query($query);
		$q=$q+1;
	 	  }
		   $q=0;
	$sql6 = mysql_query("SELECT * from nilai where id_kriteria='K05' ");
	  while ($dt6 = mysql_fetch_array($sql6)) {
	     $bobo5[]=$dt6['nilai'];
		  $id_wilayah=$dt6['id_wilayah'];
		   $id_kriteria=$dt6['id_kriteria'];
		$p5[]= $bobo5[$q] / $jumlahPangkatC[4] * $bobot[4];
	
		$y5 = max($p3);
	    $y5m = min($p3);
		$query = "INSERT INTO fungsid (sy,y,id_kriteria) VALUES('$p5[$q]','$id_wilayah','$id_kriteria')";
		$hasil = mysql_query($query);
		$query = "update fungsid set ymax='$y5',ymin='$y5m' where id_kriteria='$id_kriteria' ";
		$hasil = mysql_query($query);
		$q=$q+1;
	 	  }
	  
	
	
 
$y1a = $a[0] / $jumlahPangkatC[0] * $bobot[0];
$y1b = $a[5] / $jumlahPangkatC[0] * $bobot[0];
$y1c = $a[10] / $jumlahPangkatC[0] * $bobot[0];  
$y2a = $a[1] / $jumlahPangkatC[1] * $bobot[1];
$y2b = $a[6] / $jumlahPangkatC[1] * $bobot[1];
$y2c = $a[11] / $jumlahPangkatC[1] * $bobot[1];
 
$y3a = $a[2] / $jumlahPangkatC[2] * $bobot[2];
$y3b = $a[7] / $jumlahPangkatC[2] * $bobot[2];
$y3c = $a[12] / $jumlahPangkatC[2] * $bobot[2];
$y4a = $a[3] / $jumlahPangkatC[3] * $bobot[3];
$y4b = $a[8] / $jumlahPangkatC[3] * $bobot[3];
$y4c = $a[13] / $jumlahPangkatC[3] * $bobot[3];
$y5a = $a[4] / $jumlahPangkatC[4] * $bobot[4];
$y5b = $a[9] / $jumlahPangkatC[4] * $bobot[4];
$y5c = $a[14] / $jumlahPangkatC[4] * $bobot[4];

//variabel type
$type1=$type[0];
$type2=$type[1];
$type3=$type[2];
$type4=$type[3];
$type5=$type[4];

//kriteria
if ($type[0]=="Cost"){

    $statusy1="min";
    $status2y1="max";
    

	
	 //y1
	//$y1 = min($y1a, $y1b, $y1c);
	//echo "asddgfhgjhkjlk;l;jlhkgfgdfgfhgjkhl";
	//echo $y1; 
    //y1m
//	$y1m = max($y1a, $y1b, $y1c);
    
    

    
    
} else if ($type[0]=="Benefit"){
    $statusy1="max";
    $status2y1="min";
   
    //y1
	//$y1 = max($y1a, $y1b, $y1c);
	//echo "<br>";
	//echo $y1; 
    //y1m
	//$y1m = min($y1a, $y1b, $y1c);

    
    
    
   
}

if ($type[1]=="Cost"){
    $statusy2="min";
    $status2y2="max";
	//$y2 = min($y2a, $y2b, $y2c);    
	//$y2m = max($y2a, $y2b, $y2c);    
	
} else if ($type[1]=="Benefit"){
    $statusy2="max";
    $status2y2="min";
    
	//$y2 = max($y2a, $y2b, $y2c);    
	//$y2m = min($y2a, $y2b, $y2c);    
   
}

if ($type[2]=="Cost"){
    $statusy3="min";
    $status2y3="max";
	//$y3 = min($y3a, $y3b, $y3c);    
	//$y3m = max($y3a, $y3b, $y3c);    

   
} else if ($type[2]=="Benefit"){
    $statusy3="max";
    $status2y3="min";
    //$y3 = max($y3a, $y3b, $y3c);    
	//$y3m = min($y3a, $y3b, $y3c); 

}

if ($type[3]=="Cost"){
    $statusy4="min";
    $status2y4="max";
	
	//$y4 = min($y4a, $y4b, $y4c);    
	//$y4m = max($y4a, $y4b, $y4c);    
	
} else if ($type[3]=="Benefit"){
    $statusy4="max";
    $status2y4="min";
	//$y4 = max($y4a, $y4b, $y4c);    
	//$y4m = min($y4a, $y4b, $y4c);    
}

if ($type[4]=="Cost"){
    $statusy5="min";
    $status2y5="max";
   
	//$y5 = min($y5a, $y5b, $y5c);    
	//$y5m = max($y5a, $y5b, $y5c);    

    

} else if ($type[4]=="Benefit"){
    $statusy5="max";
    $status2y5="min";
    //$y5 = max($y5a, $y5b, $y5c);    
	//$y5m = min($y5a, $y5b, $y5c);
}
//end   fungsi Y min max
//
        //start fungsi D
		
		echo "asfsgddgggggggggggggggggggggggg";
*/
	 $m=0;
$sql11 = mysql_query("SELECT * FROM fungsid group by y");
	  while ($dt11 = mysql_fetch_array($sql11)) {
	  $y[]=$dt11['y'];
$sql12 = mysql_query("SELECT sqrt(sum(pow(ymax-sy,2))) As Jumlah FROM fungsid As Jumlah WHERE y='$y[$m]'");
	  while ($dt12 = mysql_fetch_array($sql12)) {
	  $d[]=$dt12['Jumlah'];
	   
	  }
	  $m=$m+1;
	  }		
	  $m=0;
	  $sql15 = mysql_query("SELECT * FROM fungsid group by y");
	  while ($dt15 = mysql_fetch_array($sql15)) {
	  $y[]=$dt15['y'];
$sql15 = mysql_query("SELECT sqrt(sum(pow(ymin-sy,2))) As Jumlah FROM fungsid As Jumlah WHERE y='$y[$m]'");
	  while ($dt15 = mysql_fetch_array($sql15)) {
	  $dm[]=$dt15['Jumlah'];
	   
	  }
	  $m=$m+1;
	  }		
	  
//$d1 = sqrt((($y1 - $y1a) * ($y1 - $y1a)) + (($y2 - $y2a) * ($y2 - $y2a)) + (($y3 - $y3a) * ($y3 - $y3a)) + (($y4 - $y4a) * ($y4 - $y4a)) + (($y5 - $y5a) * ($y5 - $y5a)));
//$d2 = sqrt((($y1 - $y1b) * ($y1 - $y1b)) + (($y2 - $y2b) * ($y2 - $y2b)) + (($y3 - $y3b) * ($y3 - $y3b)) + (($y4 - $y4b) * ($y4 - $y4b)) + (($y5 - $y5b) * ($y5 - $y5b)));
//$d3 = sqrt((($y1 - $y1c) * ($y1 - $y1c)) + (($y2 - $y2c) * ($y2 - $y2c)) + (($y3 - $y3c) * ($y3 - $y3c)) + (($y4 - $y4c) * ($y4 - $y4c)) + (($y5 - $y5c) * ($y5 - $y5c)));
 
//$d1m = sqrt((($y1m - $y1a) * ($y1m - $y1a)) + (($y2m - $y2a) * ($y2m - $y2a)) + (($y3m - $y3a) * ($y3m - $y3a)) + (($y4m - $y4a) * ($y4m - $y4a)) + (($y5m - $y5a) * ($y5m - $y5a)));
//$d2m = sqrt((($y1m - $y1b) * ($y1m - $y1b)) + (($y2m - $y2b) * ($y2m - $y2b)) + (($y3m - $y3b) * ($y3m - $y3b)) + (($y4m - $y4b) * ($y4m - $y4b)) + (($y5m - $y5b) * ($y5m - $y5b)));
//$d3m = sqrt((($y1m - $y1c) * ($y1m - $y1c)) + (($y2m - $y2c) * ($y2m - $y2c)) + (($y3m - $y3c) * ($y3m - $y3c)) + (($y4m - $y4c) * ($y4m - $y4c)) + (($y5m - $y5c) * ($y5m - $y5c)));
//echo $d1m;
	//  	  echo $d2m;
		//  	  echo $d3m;
//end   fungsi D
//
        //start fungsi hasil
		
//$v1 = $d1m / ($d1 + $d1m);
//$v2 = $d2m / ($d2 + $d2m);
//$v3 = $d3m / ($d3 + $d3m);
$sql9 = mysql_query("TRUNCATE TABLE hasil");
$m=0;
echo "hasil akhir";
$sqlhasil = mysql_query("SELECT * FROM fungsid group by y");
	  while ($dthasil = mysql_fetch_array($sqlhasil)) {
	  $yhasil[]=$dthasil['y'];
$sqlhasil2 = mysql_query("SELECT  (sqrt(sum(pow(ymin-sy,2))))/((sqrt(sum(pow(ymax-sy,2)))) + (sqrt(sum(pow(ymin-sy,2))))) As Jumlah FROM fungsid WHERE y='$yhasil[$m]'");
	  while ($dthasil2 = mysql_fetch_array($sqlhasil2)) {
	  //$dmhasil[]=$dthasil2['Jumlah'];
	   $dmhasil[]=number_format($dthasil2['Jumlah'],3);
	  
	 if ($dmhasil[$m]<=1 && $dmhasil[$m] >0.6)
			{
		
			$keterangan='Berpotensi';
			}
			elseif ($dmhasil[$m]<=0.6 )
			{
			
			$keterangan='Tidak Berpotensi';
			}
			
	  
			$query = "INSERT INTO hasil (id_wilayah,hasil,keterangan) VALUES('$yhasil[$m]','$dmhasil[$m]','$keterangan')";
		$hasil = mysql_query($query);
	  }
	  $m=$m+1;
	  }		
header('Location: ../penilaian/hasil_akhir.php');
	 
//end   fungsi hasil
?>