<?php
//echo "Value Dari Select Adalah : <b>".$_POST['pegawai']."</b>*".$_POST['colorRadio']."*".$_POST['paralelmk']."*".$_POST['sesi']."<br>".$_POST['Tanggal']."*".$_POST['matakuliah']."*".$_POST['lab']."*".$_POST['keperluan']."*".$_POST['keterangan']."<br><a href='./'>Kembali</a>";

include ('config.php');
$stspinjam=$_POST['colorRadio'];
if ($stspinjam === 'dosen') {
if(($_POST['pegawai']=="") or ($_POST['paralelmk']=="") or ($_POST['matakuliah']=="") or ($_POST['Tanggal']=="") or ($_POST['sesi']=="") or ($_POST['keperluan']=="")){
     echo "<br><br><br><center><img src='gambar/datakosong.png' width='200' height='150' alt='Data Kosong'/><br/><p style='font-size:14pt; font-family:arial;'><b>Data is Empty</b>, Please fill them all.</p><a href='./tambahdata.php'><b>BACK</b></a></center>";
  }else{  
$namapeg="";
$query = mysqli_query($conn, "Select * from pegawai where nip='".$_POST['pegawai']."'");
  if (mysqli_num_rows($query) > 0){
   while ($data = mysqli_fetch_array($query)){
   $namapeg=$data['namapegawai'];
   }
}
$cek=0;
$namalab="";
$sesijd="";
$mk="";
$paral="";
$query = mysqli_query($conn, "select count(*) as nom from detiltransaksipinjam where tglpinjam='".$_POST['Tanggal']."' and sesi='".$_POST['sesi']."' and kdlab='".$_POST['lab']."'");
  if (mysqli_num_rows($query) > 0){
     while ($data = mysqli_fetch_array($query)){
       $cek=$data['nom'];
     }
}
$query = mysqli_query($conn, "select nickname from laboratorium where kodelab='".$_POST['lab']."'");
   if (mysqli_num_rows($query) > 0){
   while ($data = mysqli_fetch_array($query)){
   $namalab=$data['nickname'];
   }
}

$query = mysqli_query($conn, "select namasesi from sesi where kodesesi='".$_POST['sesi']."'");
   if (mysqli_num_rows($query) > 0){
   while ($data = mysqli_fetch_array($query)){
   $sesijd=$data['namasesi'];
   }
}
$query = mysqli_query($conn, "select namamk from matakuliah where kodemk=(select kodemk from detiltransaksipinjam where tglpinjam='".$_POST['Tanggal']."' and sesi='".$_POST['sesi']."' and kdlab='".$_POST['lab']."' limit 0,1)");
   if (mysqli_num_rows($query) > 0){
   while ($data = mysqli_fetch_array($query)){
   $mk=$data['namamk'];
   }
}
$query = mysqli_query($conn, "select paralel from paralelmk where idparalel=(select paralel from detiltransaksipinjam where tglpinjam='".$_POST['Tanggal']."' and sesi='".$_POST['sesi']."' and kdlab='".$_POST['lab']."' limit 0,1)");
   if (mysqli_num_rows($query) > 0){
   while ($data = mysqli_fetch_array($query)){
   $paral=$data['paralel'];
   }
}
$textb=nl2br("Maaf, Peminjaman Lab. ".$namalab." di ".$sesijd." dan tanggal=".$_POST['Tanggal'].", \n sudah dipesan oleh Matakuliah ".$mk."(".$paral."), terima kasih.");  
    if($cek === "0"){
    $query = mysqli_query($conn, "insert into detiltransaksipinjam (nip_npm,namapeminjam,kdlab,kodemk,paralel,status,tglpinjam,sesi,keperluan,keterangan, tgltransaksi) values ('".$_POST['pegawai']."','".$namapeg."','".$_POST['lab']."','".$_POST['matakuliah']."','".$_POST['paralelmk']."','Dosen','".$_POST['Tanggal']."','".$_POST['sesi']."','".$_POST['keperluan']."','".$_POST['keterangan']."',current_date())");
?>
  <script type="text/javascript">
   alert("Data processed successfully :)");
   window.location = "datapinjam.php";
</script>
<?php
    }else {
echo "<br><br><br><center><p style='font-size:14pt; font-family:arial;'>".$textb."</p><a href='./'>Kembali</a></center>";
    }
   }   
}
if ($stspinjam === 'mahasiswa') {
       if(($_POST['npm']=="") or ($_POST['namamhs']=="") or ($_POST['labmhs']=="") or ($_POST['mkmhs']=="") or ($_POST['tglmhs']=="") or ($_POST['sesimhs']=="") or ($_POST['keperluanmhs']=="")){
     echo "<br><br><br><center><img src='gambar/datakosong.png' width='200' height='150' alt='Data Kosong'/><br/><p style='font-size:14pt; font-family:arial;'><b>Data is Empty</b>, Please fill them all.</p><a href='./tambahdata.php'><b>BACK</b></a></center>";
  }else{
$cek=0;
$namalab="";
$sesijd="";
$mk="";
$paral="";
$query = mysqli_query($conn, "select count(*) as nom from detiltransaksipinjam where tglpinjam='".$_POST['tglmhs']."' and sesi='".$_POST['sesimhs']."' and kdlab='".$_POST['labmhs']."'");
  if (mysqli_num_rows($query) > 0){
     while ($data = mysqli_fetch_array($query)){
       $cek=$data['nom'];
     }
}
$query = mysqli_query($conn, "select nickname from laboratorium where kodelab='".$_POST['labmhs']."'");
   if (mysqli_num_rows($query) > 0){
   while ($data = mysqli_fetch_array($query)){
   $namalab=$data['nickname'];
   }
}

$query = mysqli_query($conn, "select namasesi from sesi where kodesesi='".$_POST['sesimhs']."'");
   if (mysqli_num_rows($query) > 0){
   while ($data = mysqli_fetch_array($query)){
   $sesijd=$data['namasesi'];
   }
}
$query = mysqli_query($conn, "select namamk from matakuliah where kodemk=(select kodemk from detiltransaksipinjam where tglpinjam='".$_POST['tglmhs']."' and sesi='".$_POST['sesimhs']."' and kdlab='".$_POST['labmhs']."' limit 0,1)");
   if (mysqli_num_rows($query) > 0){
   while ($data = mysqli_fetch_array($query)){
   $mk=$data['namamk'];
   }
}
$query = mysqli_query($conn, "select paralel from paralelmk where idparalel=(select paralel from detiltransaksipinjam where tglpinjam='".$_POST['tglmhs']."' and sesi='".$_POST['sesimhs']."' and kdlab='".$_POST['labmhs']."' limit 0,1)");
   if (mysqli_num_rows($query) > 0){
   while ($data = mysqli_fetch_array($query)){
   $paral=$data['paralel'];
   }
}
$textb=nl2br("Maaf, Peminjaman Lab. ".$namalab." di ".$sesijd." dan tanggal=".$_POST['tglmhs'].", \n sudah dipesan oleh Matakuliah ".$mk."(".$paral."), terima kasih.");  
    if($cek === "0"){
    $query = mysqli_query($conn, "insert into detiltransaksipinjam (nip_npm,namapeminjam,kdlab,kodemk,paralel,status,tglpinjam,sesi,keperluan,keterangan, tgltransaksi) values ('".$_POST['npm']."','".$_POST['namamhs']."','".$_POST['labmhs']."','".$_POST['mkmhs']."','".$_POST['paralelmhs']."','Mahasiswa','".$_POST['tglmhs']."','".$_POST['sesimhs']."','".$_POST['keperluanmhs']."','".$_POST['keteranganmhs']."',current_date())");
?>
  <script type="text/javascript">
   alert("Data processed successfully :)");
   window.location = "datapinjam.php";
</script>
<?php
    }else {
echo "<br><br><br><center><p style='font-size:14pt; font-family:arial;'>".$textb."</p><a href='./'>Kembali</a></center>";
    } 
  }   
} 

