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

class Pengumuman_setting extends CI_Controller {

	//load model
	public function __construct()
	{
		parent::__construct();
		$this->load->model('pengumuman_model');
    $this->load->model('biayaformulir_model'); 
	  $this->simple_login->check_login();
	    //proteksi admin dan peserta
		if($this->session->userdata('level') !='Administrator') 
		{
			if($this->session->userdata('level') !='Operator') 
			{
			//kalau bukan adamin, lempar ke login
			$this->session->set_flashdata('warning', 'Hak akses anda tidak diijinkan mengakses menu pengumuman');
			redirect(base_url('login'),'refresh');		
			}
		}

	}

	public function search(){
    // Ambil data pendaftar yang dikirim via ajax post
    $no_pendaftaran = $this->input->post('no_pendaftaran');    
    $siswa = $this->pengumuman_model->viewByNo($no_pendaftaran);
    
    if( ! empty($siswa)){ // Jika data siswa ada/ditemukan
      // Buat sebuah array
      $callback = array(
        'status'          => 'success', // Set array status dengan success
        'nama_lengkap'    => $siswa->nama_lengkap, // Set array nama 
        'sekolah_asal'    => $siswa->sekolah_asal, // Set array sekolah        
        'kompetensi'      => $siswa->kompetensi, // Set array kompetensi          
      );
    }else{
      $callback = array('status' => 'failed'); // set array status dengan failed
    }
      echo json_encode($callback); // konversi varibael $callback menjadi JSON   
    }


	public function index()
	{
		$pengumuman = $this->pengumuman_model->listing();
		$total = $this->pengumuman_model->total();
		

		//validasi input
		$valid = $this->form_validation;
		 //check nama
   	    $this->form_validation->set_rules('nama_lengkap', 'Data tidak dapat disimpan, nama peserta harus terisi ', 'required',
        array(  'required'    => '%s dengan menekan tombol cari terlebih dahulu'));

        $valid->set_rules('no_pendaftaran','Data tidak dapat disimpan, ','required|is_unique[pengumuman.no_pendaftaran]',
        array( 'required'    => '%s harus diisi',
             'is_unique'   => '%s nomor pendaftaran yang diinput sudah terdata kelulusannya jika ingin merubah silahkan di menu edit.'));   

		//jika sudah dicek dan error
		if($valid->run()===FALSE) {
		$pengumuman = array(	 'title'		=> 'Data Pengumuman Hasil Tes [ '.$total->total.' ]',
						 	 'pengumuman'		=> $pengumuman,						 	 
						 	 'content' 		=> 'pengumuman_setting/index'						 
						);
		$this->load->view('layout/wrapper', $pengumuman, FALSE);
		//jika validasi oke, masuk database
		}else{
			$extensi = '.pdf';
      
			$inp  = $this->input;			
			$data = array(	'no_pendaftaran'		=> $inp->post('no_pendaftaran'),								
							'status'				=> $inp->post('status'),
							'tanggal_pengumuman'	=> date('Y-m-d H:i:s'),
							'file_upload'			=> $inp->post('no_pendaftaran') .$extensi
						);
          
			$no_pendaftaran = $this->input->post('no_pendaftaran');    
			$data2 = array(	'userid'	=> $no_pendaftaran,
							'password'	      => '5ada545f85f69b85e6d7c848274f4da4',
							'nama'			      => $no_pendaftaran,
							'email'			      => 'user@gmail.com',
							'gambar'		      => 'user1.jpg',
							'level'			      => 'Peserta'
						);
			
			//proses oleh model
			$this->pengumuman_model->tambah($data);
      //proses oleh model
			$this->pengumuman_model->tambah2($data2);
			//notifikasi dan redirect
			$this->session->set_flashdata('sukses', 'Satu Data Pengumuman Kelulusan Telah Ditambah..!');
			redirect(base_url('pengumuman_setting'),'refresh');

		}	
	}

	//Edit user
	public function edit($no_pendaftaran)
	{
		//panggil data user yang akan diedit
		$pengumuman = $this->pengumuman_model->detail($no_pendaftaran);
		

		//validasi input
		$valid = $this->form_validation;
		//check nama
		$valid->set_rules('status','Data Pengumuman Kelulusan','required',
			array( 'required'		=> '%s Harus Diisi'));
		
		//jika sudah dicek dan error
		if($valid->run()===FALSE) {
		//end validasi

			$data= array(	'title'		=> 'Edit Data Hasil Tes No. Pend : '.$pengumuman->no_pendaftaran,
							  'pengumuman'	=> $pengumuman,
						  	'content' 	=> 'pengumuman_setting/edit'
						);
			$this->load->view('layout/wrapper', $data, FALSE);	
		//jika validasi oke, masuk database
		}else{
			$inp  = $this->input;
			$data = array(	'no_pendaftaran'	=> $no_pendaftaran,
              'status'				          => $inp->post('status'),
							'tanggal_pengumuman'	    => date('Y-m-d H:i:s')
						);
			//proses oleh model
			$this->pengumuman_model->edit($data);
			//notifikasi dan redirect
			$this->session->set_flashdata('sukses', 'Data Pengumuman Kelulusan No. Pend : '.$pengumuman->no_pendaftaran.' Telah Diedit.');
			redirect(base_url('pengumuman_setting'),'refresh');
		}
		//end masuk database
	}
	
