<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
	
	class Home extends CI_Controller
	{
		public function __construct()
		{
			parent::__construct();
			$this->load->model('home/Home_model', 'home');
			$this->load->library('form_validation');
			//$this->load->library('Dompdf_gen','dompdf');
			$this->load->model('login/M_login', 'login');
			$this->auth->cek_auth_pengguna(); //ngambil auth dari library
		}
		
		public function identitas()
		{	
			
			$id_log = $this->session->userdata('id');
			$user   = $this->login->pengguna($id_log);
			
			if(empty($id_log)){
				redirect('beranda');	
			}
			
			$layanan = $this->db->query("SELECT * FROM jam_layanan")->row();
			$jam     = date('H:i:s');
			$hari    = date('D');
			
			if($hari == 'Mon' OR $hari == 'Tue' OR $hari == 'Wed' OR $hari == 'Thu' OR $hari == 'Fri'){
				if($layanan->status=='1' AND ($jam>=$layanan->jam_mulai AND $jam<=$layanan->jam_selesai)){
					$status = 'buka';
					}else{
					$status = 'tutup';
				}
				}else{
				$status = 'tutup';
			}
			
			//$jam = date('H:i');
			
			//atur salam menggunakan IF
			if ($jam > '05:30:00' && $jam < '10:00:00') {
				$salam = 'pagi';
				} elseif ($jam >= '10:00:00' && $jam < '15:00:00') {
				$salam = 'siang';
				} elseif ($jam < '18:00:00') {
				$salam = 'sore';
				} else {
				$salam = 'malam';
			}
			$cek_bantuan = $this->db->query("SELECT count(id) as total FROM ptsp_pengunjung WHERE p_id = '$user->id' AND status='0' ")->row()->total;
			$bantuan	 = $this->db->query("SELECT id FROM ptsp_pengunjung WHERE p_id = '$user->id' AND status='0' ORDER BY id DESC LIMIT 0,1 ")->row();
			if($cek_bantuan > 0){
				$this->session->set_flashdata('message', '<div class="alert alert-danger text-center">Maaf, apakah anda ingin melanjutkan permohonan sebelumnya ?</div>');
				redirect('home/bantuan/'.md5($bantuan->id));
				}else{	
				$data = array(
				'user'			=> $user,
				'set'			=> $this->db->query("SELECT * FROM pengaturan WHERE id='2' ")->row(),
				'judul_halaman' => 'Halaman Utama',
				'status_layanan'=> $status,
				'jam'	 	    => $jam,
				'jam_mulai'     => $layanan->jam_mulai,
				'jam_selesai'   => $layanan->jam_selesai,
				'salam'			=> $salam,
				'login' => site_url('home/proses_identitas'),
				'id'  => set_value('id',$id_log),
				'nik' => set_value('nik',$user->nik),
				'hp' => set_value('hp',$user->hp),
				'email' => set_value('email',$user->email),
				'nama' => set_value('nama',$user->nama),
				'pekerjaan' => set_value('pekerjaan',$user->pekerjaan),
				'alamat' => set_value('alamat',$user->alamat),
				);
				//print_r($data);
				$this->template->load('template_login', 'home/home_identitas', $data);
			}
		}
		
		function untuk_upload_gambar()
		{
			$this->load->library('upload');
			$tahun = date('Y');
			$bulan = date('m');
			$tanggal = date('d');
			$id_per = md5($this->input->post('id', TRUE));
			$nmfile = 'dok_' . $id_per .  '_' . $tanggal . '_' . $bulan . '_' . $tahun . '_' . time(); //nama file saya beri nama langsung dan diikuti fungsi time
			$config['rtf']   =     array('application/msword', 'text/rtf');
			$config['upload_path'] = './data_dukung/'; //path folder
			$config['allowed_types'] = 'jpg|JPG|png|PNG|jpeg|JPEG|pdf|PDF'; //type yang dapat diakses bisa anda sesuaikan
			$config['max_size'] = '2050'; //maksimum besar file 2mb
			$config['file_name'] = $nmfile; //nama yang terupload nantinya
			$this->load->library('upload', $config);
			$this->upload->initialize($config);
		}
		
		function proses_identitas(){
			$id_log = $this->session->userdata('id');
			$user   = $this->login->pengguna($id_log);
			
			if(empty($id_log)){
				redirect('beranda');	
			}
			
			$layanan = $this->db->query("SELECT * FROM jam_layanan")->row();
			$jam     = date('H:i:s');
			$hari    = date('D');
			
			$dok_id = $id_log;
			
			$this->untuk_upload_gambar();
			if (!empty($_FILES['file_ktp']['name'])) { // empty
				if ($this->upload->do_upload('file_ktp')) {
					$gbr = $this->upload->data();
					$data['file_ktp'] = $gbr['file_name'];
					$data['pekerjaan']= $this->input->post('pekerjaan',TRUE);
					$data['alamat']   = $this->input->post('alamat',TRUE);
					$data['nama']     = $this->input->post('nama',TRUE);
					$this->login->update($dok_id, $data);
					$this->session->set_flashdata('message', '<div class="alert alert-success text-center">BERHASIL, file Kartu Identitas telah diunggah, silahkan melanjutkan permohonan anda</div>');
					//echo 'berhasil';
					redirect(site_url('home'));
					} else {
					$this->session->set_flashdata('message', '<div class="text-danger">GAGAL ! Silahkan Coba lagi ! ! !<b>' . $this->upload->display_errors() . ' ( type file yang diizinkan .jpg / .png / .pdf maksimal ukuran file 2Mb)</b></div>');
					//echo 'gagal';
					$this->identitas();
				}
				} else {
				$this->session->set_flashdata('message', '<div class="alert alert-danger text-center">Data Tidak Ditermukan</div>');
				//echo 'tidak ada data';
				redirect(site_url('home/identitas'));
			}			
		}
		
		public function index()
		{	
			$id_log = $this->session->userdata('id');
			$user   = $this->login->pengguna($id_log);
			
			if(empty($id_log)){
				redirect('beranda');	
			}
			
			$layanan = $this->db->query("SELECT * FROM jam_layanan")->row();
			$jam     = date('H:i:s');
			$hari    = date('D');
			
			if($hari == 'Mon' OR $hari == 'Tue' OR $hari == 'Wed' OR $hari == 'Thu' OR $hari == 'Fri'){
				if($layanan->status=='1' AND ($jam>=$layanan->jam_mulai AND $jam<=$layanan->jam_selesai)){
					$status = 'buka';
					}else{
					$status = 'tutup';
				}
				}else{
				$status = 'tutup';
			}
			
			if($user->file_ktp == ''){
				redirect('home/identitas');
			}
			
			//$jam = date('H:i');
			
			//atur salam menggunakan IF
			if ($jam > '05:30:00' && $jam < '10:00:00') {
				$salam = 'pagi';
				} elseif ($jam >= '10:00:00' && $jam < '15:00:00') {
				$salam = 'siang';
				} elseif ($jam < '18:00:00') {
				$salam = 'sore';
				} else {
				$salam = 'malam';
			}
			$cek_bantuan = $this->db->query("SELECT count(id) as total FROM ptsp_pengunjung WHERE p_id = '$user->id' AND status='0' ")->row()->total;
			$bantuan	 = $this->db->query("SELECT id FROM ptsp_pengunjung WHERE p_id = '$user->id' AND status='0' ORDER BY id DESC LIMIT 0,1 ")->row();
			if($cek_bantuan > 0){
				$this->session->set_flashdata('message', '<div class="alert alert-danger text-center">Maaf, apakah anda ingin melanjutkan permohonan sebelumnya ?</div>');
				redirect('home/bantuan/'.md5($bantuan->id));
				}else{	
				$data = array(
				'user'			=> $user,
				'set'			=> $this->db->query("SELECT * FROM pengaturan WHERE id='2' ")->row(),
				'judul_halaman' => 'Halaman Utama',
				'status_layanan'=> $status,
				'jam'	 	    => $jam,
				'jam_mulai'     => $layanan->jam_mulai,
				'jam_selesai'   => $layanan->jam_selesai,
				'salam'			=> $salam,
				);
				//print_r($data);
				$this->template->load('template_beranda', 'home/home_index', $data);
			}
		}
		
		public function kepaniteraan()
		{	
			$id_log = $this->session->userdata('id');
			$user   = $this->login->pengguna($id_log);
			
			if(empty($id_log)){
				redirect('beranda');	
			}
			
			$layanan = $this->db->query("SELECT * FROM jam_layanan")->row();
			$jam     = date('H:i:s');
			$hari    = date('D');
			
			if($hari == 'Mon' OR $hari == 'Tue' OR $hari == 'Wed' OR $hari == 'Thu' OR $hari == 'Fri'){
				if($layanan->status=='1' AND ($jam>=$layanan->jam_mulai AND $jam<=$layanan->jam_selesai)){
					$status = 'buka';
					}else{
					$status = 'tutup';
				}
				}else{
				$status = 'tutup';
			}
			
			if($user->file_ktp == ''){
				redirect('home/identitas');
			}
			//$jam = date('H:i');
			
			//atur salam menggunakan IF
			if ($jam > '05:30:00' && $jam < '10:00:00') {
				$salam = 'pagi';
				} elseif ($jam >= '10:00:00' && $jam < '15:00:00') {
				$salam = 'siang';
				} elseif ($jam < '18:00:00') {
				$salam = 'sore';
				} else {
				$salam = 'malam';
			}
			if($status=='buka'){
				$cek_bantuan = $this->db->query("SELECT count(id) as total FROM ptsp_pengunjung WHERE p_id = '$user->id' AND status='0' ")->row()->total;
				$bantuan	 = $this->db->query("SELECT id FROM ptsp_pengunjung WHERE p_id = '$user->id' AND status='0' ORDER BY id DESC LIMIT 0,1 ")->row();
				if($cek_bantuan > 0){
					$this->session->set_flashdata('message', '<div class="alert alert-danger text-center">Maaf, apakah anda ingin melanjutkan permohonan sebelumnya ?</div>');
					redirect('home/bantuan/'.md5($bantuan->id));
					}else{	
					$data = array(
					'user'			=> $user,
					'set'			=> $this->db->query("SELECT * FROM pengaturan WHERE id='2' ")->row(),
					'judul_halaman' => 'Halaman Utama',
					'status_layanan'=> $status,
					'jam'	 	    => $jam,
					'jam_mulai'     => $layanan->jam_mulai,
					'jam_selesai'   => $layanan->jam_selesai,
					'salam'			=> $salam,
					'jenis_layanan' => $this->home->jenis_layanan(1),
					'data_layanan'  => $this->home->data_layanan(1),
					);
					//print_r($data);
					$this->template->load('template_login','home/home_layanan',$data);
				}
				}else{
				redirect('beranda/close');
			}
		}
		
		public function kesekretariatan()
		{	
			$id_log = $this->session->userdata('id');
			$user   = $this->login->pengguna($id_log);
			
			if(empty($id_log)){
				redirect('beranda');	
			}
			
			$layanan = $this->db->query("SELECT * FROM jam_layanan")->row();
			$jam     = date('H:i:s');
			$hari    = date('D');
			
			if($hari == 'Mon' OR $hari == 'Tue' OR $hari == 'Wed' OR $hari == 'Thu' OR $hari == 'Fri'){
				if($layanan->status=='1' AND ($jam>=$layanan->jam_mulai AND $jam<=$layanan->jam_selesai)){
					$status = 'buka';
					}else{
					$status = 'tutup';
				}
				}else{
				$status = 'tutup';
			}
			
			if($user->file_ktp == ''){
				redirect('home/identitas');
			}
			
			//$jam = date('H:i');
			
			//atur salam menggunakan IF
			if ($jam > '05:30:00' && $jam < '10:00:00') {
				$salam = 'pagi';
				} elseif ($jam >= '10:00:00' && $jam < '15:00:00') {
				$salam = 'siang';
				} elseif ($jam < '18:00:00') {
				$salam = 'sore';
				} else {
				$salam = 'malam';
			}
			
			if($status=='buka'){
				$cek_bantuan = $this->db->query("SELECT count(id) as total FROM ptsp_pengunjung WHERE p_id = '$user->id' AND status='0' ")->row()->total;
				$bantuan	 = $this->db->query("SELECT id FROM ptsp_pengunjung WHERE p_id = '$user->id' AND status='0' ORDER BY id DESC LIMIT 0,1 ")->row();
				if($cek_bantuan > 0){
					$this->session->set_flashdata('message', '<div class="alert alert-danger text-center">Maaf, apakah anda ingin melanjutkan permohonan sebelumnya ?</div>');
					redirect('home/bantuan/'.md5($bantuan->id));
					}else{	
					$data = array(
					'user'			=> $user,
					'set'			=> $this->db->query("SELECT * FROM pengaturan WHERE id='2' ")->row(),
					'judul_halaman' => 'Halaman Utama',
					'status_layanan'=> $status,
					'jam'	 	    => $jam,
					'jam_mulai'     => $layanan->jam_mulai,
					'jam_selesai'   => $layanan->jam_selesai,
					'salam'			=> $salam,
					'jenis_layanan' => $this->home->jenis_layanan(2),
					'data_layanan'  => $this->home->data_layanan(2),
					);
					//print_r($data);
					//$this->template->load('template_login','home/home_layanan',$data);
					redirect('home/kepaniteraan');
				}
				}else{
				redirect('beranda/close');
			}
		}
		
		
		function proses(){
			$id_log = $this->session->userdata('id');
			$user   = $this->login->pengguna($id_log);
			
			$layanan = $this->db->query("SELECT * FROM jam_layanan")->row();
			$jam     = date('H:i:s');
			$hari    = date('D');
			
			if($hari == 'Mon' OR $hari == 'Tue' OR $hari == 'Wed' OR $hari == 'Thu' OR $hari == 'Fri'){
				if($layanan->status=='1' AND ($jam>=$layanan->jam_mulai AND $jam<=$layanan->jam_selesai)){
					$status = 'buka';
					}else{
					$status = 'tutup';
				}
				}else{
				$status = 'tutup';
			}
			
			//$jam = date('H:i');
			
			//atur salam menggunakan IF
			if ($jam > '05:30:00' && $jam < '10:00:00') {
				$salam = 'pagi';
				} elseif ($jam >= '10:00:00' && $jam < '15:00:00') {
				$salam = 'siang';
				} elseif ($jam < '18:00:00') {
				$salam = 'sore';
				} else {
				$salam = 'malam';
			}
			
			$p_id 			  = $this->input->post('p_id',TRUE);
			$id_jenis_layanan = $this->input->post('l_id_jenis_layanan',TRUE);
			
			$this->_rules();
			
			if ($this->form_validation->run() == FALSE) {
				if($id_jenis_layanan==1){
					$this->kepaniteraan();
					}else if($id_jenis_layanan==2){
					$this->kesekretariatan();
					}else{
					redirect('home');
				}
				
				} else {
				$data = array(
				'p_id' => $this->input->post('p_id',TRUE),
				'p_nik' => $this->input->post('p_nik',TRUE),
				'p_hp' => $this->input->post('p_hp',TRUE),
				'p_email' => $this->input->post('p_email',TRUE),
				'p_nama' => $this->input->post('p_nama',TRUE),
				'p_tgllahir' => $this->input->post('p_tgllahir',TRUE),
				'p_jk' => $this->input->post('p_jk',TRUE),
				'p_pekerjaan' => $this->input->post('p_pekerjaan',TRUE),
				'l_id' => $this->input->post('l_id',TRUE),
				'l_layanan' => $this->input->post('l_layanan',TRUE),
				'l_id_jenis_layanan' => $this->input->post('l_id_jenis_layanan',TRUE),
				'l_nama_jenis_layanan' => $this->input->post('l_nama_jenis_layanan',TRUE),
				'catatan' => $this->input->post('catatan',TRUE),
				'bantuan' => 0,
				'status' => 0,
				'waktu' => date('Y-m-d H:i:s'),
				);
				
				$this->home->insert($data);
				$bantuan = $this->db->query("SELECT id FROM ptsp_pengunjung WHERE p_id = '$p_id' ORDER BY id DESC LIMIT 0,1 ")->row();
				$this->session->set_flashdata('message', 'Create Record Success');
				redirect(site_url('home/bantuan/'.md5($bantuan->id)));
			}
			
		}
		
		public function _rules() 
		{
			$this->form_validation->set_rules('catatan', 'catatan', 'trim|required');
			
			$this->form_validation->set_rules('id', 'id', 'trim');
			$this->form_validation->set_error_delimiters('<span class="text-danger">', '</span>');
		}
		
		public function bantuan($id)
		{	
			$id_log = $this->session->userdata('id');
			$user   = $this->login->pengguna($id_log);
			
			
			
			$layanan = $this->db->query("SELECT * FROM jam_layanan where id='1' ")->row();
			$jam     = date('H:i:s');
			$hari    = date('D');
			
			if($hari == 'Mon' OR $hari == 'Tue' OR $hari == 'Wed' OR $hari == 'Thu' OR $hari == 'Fri'){
				if($layanan->status=='1' AND ($jam>=$layanan->jam_mulai AND $jam<=$layanan->jam_selesai)){
					$status = 'buka';
					}else{
					$status = 'tutup';
				}
				}else{
				$status = 'tutup';
			}
			
			//$jam = date('H:i');
			
			//atur salam menggunakan IF
			if ($jam > '05:30:00' && $jam < '10:00:00') {
				$salam = 'pagi';
				} elseif ($jam >= '10:00:00' && $jam < '15:00:00') {
				$salam = 'siang';
				} elseif ($jam < '18:00:00') {
				$salam = 'sore';
				} else {
				$salam = 'malam';
			}
			if($status=='buka'){
				$ptsp_pengunjung = $this->home->get_layanan_id($id,$user->id);
				if($ptsp_pengunjung){
					if($ptsp_pengunjung->status==0){
						$tanggal = date('Y-m-d');
						$antrian = $this->db->query("SELECT count(id) as total FROM ptsp_pengunjung WHERE LEFT(waktu_hubungi,10)='$tanggal' AND waktu_jawab IS NULL")->row();
						$data = array(
						'user'			=> $user,
						'set'			=> $this->db->query("SELECT * FROM pengaturan WHERE id='2' ")->row(),
						'judul_halaman' => 'Halaman Utama',
						'status_layanan'=> $status,
						'jam'	 	    => $jam,
						'jam_mulai'     => $layanan->jam_mulai,
						'jam_selesai'   => $layanan->jam_selesai,
						'salam'			=> $salam,
						'pengunjung'	=> $ptsp_pengunjung,
						'antrian'		=> $antrian->total + 1,
						);
						//print_r($data);
						$this->template->load('template_login', 'home/home_bantuan', $data);					
						}else{
						$this->session->set_flashdata('message', '<div class="alert alert-warning text-center">Maaf, session ini sudah selesai, harap ajukan permohonan ulang</div>');
						redirect('home');
					}
					
					}else{
					redirect('home');
				}
				}else{
				redirect('beranda/close');
			}
		}
		
		function hubungi(){
			$id_log = $this->session->userdata('id');
			$user   = $this->login->pengguna($id_log);
			
			$layanan      = $this->db->query("SELECT * FROM jam_layanan")->row();
			$jam     = date('H:i:s');
			$hari    = date('D');
			
			if($hari == 'Mon' OR $hari == 'Tue' OR $hari == 'Wed' OR $hari == 'Thu' OR $hari == 'Fri'){
				if($layanan->status=='1' AND ($jam>=$layanan->jam_mulai AND $jam<=$layanan->jam_selesai)){
					$status = 'buka';
					}else{
					$status = 'tutup';
				}
				}else{
				$status = 'tutup';
			}
			
			//$jam = date('H:i');
			
			//atur salam menggunakan IF
			if ($jam > '05:30:00' && $jam < '10:00:00') {
				$salam = 'pagi';
				} elseif ($jam >= '10:00:00' && $jam < '15:00:00') {
				$salam = 'siang';
				} elseif ($jam < '18:00:00') {
				$salam = 'sore';
				} else {
				$salam = 'malam';
			}
			
			$id				  = $this->input->post('id',TRUE);
			$bantuan		  = $this->input->post('bantuan',TRUE);
			$p_id 			  = $this->input->post('p_id',TRUE);
			$id_jenis_layanan = $this->input->post('l_id_jenis_layanan',TRUE);
			
			$ptsp_pengunjung  = $this->home->ptsp_pengunjung_id($id,$user->id);
			
			$ptsp_hubungi	  = $this->home->ptsp_hubungi_id($bantuan);
			$ptsp_hubungi2	  = $this->home->ptsp_hubungi_id2($bantuan);
			
			$this->_rules_hubungi();
			
			if($ptsp_pengunjung){
				if ($this->form_validation->run() == FALSE) {
					$this->bantuan(md5($id));		
					} else {
					if($ptsp_pengunjung->status==0){
						$data = array(
						'p_id' 	  		=> $this->input->post('p_id',TRUE),
						'bantuan' 		=> $this->input->post('bantuan',TRUE),
						'status'  		=> $this->input->post('status',TRUE),
						'waktu_hubungi' => date('Y-m-d H:i:s'),
						);
						if($ptsp_pengunjung->l_id_jenis_layanan==1){
							if($bantuan==1){
								$this->home->update($this->input->post('id', TRUE), $data);
								redirect('https://api.whatsapp.com/send?phone=' .$ptsp_hubungi->link_layanan. '&text=Assalamualaikum, saya *'.$user->nama.'* ingin konsultasi*. Berkaitan dengan *'.$ptsp_pengunjung->l_layanan.'* tentang _'.$ptsp_pengunjung->catatan.'_');
								}else if($bantuan==2){
								$this->home->update($this->input->post('id', TRUE), $data);
								redirect($ptsp_hubungi->link_layanan);
								}else if($bantuan==3){
								$this->home->update($this->input->post('id', TRUE), $data);
								redirect($ptsp_hubungi->link_layanan);
								}else{
								$this->session->set_flashdata('message', '<div class="alert alert-danger text-center">Error Akses Bypass Langsung dari Proses "HUBUNGI" Ditolak</div>');
								redirect('home');
							}
							}else{
							if($bantuan==1){
								$this->home->update($this->input->post('id', TRUE), $data);
								redirect('https://api.whatsapp.com/send?phone=' .$ptsp_hubungi2->link_layanan. '&text=Selamat '.$salam.', saya *'.$user->nama.' (NIK : '.$user->nik.')* ingin konsultasi tentang Jenis Layanan *'.$ptsp_pengunjung->l_nama_jenis_layanan.'*. Berkaitan dengan *'.$ptsp_pengunjung->l_layanan.'* tentang _'.$ptsp_pengunjung->catatan.'_');
								}else if($bantuan==2){
								$this->home->update($this->input->post('id', TRUE), $data);
								redirect($ptsp_hubungi2->link_layanan);
								}else if($bantuan==3){
								$this->home->update($this->input->post('id', TRUE), $data);
								redirect($ptsp_hubungi2->link_layanan);
								}else{
								$this->session->set_flashdata('message', '<div class="alert alert-danger text-center">Error Akses Bypass Langsung dari Proses "HUBUNGI" Ditolak</div>');
								redirect('home');
							}						
						}
						}else{
						$this->session->set_flashdata('message', '<div class="alert alert-warning text-center">Maaf, session ini sudah selesai, harap ajukan permohonan ulang</div>');
						redirect('home');
					}
				}
				}else{
				$this->session->set_flashdata('message', '<div class="alert alert-danger text-center">Error Akses Langsung dari Proses "HUBUNGI"</div>');
				redirect('home');
			}
			
		}
		
		public function _rules_hubungi() 
		{
			//$this->form_validation->set_rules('catatan', 'catatan', 'trim|required');
			$this->form_validation->set_rules('bantuan', 'bantuan', 'trim|required');
			$this->form_validation->set_rules('status', 'status', 'trim|required');
			
			$this->form_validation->set_rules('id', 'id', 'trim');
			$this->form_validation->set_error_delimiters('<span class="text-danger">', '</span>');
		}
		
		
	} // end controller home																																																											