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

class Admin extends CI_Controller 
{
	public function __construct() 
	{
        parent::__construct();
		$this->load->model('M_admin');
		$this->load->model('M_daftar');
    }
	
	public function index()
	{
		if($this->session->userdata('admin'))
		{
			date_default_timezone_set('Asia/Jakarta');
			$tahun				= date('Y');	
			$data['act1']		= "active";
			$data['act2']		= "";
			$data['act3']		= "";
			$data['act4']		= "";
			$data['act5']		= "";
			$data['act6']		= "";
			$data['calon_siswa']= $this->M_daftar->getCalonSiswaJurusanStatus($tahun);
			$data['jumlah_calon1'] = $this->M_daftar->getJumlahCalon1($tahun);
			$data['jumlah_calon2a'] = $this->M_daftar->getJumlahCalon2a($tahun);
			$data['jumlah_calon2b'] = $this->M_daftar->getJumlahCalon2b($tahun);
			$data['jumlah_calon3a'] = $this->M_daftar->getJumlahCalon3a($tahun);
			$data['jumlah_calon3b'] = $this->M_daftar->getJumlahCalon3b($tahun);
			$data['jumlah_calon4a'] = $this->M_daftar->getJumlahCalon4a($tahun);
			$data['jumlah_calon4b'] = $this->M_daftar->getJumlahCalon4b($tahun);
			$data['jumlah_calon5'] = $this->M_daftar->getJumlahCalon5($tahun);
			$data['jumlah_calon6'] = $this->M_daftar->getJumlahCalon6($tahun);
			$data['jumlah_calon7'] = $this->M_daftar->getJumlahCalon7($tahun);
			$data['jumlah_calon8'] = $this->M_daftar->getJumlahCalon8($tahun);
			$data['jumlah_calon2c'] = $this->M_daftar->getJumlahCalon2c($tahun);
			$data['jumlah_calon4c'] = $this->M_daftar->getJumlahCalon4c($tahun);
			/* $data['jurusan2']	= array();
			// $x					= 0;
			foreach($data['calon_siswa'] as $dcs)
			{
				$jurusan			= $this->M_admin->getJurusanDetail($dcs['id_jurusan2']);
				$data['jurusan2']	= array_merge($data['jurusan2'],$jurusan);
			} */
			$data['jurusan']	= $this->M_daftar->getJurusan();
			// print_r($data['calon_siswa']);die();
			$this->load->view('header',$data);
			$this->load->view('calonSiswa',$data);
			$this->load->view('footer');
		}
		else
		{
			$this->session->set_flashdata('msg', 
					'<div class="alert alert-danger">
						<center>
							<h4>Maaf!</h4>
							<p>Anda belum login. Silakan login terlebih dahulu!</p>
						</center>
					</div>');	
			redirect('admin/Login');
		}
	}
	
	public function resetPassword()
	{
		if($this->session->userdata('admin'))
		{
			$id_calon_siswa			= $this->uri->segment(4);
			$data['password']		= md5("123456");
			$this->M_daftar->updateCalonSiswa($id_calon_siswa,$data);
			redirect('admin/Admin','refresh');
		}
		else
		{
			$this->session->set_flashdata('msg', 
					'<div class="alert alert-danger">
						<center>
							<h4>Maaf!</h4>
							<p>Anda belum login. Silakan login terlebih dahulu!</p>
						</center>
					</div>');	
			redirect('admin/Login');
		}
	}
	
	public function calonSiswaDelete()
	{
		if($this->session->userdata('admin'))
		{
			$id_calon_siswa			= $this->uri->segment(4);
			$calon_siswa			= $this->M_daftar->getCalonSiswa($id_calon_siswa);
			unlink('./files/calon_siswa/foto/'.$calon_siswa->foto);
			// print_r($id_penyakit);die();
			$this->M_daftar->deleteCalonSIswa($id_calon_siswa);
			$this->session->set_flashdata('msg', 
					'<div class="alert alert-success">
						<center>
							<p>Data calon siswa berhasil dihapus!</p>
						</center>
					</div>');
			redirect('admin/Admin','refresh');
		}
		else
		{
			$this->session->set_flashdata('msg', 
					'<div class="alert alert-danger">
						<center>
							<h4>Maaf!</h4>
							<p>Anda belum login. Silakan login terlebih dahulu!</p>
						</center>
					</div>');	
			redirect('admin/Login');
		}
	}
	
	public function calonMhsUjianReset()
	{
		if($this->session->userdata('admin'))
		{
			$id_calon_siswa			= $this->uri->segment(4);
			$calon_siswa			= $this->M_daftar->getMahasiswaID($id_calon_siswa);
			$this->M_daftar->resetUjianSiswa($id_calon_siswa);
			$this->session->set_flashdata('msg', 
					'<div class="alert alert-success">
						<center>
							<p>Calon siswa berhasil direset!</p>
						</center>
					</div>');
			redirect('admin/Admin','refresh');
		}
		else
		{
			$this->session->set_flashdata('msg', 
					'<div class="alert alert-danger">
						<center>
							<h4>Maaf!</h4>
							<p>Anda belum login. Silakan login terlebih dahulu!</p>
						</center>
					</div>');	
			redirect('admin/Login');
		}
	}
	
	public function downloadExcel()
	{
		if($this->session->userdata('admin'))
		{
			$data['calonsiswa']	= $this->M_daftar->getCalonSiswaExcel();
			//$data['calonsiswa1'] = $this->M_daftar->getCalonSiswa();
			// print_r($data['calonsiswa']);die();
			$data['jawaban']	= array();
			$x					= 0;
			foreach($data['calonsiswa'] as $dcs)
			{
				$jawaban[$x]	= $this->M_admin->getCalonSiswaJawaban($dcs['id_calon_siswa']);
				$data['jawaban']= array_merge($data['jawaban'],$jawaban[$x]);
			}
			// print_r($data['jawaban']);die();
			$data['jurusan']		= $this->M_daftar->getJurusan();
			$this->load->view('calonSiswaExcel',$data);
		}
		else
		{
			$this->session->set_flashdata('msg', 
					'<div class="alert alert-danger">
						<center>
							<h4>Maaf!</h4>
							<p>Anda belum login. Silakan login terlebih dahulu!</p>
						</center>
					</div>');
			redirect('admin/Login');
		}
	}
	
	public function calonSiswaDetail()
	{
		if($this->session->userdata('admin'))
		{
			$id_calon_siswa			= $this->uri->segment(4);
			$data['act1']			= "active";
			$data['act2']			= "";
			$data['act3']			= "";
			$data['act4']			= "";
			$data['act5']			= "";
			$data['act6']			= "";
			$data['calonsiswa']		= $this->M_daftar->getCalonSiswa($id_calon_siswa);
			$data['raport']			= $this->M_daftar->getCalonSiswaRaport($id_calon_siswa);
			$data['prestasi']		= $this->M_daftar->getPrestasi($id_calon_siswa);
			$data['berkas']			= $this->M_daftar->getBerkas($id_calon_siswa);
			$data['jurusan']		= $this->M_daftar->getJurusan();
			// print_r($data['pilihan_jawaban']);die();
			$this->load->view('header',$data);
			$this->load->view('calonSiswaDetail',$data);
			$this->load->view('footer');
		}
		else
		{
			$this->session->set_flashdata('msg', 
					'<div class="alert alert-danger">
						<center>
							<h4>Maaf!</h4>
							<p>Anda belum login. Silakan login terlebih dahulu!</p>
						</center>
					</div>');	
			redirect('admin/Login');
		}
	}
	
	public function calonSiswaEdit()
	{
		if($this->session->userdata('admin'))
		{
			$id_calon_siswa			= $this->uri->segment(4);
			$data['act1']			= "active";
			$data['act2']			= "";
			$data['act3']			= "";
			$data['act4']			= "";
			$data['act5']			= "";
			$data['act6']			= "";
			$data['calonsiswa']		= $this->M_daftar->getCalonSiswa($id_calon_siswa);
			$data['raport']			= $this->M_daftar->getCalonSiswaRaport($id_calon_siswa);
			$data['jurusan']		= $this->M_daftar->getJurusan();
			// print_r($data['pilihan_jawaban']);die();
			$this->load->view('header',$data);
			$this->load->view('calonSiswaEdit',$data);
			$this->load->view('footer');
		}
		else
		{
			$this->session->set_flashdata('msg', 
					'<div class="alert alert-danger">
						<center>
							<h4>Maaf!</h4>
							<p>Anda belum login. Silakan login terlebih dahulu!</p>
						</center>
					</div>');	
			redirect('admin/Login');
		}
	}
	
	public function calonSiswaEditExec()
	{
		if($this->session->userdata('admin'))
		{
			date_default_timezone_set('Asia/Jakarta');
			$id_calon_siswa				= $this->input->post('id_calon_siswa');
			$data['nama']				= $this->input->post('nama');
			
			$data['tempat_lahir']		= $this->input->post('tempat_lahir');
			$data['tanggal_lahir']		= $this->input->post('tanggal_lahir');
			$data['jen_kel']			= $this->input->post('jen_kel');
			$data['agama']				= $this->input->post('agama');
			$data['no_hp']				= $this->input->post('no_hp');
			$data['alamat']				= $this->input->post('alamat');
			$data['transportasi']		= $this->input->post('transportasi');
			$data['jarak']				= $this->input->post('jarak');
			$data['waktu_tempuh']		= $this->input->post('waktu_tempuh');
			$data['cita_cita']			= $this->input->post('cita_cita');
			$data['hoby']				= $this->input->post('hoby');
			$data['kip']				= $this->input->post('kip');
			$data['kks']				= $this->input->post('kks');
			$data['pkh']				= $this->input->post('pkh');
			$milliseconds 				= round(microtime(true) * 1000);
			if($_FILES['foto']['name'] != NULL)
			{
				$calonsiswa				= $this->M_daftar->getCalonSiswa($id_calon_siswa);
				if($calonsiswa->foto != NULL)
				{
					unlink('./files/calon_siswa/foto/'.$calonsiswa->foto);
				}
				$this->load->library('upload');
				$config['upload_path'] 		= './files/calon_siswa/foto/';
				$config['allowed_types']	= '*';
				$config['max_size'] 		= '20000';
				$config['rename'] 			= TRUE;
				$config['file_name']		= $milliseconds.$_FILES['foto']['name'];
				$this->upload->initialize($config);
				$this->upload->do_upload('foto');
				$foto						= $_FILES['foto']['name'];
				$data['foto']				= str_replace(" ","_",$config['file_name']);
			}
			$data['no_kk']				= $this->input->post('no_kk');
			$data['nama_kk']			= $this->input->post('nama_kk');
			$data['pekerjaan_kk']		= $this->input->post('pekerjaan_kk');
			$data['anak_ke']			= $this->input->post('anak_ke');
			$data['jumlah_saudara']		= $this->input->post('jumlah_saudara');
			$data['nama_ayah']			= $this->input->post('nama_ayah');
			$data['pekerjaan_ayah']		= $this->input->post('pekerjaan_ayah');
			$data['no_hp_ayah']			= $this->input->post('no_hp_ayah');
			$data['alamat_ayah']		= $this->input->post('alamat_ayah');
			$data['nama_ibu']			= $this->input->post('nama_ibu');
			$data['pekerjaan_ibu']		= $this->input->post('pekerjaan_ibu');
			$data['no_hp_ibu']			= $this->input->post('no_hp_ibu');
			$data['alamat_ibu']			= $this->input->post('alamat_ibu');
			$data['nama_wali']			= $this->input->post('nama_wali');
			$data['pekerjaan_wali']		= $this->input->post('pekerjaan_wali');
			$data['no_hp_wali']			= $this->input->post('no_hp_wali');
			$data['alamat_wali']		= $this->input->post('alamat_wali');
			$data['nik_ayah']			= $this->input->post('nik_ayah');
			$data['nik_ibu']			= $this->input->post('nik_ibu');
			$data['nik_wali']			= $this->input->post('nik_wali');
			$data['hasilayah']			= $this->input->post('hasilayah');
			$data['hasilibu']			= $this->input->post('hasilibu');
			$data['hasilwali']			= $this->input->post('hasilwali');
			$data['status_ayah']		= $this->input->post('status_ayah');
			$data['status_ibu']			= $this->input->post('status_ibu');
			$data['pendidikan_ayah']	= $this->input->post('pendidikan_ayah');
			$data['pendidikan_ibu']		= $this->input->post('pendidikan_ibu');
			$data['tempat_lahir_ayah']	= $this->input->post('tempat_lahir_ayah');
			$data['tanggal_lahir_ayah']	= $this->input->post('tanggal_lahir_ayah');
			$data['tempat_lahir_ibu']	= $this->input->post('tempat_lahir_ibu');
			$data['tanggal_lahir_ibu']	= $this->input->post('tanggal_lahir_ibu');
			$id_status					= $this->input->post('id_status');
			// if($id_status < 3 )
			// {
			$data['id_jurusan']			= $this->input->post('id_jurusan');
			$data['id_jurusan2']		= $this->input->post('id_jurusan2');
			// }
			// else
			// {
				// $data['id_jurusan_diterima']= $this->input->post('id_jurusan_diterima');
			// }
			// print_r($id_calon_siswa);die();
			// print_r($data);die();
			$this->M_daftar->updateCalonSiswa($id_calon_siswa,$data);
			redirect('admin/Admin','refresh');
		}
		else
		{
			$this->session->set_flashdata('msg', 
					'<div class="alert alert-danger">
						<center>
							<h4>Maaf!</h4>
							<p>Anda belum login. Silakan login terlebih dahulu!</p>
						</center>
					</div>');	
			redirect('admin/Login');
		}
	}
	
	public function calonSiswaWawancara()
	{
		if($this->session->userdata('admin'))
		{
			$id_calon_siswa			= $this->uri->segment(4);
			$data['id_status']		= 3;
			$this->M_daftar->updateCalonSiswa($id_calon_siswa,$data);
			$data['act1']			= "active";
			$data['act2']			= "";
			$data['act3']			= "";
			$data['act4']			= "";
			$data['act5']			= "";
			$data['act6']			= "";
			$data['calon_siswa']	= $this->M_daftar->getSiswaJurusan($id_calon_siswa);
			$data['pertanyaan']		= $this->M_admin->getPertanyaanSiswa($data['calon_siswa']->kode_jurusan);
			$x						= 0;
			$data['pilihan_jawaban']=array();
			foreach($data['pertanyaan'] as $p)
			{
				$pilihan[$x]			= $this->M_admin->getPilihanJawaban($p['id_pertanyaan']);
				$data['pilihan_jawaban']= array_merge($data['pilihan_jawaban'],$pilihan[$x]);
				$x++;
			}
			// print_r($data['pilihan_jawaban']);die();
			$this->load->view('header',$data);
			$this->load->view('calonSiswaWawancara',$data);
			$this->load->view('footer');
		}
		else
		{
			$this->session->set_flashdata('msg', 
					'<div class="alert alert-danger">
						<center>
							<h4>Maaf!</h4>
							<p>Anda belum login. Silakan login terlebih dahulu!</p>
						</center>
					</div>');	
			redirect('admin/Login');
		}
	}
	
	public function calonSiswaWawancaraExec()
	{
		if($this->session->userdata('admin'))
		{
			$x						= $this->input->post('x');
			$data['id_calon_siswa']	= $this->input->post('id_calon_siswa');
			for($i=1;$i<=$x;$i++)
			{
				$jawaban					= $this->input->post('jawaban'.$i);
				$pecah						= explode("-",$jawaban);
				$data['id_pertanyaan']		= $pecah[0];
				$data['id_pilihan_jawaban']	= $pecah[1];
				$this->M_admin->addJawaban($data);
			}
			redirect('admin/Admin/calonSiswaFinalisasi/'.$data['id_calon_siswa'],'refresh');
		}
		else
		{
			$this->session->set_flashdata('msg', 
					'<div class="alert alert-danger">
						<center>
							<h4>Maaf!</h4>
							<p>Anda belum login. Silakan login terlebih dahulu!</p>
						</center>
					</div>');	
			redirect('admin/Login');
		}
	}
	
	public function calonSiswaFinalisasi()
	{
		if($this->session->userdata('admin'))
		{
			$data['act1']			= "active";
			$data['act2']			= "";
			$data['act3']			= "";
			$data['act4']			= "";
			$data['act5']			= "";
			$data['act6']			= "";
			$id_calon_siswa			= $this->uri->segment(4);
			$data2['jawaban']		= $this->M_admin->getCalonSiswaJawaban($id_calon_siswa);
			$data2['calon_siswa']	= $this->M_daftar->getSiswaJurusan($id_calon_siswa	);
			$data2['jurusan']		= $this->M_daftar->getJurusan();
			$this->load->view('header',$data);
			$this->load->view('calonSiswaFinalisasi',$data2);
			$this->load->view('footer');
		}
		else
		{
			$this->session->set_flashdata('msg', 
					'<div class="alert alert-danger">
						<center>
							<h4>Maaf!</h4>
							<p>Anda belum login. Silakan login terlebih dahulu!</p>
						</center>
					</div>');	
			redirect('admin/Login');
		}
	}
	
	public function calonSiswaFinalisasiExec()
	{
		if($this->session->userdata('admin'))
		{
			date_default_timezone_set('Asia/Jakarta');
			$data['admin_update']		= date('Y-m-d H:i:s');
			$session_data				= $this->session->userdata('admin');
			$data['id_admin']			= $session_data['id_admin'];
			$id_calon_siswa				= $this->input->post('id_calon_siswa');
			$data['hasil_wawancara']	= $this->input->post('hasil_wawancara');
			$data['id_jurusan_diterima']= $this->input->post('id_jurusan');
			if($data['hasil_wawancara'] == "Diterima")
			{
				$data['id_status']			= 4;
			}
			else
			{
				$data['id_status']			= 5;
			}
			$this->M_daftar->updateCalonSiswa($id_calon_siswa,$data);
			redirect('admin/Admin','refresh');
		}
		else
		{
			$this->session->set_flashdata('msg', 
					'<div class="alert alert-danger">
						<center>
							<h4>Maaf!</h4>
							<p>Anda belum login. Silakan login terlebih dahulu!</p>
						</center>
					</div>');	
			redirect('admin/Login');
		}
	}
	
	public function downloadHasilWawancara()
	{
		if($this->session->userdata('admin'))
		{
			date_default_timezone_set('Asia/Jakarta');
			$month 					= array("","Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");
			$data['today']			= date('d')." ".$month[date('n')]." ".date('Y');
			$data['act1']			= "active";
			$data['act2']			= "";
			$data['act3']			= "";
			$data['act4']			= "";
			$data['act5']			= "";
			$data['act6']			= "";
			$id_calon_siswa			= $this->uri->segment(4);
			$data['calon_siswa']	= $this->M_daftar->getSiswaJurusan($id_calon_siswa);
			$data['pertanyaan']		= $this->M_admin->getPertanyaanSiswa($data['calon_siswa']->kode_jurusan);
			$x						= 0;
			$data['pilihan_jawaban']=array();
			foreach($data['pertanyaan'] as $p)
			{
				$pilihan[$x]			= $this->M_admin->getPilihanJawaban($p['id_pertanyaan']);
				$data['pilihan_jawaban']= array_merge($data['pilihan_jawaban'],$pilihan[$x]);
				$x++;
			}
			$data['jawaban']		= $this->M_admin->getCalonSiswaJawaban($id_calon_siswa);
			$data['jurusan']		= $this->M_daftar->getJurusan();
			$data['tahun']			= date('Y');
			// print_r($data['jawaban']);die();
			$this->load->library('pdfgenerator');
			$file_pdf = 'Hasil Wawancara Full - '.$data['calon_siswa']->nama." - ".$data['calon_siswa']->nomor_pendaftaran;
			$data['title_pdf']		= $file_pdf;
			$paper = 'A4';
			$orientation = "portrait";
			$html = $this->load->view('hasilWawancara',$data, true);
			$this->pdfgenerator->generate($html, $file_pdf,$paper,$orientation);
			
			// $this->load->view('header',$data);
			// $this->load->view('hasilWawancara',$data);
			// $this->load->view('footer');
		}
		else
		{
			$this->session->set_flashdata('msg', 
					'<div class="alert alert-danger">
						<center>
							<h4>Maaf!</h4>
							<p>Anda belum login. Silakan login terlebih dahulu!</p>
						</center>
					</div>');	
			redirect('admin/Login');
		}
	}
	
	public function downloadHasilWawancaraSiswa()
	{
		if($this->session->userdata('admin'))
		{
			date_default_timezone_set('Asia/Jakarta');
			$month 					= array("","Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");
			$data['today']			= date('d')." ".$month[date('n')]." ".date('Y');
			$data['act1']			= "active";
			$data['act2']			= "";
			$data['act3']			= "";
			$data['act4']			= "";
			$data['act5']			= "";
			$data['act6']			= "";
			$id_calon_siswa			= $this->uri->segment(4);
			$data['calon_siswa']	= $this->M_daftar->getSiswaJurusan($id_calon_siswa);
			$data['pertanyaan']		= $this->M_admin->getPertanyaanSiswa($data['calon_siswa']->kode_jurusan);
			$x						= 0;
			$data['pilihan_jawaban']=array();
			foreach($data['pertanyaan'] as $p)
			{
				$pilihan[$x]			= $this->M_admin->getPilihanJawaban($p['id_pertanyaan']);
				$data['pilihan_jawaban']= array_merge($data['pilihan_jawaban'],$pilihan[$x]);
				$x++;
			}
			$data['jawaban']		= $this->M_admin->getCalonSiswaJawaban($id_calon_siswa);
			$data['jurusan']		= $this->M_daftar->getJurusan();
			$data['tahun']			= date('Y');
			// print_r($data['jawaban']);die();
			$this->load->library('pdfgenerator');
			$file_pdf = 'Hasil Wawancara - '.$data['calon_siswa']->nama." - ".$data['calon_siswa']->nomor_pendaftaran;
			$data['title_pdf']		= $file_pdf;
			$paper = 'A4';
			$orientation = "potrait";
			$html = $this->load->view('hasilWawancaraSiswa',$data, true);
			$this->pdfgenerator->generate($html, $file_pdf,$paper,$orientation);
			
			// $this->load->view('header',$data);
			// $this->load->view('hasilWawancara',$data);
			// $this->load->view('footer');
		}
		else
		{
			$this->session->set_flashdata('msg', 
					'<div class="alert alert-danger">
						<center>
							<h4>Maaf!</h4>
							<p>Anda belum login. Silakan login terlebih dahulu!</p>
						</center>
					</div>');	
			redirect('admin/Login');
		}
	}
}
