<?php
session_start();


include "../../inc/config.php";
session_check();
$all_errors = array();
$msg = "";
$jumlah_sukses = 0;
$jumlah_error = 0;
    $client = new nusoap_client($db->get_service_url('soap'), true);
    $proxy = $client->getProxy();
    $token = $db->get_token();
switch ($_GET["act"]) {
  case "restore":
  if ($_POST['type']=='mhs') {
    $table = "mahasiswa";
    $key = array('id_pd' => $_POST['id']);
  } else {
    $table = "mahasiswa_pt";
    $key = array('id_reg_pd' => $_POST['id']);
  }

  
  $restore = $proxy->RestoreRecord($token,$table,json_encode($key));

  if ($restore['error_code']==0) {
      echo "good";
  } else {
      echo $restore['error_desc'];
  }
  break;
  case 'nilai':
    foreach ($_POST['data_ids'] as $dt) {
      $xpl = explode("#", $dt);
      $array_restore[] = array('id_kls' => $xpl[0],'id_reg_pd' => $xpl[1]);
    }

    $restore = $proxy->RestoreRecordset($token, 'nilai',json_encode($array_restore));
    foreach ($restore['result'] as $rs) {
      if ($rs['error_desc']=='') {
        $jumlah_sukses++;
      } else {
        $jumlah_error++;
        $all_errors[] = $dt['error_desc'];
      }
    }

    $msg =  "<div class=\"alert alert-warning\" role=\"alert\">
    <button type=\"button\" class=\"close hide_alert_notif\" data-dismiss=\"alert\">&times;</button>
    <font color=\"#3c763d\">".$jumlah_sukses." data Nilai berhasil direstore</font><br />
    <font color=\"#ce4844\" >".$jumlah_error." data tidak bisa direstore </font>";
    
    if (!$jumlah_error==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 ($all_errors as $pesan) {
            $msg .= "<div class=\"bs-callout bs-callout-danger\">".$i.". ".$pesan."</div><br />";
          $i++;
          }
    $msg .= "</div>
    </div>";
    echo $msg;
    break;
 case 'akm':
    foreach ($_POST['data_ids'] as $dt) {
      $xpl = explode("#", $dt);
      $array_restore[] = array('id_smt' => $xpl[0],'id_reg_pd' => $xpl[1]);
    }

    $restore = $proxy->RestoreRecordset($token, 'kuliah_mahasiswa',json_encode($array_restore));
    foreach ($restore['result'] as $rs) {
      if ($rs['error_desc']=='') {
        $jumlah_sukses++;
      } else {
        $jumlah_error++;
        $all_errors[] = $dt['error_desc'];
      }
    }

    $msg =  "<div class=\"alert alert-warning\" role=\"alert\">
    <button type=\"button\" class=\"close hide_alert_notif\" data-dismiss=\"alert\">&times;</button>
    <font color=\"#3c763d\">".$jumlah_sukses." data AKM berhasil direstore</font><br />
    <font color=\"#ce4844\" >".$jumlah_error." data tidak bisa direstore </font>";
    
    if (!$jumlah_error==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 ($all_errors as $pesan) {
            $msg .= "<div class=\"bs-callout bs-callout-danger\">".$i.". ".$pesan."</div><br />";
          $i++;
          }
    $msg .= "</div>
    </div>";
    echo $msg;
    break;
  case 'transfer':
    foreach ($_POST['data_ids'] as $dt) {
      $array_restore[] = array('id_ekuivalensi' => $dt);
    }

    $restore = $proxy->RestoreRecordset($token, 'nilai_transfer',json_encode($array_restore));
    foreach ($restore['result'] as $rs) {
      if ($rs['error_desc']=='') {
        $jumlah_sukses++;
      } else {
        $jumlah_error++;
        $all_errors[] = $dt['error_desc'];
      }
    }

    $msg =  "<div class=\"alert alert-warning\" role=\"alert\">
    <button type=\"button\" class=\"close hide_alert_notif\" data-dismiss=\"alert\">&times;</button>
    <font color=\"#3c763d\">".$jumlah_sukses." data Nilai Transfer berhasil direstore</font><br />
    <font color=\"#ce4844\" >".$jumlah_error." data tidak bisa direstore </font>";
    
    if (!$jumlah_error==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 ($all_errors as $pesan) {
            $msg .= "<div class=\"bs-callout bs-callout-danger\">".$i.". ".$pesan."</div><br />";
          $i++;
          }
    $msg .= "</div>
    </div>";
    echo $msg;
    break;
case 'aktivitas':
    foreach ($_POST['data_ids'] as $dt) {
      $array_restore[] = array('id_ang_akt_mhs' => $dt);
    }

    $restore = $proxy->RestoreRecordset($token, 'anggota_aktivitas_mahasiswa',json_encode($array_restore));
    print_r($restore);
    foreach ($restore['result'] as $rs) {
      if ($rs['error_desc']=='') {
        $jumlah_sukses++;
      } else {
        $jumlah_error++;
        $all_errors[] = $dt['error_desc'];
      }
    }

    $msg =  "<div class=\"alert alert-warning\" role=\"alert\">
    <button type=\"button\" class=\"close hide_alert_notif\" data-dismiss=\"alert\">&times;</button>
    <font color=\"#3c763d\">".$jumlah_sukses." data Aktivitas berhasil direstore</font><br />
    <font color=\"#ce4844\" >".$jumlah_error." data tidak bisa direstore </font>";
    
    if (!$jumlah_error==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 ($all_errors as $pesan) {
            $msg .= "<div class=\"bs-callout bs-callout-danger\">".$i.". ".$pesan."</div><br />";
          $i++;
          }
    $msg .= "</div>
    </div>";
    echo $msg;
    break;
  case "in":
  
  
  
  $data = array("nm_pd"=>$_POST["nm_pd"],);
  
  
  
   
    $in = $db->insert("mhs",$data);
    
    if ($in=true) {
      echo "good";
    } else {
      return false;
    }
    break;
  case "delete":
    
    
    
    $db->delete("mhs","id",$_GET["id"]);
    break;
  case "up":
   $data = array("nm_pd"=>$_POST["nm_pd"],);
   
   
   

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

?>