<?php
session_start();
$time_start = microtime(true); 
include "../../inc/config.php";
require('../../lib/simplexlsx.class.php');
session_check();
switch ($_GET["act"]) {
  case 'import':
    
 if (!is_dir("../../../upload/kurikulum")) {
              mkdir("../../../upload/kurikulum");
            }
   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/kurikulum/".$_FILES['semester']['name']);
              $semester = array("semester"=>$_FILES["semester"]["name"]);

            }


$error_count = 0;
$error = array();
$sukses = 0;
$values = "";
$array_insert = array();
  $Reader = new SimpleXLSX("../../../upload/kurikulum/".$_FILES['semester']['name']);
  foreach ($Reader->rows() as $key => $val)
  {
    if ($key>0) {

      if ($val[0]!='') {

         $check = $db->check_exist('kurikulum',array(
          'mulai_berlaku' => $db->trimmer($val[1]),
          'kode_jurusan' => $db->trimmer($val[5])
          ));
                if ($check==true) {
                  $error_count++;
                  $error[] = $val[0]." Sudah Ada";
                } else {
                  $sukses++;
                  $array_insert[] = array(
                  'nama_kur' => $db->trimmer($val[0]),
                  'mulai_berlaku' => $db->trimmer($val[1]),
                  'kode_jurusan' => $db->trimmer($val[5]),
                  'jml_sks_wajib' => $db->trimmer($val[2]),
                  'jml_sks_pilihan' => $db->trimmer($val[3]),
                  'total_sks' => $db->trimmer($val[4])
                  );
              }

      }

    }

  }

if (!empty($array_insert)) {
  $db->insertMulti('kurikulum',$array_insert);
}


unlink("../../../upload/kurikulum/".$_FILES['semester']['name']);
//$time2 = microtime(true);
$time_end = microtime(true);
$execution_time = ($time_end - $time_start);
          $msg = '';
      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 kurikulum 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 "in":
  
  $data = array("nama_kur"=>$_POST["nama_kur"],"mulai_berlaku"=>$_POST["mulai_berlaku"],"jml_sks_wajib"=>$_POST["jml_sks_wajib"],"jml_sks_pilihan"=>$_POST["jml_sks_pilihan"],"kode_jurusan"=>$_POST["kode_jurusan"],'total_sks'=>$_POST["total_sks"]);
  
  
  
   
    $in = $db->insert("kurikulum",$data);
    
    if ($in=true) {
      echo "good";
    } else {
      return false;
    }
    break;
  case "delete":
    
    
    
    $db->delete("kurikulum","id",$_GET["id"]);
    break;
  case "up":
   $data = array("nama_kur"=>$_POST["nama_kur"],"mulai_berlaku"=>$_POST["mulai_berlaku"],"jml_sks_wajib"=>$_POST["jml_sks_wajib"],"jml_sks_pilihan"=>$_POST["jml_sks_pilihan"],"kode_jurusan"=>$_POST["kode_jurusan"],'total_sks'=>$_POST["total_sks"]);
   
 $db->fetch_custom("update kurikulum set kode_jurusan='".$_POST["kode_jurusan"]."' where id_kurikulum='".$_POST['id']."'");
   
   
    
    $up = $db->update("kurikulum",$data,"id",$_POST["id"]);
    if ($up=true) {
      echo "good";
    } else {
      return false; 
    }
    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 mulai_berlaku='".$_POST['sem']."'";
    }

      if ($status=="" && $jurusan=="" && $semester=="") {
      
      if ($_SESSION['level']==1) {
         $db->fetch_custom("TRUNCATE `kurikulum`");
       } else {
          $db->fetch_custom("delete from kurikulum where kode_jurusan='".$_SESSION['jurusan']."'");
       }
    } else {
      $ids = $db->fetch_custom("select id from 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 kurikulum where id!='' $semester $jurusan $status and id in ($data_id)");
    }


    
    break;
  default:
    # code...
    break;
}

?>