<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 
Class Sertifikat extends CI_Controller{

	function __Construct()
    {
         parent ::__construct();
		$this->load->model('m_sertifikat');
		$this->load->model('m_acara');//wa dan email
		$this->load->helper('tgl_indonesia');//helper tanggal indonesia
    	$this->is_logged_in();
	}
	
	public function is_logged_in(){
	
		$is_logged_in=$this->session->userdata('is_logged_in');
		if(!isset($is_logged_in)||$is_logged_in!= true) {
		redirect(base_url());
		} 
	}
	
	
	function ubah($id_tamu,$id_acara)	
    {
		$nama=$this->input->post('nama');
		$unit_kerja=$this->input->post('unit_kerja');
		$no_hp=$this->input->post('no_hp');
		$email=$this->input->post('email');
		
		$result =$this->db->query("SELECT * FROM tbl_registrasi
		where id_tamu=$id_tamu");
		foreach ($result->result_array() as $x)
		$unik=$x['unik'];
		
		
		$result =$this->db->query("SELECT * FROM tbl_acara
		where id_acara=$id_acara");
		foreach ($result->result_array() as $s)
		$acara=$s['nama_acara'];
		$tgl_acara=tgl_indo($s['tgl_acara']);
		$link_acara=$s['link_acara'];
		
		//Upload sertifikat
			$file = $_FILES['userfile']['name'];
			if (empty($file)) {
			$this->m_sertifikat->simpan_ubah($id_tamu,$id_acara);//tanpa file
			$this->session->set_flashdata('message', '<div class="alert alert-success alert-dismissible" role="alert">
				  <button type="button" class="close" data-dismiss="alert" aria-label="Close">
				   <span aria-hidden="true">&times;</span>
				  </button>
				  <strong>Anda berhasil mengubah data</strong>
				</div>');	
			redirect('c_acara/verifikasi/'.$id_acara.'');
			}else{
			$this->db->where('id_tamu',$id_tamu);
			$gambar=$this->db->get('tbl_registrasi');
			if($gambar->num_rows()>0) {
				$row = $gambar->row();
				unlink('./cetak_sertifikat/'.$row->unik.'.pdf');
			}
			
			$nama_file=''.$unik.'.pdf';
			
			
	    $config = array(
            'allowed_types' => '*',
            'upload_path' => realpath('./cetak_sertifikat/'),
	    'file_name'=>$nama_file,
            'max_size' => 50000
            );
            $this->load->library('upload', $config);
            $this->upload->do_upload();
            $file1 = $this->upload->data();
            $userfile = $file1['file_name'];
			//upload sertifikat
			$this->m_sertifikat->simpan_ubah_file($id_tamu,$id_acara,$userfile);
			}
		//Pengubahan jika sudah diupload
			
		$sts_sertifikat=$this->input->post('sts_sertifikat');
		if($sts_sertifikat==1){
		
		$sts_pesan=$this->input->post('sts_pesan');
		if($sts_pesan==1){
		$nomor=$no_hp;
		$pesan='Yth  *'.$nama.'* Sertifikat kegiatan '.$acara.' tanggal '.$tgl_acara.' sudah bisa di download melalui link berikut '.base_url().'verify/no/'.$unik.' pada tombol CETAK SERTIFIKAT
		Jika terdapat kesalahan bisa menghubungi panitia kegiatan tersebut , Terimakasih';
		
		
		$this->m_acara->kirimWa($nomor,$pesan);
		
		//KIRIM EMAIL
		$subject= 'Download Sertifikat telah tersedia';
		$nm_tujuan= 'Yth. '.$nama.'';
		$isi=' Sertifikat kegiatan '.$acara.' tanggal '.$tgl_acara.' sudah bisa di download melalui link berikut '.base_url().'verify/no/'.$unik.' pada tombol CETAK SERTIFIKAT
		Jika terdapat kesalahan bisa menghubungi panitia kegiatan tersebut , Terimakasih';
		$email_tujuan=$email;
		//$link_file='tes.txt';
		
		
		$this->m_acara->kirimEMAIL($subject,$nm_tujuan,$isi,$email_tujuan);
			}
		}
		
		redirect('c_acara/verifikasi/'.$id_acara.'');
    }
	
	//Hapus Data
	function hapus_peserta($id_acara,$id_tamu)
	{
		//untuk delete datanya menuju model delete
		$this->m_sertifikat->hapus_peserta($id_acara,$id_tamu); 
		$this->session->set_flashdata('message', '<div class="alert alert-danger alert-dismissible" role="alert">
				  <button type="button" class="close" data-dismiss="alert" aria-label="Close">
				   <span aria-hidden="true">&times;</span>
				  </button>
				  <strong>Data berhasil dihapus</strong>
				</div>');
		redirect('c_acara/verifikasi/'.$id_acara.'');
	}

	function cari_peserta()
	{
		
		$data['daftar_hadir']   = $this->m_sertifikat->cari();
		if($data['daftar_hadir']==null) 
		{
			//Konfigurasi title, header
		$data['title']='E-Sertifikat';
		$data['menu']='Data Verifikasi';
		$data['submenu']='Data Verifikasi';
		$data['header']='Aplikasi E-Sertifikat';
		$data['subheader']='Data Verifikasi';
		$data['top_navbar']='menu/top';
		$data['left_navbar']='menu/left';
		$data['notifikasi'] = '
				<center><div class="alert alert-danger">
                <a class="close" data-dismiss="alert" href="#">&times;</a>
                <h3>Peringatan !</h3>
                Data yang anda cari tidak ada atau keywordnya salah<br>
				<a href="javascript:history.back()" class="btn btn-xs btn-warning" type="reset" data-action="collapse">
					<i class="ace-icon fa fa-undo bigger-110"></i>
					Kembali
				</button></a>
				</center>
             </div>';
			$data['content']='admin/notifikasi';
		$data['footer']='menu/bottom';
		$this->load->view('admin/tmp',$data);
			
		}else {
			
		$data['title']='E-Sertifikat';
		$data['menu']='Data Verifikasi';
		$data['submenu']='Data Verifikasi';
		$data['header']='Aplikasi E-Sertifikat';
		$data['subheader']='Data Verifikasi';
		$data['top_navbar']='menu/top';
		$data['left_navbar']='menu/left';
		$data['content']='modul/acara/data_verifikasi';
		$data['footer']='menu/bottom';
		$this->load->view('admin/tmp',$data);	
			}
	}
	
}