if ($stspinjam === 'staff') {
    if(($_POST['nippeg']=="") or ($_POST['namapeg']=="") or ($_POST['labpeg']=="") or ($_POST['namainstitusi']=="") or ($_POST['tglpeg']=="") or ($_POST['sesipeg']=="") or ($_POST['keperluanpeg']=="")){
     echo "<br><br><br><center><img src='gambar/datakosong.png' width='200' height='150' alt='Data Kosong'/><br/><p style='font-size:14pt; font-family:arial;'><b>Data is Empty</b>, Please fill them all.</p><a href='./tambahdata.php'><b>BACK</b></a></center>";
  }else{
$cek=0;
$namalab="";
$sesijd="";
$mk="";
$paral="";
$query = mysqli_query($conn, "select count(*) as nom from detiltransaksipinjam where tglpinjam='".$_POST['tglpeg']."' and sesi='".$_POST['sesipeg']."' and kdlab='".$_POST['labpeg']."'");
  if (mysqli_num_rows($query) > 0){
     while ($data = mysqli_fetch_array($query)){
       $cek=$data['nom'];
     }
}
$query = mysqli_query($conn, "select nickname from laboratorium where kodelab='".$_POST['labpeg']."'");
   if (mysqli_num_rows($query) > 0){
   while ($data = mysqli_fetch_array($query)){
   $namalab=$data['nickname'];
   }
}

$query = mysqli_query($conn, "select namasesi from sesi where kodesesi='".$_POST['sesipeg']."'");
   if (mysqli_num_rows($query) > 0){
   while ($data = mysqli_fetch_array($query)){
   $sesijd=$data['namasesi'];
   }
}
/*$query = mysqli_query($conn, "select namamk from matakuliah where kodemk=(select kodemk from detiltransaksipinjam where tglpinjam='".$_POST['tglmhs']."' and sesi='".$_POST['sesimhs']."' and kdlab='".$_POST['labmhs']."' limit 0,1)");
   if (mysqli_num_rows($query) > 0){
   while ($data = mysqli_fetch_array($query)){
   $mk=$data['namamk'];
   }
}
$query = mysqli_query($conn, "select paralel from paralelmk where idparalel=(select paralel from detiltransaksipinjam where tglpinjam='".$_POST['tglmhs']."' and sesi='".$_POST['sesimhs']."' and kdlab='".$_POST['labmhs']."' limit 0,1)");
   if (mysqli_num_rows($query) > 0){
   while ($data = mysqli_fetch_array($query)){
   $paral=$data['paralel'];
   }
}*/

$textb=nl2br("Maaf, Peminjaman Lab. ".$namapeg." di ".$sesipeg." dan tanggal=".$_POST['tglpeg'].", \n sudah dipesan oleh Matakuliah ".$mk."(".$paral."), terima kasih.");  
    if($cek === "0"){
    $query = mysqli_query($conn, "insert into detiltransaksipinjam (nip_npm,namapeminjam,kdlab,kodemk,paralel,status,tglpinjam,sesi,keperluan,keterangan, tgltransaksi) values ('".$_POST['nippeg']."','".$_POST['namapeg']."','".$_POST['labpeg']."','-','-','Staff/Umum','".$_POST['tglpeg']."','".$_POST['sesipeg']."','".$_POST['keperluanpeg']."','".$_POST['namainstitusi']."',current_date())");
?>
  <script type="text/javascript">
   alert("Data processed successfully :)");
   window.location = "datapinjam.php";
</script>
<?php
    }else {
echo "<br><br><br><center><p style='font-size:14pt; font-family:arial;'>".$textb."</p><a href='./'>BACK</a></center>";
    }
  }    
}
?>