<?php
	include "koneksi.php";
	//$id=mysqli_real_escape_string($conn,$_POST["id"]);
	

	$id=$_POST["nama"];
	$hakakses=$_POST["hakakses"];// ini untuk id tampilan setiap departemen karna isi beda setiap spv nya
	$password=$_POST["password"];
    $tanggal_pembuatan = date('d - m - Y');
	
	$b = "SELECT * FROM data_karyawan WHERE id =".$id;
    $res=mysqli_query($conn,$b);
	$data=mysqli_fetch_assoc($res);
	$nama = $data["nama_karyawan"];
    $nik = $data["nik"];	
	
	//pengecekan spv
	if ($hakakses >= 1000)
	{
		//ini untuk pengecekan id dari masing - masing spv
		$spvPrc = 10010;
		$spvAcc = 1006;
		$spvQcEng = 1008;
		$spvProduksi = 1009;
		$spvHseItSystem = 1002;
		echo "masuk spv";
		
		if ($hakakses == $spvPrc)
		{
			echo "masuk spv prc";
             $query = "INSERT INTO user (nama,level,status,password,nik_data_karyawan,level_spv)
			             VALUE ('$nama','$hakakses','$hakakses','$password','$nik','10')";
	         $result = mysqli_query($conn, $query);	
			 
			 $satu = 1;
		$query2="UPDATE data_karyawan 
		           SET hakakses='$satu' 
				   WHERE nik=$nik";
	         $result2 = mysqli_query($conn, $query2);	

             if($result & $result2)
	         {
		           echo "Data berhasil masuk";
	   	           header("location:tambahHakaksesSystem.php?p=1");
	         }
	         else
             {
	               header("location:tambahHakaksesSystem.php?p=2");
	               die("Error");
             } 			 
		}
        if ($hakakses == $spvAcc)
		{
			echo "masuk spv acc";
			$query = "INSERT INTO user (nama,level,status,password,nik_data_karyawan,level_spv)
			             VALUE ('$nama','$hakakses','$hakakses','password','$nik','6')";
	         $result = mysqli_query($conn, $query);	
			 
		     $satu = 1;
		$query2="UPDATE data_karyawan 
		           SET hakakses='$satu' 
				   WHERE nik=$nik";
	         $result2 = mysqli_query($conn, $query2);	

             if($result & $result2)
	         {
		           echo "Data berhasil masuk";
	   	           header("location:tambahHakaksesSystem.php?p=1");
	         }
	         else
             {
	               header("location:tambahHakaksesSystem.php?p=2");
	               die("Error");
             } 	
		} 	
        if ($hakakses == $spvQcEng)
		{
			echo "masuk eng";
			$query = "INSERT INTO user (nama,level,status,password,nik_data_karyawan,level_spv)
			             VALUE ('$nama','$hakakses','$hakakses','$password','$nik','8')";
					
	         $result = mysqli_query($conn, $query);	
			  $satu = 1;
		$query2="UPDATE data_karyawan 
		           SET hakakses='$satu' 
				   WHERE nik=$nik";
	         $result2 = mysqli_query($conn, $query2);	

             if($result & $result2)
	         {
		           echo "Data berhasil masuk";
	   	          header("location:tambahHakaksesSystem.php?p=1");
	         }
	         else
             {
	               header("location:tambahHakaksesSystem.php?p=2");
	               
				   echo "<br>".$query."ini";
             } 		
		} 	
        if ($hakakses == $spvProduksi)
		{
			echo "masuk spv produksi";
			$query = "INSERT INTO user (nama,level,status,password,nik_data_karyawan,level_spv)
			             VALUE ('$nama','$hakakses','$hakakses','$password','$nik','9')";
	         $result = mysqli_query($conn, $query);	
			 $satu = 1;
		$query2="UPDATE data_karyawan 
		           SET hakakses='$satu' 
				   WHERE nik=$nik";
	         $result2 = mysqli_query($conn, $query2);	

             if($result & $result2)
	         {
		           echo "Data berhasil masuk";
	   	           header("location:tambahHakaksesSystem.php?p=1");
	         }
	         else
             {
	               header("location:tambahHakaksesSystem.php?p=2");
	               die("Error");
             } 		
		}
        if ($hakakses == $spvHseItSystem)
		{
			echo "masuk spv hse it system";
			$query = "INSERT INTO user (nama,level,status,password,nik_data_karyawan,level_spv)
			             VALUE ('$nama','$hakakses','$hakakses','$password','$nik','2')";
	         $result = mysqli_query($conn, $query);	
			$satu = 1;
		$query2="UPDATE data_karyawan 
		           SET hakakses='$satu' 
				   WHERE nik=$nik";
	         $result2 = mysqli_query($conn, $query2);	

             if($result & $result2)
	         {
		           echo "Data berhasil masuk";
	   	           header("location:tambahHakaksesSystem.php?p=1");
	         }
	         else
             {
	               header("location:tambahHakaksesSystem.php?p=2");
	               die("Error");
             } 		
		} 		 		
	}
	else
	{
		//ini selain spv
		echo "masuk selain spv";
		$query = "INSERT INTO user (nama,level,status,password,nik_data_karyawan)
			             VALUE ('$nama','$hakakses','$hakakses','$password','$nik')";
	    $result = mysqli_query($conn, $query);
		//$q="UPDATE data_karyawan SET no_bpjs_kesehatan='$no_bpjs_kesehatan' WHERE id=$id";
		$satu = 1;
		$query2="UPDATE data_karyawan 
		           SET hakakses='$satu' 
				   WHERE nik=$nik";
	         $result2 = mysqli_query($conn, $query2);	

             if($result & $result2)
	         {
		          echo "Data berhasil masuk";
	   	           header("location:tambahHakaksesSystem.php?p=1");
	         }
	         else
             {
	               header("location:tambahHakaksesSystem.php?p=2");
	               //die("Error");
				   echo "query gagal=".$query2;
             } 		
	}
	
?>