<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Registrasi extends CI_Controller {

	//load model
	public function __construct()
	{
		parent::__construct();
		$this->load->model('registrasi_model');
		$this->simple_login->check_login();
	}

	public function index()
	{
		$registrasi = $this->registrasi_model->listing();
		$total = $this->registrasi_model->total();

		$registrasi = array(	 'title'		=> 'Registrasi PPDB/Salin Slip [ '.$total->total.' ]',
						 		'registrasi'	=> $registrasi,
						 		'content' 		=> 'registrasi/index'						 
						);
		$this->load->view('layout/wrapper', $registrasi, FALSE);	
	}

  //Edit user
  public function edit($no_trans)
  {
    //panggil data user yang akan diedit
    $registrasi = $this->registrasi_model->detail($no_trans);
    

    //validasi input
    $valid = $this->form_validation;
    //check nama
    $valid->set_rules('bayar','Pembayaran','required',
      array( 'required'   => '%s harus diisi'));
    
    //jika sudah dicek dan error
    if($valid->run()===FALSE) {
    //end validasi

      $data= array( 'title'   => 'Edit Data Registrasi, No. Transaksi : '.$registrasi->no_trans,
                    'registrasi'  => $registrasi,
                    'content'   => 'registrasi/edit'
            );
      $this->load->view('layout/wrapper', $data, FALSE);  
    //jika validasi oke, masuk database
    }else{
      $inp  = $this->input;
      $data = array(  'no_trans'  => $no_trans,                             
              'bayar'        => $inp->post('bayar')              
            );
      //proses oleh model
      $this->registrasi_model->edit($data);
      //notifikasi dan redirect
      $this->session->set_flashdata('sukses', 'Data Registrasi/Salin Slip '.$registrasi->no_trans.' - '.$registrasi->angsuran.' atas nama '.$registrasi->nama_lengkap.' Telah Diedit');
      redirect(base_url('registrasi'),'refresh');
    }
    //end masuk database
  }

  //Delete regis
  public function delete($no_trans)
  {
    $data = array('no_trans' => $no_trans);
    //proses hapus
    $this->registrasi_model->delete($data);
    //notifikasi
    $this->session->set_flashdata('sukses', 'Data registrasi telah dihapus');
    redirect(base_url('registrasi'),'refresh');
  }

	//EXPORT NILAI KE EXCEL
	public function export(){
    // Load plugin PHPExcel nya
    include APPPATH.'third_party/PHPExcel/PHPExcel.php';
    
    // Panggil class PHPExcel nya
    $excel = new PHPExcel();
    // Settingan awal fil excel
    $excel->getProperties()->setCreator('By : 415 DIL')
                 ->setLastModifiedBy('By : 415 DIL')
                 ->setTitle("Data Registrasi")
                 ->setSubject("Registrasi")
                 ->setDescription("Laporan Data Registrasi")
                 ->setKeywords("Data Registrasi");
    // Buat sebuah variabel untuk menampung pengaturan style dari header tabel
    $style_col = array(
      'font' => array('bold' => true), // Set font nya jadi bold
      'alignment' => array(
        'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER, // Set text jadi ditengah secara horizontal (center)
        'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER // Set text jadi di tengah secara vertical (middle)
      ),
      'borders' => array(
        'top' => array('style'  => PHPExcel_Style_Border::BORDER_THIN), // Set border top dengan garis tipis
        'right' => array('style'  => PHPExcel_Style_Border::BORDER_THIN),  // Set border right dengan garis tipis
        'bottom' => array('style'  => PHPExcel_Style_Border::BORDER_THIN), // Set border bottom dengan garis tipis
        'left' => array('style'  => PHPExcel_Style_Border::BORDER_THIN) // Set border left dengan garis tipis
      )
    );
    // Buat sebuah variabel untuk menampung pengaturan style dari isi tabel
    $style_row = array(
      'alignment' => array(
        'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER // Set text jadi di tengah secara vertical (middle)
      ),
      'borders' => array(
        'top' => array('style'  => PHPExcel_Style_Border::BORDER_THIN), // Set border top dengan garis tipis
        'right' => array('style'  => PHPExcel_Style_Border::BORDER_THIN),  // Set border right dengan garis tipis
        'bottom' => array('style'  => PHPExcel_Style_Border::BORDER_THIN), // Set border bottom dengan garis tipis
        'left' => array('style'  => PHPExcel_Style_Border::BORDER_THIN) // Set border left dengan garis tipis
      )
    );
    $excel->setActiveSheetIndex(0)->setCellValue('A1', "DATA REKAP REGISTRASI/SALIN SLIP PPDB SMK AL-MUHTADIN DEPOK"); // Set kolom A1 dengan tulisan "DATA SISWA"
    $excel->getActiveSheet()->mergeCells('A1:L1'); // Set Merge Cell pada kolom A1 sampai E1
    $excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(TRUE); // Set bold kolom A1
    $excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(15); // Set font size 15 untuk kolom A1
    $excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); // Set text center untuk kolom A1
    // Buat header tabel nya pada baris ke 3
    $excel->setActiveSheetIndex(0)->setCellValue('A3', "NO."); // Set kolom A3 dengan tulisan "NO"
    $excel->setActiveSheetIndex(0)->setCellValue('B3', "NO. PEND"); // Set kolom B3 dengan tulisan "NO.PEND"
    $excel->setActiveSheetIndex(0)->setCellValue('C3', "NAMA LENGKAP"); // Set kolom C3 dengan tulisan "NAMA"    
    $excel->setActiveSheetIndex(0)->setCellValue('D3', "SEKOLAH ASAL"); // Set kolom E3 dengan tulisan "SEKOLAH"
    $excel->setActiveSheetIndex(0)->setCellValue('E3', "KOMPETENSI");
    $excel->setActiveSheetIndex(0)->setCellValue('F3', "GELOMBANG");
    $excel->setActiveSheetIndex(0)->setCellValue('G3', "TGL. SALIN");
    $excel->setActiveSheetIndex(0)->setCellValue('H3', "NO. TRANSAKSI");
    $excel->setActiveSheetIndex(0)->setCellValue('I3', "KETENTUAN BIAYA");
    $excel->setActiveSheetIndex(0)->setCellValue('J3', "KET. ANGSURAN");
    $excel->setActiveSheetIndex(0)->setCellValue('K3', "SUDAH DIBAYARKAN");
    $excel->setActiveSheetIndex(0)->setCellValue('L3', "PETUGAS");
       
   
    // Apply style header yang telah kita buat tadi ke masing-masing kolom header
    $excel->getActiveSheet()->getStyle('A3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('B3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('C3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('D3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('E3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('F3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('G3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('H3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('I3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('J3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('K3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('L3')->applyFromArray($style_col);
    
   
    // Panggil function view yang ada di registrasi_model untuk menampilkan semua data registrasinya
    $registrasi = $this->registrasi_model->ViewUrut();
    $no = 1; // Untuk penomoran tabel, di awal set dengan 1
    $numrow = 4; // Set baris pertama untuk isi tabel adalah baris ke 4
    foreach($registrasi as $data){ // Lakukan looping pada variabel registrasi
      $excel->setActiveSheetIndex(0)->setCellValue('A'.$numrow, $no);
      $excel->setActiveSheetIndex(0)->setCellValue('B'.$numrow, $data->no_pendaftaran);
      $excel->setActiveSheetIndex(0)->setCellValue('C'.$numrow, $data->nama_lengkap);
      $excel->setActiveSheetIndex(0)->setCellValue('D'.$numrow, $data->sekolah_asal);
      $excel->setActiveSheetIndex(0)->setCellValue('E'.$numrow, $data->kd_kompetensi);
      $excel->setActiveSheetIndex(0)->setCellValue('F'.$numrow, $data->gelombang);
      $excel->setActiveSheetIndex(0)->setCellValue('G'.$numrow, $data->tgl_bayar);      
      $excel->setActiveSheetIndex(0)->setCellValue('H'.$numrow, $data->no_trans);
      $excel->setActiveSheetIndex(0)->setCellValue('I'.$numrow, $data->ketentuan);
      $excel->setActiveSheetIndex(0)->setCellValue('J'.$numrow, $data->angsuran);
      $excel->setActiveSheetIndex(0)->setCellValue('K'.$numrow, $data->bayar);
      $excel->setActiveSheetIndex(0)->setCellValue('L'.$numrow, $data->operator);
           
      
      // Apply style row yang telah kita buat tadi ke masing-masing baris (isi tabel)
      $excel->getActiveSheet()->getStyle('A'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('B'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('C'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('D'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('E'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('F'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('G'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('H'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('I'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('J'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('K'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('L'.$numrow)->applyFromArray($style_row);
          
      
      $no++; // Tambah 1 setiap kali looping
      $numrow++; // Tambah 1 setiap kali looping
    }
    // Set width kolom
    $excel->getActiveSheet()->getColumnDimension('A')->setWidth(5); // Set width kolom A
    $excel->getActiveSheet()->getColumnDimension('B')->setWidth(12); // Set width kolom B
    $excel->getActiveSheet()->getColumnDimension('C')->setWidth(35); // Set width kolom C
    $excel->getActiveSheet()->getColumnDimension('D')->setWidth(35); // Set width kolom D
    $excel->getActiveSheet()->getColumnDimension('E')->setWidth(13); // Set width kolom E
    $excel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
    $excel->getActiveSheet()->getColumnDimension('G')->setWidth(13);
    $excel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
    $excel->getActiveSheet()->getColumnDimension('I')->setWidth(18);
    $excel->getActiveSheet()->getColumnDimension('J')->setWidth(16);
    $excel->getActiveSheet()->getColumnDimension('K')->setWidth(19);
    $excel->getActiveSheet()->getColumnDimension('L')->setWidth(15);
    
    
    // Set height semua kolom menjadi auto (mengikuti height isi dari kolommnya, jadi otomatis)
    $excel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(-1);
    // Set orientasi kertas jadi LANDSCAPE
    $excel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
    // Set judul file excel nya
    $excel->getActiveSheet(0)->setTitle("Data Rekap Registrasi PPDB");
    $excel->setActiveSheetIndex(0);
    // Proses file excel
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment; filename="Data Rekap Registrasi PPDB.xlsx"'); // Set nama file excel nya
    header('Cache-Control: max-age=0');
    $write = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
    $write->save('php://output');
  }

}

/* End of file registrasi.php */
/* Location: ./application/controllers/registrasi.php */