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

	function __Construct()
    {
        parent ::__construct();
		$this->load->model('m_acara');
		$this->load->library('excel');
		$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 upload_bgdepan()
	{
			$id_acara=$this->input->post('id_acara');
			$nama='bgdepan';
			$hubung='_';

			//Hapus dulu yang lama 
			$this->db->where('id_acara',$id_acara);
			$gambar=$this->db->get('tbl_acara');
			
			if($gambar->num_rows()>0) {
				$row = $gambar->row();
				unlink('./sertifikat/'.$row->gbr_sert_depan);
			}

			$config = array(
			'allowed_types' => '*',
			'upload_path' => realpath('./sertifikat/'),
			'file_name'=>$nama.$hubung.$id_acara,
			'max_size' => 50000
			);

			$this->load->library('upload', $config);
			$this->upload->do_upload();
			$file1 = $this->upload->data();
			$userfile = $file1['file_name'];
			//Ubah nama file di database
				$data = array(
				'gbr_sert_depan'   				=>$userfile
				);
			
			$this->db->where('id_acara', $id_acara);
			$this->db->update('tbl_acara', $data);
			//pesan
			$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>Data berhasil disimpan</strong>
				</div>');
			redirect('c_acara');
		
	}

	function upload_bgblk()
	{
			$id_acara=$this->input->post('id_acara');
			$nama='bgblk';
			$hubung='_';

			//Hapus dulu yang lama 
			$this->db->where('id_acara',$id_acara);
			$gambar=$this->db->get('tbl_acara');
			
			if($gambar->num_rows()>0) {
				$row = $gambar->row();
				unlink('./sertifikat/'.$row->gbr_sert_blk);
			}

			$config = array(
			'allowed_types' => '*',
			'upload_path' => realpath('./sertifikat/'),
			'file_name'=>$nama.$hubung.$id_acara,
			'max_size' => 50000
			);

			$this->load->library('upload', $config);
			$this->upload->do_upload();
			$file1 = $this->upload->data();
			$userfile = $file1['file_name'];
			//Ubah nama file di database
				$data = array(
				'gbr_sert_blk'   				=>$userfile
				);
			
			$this->db->where('id_acara', $id_acara);
			$this->db->update('tbl_acara', $data);
			//pesan
			$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>Data berhasil disimpan</strong>
				</div>');
			redirect('c_acara');
		
	}

	function upload_cap()
	{
			$id_acara=$this->input->post('id_acara');
			$nama='cap';
			$hubung='_';

			//Hapus dulu yang lama 
			$this->db->where('id_acara',$id_acara);
			$gambar=$this->db->get('tbl_acara');
			
			if($gambar->num_rows()>0) {
				$row = $gambar->row();
				unlink('./ttd/'.$row->cap);
			}

			$config = array(
			'allowed_types' => '*',
			'upload_path' => realpath('./ttd/'),
			'file_name'=>$nama.$hubung.$id_acara,
			'max_size' => 50000
			);

			$this->load->library('upload', $config);
			$this->upload->do_upload();
			$file1 = $this->upload->data();
			$userfile = $file1['file_name'];
			//Ubah nama file di database
				$data = array(
				'cap'   				=>$userfile
				);
			
			$this->db->where('id_acara', $id_acara);
			$this->db->update('tbl_acara', $data);
			//pesan
			$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>Data berhasil disimpan</strong>
				</div>');
			redirect('c_acara');
		
	}

	function upload_ttd()
	{
			$id_acara=$this->input->post('id_acara');
			$nama='ttd';
			$hubung='_';

			//Hapus dulu yang lama 
			$this->db->where('id_acara',$id_acara);
			$gambar=$this->db->get('tbl_acara');
			
			if($gambar->num_rows()>0) {
				$row = $gambar->row();
				unlink('./ttd/'.$row->cap);
			}

			$config = array(
			'allowed_types' => '*',
			'upload_path' => realpath('./ttd/'),
			'file_name'=>$nama.$hubung.$id_acara,
			'max_size' => 50000
			);

			$this->load->library('upload', $config);
			$this->upload->do_upload();
			$file1 = $this->upload->data();
			$userfile = $file1['file_name'];
			//Ubah nama file di database
				$data = array(
				'ttd'   				=>$userfile
				);
			
			$this->db->where('id_acara', $id_acara);
			$this->db->update('tbl_acara', $data);
			//pesan
			$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>Data berhasil disimpan</strong>
				</div>');
			redirect('c_acara');
		
	}



	//Input File
	function uploud_template($id_acara){

		
		$nama='template';
		$hubung='_';


		$this->db->where('id_acara',$id_acara);
		$gambar=$this->db->get('tbl_acara');
		
		if($gambar->num_rows()>0) {
			$row = $gambar->row();
			unlink('./application/views/modul/sertifikat/'.$row->template);
			
		}
	//upload desain sertifikat
		$config = array(
		'allowed_types' => '*',
		'upload_path' => realpath('./application/views/modul/sertifikat/'),
		'file_name'=>$nama.$hubung.$id_acara,
		'max_size' => 50000
		);
		
		$this->load->library('upload', $config);
		$this->upload->do_upload('userfile');
		$userfile = $this->upload->data();
		$userfile = $userfile['file_name'];
		

		
		$data = array(
		'template'   				=>$userfile
		);
		$this->db->where('id_acara', $id_acara);
		$this->db->update('tbl_acara', $data);
		//pesan
		$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>Data berhasil disimpan</strong>
	  </div>');
		redirect('c_acara');
	
	
}

	//Tampilkan data
	function index()	
    {
		$config = array(                   
                   'base_url'    => site_url() . '/c_acara/index',
                   'total_rows'  => $this->db->count_all('tbl_acara'),
				   'first_link' =>'Awal',
				   'last_link'  =>'Akhir',
                   'per_page'    => 10,
                   'uri_segment' => 3
                   );
		$this->pagination->initialize($config); 
		$data["num"]=$this->uri->segment(3);
		$data['acara']   = $this->m_acara->tampil($config['per_page'],$this->uri->segment(3));
		
		$data['title']='Aplikasi Letter of Acceptance';
		$data['menu']='Nama Kegiatan';
		$data['submenu']='Daftar Nama Kegiatan';
		$data['header']='Aplikasi Letter of Acceptance';
		$data['subheader']='Daftar Nama Kegiatan';
		$data['top_navbar']='menu/top';
		$data['left_navbar']='menu/left';
		$data['content']='modul/acara/acara';
		$data['footer']='menu/bottom';
		$this->load->view('admin/tmp',$data);	
		
    }
	
	//Input File
	function tbh(){
		if($_POST == NULL) {
		$data['title']='Aplikasi Letter of Acceptance';
		$data['menu']='Nama Kegiatan';
		$data['submenu']='Tambah Nama Kegiatan';
		$data['header']='Aplikasi Letter of Acceptance';
		$data['subheader']='Tambah Nama Kegiatan';
		$data['top_navbar']='menu/top';
		$data['left_navbar']='menu/left';
		$data['content']='modul/acara/tbh_acara';
		$data['footer']='menu/bottom';
		$this->load->view('admin/tmp',$data);	
		
		}else{
			
			//upload gambar
			$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>Data berhasil disimpan</strong>
				</div>');
			$this->m_acara->simpan_acara();
			redirect('c_acara');
		}
		
	}




	function upload_materi()
	{
			$config = array(
			'allowed_types' => '*',
			'upload_path' => realpath('./materi/'),
			'max_size' => 50000
			);
			$this->load->library('upload', $config);
			$this->upload->do_upload();
			$file1 = $this->upload->data();
			$userfile = $file1['file_name'];
			$this->m_acara->upload_materi($userfile);
			redirect('c_acara');
		
	}
	
	
	
	//Hapus Data
	function hapus_materi($id_program,$id_acara)
	{
		$this->db->where('id_program', $id_program);
		$this->db->where('id_acara',$id_acara);
		$gambar=$this->db->get('tbl_program_acara');
		
		if($gambar->num_rows()>0) {
			$row = $gambar->row();
			unlink('./materi/'.$row->link_file);
		}
		
		//untuk delete datanya menuju model delete
		$this->m_acara->delete_program($id_program,$id_acara); 
		redirect('c_acara');
	}
	
	
	


	
	
	//Edit Data
	function ubah($id_acara = null)
	{		
		if($_POST == NULL) {
			$data['edit'] = $this->m_acara->select($id_acara);// 
			
			$data['title']='Aplikasi Letter of Acceptance';
			$data['menu']='Nama Kegiatan';
			$data['submenu']='Ubah Nama Kegiatan';
			$data['header']='Aplikasi Letter of Acceptance';
			$data['subheader']='Ubah Nama Kegiatan';
			$data['top_navbar']='menu/top';
			$data['left_navbar']='menu/left';
			$data['content']='modul/acara/ed_acara';
			$data['footer']='menu/bottom';
			$this->load->view('admin/tmp',$data);	
			
		}else{
			
			$this->m_acara->simpan_edit($id_acara);
			$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>Data berhasil diubah</strong>
				</div>');
			redirect('c_acara');
			
		}
	}
	
	//Hapus Data
	function hapus($id_acara)
	{
		$this->db->where('id_acara',$id_acara);
			$gambar=$this->db->get('tbl_acara');
			
			if($gambar->num_rows()>0) {
				$row = $gambar->row();
				unlink('./sertifikat/'.$row->gbr_sert_depan);
				unlink('./sertifikat/'.$row->gbr_sert_blk);
			}
		//untuk delete datanya menuju model delete
		$this->m_acara->delete($id_acara);//delete  di tabel 
		$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>Data berhasil diubah</strong>
				</div>');
		redirect('c_acara');
	}
	
	
	function download($id_acara)
	{
		$data['daftar_hadir']   = $this->m_acara->download($id_acara);
		$data['keterangan']   ='DATA SELURUH PESERTA';
		$this->load->view('modul/acara/daftar_hadir',$data);
	}
	
	function download_cadangan($id_acara)
	{
		$data['daftar_hadir']   = $this->m_acara->cadangan($id_acara);
		$data['keterangan']   ='DATA CADANGAN';
		$this->load->view('modul/acara/daftar_hadir',$data);
	}
	
	function download_verifikasi($id_acara)
	{
		$data['daftar_hadir']   = $this->m_acara->verifikasi($id_acara);
		$data['keterangan']   ='DATA YANG MELAKUKAN VERIFIKASI';
		$this->load->view('modul/acara/daftar_hadir',$data);
	}
	
	function download_verifikasi_word($id_acara)
	{
		$data['daftar_hadir']   = $this->m_acara->verifikasi($id_acara);
		$data['keterangan']   ='DATA SERTIFIKAT';
		$this->load->view('modul/acara/daftar_hadir_word',$data);
	}
	
	function lihat($id_acara)
	{
		$data['daftar_hadir']   = $this->m_acara->download($id_acara);
		
		$data['id_acara']=$id_acara;//tambahan
		
		$data['title']='Aplikasi Letter of Acceptance';
		$data['menu']='Data Pesrta';
		$data['submenu']='Data Pesrta';
		$data['header']='Aplikasi Letter of Acceptance';
		$data['subheader']='Data Pesrta';
		$data['top_navbar']='menu/top';
		$data['left_navbar']='menu/left';
		$data['content']='modul/acara/data_peserta';
		$data['footer']='menu/bottom';
		$this->load->view('admin/tmp',$data);	
	}
	
	function kosongkan_peserta($id_acara)
	{
		
		//untuk delete datanya menuju model kosongkan
		$this->m_acara->kosongkan_peserta($id_acara);//delete  di tabel 
		redirect('c_acara/lihat/'.$id_acara.'');
	}
	
	function cadangan($id_acara)
	{
		$data['daftar_hadir']   = $this->m_acara->cadangan($id_acara);
		
		$data['id_acara']=$id_acara;//tambahan
		
		$data['title']='Aplikasi Letter of Acceptance';
		$data['menu']='Data Cadangan';
		$data['submenu']='Data Cadangan';
		$data['header']='Aplikasi Letter of Acceptance';
		$data['subheader']='Data Cadangan';
		$data['top_navbar']='menu/top';
		$data['left_navbar']='menu/left';
		$data['content']='modul/acara/data_cadangan';
		$data['footer']='menu/bottom';
		$this->load->view('admin/tmp',$data);	
	}
	
	function verifikasi($id_acara)
	{
		$data['daftar_hadir']   = $this->m_acara->verifikasi($id_acara);
		
		$data['id_acara']=$id_acara;//tambahan
		
		$data['title']='Aplikasi Letter of Acceptance';
		$data['menu']='Data Verifikasi';
		$data['submenu']='Data Verifikasi';
		$data['header']='Aplikasi Letter of Acceptance';
		$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);	
	}
	
	function pindah_peserta($id_tamu,$id_acara)
	{
		
		$this->m_acara->simpan_pindah($id_tamu,$id_acara);
		redirect('c_acara');
	}
	
	function pindah_cadangan($id_tamu,$id_acara)
	{
		
		$this->m_acara->simpan_pindah_cadangan($id_tamu,$id_acara);
		redirect('c_acara');
	}
	
	//=================
	
	function tbh_exel()				
    { 		
			//Konfigurasi title, header
			$data['title']='Aplikasi Letter of Acceptance';
			$data['menu']='Broadcast';
			$data['submenu']='Broadcast Whatsap';
			$data['header']='Simpeg';
			$data['subheader']='Broadcast Whatsap';
			$data['top_navbar']='menu/top';
			$data['left_navbar']='menu/left';
			$data['navigasi']='menu/navigasi';
			$data['content']='modul/acara/upload_cadangan';
			$data['footer']='menu/bottom';
			$this->load->view('admin/tmp',$data);
		
	}
	
	
	public function kirim_exel($id_acara)
    {
         	if(isset($_FILES["userfile"]["name"]))
			{
				$path = $_FILES["userfile"]["tmp_name"];
				$object = PHPExcel_IOFactory::load($path);
				foreach($object->getWorksheetIterator() as $worksheet)
				{
					$highestRow = $worksheet->getHighestRow();
					$highestColumn = $worksheet->getHighestColumn();
					for($row=2; $row<=$highestRow; $row++)
					{   
						//$id_acara		= $worksheet->getCellByColumnAndRow(0, $row)->getValue();;
						$nama 			= $worksheet->getCellByColumnAndRow(1, $row)->getValue();
						$nip 			= $worksheet->getCellByColumnAndRow(2, $row)->getValue();
						$unit_kerja		= $worksheet->getCellByColumnAndRow(3, $row)->getValue();
						$jabatan		= $worksheet->getCellByColumnAndRow(4, $row)->getValue();
						$no_hp			= $worksheet->getCellByColumnAndRow(5, $row)->getValue();
						$email			= $worksheet->getCellByColumnAndRow(6, $row)->getValue();
						$sts			= $worksheet->getCellByColumnAndRow(7, $row)->getValue();
						$nuptk			= $worksheet->getCellByColumnAndRow(8, $row)->getValue();
						$judul			= $worksheet->getCellByColumnAndRow(9, $row)->getValue();
						$kategori		= $worksheet->getCellByColumnAndRow(10, $row)->getValue();
						
						$data[] = array(
							
							'id_acara'     		=>$id_acara,
							'nama'       		=>$nama,
							'nip'     		    =>$nip,
							'unit_kerja'     	=>$unit_kerja,
							'jabatan'     		=>$jabatan,
							'no_hp'     		=>$no_hp,
							'email'     		=>$email,
							'sts'     			=>$sts,
							'nuptk'     		=>$nuptk,
							'judul'     		=>$judul,
							'kategori'     		=>$kategori
							
						);
					}
				}
				$this->m_acara->insert($data);
				
			} 
         
				
		redirect('c_acara');
    
    }
	
	function generate($id_acara,$id_tamu)
	{
		$simpan_data=array(
		'unik'			=>MD5($id_tamu)
		);
		$this->db->where('id_tamu', $id_tamu);
		$this->db->where('id_acara', $id_acara);
        $this->db->update('tbl_registrasi', $simpan_data);
		
		redirect('c_acara/verifikasi/'.$id_acara.'');
	}
	
	function render_all()
	{
		$filter=$this->input->post('filter');
		$id_acara = $this->input->post('id_acara');

		if($filter==1){
			$this->m_acara->generate_all($id_acara);
			$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 membuat QR Code</strong>
				</div>');	
			redirect('c_acara/verifikasi/'.$id_acara.'');
		}else if($filter==2){
			$this->m_acara->kirim_wa_all($id_acara);
			$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>Whatsapp telah terkirim</strong>
				</div>');
			redirect('c_acara/verifikasi/'.$id_acara.'');
		}else if($filter==3){
			$this->m_acara->kirim_email_all($id_acara);
			$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>Email telah terkirim</strong>
				</div>');
			redirect('c_acara/verifikasi/'.$id_acara.'');
		}else{
			$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>Anda belum menentukan Pilihan</strong>
				</div>');
			redirect('c_acara/verifikasi/'.$id_acara.'');

		}
		
		
	}
	
	
	
}