<?php
session_start();

include "../../../inc/config.php";

$json_response = array();
$jumlah = array();
if ($_POST['total_data']>0) {

		//soap
		/*$client = new nusoap_client(get_service_url('soap'), true);
		$proxy = $client->getProxy();*/
		$url_rest = get_service_url('rest');

		$offset = $_POST['offset'];
		$jumlah['offset'] = $offset;
		$token = $_POST['token'];
		$jumlah['token'] = $_POST['token'];

		if ($offset==0) {
			$token = get_token();
		    $jumlah['token'] = $token;
		}
        $sukses_count = 0;

        $error_count = 0;
        $error_count_in = 0;

        $error_msg = array();

        $check_kur_exist= array();

        $update_data = array();
        $update_data_status = array();

        $update_data_er =array();
        $update_data_er_status =array();
        $error_mk = "";
        $array_id_anggota = array();
        $array_exist_konversi = array();
        $array_id_mk = array();
        $array_id_mk_cross = array();
        $data_id_sukses = array();
        $data_id_error= array();

        $all_errors = array();
        $all_error = array();
        $query_not_in = "";
        $id_mk = "";
        $error_mk = "";

        $msg = "";
        $array_kurikulum = array();
        $array_kurikulum_update = array();
        
        $query_jenis = '';
        $query_jurusan = '';
        $all_id = array();
        $id_konversi = array();

        $sem = "";
        $data_ids = array();

        $data_kur = array();
        $aktivitas_id = array();
        $data_id_aktivitas = array();
        $nim_data = array();
        $check_konversi_exist = array();

        $jurusan = "";
        $query_matkul = "";

	
    	if ($_POST['sem']!='all') {
      		$sem = "and kurikulum.mulai_berlaku='".$_POST['sem']."'";
    	}
		
		if ($_POST['jurusan']!='all') {
			$jurusan = "and trim(jurusan.kode_jurusan)='".trim($_POST['jurusan'])."'";
		} else {
			if ($_SESSION['level']!=1) {
				$jurusan = "and trim(jurusan.kode_jurusan)='".$_SESSION['jurusan']."'";
			}
		}

		 $query_not_in = "and unik_id not in(".$_POST['random_number'].")"; ;

		$data_loops = $db->fetch_custom("select *,kurikulum.id as id_krs from kurikulum inner join jurusan on kurikulum.kode_jurusan=jurusan.kode_jurusan where status_error!=1 $sem $jurusan  $query_not_in limit 8");

		foreach ($data_loops as $dt_id) {
			$data_loop[] = $dt_id;
			$check_kur_exist[$dt_id->id_sms.convert_ascii($dt_id->mulai_berlaku)] = "('".$dt_id->id_sms."','".convert_ascii($dt_id->mulai_berlaku)."')";
		}

		$kur_concat_where = implode(",", $check_kur_exist);
		//check kurikulum exist
		$data_req_kur = [
			'act' => 'GetListKurikulum',
		    'token' => $token,
		    'filter' => "(id_prodi,id_semester) in($kur_concat_where)",
		    'order' => "",
		    'limit' => "",
		    'offset' => ""
		];
		$get_kurikulum = service_request($data_req_kur);
		if (!empty($get_kurikulum->data)) {
			foreach ($get_kurikulum->data as $kur_data) {
				$data_kur[convert_ascii($kur_data->id_prodi).convert_ascii($kur_data->id_semester)] = $kur_data->id_kurikulum;
			}
		}

		foreach ($data_loop as $value) {
		$id_sms = $value->id_sms;
		$jml_sks_wajib = $value->jml_sks_wajib;
		$jml_sks_pilihan = $value->jml_sks_pilihan;

		if ($_POST['method']=='up') {
			if (in_array($value->id_sms.convert_ascii($value->mulai_berlaku), array_keys($data_kur))) {
					$array_kurikulum_update[] = array(
						'id_kurikulum' => $data_kur[$value->id_sms.convert_ascii($value->mulai_berlaku)],
						'nama_kurikulum' => $value->nama_kur,
						'id_prodi' => $value->id_sms,
						'id_semester' => $value->mulai_berlaku,
						'jumlah_sks_lulus' => $jml_sks_wajib+$jml_sks_pilihan,
						'jumlah_sks_wajib' => $jml_sks_wajib,
						'jumlah_sks_pilihan' => $jml_sks_pilihan
					);
					$data_ids[] = $value->id_krs;
			} else {
				$error_update_data[] = array(
					'keterangan' => "kurikulum Tidak ditemukan di feeder",
					'status_error' => 2
				);
				$data_id_error[] = $value->id_krs;
			}

		} else {
			if (in_array($value->id_sms.convert_ascii($value->mulai_berlaku), array_keys($data_kur))) {
				$error_update_data[] = array(
					'keterangan' => "kurikulum Sudah ada di feeder",
					'status_error' => 2
				);
				$data_id_error[] = $value->id_krs;
			} else {
				$array_kurikulum[] = array(
					'nama_kurikulum' => $value->nama_kur,
					'id_prodi' => $value->id_sms,
					'id_semester' => $value->mulai_berlaku,
					'jumlah_sks_lulus' => $jml_sks_wajib+$jml_sks_pilihan,
					'jumlah_sks_wajib' => $jml_sks_wajib,
					'jumlah_sks_pilihan' => $jml_sks_pilihan
				);
				$data_ids[] = $value->id_krs;
			}

		}
			$all_id[] = $value->id_krs;

		}


if (!empty($array_kurikulum_update)) {
	foreach ($array_kurikulum_update as $key => $dt_kur) {
		$key_id['id_kurikulum'] = $dt_kur['id_kurikulum'];
		unset($dt_kur['id_kurikulum']);
		$data_dic = [
	      'act' => 'UpdateKurikulum',
	        'token' => $token,
	        'key' => $key_id,
	        'record' => $dt_kur,
	    ];

			$temp_recs = json_decode($db->run($data_dic,$url_rest));

			$data_key = $data_ids[$key];
			if (is_object($temp_recs)) {
				$value_data = addslashes($temp_recs->error_desc);
				if ($value_data!='') {
					$error_update_data[] = array(
						'keterangan' => $value_data,
						'status_error' => 2
					);
				$data_id_error[] = $data_key;
				} else {
					$update_data[] = array(
						'keterangan' => '',
						'status_error' => 1
					);
					$data_id_sukses[] = $data_key;
				}
			} else {
				$error_update_data[] = array(
						'keterangan' => $temp_recs,
						'status_error' => 2
					);
				$data_id_error[] = $data_key;
			}
			
			


	}
}


if (!empty($array_kurikulum)) {

	foreach ($array_kurikulum as $key => $kur_data) {
	  $data_dic = [
	      'act' => 'InsertKurikulum',
	        'token' => $token,
	        'record' => $kur_data,
	    ];

	  
		$temp_recs = json_decode($db->run($data_dic,$url_rest));
/*	dump($data_dic);
	dump($temp_recs);*/
		
			$data_key = $data_ids[$key];
			if (is_object($temp_recs)) {
				$value_data = addslashes($temp_recs->error_desc);
				if ($value_data!='') {
					$error_update_data[] = array(
						'keterangan' => $value_data,
						'status_error' => 2
					);
				$data_id_error[] = $data_key;
				} else {
					$update_data[] = array(
						'keterangan' => '',
						'status_error' => 1
					);
					$data_id_sukses[] = $data_key;
				}
			} else {
				$error_update_data[] = array(
						'keterangan' => $temp_recs,
						'status_error' => 2
					);
				$data_id_error[] = $data_key;
			}
	}

}

		//update error
		if (!empty($error_update_data)) {
			updateMulti('kurikulum',$error_update_data,'id',$data_id_error);
		}
		$error_update_data = array();
		$data_id_error = array();
		//update sukses
		if (!empty($update_data)) {
			updateMulti('kurikulum',$update_data,'id',$data_id_sukses);
		}
		$update_data = array();
		$data_id_sukses = array();

		$implode_id = implode(",", $all_id);
		

		$db->fetch_custom("update kurikulum set unik_id=".$_POST['random_number']." where id in (".$implode_id.")");



	if (isset($_POST['id_data'])) {

		$jml_error_sukses = $db->fetch_custom_single("SELECT COUNT(IF(status_error='1',1,NULL)) AS jml_sukses,COUNT(IF(status_error='2',1,NULL)) AS jml_error FROM kurikulum where unik_id=".$_POST['random_number']);

		$jumlah_sukses = $jml_error_sukses->jml_sukses;
		$jumlah_error = $jml_error_sukses->jml_error;

		$db->fetch_custom("update kurikulum set unik_id=0 where  unik_id=".$_POST['random_number']);
		
		$msg =  "<div class=\"alert alert-warning \" role=\"alert\">
		<font color=\"#3c763d\">".$jumlah_sukses." data kurikulum berhasil Di Import Ke Feeder DIKTI</font><br />
		<font color=\"#ce4844\" >".$jumlah_error." data tidak bisa Di Import </font>";
		
		if (!$jumlah_error==0) {
			$msg .= "<a data-toggle=\"collapse\" href=\"#collapseExample\" aria-expanded=\"false\" aria-controls=\"collapseExample\">Detail error</a>";
			//pesan error 

			$all_errors = array('Silakan lihat detail error di list data');
			
		}
		//echo "<br />Total: ".$i." baris data";
		$msg .= "<div class=\"collapse\" id=\"collapseExample\">";
				$i=1;
				foreach ($all_errors as $pesan) {
						$msg .= "<div class=\"bs-callout bs-callout-danger\">".$i.". ".$pesan."</div><br />";
					$i++;
					}
		$msg .= "</div>
		</div>";


		$jumlah['last_notif'] = $msg;
	}


} else {
	$msg =  "Tidak ada data yang bisa diproses<p>";
		$jumlah['last_notif'] = $msg;
}

array_push($json_response, $jumlah);
echo json_encode($json_response);
//echo convert(memory_get_usage(true)); // 123 kb