<?php
include "../../inc/config.php";
include "../../lib/nusoap/nusoap.php";
include "../../inc/feeder_function.php";
require_once '../../lib/Writer.php';

$json_response = array();

$nama_kurikulum = "";

if ($_POST['total_data']<1) {
    $msg =  "<div class=\"alert alert-warning \" role=\"alert\">
        <font color=\"#3c763d\">Tidak ada data berhasil di generate</font><br />
        </div>";

    $jumlah['last_notif'] = $msg;
    array_push($json_response, $jumlah);

    echo json_encode($json_response);
    exit();
}

$url = get_service_url('soap');
$client = new nusoap_client($url, true);
$proxy = $client->getProxy();


$data_rec = array();

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

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

$no = $offset;

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


require_once '../../lib/Writer.php';

//require_once '../../lib/php-export-data.class.php';



        $id_sms = '';


        
         $prodi = $_POST['jurusan'];

        $jurusan = $db->fetch_custom_single("select id_sms,nama_jurusan,kode_jurusan from jurusan where trim(kode_jurusan)='$prodi'");
        $id_sms = "p.id_sms='".$jurusan->id_sms."'";
        $nama_jurusan = $jurusan->nama_jurusan;
        $data_array_jur = array($jurusan->id_sms => $jurusan->kode_jurusan);


        if ($_POST['matkul']=='all') {
          $table = "mata_kuliah";
          $filter_kelas = "$id_sms";
          $loop_kode=1;
        } else {
           $table = "mata_kuliah_kurikulum";
          $filter_kelas = "p.id_kurikulum_sp='".$_POST['matkul']."'";
          $loop_kode=0;
        }



        $order_by = "";


        $count = $proxy->GetCountRecordset($token,$table,$filter_kelas);
        $jml = $count['result'];

        if ($_POST['total_data']<=100) {
            $limit = $_POST['total_data'];
        } else {
            $limit = 100;
        }

        //$jumlah['limit'] = $limit;
    
        $temp_rec = $proxy->GetRecordset($token,$table, $filter_kelas,$order_by, $limit,$offset);
        if ($temp_rec['result']) {
              if ($loop_kode==1) {
                foreach ($temp_rec['result'] as $key) {
                          if (in_array($key['id_sms'], array_keys($data_array_jur))) { 
                             $data_rec[] = array(
                             convert_ascii($key['kode_mk']),
                             convert_ascii($key['nm_mk']),
                             convert_ascii($key['jns_mk']),
                             convert_ascii($key['sks_tm']),
                             convert_ascii($key['sks_prak']),
                             convert_ascii($key['sks_prak_lap']),
                             convert_ascii($key['sks_sim']),
                             convert_ascii($key['tgl_mulai_efektif']),
                             convert_ascii($key['tgl_akhir_efektif']),
                             '',
                             $data_array_jur[$key['id_sms']]
                            );
                          }
                  }
              } else {
                  foreach ($temp_rec['result'] as $key) {
                      $data_mk = $proxy->GetRecord($token,'mata_kuliah',"p.id_mk='".$key['id_mk']."'");
                      $data_mk = $data_mk['result'];
                          if (in_array($data_mk['id_sms'], array_keys($data_array_jur))) { 
                             $data_rec[] = array(
                             convert_ascii($data_mk['kode_mk']),
                             convert_ascii($data_mk['nm_mk']),
                             convert_ascii($data_mk['jns_mk']),
                             convert_ascii($data_mk['sks_tm']),
                             convert_ascii($data_mk['sks_prak']),
                             convert_ascii($data_mk['sks_prak_lap']),
                             convert_ascii($data_mk['sks_sim']),
                             convert_ascii($data_mk['tgl_mulai_efektif']),
                             convert_ascii($data_mk['tgl_akhir_efektif']),
                             convert_ascii($key['smt']),
                             $data_array_jur[$data_mk['id_sms']]
                            );
                          }

                  }
                  $nama_kurikulum = convert_ascii($key['fk__id_kurikulum_sp']);

              }

        }
    
   
               

$jumlah['data_rec'] = json_encode($data_rec);


    //        }
if ($_POST['last']=='yes') {
    //echo "<pre>";

    if ($_POST['total_data']>100) {
            $final_array = array();
        foreach ($_POST['data_rec'] as $dat) {
             $data = json_decode($dat);
             foreach ($data as $dt) {
                 $final_array[]=$dt;
             }
             
        }

        $final = array_merge($final_array,$data_rec);
        $jumlah_total = count($final);

    } else {
        $final = $data_rec;
        $jumlah_total = count($final);
    }


$writer = new XLSXWriter();
$style =
        array (
                      array(
              'border' => array(
                'style' => 'thin',
                'color' => '000000'
                ),
            'allfilleddata' => true
            ),
            array(
                'fill' => array(
                    'color' => 'ff0000'
                    ),
                'cells' => array(
                    'A1',
                    'B1',
                    'C1',
                    'D1',
                    'E1',
                    'F1',
                    'G1',
                    'J1',
                    'K1',
                    ),
                'border' => array(
                    'style' => 'thin',
                    'color' => '000000'
                    ),
                'verticalAlign' => 'center',
                'horizontalAlign' => 'center',
            ),
                        array(
                'fill' => array(
                    'color' => '00ff00'
                    ),
                'cells' => array(
                  'H1',
                  'I1'
                    ),
                'border' => array(
                    'style' => 'thin',
                    'color' => '000000'
                    ),
                'verticalAlign' => 'center',
                'horizontalAlign' => 'center',
                )
            );

//column width
$col_width = array(
  1 => 15,
  2 => 20,
  3 => 10,
  4 => 15,
  5 => 15,
  6 => 15,
  7 => 13,
  8 => 15,
  9 => 15,
  10 => 10,
  11 => 19
  );

$writer->setColWidth($col_width);


$header = array(
  'Kode MK'=>'string',
  'Nama MK'=>'string',
  'Jenis MK'=>'string',
  'SKS Tatap Muka'=>'string',
  'SKS Praktek' => 'string',
  'SKS Praktek Lapangan' => 'string',
  'SKS Simulasi' => 'string',
  'Tgl Mulai Efektif' => 'string',
  'Tgl Akhir Efektif' => 'string',
  'Semester' => 'string',
  'Kode Prodi' => 'string'
);

$filename = str_replace(" ", "_", $nama_jurusan.$nama_kurikulum).'.xlsx';
/*header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');

$writer->writeToFile('xlsx-simple.xlsx');*/
$writer->writeSheet($final,'Data Matakuliah', $header, $style);
$writer->writeToFile(XLSXWriter::sanitize_filename(str_replace(' ','_',$filename)));
//$writer->writeToStdOut();
//exit(0);
$modul_name = explode("/", $_SERVER['REQUEST_URI']);

$total_length = count($modul_name);
$index_modul = $total_length-2;

$msg =  "<div class=\"alert alert-warning \" role=\"alert\">
        <font color=\"#3c763d\">".$jumlah_total." data Matakuliah berhasil di generate</font><br />";
        $msg .= "<a class='btn btn-block btn-primary btn-xs' href='".base_admin()."modul/".$modul_name[$index_modul]."/".XLSXWriter::sanitize_filename(str_replace(' ','_',$filename))."' target='_blank'><i class='fa  fa-cloud-download'></i> Download File</a></div>
        </div>";

        $jumlah['data_uri'] = SITE_ROOT."admina/modul/".$modul_name[$index_modul]."/".XLSXWriter::sanitize_filename(str_replace(' ','_',$filename));

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



array_push($json_response, $jumlah);

echo json_encode($json_response);
exit();
?>