<?php
session_start();
include "../../inc/config.php";
session_check();
$json_response = array();
$sukses_count = 0;	
$error_count = 0;


$query_not_in = "";

$update_data_valid_30 = array();
$update_data_sks_sem_30= array();
$update_data_ket_valid_ips_30= array();
$update_data_ket_valid_ipk_30= array();
$update_data_ket_valid_ips_ipk_30= array();
$update_data_ket_krs_ada_30= array();
$data_id_error_30 = array();
$all_id = array();
$update_data_valid_ips_fours = array();
$update_data_ket_ips_fours= array();
$data_id_error_ipss = array();

$update_data_valid_ipk_fours = array();
$update_data_sks_sem_four_ipks= array();
$data_id_error_ipks = array();

$update_data_valid_ipk_ipss = array();
$update_data_valid_ket_ipk_ipss = array();
$data_id_error_ipk_ipss = array();

$update_data_valid_ipk_ips_status = array();
$update_data_valid_ket_ipk_ips_status = array();
$data_id_error_ipk_ips_status = array();

$update_data_valid_krss = array();
$update_data_valid_ket_krss = array();
$data_id_error_skss = array();

$update_data_valid_good = array();
$data_id_sukses = array();

if ($_POST['total_data']>0) {

		if (!empty($_POST['all_id'])) {
			$trim_data_id = rtrim($_POST['all_id'], ',');
			$query_not_in = "and nilai_akm.id not in(".$trim_data_id.")";

		}


 $rule_akm = $db->fetch_single_row('rule_akm','id',1);


  $sks = $rule_akm->sks;
  if ($rule_akm->kecuali!="") { 
      $not_in = explode(",", $rule_akm->kecuali);
  } else {
    $not_in = array();
  }

	$sem = "";
    if ($_POST['sem']!='all') {
      $sem = "and semester='".$_POST['sem']."'";
    }

    $jurusan = "";
	if ($_SESSION['level']!=1) {
		$jurusan = "and trim(kode_jurusan)='".$_SESSION['jurusan']."'";
	}


$status_kuliah = $db->fetch_custom("select * from status_mahasiswa_akm");
foreach ($status_kuliah as $st) {
	$stat_kuliah[$st->id_status] = $st->nama_status;
}


  //  echo "<pre>";
          //SKS per semester > 30 sks
     $valids_smt = $db->fetch_custom("select * from nilai_akm where id is not null $sem $jurusan  and valid!=1 $query_not_in limit 150");

         foreach ($valids_smt as $valid) {

        //print_r($valid);
        //semester > 30
        if ((!in_array($valid->kode_jurusan, $not_in) && $valid->sks_smt>24) || $valid->ips>4 || $valid->ipk>4 || ($valid->ipk==0 and $valid->ips>0) || (($valid->sks_smt==0 or $valid->sks_total==0) and $valid->ips>0) || !in_array($valid->status_kuliah,array_keys($stat_kuliah))) {

        	if ((!in_array($valid->kode_jurusan, $not_in) && $valid->sks_smt>24)) {
	        		$ket_valids_smt = "SKS per semester > 24 sks";
					$update_data_valid_30[] = "WHEN $valid->id THEN '2'";
					$update_data_sks_sem_30[]= "WHEN $valid->id THEN '$ket_valids_smt'";
					$update_data_ket_valid_ips_30[]= "WHEN $valid->id THEN ''";
					$update_data_ket_valid_ipk_30[]= "WHEN $valid->id THEN ''";
					$update_data_ket_valid_ips_ipk_30[]= "WHEN $valid->id THEN ''";
					$update_data_ket_krs_ada_30[]= "WHEN $valid->id THEN ''";
					$data_id_error_30[] = $valid->id;
					$error_count++;
        	}

        	if ($valid->ips>4) {
       		    //check if ips is valid
       		$ket_valid_ips = "Nilai IPS > 4.00";
	      	$update_data_valid_ips_fours[] = "WHEN $valid->id THEN '2'";
			$update_data_ket_ips_fours[]= "WHEN $valid->id THEN '$ket_valid_ips'";
			$data_id_error_ipss[] = $valid->id;
			$error_count++;

			}

			if ($valid->ipk>4) {
       		    //check if ipk is valid
       		$ket_valid_ipk = "Nilai IPK > 4.00";
	      	$update_data_valid_ipk_fours[] = "WHEN $valid->id THEN '2'";
			$update_data_sks_sem_four_ipks[]= "WHEN $valid->id THEN '$ket_valid_ipk'";
			$data_id_error_ipks[] = $valid->id;
			$error_count++;
       		}

       		if ($valid->ipk==0 and $valid->ips>0) {
       		    //check if ipk=0 but ips > 0
       		$ket_valid_ips_ipk = "NIlai IPK = 0 , tapi IPS > 0";
	      	$update_data_valid_ipk_ipss[] = "WHEN $valid->id THEN '2'";
	      	$update_data_valid_ket_ipk_ipss[] = "WHEN $valid->id THEN '$ket_valid_ips_ipk'";
			$data_id_error_ipk_ipss[] = $valid->id;
			$error_count++;
       		}

       		if (!in_array($valid->status_kuliah,array_keys($stat_kuliah))) {
       	
	      	$update_data_valid_ipk_ips_status[] = "WHEN $valid->id THEN '2'";
	      	$update_data_valid_ket_ipk_ips_status[] = "WHEN $valid->id THEN 'Id Status Kuliah Salah'";
					$data_id_error_ipk_ips_status[] = $valid->id;
					$error_count++;
       		}

       		

       		if (($valid->sks_smt==0 or $valid->sks_total==0) and $valid->ips>0) {
       		   //check if SKS semester = 0 , tapi KRS atau ips > 0
       		$ket_krsnya = "SKS semester = 0 , tapi KRS atau ips > 0";
           	$update_data_valid_krss[] = "WHEN $valid->id THEN '2'";
	      	$update_data_valid_ket_krss[] = "WHEN $valid->id THEN '$ket_krsnya'";
			$data_id_error_skss[] = $valid->id;
			$error_count++;

			}

       	} else {
       		//otherwise set valid to 1
           	$update_data_valid_good[] = "WHEN $valid->id THEN '1'";
			$data_id_sukses[] = $valid->id;
           	$sukses_count++;
       	}

     		$all_id[] = $valid->id;
			
    }


    		if (!empty($data_id_error_30)) {
    			$update_data_valid = implode(" ", $update_data_valid_30);
				$update_data_sks_sem = implode(" ", $update_data_sks_sem_30);
				$update_data_ket_valid_ips = implode(" ", $update_data_ket_valid_ips_30);
				$update_data_ket_valid_ipk = implode(" ", $update_data_ket_valid_ipk_30);
				$update_data_ket_valid_ips_ipk = implode(" ", $update_data_ket_valid_ips_ipk_30);
				$update_data_ket_krs_ada = implode(" ", $update_data_ket_krs_ada_30);
				$data_id_error = implode(",", $data_id_error_30);

	    		$bulk_update_semester_30 = "UPDATE nilai_akm 
			SET valid = (CASE id 
				$update_data_valid
				END),ket_sks_sem = (CASE id 
				$update_data_sks_sem
				END),ket_valid_ips = (CASE id 
				$update_data_ket_valid_ips
				END),ket_valid_ipk = (CASE id 
				$update_data_ket_valid_ipk
				END),ket_valid_ips_ipk = (CASE id 
				$update_data_ket_valid_ips_ipk
				END),ket_krs_ada = (CASE id 
				$update_data_ket_krs_ada
				END)
			WHERE id IN($data_id_error);";

				
			$db->fetch_custom($bulk_update_semester_30);
    		}

    		if (!empty($data_id_error_ipss)) {

    				$update_data_valid_ips_four = implode(" ", $update_data_valid_ips_fours); 
					$update_data_ket_ips_four = implode(" ", $update_data_ket_ips_fours);
					$data_id_error_ips = implode(",", $data_id_error_ipss);
				$bulk_update_semester_ips = "UPDATE nilai_akm 
				SET valid = (CASE id 
					$update_data_valid_ips_four
					END),ket_valid_ips = (CASE id 
					$update_data_ket_ips_four
					END)
				WHERE id IN($data_id_error_ips);
				";
				$db->fetch_custom($bulk_update_semester_ips);
    		}

    		if (!empty($data_id_error_ipks)) {

    			$update_data_valid_ipk_four = implode(" ", $update_data_valid_ipk_fours); 
					$update_data_sks_sem_four_ipk = implode(" ", $update_data_sks_sem_four_ipks);
					$data_id_error_ipk = implode(",", $data_id_error_ipks);
				$bulk_update_semester_ipk = "UPDATE nilai_akm 
				SET valid = (CASE id 
					$update_data_valid_ipk_four
					END),ket_valid_ipk = (CASE id 
					$update_data_sks_sem_four_ipk
					END)
				WHERE id IN($data_id_error_ipk);
				";
				$db->fetch_custom($bulk_update_semester_ipk);
    		}

    		if (!empty($data_id_error_ipk_ipss)) {
				$update_data_valid_ipk_ips = implode(" ", $update_data_valid_ipk_ipss); 
					$update_data_valid_ket_ipk_ips = implode("", $update_data_valid_ket_ipk_ipss);
					$data_id_error_ipk_ips = implode(",", $data_id_error_ipk_ipss);
				$bulk_update_semester_ipk_ips = "UPDATE nilai_akm 
				SET valid = (CASE id 
					$update_data_valid_ipk_ips
					END),ket_valid_ips_ipk = (CASE id 
					$update_data_valid_ket_ipk_ips
					END)
				WHERE id IN($data_id_error_ipk_ips);
				";
				$db->fetch_custom($bulk_update_semester_ipk_ips);
    		}

    		if (!empty($data_id_error_ipk_ips_status)) {
				$update_data_valid_ipk_ips = implode(" ", $update_data_valid_ipk_ips_status); 
					$update_data_valid_ket_ipk_ips = implode("", $update_data_valid_ket_ipk_ips_status);
					$data_id_error_ipk_ips = implode(",", $data_id_error_ipk_ips_status);
				$bulk_update_semester_ipk_ips = "UPDATE nilai_akm 
				SET valid = (CASE id 
					$update_data_valid_ipk_ips
					END),ket_valid_ips_ipk = (CASE id 
					$update_data_valid_ket_ipk_ips
					END)
				WHERE id IN($data_id_error_ipk_ips);
				";
				$db->fetch_custom($bulk_update_semester_ipk_ips);
    		}

    		if (!empty($data_id_error_skss)) {

				$update_data_valid_krs = implode(" ", $update_data_valid_krss); 
					$update_data_valid_ket_krs = implode(" ", $update_data_valid_ket_krss);
					$data_id_error_sks = implode(",", $data_id_error_skss);
				$bulk_update_semester_krs = "UPDATE nilai_akm 
				SET valid = (CASE id 
					$update_data_valid_krs
					END),ket_krs_ada = (CASE id 
					$update_data_valid_ket_krs
					END)
				WHERE id IN($data_id_error_sks);
				";
				$db->fetch_custom($bulk_update_semester_krs);
    		}


    		if (!empty($data_id_sukses)) {

    			$update_data_valid_goodd = implode(" ", $update_data_valid_good); 
				$data_id_sukse = implode(",", $data_id_sukses);
				$bulk_update_sukses = "UPDATE nilai_akm 
				SET valid = (CASE id 
					$update_data_valid_goodd
					END)
				WHERE id IN($data_id_sukse);
				";
				//echo $bulk_update_sukses;
				$db->fetch_custom($bulk_update_sukses);
    		}




      		

    $jumlah['all_id'] = $all_id;
	$jumlah['jumlah_error'] = $error_count;
	$jumlah['jumlah_sukses'] = $sukses_count;

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

		$jumlah_sukses = $_POST['jumlah_sukses']+$sukses_count;
		$jumlah_error = $_POST['jumlah_error']+$error_count;
		
		$msg =  "<div class=\"alert alert-warning \" role=\"alert\">
		<font color=\"#3c763d\">".$jumlah_sukses." data AKM berhasil di Validasi</font><br />
		<font color=\"#ce4844\" >".$jumlah_error." data error divalidasi, Silakan Perbaiki </font>";
		

		$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);
?>