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

	function __Construct()
    {
        parent ::__construct();
		$this->load->model('m_acara');
		$this->load->helper('tgl_indonesia');//helper tanggal indonesia
	}
	
	//Edit Data
	function verifikasi($id_acara = null)
	{		
		if($_POST == NULL) {
			
			
			$data['daftar'] = $this->m_acara->select($id_acara);// 
			//Ambil data acara
			$result =$this->db->query("SELECT * FROM tbl_acara
			where id_acara=$id_acara");
			foreach ($result->result_array() as $s)
			
			$info=$s['nama_acara'];
			$tgl_acara=$s['tgl_acara'];
			$jam_awal=$s['waktu_awal'];
			$jam_akhir=$s['waktu_akhir'];
			
			$tanggal = $tgl_acara;
			 $hari   = date("l",strtotime($tanggal));
			 //echo $hari;
			 $hari_indonesia = array('Monday'  => 'Senin',
				'Tuesday'  => 'Selasa',
				'Wednesday' => 'Rabu',
				'Thursday' => 'Kamis',
				'Friday' => 'Jumat',
				'Saturday' => 'Sabtu',
				'Sunday' => 'Minggu');
			 $nama_hari=$hari_indonesia[$hari];
			
			$data['nama_hari']=$nama_hari;
			$data['awal']=$jam_awal;
			$data['akhir']=$jam_akhir;
			$data['info']=$info;
			$data['tgl_acara']=$tgl_acara;
			$data['content']='modul/acara/form_verifikasi';
			$this->load->view('modul/acara/tmp',$data);	
		}else{
			
			//echo "pengecekan";
			$id_acara=$this->input->post('id_acara');
			$data['email']   = $this->m_acara->cek_verifikasi($id_acara);
			if($data['email']!=null) //email dan Nomor HP ada	
			{
				$result =$this->db->query("SELECT * FROM tbl_acara
				where id_acara=$id_acara");
				foreach ($result->result_array() as $s)
				
				$info=$s['nama_acara'];
				$tgl_acara=$s['tgl_acara'];
				$jam_awal=$s['waktu_awal'];
				$jam_akhir=$s['waktu_akhir'];
				
				$tanggal = $tgl_acara;
				 $hari   = date("l",strtotime($tanggal));
				 //echo $hari;
				 $hari_indonesia = array('Monday'  => 'Senin',
					'Tuesday'  => 'Selasa',
					'Wednesday' => 'Rabu',
					'Thursday' => 'Kamis',
					'Friday' => 'Jumat',
					'Saturday' => 'Sabtu',
					'Sunday' => 'Minggu');
				 $nama_hari=$hari_indonesia[$hari];
				
				$data['nama_hari']=$nama_hari;
				$data['awal']=$jam_awal;
				$data['akhir']=$jam_akhir;
				$data['info']=$info;
				//ambil data peserta untuk di tampilkan form edit data
				$data['edit'] = $this->m_acara->ubah_verifikasi($id_acara);
				
				$data['id_acara']=$id_acara;
				$data['content']='modul/acara/form_edit_peserta';
				$this->load->view('modul/acara/tmp',$data);	
				
				
				
				
				
			}else{
				$data['notifikasi'] ='
				<center>
				<div class="col-sm-12">
				<div class="alert alert-danger">
				<h3>Anda bukan peserta terdaftar atau email dan nomor whatsapp yang anda masukkan salah !!<br>
				<hr>
				Silahkan periksa kembali email dan nomor whatsapp yang anda masukan<br>pastikan sama dengan yang anda gunakan saat mendaftar<b>
				<br><br><br>Terimakasih<br><br><br>
				
				<a href="javascript:history.back()" class="btn btn btn-primary" type="reset" data-action="collapse">
					<i class="ace-icon fa fa-undo bigger-110"></i>
					Kembali
				</button></a>
				</h3>
				</div>
				</div>';
				$data['content']='admin/notifikasi';
				$this->load->view('modul/acara/tmp',$data);
			}
		}
		
	}
	
	
	public function upload_signature($id_tamu){
		
		
		define('UPLOAD_DIR', './upload/');
		$img = $_POST['imgBase64'];
		$img = str_replace('data:image/png;base64,', '', $img);
		$img = str_replace(' ', '+', $img);
		$data = base64_decode($img);
		$file = UPLOAD_DIR .'kehadiran'.$_POST['id'] . '.png';
		$success = file_put_contents($file, $data);
		
		$update['ttd']='kehadiran'.$_POST['id'] . '.png';
		
      	$this->db->update('tbl_registrasi',$update,array('id_tamu'=>$_POST['id']));
		//send request to ocr
		print $success ? $file : 'Unable to save the file.';
	
		
	}
	
	function update_peserta($id_tamu)
	{		
		$id_acara=$this->input->post('id_acara');
		$data = array(
		'nama'   						=>$this->input->post('nama'),
		'nip'   						=>$this->input->post('nip'),
		'unit_kerja'   					=>$this->input->post('unit_kerja'),
		'jabatan'   					=>$this->input->post('jabatan'),
		'unik'   						=>MD5($id_tamu)
		);
		$this->db->where('id_tamu', $id_tamu);
		$this->db->where('id_acara', $id_acara);
		//$this->db->where('sts', 0);
		$this->db->update('tbl_registrasi', $data);
		
		//Kirim notif
		
		$id_acara=$this->input->post('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_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'];
		
		
		$nomor=$no_hp;
		$pesan='Yth  *'.$nama.'*  Terimakasih, Anda sudah melakukan Verifikasi Kehadiran di kegiatan '.$acara.' tanggal '.$tgl_acara.'';
		$this->m_acara->kirimWa($nomor,$pesan);
		
		
		//KIRIM EMAIL
		$subject= 'Verifikasi Kehadiran anda telah Sukses !! ';
		$nm_tujuan= 'Yth. '.$nama.'';
		$isi=' Terimakasih, Anda sudah melakukan Verifikasi Kehadiran di kegiatan '.$acara.' tanggal '.$tgl_acara.'';
		$email_tujuan=$email;
		//$link_file='tes.txt';
		
		$this->m_acara->kirimEMAIL($subject,$nm_tujuan,$isi,$email_tujuan);//Ke Model kirim Email
		
		
		//=============
		
		
		//Ubah status menjadi 1 hadir
		$result = $this->db->query("SELECT sts FROM tbl_registrasi WHERE id_tamu='$id_tamu' AND id_acara='$id_acara'");
		foreach ($result->result_array() as $id)
		$sts=$id['sts'];
		//Update menjadi AKtif di tabel member
		if ($sts==0){
		$simpan_data=array(
		'sts'			=>1
		);
		$this->db->where('id_tamu', $id_tamu);
		$this->db->where('id_acara', $id_acara);
        $this->db->update('tbl_registrasi', $simpan_data);
		}
		
		
		$res['status']=true;
		$res['message']='Data anda berhasil disimpan !!';
		$res['id']=	$id_tamu;								
		echo json_encode($res);
					
		
	}
	
	function warning($id_acara,$id_tamu)
		{
				
				$result =$this->db->query("SELECT * FROM tbl_registrasi
				where id_acara=$id_acara AND id_tamu=$id_tamu");
				foreach ($result->result_array() as $s)
				$nama=$s['nama'];
				$nip=$s['nip'];
				$jabatan=$s['jabatan'];
				$unit_kerja=$s['unit_kerja'];
				$email=$s['email'];
				$no_hp=$s['no_hp'];
				$sts=$s['sts'];
				if ($sts==1){$sts='<button type="submit" class="btn btn-sm btn-warning btn-block">SUDAH MELAKUKAN VERIFIKASI ULANG DAN PERBAIKAN DATA</button>';}else{
				$sts='BELUM VERIFIKASI';	}
				$data['notifikasi'] ='
				<center>
				<div class="col-sm-12">
				<div class="alert alert-success">
				<h3>Update Data Peserta Berhasil !!</h3>
				</div>
				</div>
				<div class="container">
			<div class="row">
				<div class="col-md-12 col-xs-12">
					<div class="panel panel-default">
					<div class="panel-body">
					
					
		<table class="table table-striped table-bordered">
		<tr>
			<th>Nama Lengkap</th>
			<td>'.$nama.'</td>
		</tr>
		<tr>
			<th>NIP</th>
			<td>'.$nip.'</td>
		</tr>
		<tr>
			<th>Unit Kerja</th>
			<td>'.$unit_kerja.'</td>
		</tr>
		<tr>
			<th>Jabatan</th>
			<td>'.$jabatan.'</td>
		</tr>
		<tr>
			<th>Email</th>
			<td>'.$email.'</td>
		</tr>
		<tr>
			<th>No Whatsapp</th>
			<td>'.$no_hp.'</td>
		</tr>
		<tr>
			<th>Status Kehadiran dan Koreksi Data</th>
			<td><b>'.$sts.'</b></td>
		</tr>
		<tr>
			<th><a href="'.base_url().'acara/detail_peserta/'.$id_acara.'">LIHAT DAFTAR HADIR</a></th>
			<td></td>
		</tr>
		
	</table>
					</div>
					</div>
				</div>
			</div>
		</div>
				
				
				';
				$data['content']='admin/notifikasi';
				$this->load->view('modul/acara/tmp',$data);	
		}
	
	function detail_peserta($id_acara)
	{
		$data['peserta']   = $this->m_acara->download($id_acara);
		$data['ket']='
		<hr><center><div class="col-sm-12"><div class="alert alert-success"><h4><b>DAFTAR PESERTA KEGIATAN<b></h4></center>
				</div></div>
		';
		$data['content']='modul/acara/daftar_peserta';
		$this->load->view('modul/acara/tmp',$data);	
	
	}
	
	function cadangan_peserta($id_acara)
	{
		$data['peserta']   = $this->m_acara->cadangan($id_acara);
		$data['ket']='
		<hr><center><div class="col-sm-12"><div class="alert alert-warning"><h3><b>DAFTAR PESERTA CADANGAN<b></h3></center>
				</div></div>
		';
		$data['content']='modul/acara/daftar_peserta';
		$this->load->view('modul/acara/tmp',$data);	
	
	}
	
	//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['tot']=$this->m_acara->tot();
		
		$data['content']='modul/acara/daftar_acara';
		$this->load->view('modul/acara/tmp',$data);	
		
    }
	
	
	
	function daftar($id_acara = null)
	{		
		if($_POST == NULL) {
			
			
			$data['daftar'] = $this->m_acara->select($id_acara);// 
			//Ambil data acara
			$result =$this->db->query("SELECT * FROM tbl_acara
			where id_acara=$id_acara");
			foreach ($result->result_array() as $s)
			
			$info=$s['nama_acara'];
			$tgl_acara=$s['tgl_acara'];
			$jam_awal=$s['waktu_awal'];
			$jam_akhir=$s['waktu_akhir'];
			
			$tanggal = $tgl_acara;
			 $hari   = date("l",strtotime($tanggal));
			 //echo $hari;
			 $hari_indonesia = array('Monday'  => 'Senin',
				'Tuesday'  => 'Selasa',
				'Wednesday' => 'Rabu',
				'Thursday' => 'Kamis',
				'Friday' => 'Jumat',
				'Saturday' => 'Sabtu',
				'Sunday' => 'Minggu');
			 $nama_hari=$hari_indonesia[$hari];
			
			$data['nama_hari']=$nama_hari;
			$data['awal']=$jam_awal;
			$data['akhir']=$jam_akhir;
			$data['info']=$info;
			$data['tgl_acara']=$tgl_acara;
			$data['content']='modul/acara/form_daftar';
			$this->load->view('modul/acara/tmp',$data);	
		}else{
			$id_acara=$this->input->post('id_acara');
			$nama=$this->input->post('nama');
			$no_hp=$this->input->post('no_hp');
			$data['email']   = $this->m_acara->cek_email($id_acara);
			if($data['email']!=null) //Pengecekan apakah email sudah digunakan orang	
			{
				
				//echo 'sudah ada';
				
				$data['notifikasi'] ='
				<center><div class="col-sm-12"><div class="alert alert-success">
                <a class="close" data-dismiss="alert" href="#">&times;</a>
                <h3>Anda Sudah Terdaftar</h3>
                Bapak/Ibu '.$nama.' sudah terdaftar, silahkan  cek daftar berikut <br>
				<a href="'.base_url().'acara/detail_peserta/'.$id_acara.'"><span><<< Daftar Peserta >>></span></a>
				</center>
				</div></div>';
				$data['content']='admin/notifikasi';
				$this->load->view('modul/acara/tmp',$data);	
				
				
			}else{
			
			$result =$this->db->query("SELECT * FROM tbl_acara
			where id_acara=$id_acara");
			foreach ($result->result_array() as $s)
			$jml=$s['jumlah_peserta'];
			
			//total peserta
			$tot= $this->m_acara->tot($id_acara);
			
			
			
			
			if($tot < $jml){
				$this->m_acara->simpan($id_acara);
				redirect('acara');
			}else{
				$id_acara=$this->input->post('id_acara');
				$nama=$this->input->post('nama');
				$no_hp=$this->input->post('no_hp');
				$data['email']   = $this->m_acara->cek_email_cadangan($id_acara);
			if($data['email']!=null) //Pengecekan apakah email sudah digunakan orang	
			{
				
				//echo 'sudah ada';
				
				$data['notifikasi'] ='
				<center><div class="col-sm-12"><div class="alert alert-warning">
                <a class="close" data-dismiss="alert" href="#">&times;</a>
                <h3>Kuota sudah penuh !! Anda Terdaftar sebagai peserta cadangan</h3>
                Bapak/Ibu '.$nama.' sudah terdaftar sebagai peserta cadangan, silahkan  cek daftar berikut <br>
				<a href="'.base_url().'acara/cadangan_peserta/'.$id_acara.'"><span><<< Daftar Peserta Cadangan >>></span></a>
				</center>
				</div></div>';
				$data['content']='admin/notifikasi';
				$this->load->view('modul/acara/tmp',$data);	
				
				
			}else{
				
			//Simpen ke tabel cadangan
				$this->m_acara->simpan_cadangan($id_acara);
				
				
				
				$data['notifikasi'] = '
				<center><div class="col-sm-12"><div class="alert alert-danger">
                <a class="close" data-dismiss="alert" href="#">&times;</a>
                <h3>Peringatan !</h3>
                Maaf Kuota Sudah Penuh, Anda  masuk sebagai peserta cadangan<br>
				<a href="'.base_url().'acara/cadangan_peserta/'.$id_acara.'"><span><<< Daftar Peserta Cadangan >>></span></a>
				</center>
				</div></div>';
				$data['content']='admin/notifikasi';
				$this->load->view('modul/acara/tmp',$data);	
			}
			}
			
			}
		}
	}
	
	
	
	
	
}