<?php
//include 'session.php';
include "module/excel_reader2.php";
@$data = new Spreadsheet_Excel_Reader($_FILES['fileexcel']['tmp_name']);
@$hasildata = $data->rowcount($sheet_index=0);
//echo $hasildata;
$sukses = 0;
$gagal = 0;
for ($i=2; $i<=$hasildata; $i++)
{
		$id_sp1="id_sp='".$id_sp."'";
		//echo $id_sp1."<br>";
		//$prodi=trim($data->val($i,2);      
		//echo "kode prodi : ".trim($data->val($i,2)."<br>";
		$kode_prodi="kode_prodi ILIKE '%".trim($data->val($i,11))."%'";
		$filter=$kode_prodi." AND ".$id_sp1;
		//echo $filter."<br>";
		$result=$proxy->GetRecord($gettoken,'sms',$filter);
		//var_dump($result);
		//$coba=$result['result'];
		$id_sms=$result['result']['id_sms'];
		
		$npm=trim($data->val($i,2));
		$nipd="nipd LIKE '%$npm%'";
		//echo "<br>".$nipd."<br>";
		$result=$proxy->GetRecord($gettoken,'mahasiswa_pt',$nipd);
		$id_reg_pd=$result['result']['id_reg_pd'];
		//echo "id_reg_pd ='".$id_reg_pd."<br>";
		$kd_mk=trim($data->val($i,4));
		$smt=trim($data->val($i,6));
		$kelas=trim($data->val($i,7));
		//$filt="p.id_smt='".$smt."' AND nm_kls='".$kelas."' AND kode_mk='".$kd_mk."' AND soft_delete=0";
		$filt="p.id_smt='$smt' AND nm_kls='$kelas' AND kode_mk='$kd_mk' AND p.id_sms= '$id_sms'";
		//$filt="p.id_smt='".$smt."' AND nm_kls='".$kelas."' AND kode_mk='".$kd_mk."'";
		//echo $filt."<br>";
		$result=$proxy->GetRecord($gettoken,'kelas_kuliah',$filt);
		$id_kls=$result['result']['id_kls'];
		//echo "id kelas : ".$id_kls. "<br>";
		
	$record['id_kls'] = $id_kls; 
	$record['id_reg_pd'] = $id_reg_pd;
	  $nilai= trim($data->val($i,8));
	  $indeks=trim($data->val($i,9));
	  $angka=trim($data->val($i,10));
	  //echo "Nilai =".$nilai."dan Indeks =".$indeks;
	$record['nilai_huruf'] =$nilai;
	$record['nilai_indeks'] =$indeks;
	$record['nilai_angka'] =$angka;
  	$records[]=$record;
	
}
	$je_nil=json_encode($records);
	$jd_nil=json_decode($je_nil,true);
	//print_r($je_nil);
	
	echo "<pre style='background-color:#1699C9'>";
	$a=1;
	for($i=0;$i<1;$i++){
			foreach($jd_nil as $row){
				$key = array('id_kls' => $row['id_kls'],'id_reg_pd' => $row['id_reg_pd']);
				$data = array('nilai_huruf' => $row['nilai_huruf'],
				   'nilai_indeks'=> $row['nilai_indeks'],
				   'nilai_angka'=> $row['nilai_angka']);
				
	
				$updates[] = array('key'=>$key,'data'=>$data);
				
				echo "<br>$a | Id Kelas = ".$row['id_kls']." | Id_reg_pd :".$row['id_reg_pd']." | Nilai Huruf : ".$row['nilai_huruf']." | Index : ".$row['nilai_indeks'];
				
				if ($row['id_kls'] !='' AND $row['nilai_huruf'] !='') $sukses++;
					else $gagal++;
				$a++;
				}
			$j_upd=json_encode($updates);
			//echo "<br> json_update ".$j_upd;
		$result = $proxy->UpdateRecordset($gettoken,'nilai',$j_upd);
		//var_dump($result);
		
	}
	echo "</pre>";

		echo "<b>import data selesai.</b> <br>";
		echo "Data yang berhasil di import : " . $sukses .  "<br>";
		echo "Data yang gagal diimport : ".$gagal .  "<br>";
		echo "kembali ke <a href='?module=home'>depan</a>";
	
?>