<?php
session_start();
$time_start = microtime(true); 
include "../../../inc/config.php";
require('../../../lib/simplexlsx.class.php');
session_check();
switch ($_GET["act"]) {
   case 'delete_error':
    $db->fetch_custom("delete from mat_kurikulum where status_error=2 and kode_jurusan='".$_POST['id']."'");
    break;
    case 'delete_all':
  
    $status = $_POST['status'];
    if ($status=='all') {
        $status = '';
    } elseif ($status=='0') {
        $status = "and status_error ='0'";
    } elseif ($status=='1') {
        $status = "and status_error ='1'";
    } elseif ($status=='2') {
        $status = "and status_error ='2'";
    }

    if ($_POST['jurusan']=='all') {
        $jurusan = "";
    } else {
        $jurusan = "and kode_jurusan='".$_POST['jurusan']."'";
    }

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

      if ($status=="" && $jurusan=="" && $semester=="") {
      
      if ($_SESSION['level']==1) {
         $db->fetch_custom("TRUNCATE `mat_kurikulum`");
       } else {
          $db->fetch_custom("delete from mat_kurikulum where kode_jurusan='".$_SESSION['jurusan']."'");
       }
    } else {
      $ids = $db->fetch_custom("select id from mat_kurikulum where id!='' $semester $jurusan $status");
      foreach ($ids as $id) {
        $id_delete[] = $id->id;
      }
        $data_id = implode(",", $id_delete);
       $data_mhs_pt = $db->fetch_custom("delete from mat_kurikulum where id!='' $semester $jurusan $status and id in ($data_id)");
    }


    
    break;
case 'import':

   if (!preg_match("/.(xls|xlsx)$/i", $_FILES["semester"]["name"]) ) {

              echo "pastikan file yang anda pilih xls|xlsx";
              exit();

            } else {
              move_uploaded_file($_FILES["semester"]["tmp_name"], "../../../../upload/matakuliah/".$_FILES['semester']['name']);
              $semester = array("semester"=>$_FILES["semester"]["name"]);

            }

      $error_count = 0;
      $error = array();
      $sukses = 0;
      $values = array();

  $Reader = new SimpleXLSX("../../../../upload/matakuliah/".$_FILES['semester']['name']);
  foreach ($Reader->rows() as $key => $val)
  {
    if ($key>0) {

      if ($val[0]!='') {
          $check = $db->check_exist('mat_kurikulum',array(
            'kode_mk' =>$db->trimmer($val[0]),
            'tahun'=> $db->trimmer($val[11]),
            'kode_jurusan' => $db->trimmer($val[13])
          ));
          if ($check==true) {
            $error_count++;
            $error[] = $val[0]." Sudah Ada";
          } else {
            $sukses++;
            $value = array(
              'kode_mk' => $db->trimmer($val[0]),
              'nama_mk' => $db->trimmer($val[1]),
              'jns_mk' => $db->trimmer($val[2]),
              'sks_tm' => $db->trimmer($val[3]),
              'sks_prak' => $db->trimmer($val[4]),
              'sks_prak_lap' => $db->trimmer($val[5]),
              'sks_sim' => $db->trimmer($val[6]),
              'metode_pelaksanaan_kuliah' => $db->trimmer($val[7]),
              'tgl_mulai_efektif' => $db->trimmer($val[8]),
              'tgl_akhir_efektif' => $db->trimmer($val[9]),
              'semester' => $db->trimmer($val[10]),
              'tahun' => $db->trimmer($val[11]),
              'wajib' => $db->trimmer($val[12]),
              'kode_jurusan' => $db->trimmer($val[13]),
              'kelompok_mk' => $db->trimmer($val[14]),
            );
            if (isset($val[15])) {
              $value['id_matkul'] = $db->trimmer($val[15]);
            } else {
              $value['id_matkul'] = '';
            }
            $values[] = $value;
        }

      }

    }

      }

if (!empty($values)) {
  $db->insert_massal('mat_kurikulum',$values);
}

unlink("../../../../upload/matakuliah/".$_FILES['semester']['name']);
          $msg = '';
$time_end = microtime(true);
$execution_time = ($time_end - $time_start);

      if (($sukses>0) || ($error_count>0)) {
        $msg =  "<div class=\"alert alert-warning alert-dismissible\" role=\"alert\" >
        <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">�</button>
            <font color=\"#3c763d\">".$sukses." data Matakuliah baru berhasil di import</font><br />
            <font color=\"#ce4844\" >".$error_count." data tidak bisa ditambahkan </font>";
            if (!$error_count==0) {
              $msg .= "<a data-toggle=\"collapse\" href=\"#collapseExample\" aria-expanded=\"false\" aria-controls=\"collapseExample\">Detail error</a>";
            }
            //echo "<br />Total: ".$i." baris data";
            $msg .= "<div class=\"collapse\" id=\"collapseExample\">";
                $i=1;
                foreach ($error as $pesan) {
                    $msg .= "<div class=\"bs-callout bs-callout-danger\">".$i.". ".$pesan."</div><br />";
                  $i++;
                  }
            $msg .= "</div>
          </div>";
            $msg .= "<p>Total Waktu Import : ". waktu_import($execution_time);
      }
     echo $msg;
          break;

  case "delete":
    
    
    
    $db->delete("mat_kurikulum","id",$_GET["id"]);
    break;

      case 'del_massal':
    $data_ids = $_REQUEST['data_ids'];
    if(trim($data_ids)!="") {
        $delete =  $db->fetch_custom("delete from mat_kurikulum where id in ($data_ids)");
    }
    break;
  case "in":
  
  $data = array(
    "tahun"=>$_POST["tahun"],
    "kode_mk"=>$_POST["kode_mk"],
    "nama_mk"=>$_POST["nama_mk"],
    "jns_mk"=>$_POST["jns_mk"],
    "sks_tm"=>$_POST["sks_tm"],
    "sks_prak"=>$_POST["sks_prak"],
    "sks_prak_lap"=>$_POST["sks_prak_lap"],
    "sks_sim"=>$_POST["sks_sim"],
    'metode_pelaksanaan_kuliah' => $_POST["metode_pelaksanaan_kuliah"],
    'tgl_mulai_efektif' => $_POST["tgl_mulai_efektif"],
    'tgl_akhir_efektif' => $_POST["tgl_akhir_efektif"],
    "semester"=>$_POST["semester"],
    "kelompok_mk"=>$_POST["kelompok_mk"],
    "kode_jurusan"=>$_POST["kode_jurusan"]
  );
  
  
   
    $in = $db->insert("mat_kurikulum",$data);
    
    if ($in=true) {
      echo "good";
    } else {
      return false;
    }
    break;
  case "delete":
    
    
    
    $db->delete("mat_kurikulum","id",$_GET["id"]);
    break;
  case "up":
  
  $data = array(
    "tahun"=>$_POST["tahun"],
    "kode_mk"=>$_POST["kode_mk"],
    "nama_mk"=>$_POST["nama_mk"],
    "jns_mk"=>$_POST["jns_mk"],
    "sks_tm"=>$_POST["sks_tm"],
    "sks_prak"=>$_POST["sks_prak"],
    "sks_prak_lap"=>$_POST["sks_prak_lap"],
    "sks_sim"=>$_POST["sks_sim"],
    'metode_pelaksanaan_kuliah' => $_POST["metode_pelaksanaan_kuliah"],
    'tgl_mulai_efektif' => $_POST["tgl_mulai_efektif"],
    'tgl_akhir_efektif' => $_POST["tgl_akhir_efektif"],
    "semester"=>$_POST["semester"],
    "kelompok_mk"=>$_POST["kelompok_mk"],
    "kode_jurusan"=>$_POST["kode_jurusan"]
  );
  

    
    $up = $db->update("mat_kurikulum",$data,"id",$_POST["id"]);
    if ($up=true) {
      echo "good";
    } else {
      return false; 
    }
    break;
  default:
    # code...
    break;
}

?>