	//Delete pengumuman
	public function delete($no_pendaftaran)
	{
		$data = array('no_pendaftaran' => $no_pendaftaran);
    $data2 = array('userid' => $no_pendaftaran);
		//proses hapus
		$this->pengumuman_model->delete($data);
    $this->pengumuman_model->delete2($data2);
		//notifikasi
		$this->session->set_flashdata('sukses', 'Satu Data Pengumuman Telah Dihapus..!');
		redirect(base_url('pengumuman_setting'),'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 Berkas")
                 ->setSubject("Berkas")
                 ->setDescription("Laporan Data Berkas")
                 ->setKeywords("Data Berkas");
    // 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', "LAPORAN REKAP PENYERAHAN BERKAS PPDB SMK AL-MUHTADIN DEPOK"); // Set kolom A1 dengan tulisan "DATA SISWA"
    $excel->getActiveSheet()->mergeCells('A1:P1'); // 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', "JENIS KELAMIN"); // Set kolom D3 dengan tulisan "JENIS KELAMIN"
    $excel->setActiveSheetIndex(0)->setCellValue('E3', "SEKOLAH ASAL"); // Set kolom E3 dengan tulisan "SEKOLAH"
    $excel->setActiveSheetIndex(0)->setCellValue('F3', "KOMPETENSI");
    $excel->setActiveSheetIndex(0)->setCellValue('G3', "GELOMBANG");
    $excel->setActiveSheetIndex(0)->setCellValue('H3', "FORMULIR");
    $excel->setActiveSheetIndex(0)->setCellValue('I3', "AKTE");
    $excel->setActiveSheetIndex(0)->setCellValue('J3', "KK");
    $excel->setActiveSheetIndex(0)->setCellValue('K3', "KTP ORTU");
    $excel->setActiveSheetIndex(0)->setCellValue('L3', "NISN");
    $excel->setActiveSheetIndex(0)->setCellValue('M3', "BAIK & NARKOBA");
    $excel->setActiveSheetIndex(0)->setCellValue('N3', "KET. YATIM");
    $excel->setActiveSheetIndex(0)->setCellValue('O3', "KIP");
    $excel->setActiveSheetIndex(0)->setCellValue('P3', "SALIN SLIP");
    $excel->setActiveSheetIndex(0)->setCellValue('Q3', "SKL");   
    $excel->setActiveSheetIndex(0)->setCellValue('R3', "IJAZAH");
    $excel->setActiveSheetIndex(0)->setCellValue('S3', "TGL. UPDATE");
    $excel->setActiveSheetIndex(0)->setCellValue('T3', "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);
    $excel->getActiveSheet()->getStyle('M3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('N3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('O3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('P3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('Q3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('R3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('S3')->applyFromArray($style_col);
    $excel->getActiveSheet()->getStyle('T3')->applyFromArray($style_col);
   
    // Panggil function view yang ada di formulir_model untuk menampilkan semua data formulirnya
    $pengumuman = $this->formulir_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($pengumuman as $data){ // Lakukan looping pada variabel pengumuman
      $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->jkelamin);
      $excel->setActiveSheetIndex(0)->setCellValue('E'.$numrow, $data->sekolah_asal);
      $excel->setActiveSheetIndex(0)->setCellValue('F'.$numrow, $data->kd_kompetensi);
      $excel->setActiveSheetIndex(0)->setCellValue('G'.$numrow, $data->gelombang);
      $excel->setActiveSheetIndex(0)->setCellValue('H'.$numrow, $data->shun);
      $excel->setActiveSheetIndex(0)->setCellValue('I'.$numrow, $data->akte);
      $excel->setActiveSheetIndex(0)->setCellValue('J'.$numrow, $data->kk);
      $excel->setActiveSheetIndex(0)->setCellValue('K'.$numrow, $data->ktp);
      $excel->setActiveSheetIndex(0)->setCellValue('L'.$numrow, $data->nisn);
      $excel->setActiveSheetIndex(0)->setCellValue('M'.$numrow, $data->baik_narkoba);
      $excel->setActiveSheetIndex(0)->setCellValue('N'.$numrow, $data->ket_yatim);
      $excel->setActiveSheetIndex(0)->setCellValue('O'.$numrow, $data->kip);
      $excel->setActiveSheetIndex(0)->setCellValue('P'.$numrow, $data->slip_btn);
      $excel->setActiveSheetIndex(0)->setCellValue('Q'.$numrow, $data->skl);
      $excel->setActiveSheetIndex(0)->setCellValue('R'.$numrow, $data->ijazah);
      $excel->setActiveSheetIndex(0)->setCellValue('S'.$numrow, $data->tgl_update);
      $excel->setActiveSheetIndex(0)->setCellValue('T'.$numrow, $data->petugas);
     
      
      // 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);
      $excel->getActiveSheet()->getStyle('M'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('N'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('O'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('P'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('Q'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('R'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('S'.$numrow)->applyFromArray($style_row);
      $excel->getActiveSheet()->getStyle('T'.$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(15); // Set width kolom D
    $excel->getActiveSheet()->getColumnDimension('E')->setWidth(35); // Set width kolom E
    $excel->getActiveSheet()->getColumnDimension('F')->setWidth(13);
    $excel->getActiveSheet()->getColumnDimension('G')->setWidth(13);
    $excel->getActiveSheet()->getColumnDimension('H')->setWidth(12);
    $excel->getActiveSheet()->getColumnDimension('I')->setWidth(12);
    $excel->getActiveSheet()->getColumnDimension('J')->setWidth(12);
    $excel->getActiveSheet()->getColumnDimension('K')->setWidth(12);
    $excel->getActiveSheet()->getColumnDimension('L')->setWidth(12);
    $excel->getActiveSheet()->getColumnDimension('M')->setWidth(16);
    $excel->getActiveSheet()->getColumnDimension('N')->setWidth(12);
    $excel->getActiveSheet()->getColumnDimension('O')->setWidth(12);
    $excel->getActiveSheet()->getColumnDimension('P')->setWidth(12);
    $excel->getActiveSheet()->getColumnDimension('Q')->setWidth(12);
    $excel->getActiveSheet()->getColumnDimension('R')->setWidth(12);
    $excel->getActiveSheet()->getColumnDimension('S')->setWidth(18);
    $excel->getActiveSheet()->getColumnDimension('T')->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("Rekap Penyerahan Berkas PPDB");
    $excel->setActiveSheetIndex(0);
    // Proses file excel
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment; filename="Rekap Penyerahan Berkas PPDB.xlsx"'); // Set nama file excel nya
    header('Cache-Control: max-age=0');
    $write = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
    $write->save('php://output');
  }

  public function cetak(){
        $pdf = new FPDF('p','mm','A4');
        // membuat halaman baru
        $pdf->AddPage();
        // setting jenis font yang akan digunakan
        $pdf->SetFont('Arial','B',16);
        // mencetak string 
        $pdf->Cell(190,7,'LAPORAN REKAP DATA BERKAS PPDB',0,1,'C');
        $pdf->SetFont('Arial','B',12);
        $pdf->Cell(190,7,'SMK AL-MUHTADIN DEPOK',0,1,'C');
        // Memberikan space kebawah agar tidak terlalu rapat
        $pdf->Cell(10,7,'',0,1);
        $pdf->SetFont('Arial','B',10);            
        $pdf->Cell(10,6,'NO.',1,0);
        $pdf->Cell(20,6,'NO. PEND',1,0);
        $pdf->Cell(85,6,'NAMA SISWA',1,0);
        $pdf->Cell(30,6,'TEMPAT LAHIR',1,0);
        $pdf->Cell(33,6,'TANGGAL LAHIR',1,1);
        $pdf->SetFont('Arial','',10);
        $siswa = $this->db->get('data_siswa')->result();
        $no = 1;      
        foreach ($siswa as $row){
            $pdf->Cell(10,6,$no++,1,0);
            $pdf->Cell(20,6,$row->no_pendaftaran,1,0);
            $pdf->Cell(85,6,$row->nama_lengkap,1,0);
            $pdf->Cell(30,6,$row->tempat_lahir,1,0);
            $pdf->Cell(33,6,$row->tgl_lahir,1,1); 
        }
        $pdf->Output();
  }

  public function cetakpengumuman($no_pendaftaran)
	{
		$data['data'] = $this->db->get_where('v_rpt_trans_berkas',['no_pendaftaran'=>$no_pendaftaran])->row();
    $this->load->view('pengumuman/cetak_berkas',$data);
	}

}

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