<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//error_reporting(0);
/*
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
*/
class Pesan extends CI_Controller {
      function __construct(){
            parent::__construct();
			$this->load->library('session');
			$this->load->model('m_home');
			$this->load->model('m_pesan');
			$this->load->model('m_icon');
			$this->load->model('m_infosewa');
			$this->load->library('form_validation');
      }
	public function index()
	{
	//query
	//siang
	$data['fasilitas'] = $this->m_home->fasilitas();
	$data['fasilitas2'] = $this->m_home->fasilitas2();
	//malam
	$data['fasilitasmalam'] = $this->m_home->fasilitasmalam();
	$data['fasilitasmalam2'] = $this->m_home->fasilitasmalam2();	
	//siangmalam
	$data['fasilitassiangmalam'] = $this->m_home->fasilitassiangmalam();
	$data['fasilitasmalam3'] = $this->m_home->fasilitasmalam3();	
	$data['infosewa'] = $this->m_infosewa->tampil();	
	/*
	//cek jatuh tempo 
	$cekdata = $this->m_pesan->pesan_lama();	
	foreach($cekdata->result() as $row){
	$jttempo = date('Y-m-d', strtotime('+3 days', strtotime( $row->tglpesan )));
	
	$jtsekarang = date('Y-m-d');
	//$tempo = $jttempo - $jthari;
	if($jtsekarang >= $jttempo){
		$idpesan = $row->idpesan;

		$data_save = array(
								'status' => '2'
						);			
		$where = array(
						'idpesan' => $idpesan,
						'disetujui' => 0
				);
		$this->m_pesan->update($where,$data_save,'pesan');						
	}
	}	
	*/
	//template
	$data['icon'] = $this->m_icon->tampilhome();		
	$headeractive = $this->uri->segment(1);
	$cek = $this->m_home->getmenu($headeractive);
	$title = $cek->title;
	$description = $cek->description;
	$data['title'] = $title;
	$data['alert'] = '';
	$data['pesan'] =  '';
	$data['detail'] =  'ada';
	$data['description'] =$description;
	$data['headeractive'] = $headeractive;
	$this->load->view('top1', $data);	
	$this->load->view('header',$data);		
	$this->load->view('pesan',$data);		
	$this->load->view('foot', $data);	
	$this->load->view('footjs1');
	}

	function pesan()
	{		
	if(isset($_POST['button'])){
	$a = array('\'', '/',';', '[', ']', '{', '}', '|', '^', '~',',');
	//str_replace($a,"",str_replace($a,"",$this->security->xss_clean($this->input->post('username')));	
	$ktp = str_replace($a,"",$this->security->xss_clean($this->input->post('ktp')));	
	$namapesan = str_replace($a,"",$this->security->xss_clean($this->input->post('namapesan')));	
	$hppesan = str_replace($a,"",$this->security->xss_clean($this->input->post('hppesan')));	
	$namapemakai = str_replace($a,"",$this->security->xss_clean($this->input->post('namapemakai')));	
	$emailpemesan = str_replace($a,"",$this->security->xss_clean($this->input->post('emailpemesan')));	
	$hppakai = str_replace($a,"",$this->security->xss_clean($this->input->post('hppakai')));	
	$alamat = str_replace($a,"",$this->security->xss_clean($this->input->post('alamat')));		
	$tglpakai1 = str_replace($a,"",$this->security->xss_clean($this->input->post('tglpakai')));	
	$tglselesai1 = str_replace($a,"",$this->security->xss_clean($this->input->post('tglselesai')));	
	//jam 
	$jam = str_replace($a,"",$this->security->xss_clean($this->input->post('jam')));	
	$menit = str_replace($a,"",$this->security->xss_clean($this->input->post('menit')));		
	$jampakai = '00:00';
	$keperluan = str_replace($a,"",$this->security->xss_clean($this->input->post('keperluan')));	
				
	$keterangan = str_replace($a,"",$this->security->xss_clean($this->input->post('keterangan')));		
	$pilihfasilitas = str_replace($a,"",$this->security->xss_clean($this->input->post('pilihfasilitas')));		
	$waktu = str_replace($a,"",$this->security->xss_clean($this->input->post('waktu')));		
	//pil siang 
	$jumlah_pil1siang = str_replace($a,"",$this->security->xss_clean($this->input->post('jumlah_pil1siang')));		
	$pilihfasilitas_pil1siang = str_replace($a,"",$this->security->xss_clean($this->input->post('pilihfasilitas_pil1siang')));	
	//pil malam
	$jumlah_pil1malam = str_replace($a,"",$this->security->xss_clean($this->input->post('jumlah_pil1malam')));		
	$pilihfasilitas_pil1malam = str_replace($a,"",$this->security->xss_clean($this->input->post('pilihfasilitas_pil1malam')));	
	
	//pil full
	$jumlah_pil1malam3 = str_replace($a,"",$this->security->xss_clean($this->input->post('jumlah_pil1malam3')));		
	$pilihfasilitas_pil1malam3 = str_replace($a,"",$this->security->xss_clean($this->input->post('pilihfasilitas_pil1malam3')));		
	$disetujui = str_replace($a,"",$this->security->xss_clean($this->input->post('disetujui')));		
	//tanggal
	$date=date_create($tglpakai1);
	$tglpakai = date_format($date,"Y-m-d");
	$tglpesan = date('Y-m-d H:i:s');
	$date=date_create($tglselesai1);
	$tglselesai = date_format($date,"Y-m-d");
	//hari subtotal 
	$tanggal = new DateTime($tglpakai); 
	$sekarang = new DateTime($tglselesai);
	$selisihhari = $tanggal->diff($sekarang);	
	$hariok  = 1;
	
	//Cek Wajib dan Kress Jadwal
	for($x = $tglpakai1; $x <= $tglselesai1; $x = date('Y-m-d', strtotime('+1 days', strtotime($x)))){
		//echo $x.'/';
		//Semua Kecuali Kamar
		for ($i=0; $i < count($pilihfasilitas) ; $i++){
			$fasilitas = $pilihfasilitas[$i]; 
			$data = explode("-" , $fasilitas);
			$idfasilitas =  $data[1];
			$idkategori  =  $this->m_home->getkategorifas($idfasilitas);
			//echo 'AAA'.$idkategori=$idkategori->idkategori;
			$namaFas =  $data[2];
			$cekJadwal = $this->m_home->getjadwalkress($idkategori,$x);
			
			//var_dump($cekJadwal);
			if($cekJadwal){$cektgl=1;
			$pesan = $pesan.''.$namaFas.' telah dipesan pemesan lain pada tanggal AA '.date('d F Y', strtotime($x)).'. <br />';
			}
			$cekWajib = $this->m_home->getfaswajib($idkategori);
			if($cekWajib){
				$jmlWajib = $jmlWajib+1;
			}
		}

		//cek khusus aula fullday
			if($waktu==2){
				$idkategori=1;
				$jmlWajib = $jmlWajib+1;
				$cekJadwal1 = $this->m_home->getjadwalkressaulasiang($idkategori,$x,$waktu);
				if($cekJadwal1){
					$cektgl=1;
					$pesan = $pesan.' Aula telah  dipesan pemesan lain pada waktu siang tanggal BB '.date('d F Y', strtotime($x)).'. <br />';
					$cekWajib = $this->m_home->getfaswajib($idkategori);
					if($cekWajib){
						$jmlWajib = $jmlWajib+1;		
					}
					//break;
				}else{		
					$idkategori_1  =  $this->m_home->getkategorifas($idfasilitas);
					if(!$idkategori_1){				
						$cekJadwal = $this->m_home->getjadwalkressaulamalam($idkategori,$x,$waktu);
						if($cekJadwal){	
						$cektgl=1;
						$pesan = $pesan.' Aula telah  dipesan pemesan lain pada waktu malam tanggal '.date('d F Y', strtotime($x)).'. <br />';
						$cekWajib = $this->m_home->getfaswajib($idkategori);
						if($cekWajib){
							$jmlWajib = $jmlWajib+1;
						}
						}
					}else{
						$cekWajib = $this->m_home->getfaswajib($idkategori);
						if($cekWajib){
							$jmlWajib = $jmlWajib+1;
						}						
					}					
					//break;
				}	
			}				
			

			if($waktu==0){
				$idkategori=1;
				$jmlWajib = $jmlWajib+1;
				//cek jadwal full day
				$cekJadwal1 = $this->m_home->getjadwalkressaulafull($idkategori,$x,$waktu);
				if($cekJadwal1){
					$cektgl=1;
					$pesan = $pesan.'Aula telah  dipesan pemesan lain pada waktu Full Day tanggal '.date('d F Y', strtotime($x)).'. <br />';
					$cekWajib = $this->m_home->getfaswajib($idkategori);
					if($cekWajib){
						$jmlWajib = $jmlWajib+1;
					}
					//break; 
				}else{		
					$idkategori_1  =  $this->m_home->getkategorifas($idfasilitas);
					if(!$idkategori_1){
						$cekJadwal = $this->m_home->getjadwalkressaula($idkategori,$x,$waktu);
						if($cekJadwal){	
						$cektgl=1;
						$pesan = $pesan.' Aula telah   dipesan pemesan lain pada waktu siang tanggal CC '.date('d F Y', strtotime($x)).'. <br />';
						$cekWajib = $this->m_home->getfaswajib($idkategori);
						if($cekWajib){
							$jmlWajib = $jmlWajib+1;
						}
					}
					}else{
						$cekWajib = $this->m_home->getfaswajib($idkategori);
						if($cekWajib){
							$jmlWajib = $jmlWajib+1;
						}						
					}
					
					//break;
				}
			}
            
			if($waktu==1){
				$idkategori=1;
				$jmlWajib = $jmlWajib+1;
				//cek jadwal full day
				$cekJadwal1 = $this->m_home->getjadwalkressaulafull($idkategori,$x,$waktu);
				if($cekJadwal1){
					$cektgl=1;
					$pesan = $pesan.' Aula telah  dipesan pemesan lain pada waktu Full Day tanggal '.date('d F Y', strtotime($x)).'. <br />';
					$cekWajib = $this->m_home->getfaswajib($idkategori);
					if($cekWajib){
						$jmlWajib = $jmlWajib+1;
					}
					//break;
				}else{	
					$idkategori_1  =  $this->m_home->getkategorifas($idfasilitas);
					if(!$idkategori_1){				
						$cekJadwal = $this->m_home->getjadwalkressaula($idkategori,$x,$waktu);
						if($cekJadwal){	
						$cektgl=1;
						$pesan = $pesan.' Aula telah  dipesan pemesan lain pada waktu  malam  tanggal '.date('d F Y', strtotime($x)).'. <br />';
						$cekWajib = $this->m_home->getfaswajib($idkategori);
						if($cekWajib){
							$jmlWajib = $jmlWajib+1;
						}
						}
						}else{
							$cekWajib = $this->m_home->getfaswajib($idkategori);
							if($cekWajib){
								$jmlWajib = $jmlWajib+1;
							}						
					}					
					//break;
				}
			}
				
			//var_dump($pilihfasilitas_pil1siang);
		 
		
		//Kamar/Asrama
		if($pilihfasilitas_pil1siang != ''){
		    //echo "Siang Bro";
			$jmlWajib = $jmlWajib+1;
			$fasilitas = $pilihfasilitas_pil1siang; 
			$data = explode("-" , $fasilitas);
			$idfasilitas =  $data[1];
			$idkategori  =  $this->m_home->getkategorifas($idfasilitas);
			$idkategori=$idkategori->idkategori;
			$cekJadwalKamar = $this->m_home->getjadwalkamar($idkategori,$x);
			$jumlahKamar = $cekJadwalKamar->jumlahKamar; 
			if($jumlah_pil1siang + $jumlahKamar > 15){
				$cektgl = 1;
				$sisaKamar = 15 - $jumlahKamar;
				$pesan = $pesan.'Kamar/Asrama telah dipesan, untuk saat ini hanya tersisa '.$sisaKamar.' kamar. <br />';
			}
		}else if($pilihfasilitas_pil1malam != ''){
		    //echo "Sampai Sini!!!!";
			$jmlWajib = $jmlWajib+1;
			$fasilitas = $pilihfasilitas_pil1malam; 
			$data = explode("-" , $fasilitas);
			$idfasilitas =  $data[1];
			$idkategori  =  $this->m_home->getkategorifas($idfasilitas);
			$idkategori=$idkategori->idkategori;
			$cekJadwalKamar = $this->m_home->getjadwalkamar($idkategori,$x);
			$jumlahKamar = $cekJadwalKamar->jumlahKamar; 
			if($jumlah_pil1malam + $jumlahKamar > 15){
				$cektgl = 1;
				$sisaKamar = 15 - $jumlahKamar;
				$pesan = $pesan.'Kamar/Asrama telah dipesan, untuk saat ini hanya tersisa '.$sisaKamar.' kamar. <br />';
			}
		}else if($pilihfasilitas_pil1malam3 != ''){
			$jmlWajib = $jmlWajib+1;
			$fasilitas = $pilihfasilitas_pil1malam3; 
			$data = explode("-" , $fasilitas);
			$idfasilitas =  $data[1];
			$idkategori  =  $this->m_home->getkategorifas($idfasilitas);
			$idkategori=$idkategori->idkategori;
			$cekJadwalKamar = $this->m_home->getjadwalkamar($idkategori,$x);
			$jumlahKamar = $cekJadwalKamar->jumlahKamar; 
			if($jumlah_pil1malam3 + $jumlahKamar > 15){
				$cektgl = 1;
				$sisaKamar = 15 - $jumlahKamar;
				$pesan = $pesan.'Kamar/Asrama telah dipesan, untuk saat ini hanya tersisa '.$sisaKamar.' kamar. <br />';
			}
		}
	}
	if($jmlWajib == 0){
		$cektgl = 1;
		$pesan = $pesan.' ini hanya bisa di pesan apabila anda memesan fasilitas Wajib (Aula,Asrama/Kamar,Kelas)! <br />';
	}
	if($cektgl==0){
	//kosong
	$idpesan = $this->m_home->getidpesan();			
	$data_save = array(
										'idpesan' => $idpesan,
										'ktp' => $ktp,
										'namapesan' => $namapesan,
										'hppesan' => $hppesan,
										'namapemakai' => $namapemakai,
										'emailpemesan' => $emailpemesan,
										'hppakai' => $hppakai,
										'alamat' => $alamat,
										'tglpesan' => $tglpesan,
										'tglpakai' => $tglpakai,
										'tglselesai' => $tglselesai,
										'jampakai' => $jampakai,
										'keperluan' => $keperluan,
										'keterangan' => $keterangan,
										'status' => 1,	
										'pemkot' => 2,		
										'siangmalam' =>$waktu,
										'disetujui' =>1,	
								);
	$this->m_home->in($data_save,'pesan');		
	//getidpesan
	//var_dump($pilihfasilitas);
	if($pilihfasilitas!=''){
	//detail	
    for ($i=0; $i < count($pilihfasilitas) ; $i++){
	for($x = $tglpakai1; $x <= $tglselesai1; $x = date('Y-m-d', strtotime('+1 days', strtotime($x)))){	
	$idpesandetail = $this->m_home->getidpesandetail();		
    $fasilitas = $pilihfasilitas[$i]; 
	$data = explode("-" , $fasilitas);
	$idfasilitas =  $data[1];
	$harga =  $data[0];
	$jumlah = 1;
	$save = array(
										'idpesandetail' => $idpesandetail,
										'idpesan' => $idpesan,
										'idfasilitas' => $idfasilitas,
										'hari' => $hariok,
										'jumlah' => $jumlah,
										'harga' => $harga,
										'subtotal' =>$hariok * $harga * $jumlah, 
										'tanggal' => $x,
								);
	$this->m_home->in($save,'pesandetail');	}		
	}	
	}
	//pilihan siang
	if($pilihfasilitas_pil1siang!=''){
	for($x = $tglpakai1; $x <= $tglselesai1; $x = date('Y-m-d', strtotime('+1 days', strtotime($x)))){	
	//detail malam
	$idpesandetail = $this->m_home->getidpesandetail();		
    //$fasilitas = $pilihfasilitas[$i]; 
	$data = explode("-" , $pilihfasilitas_pil1siang);
	$idfasilitas1 =  $data[1];
	$harga1 =  $data[0];
	$save = array(
										'idpesandetail' => $idpesandetail,
										'idpesan' => $idpesan,
										'idfasilitas' => $idfasilitas1,
										'hari' => $hariok,
										'jumlah' => $jumlah_pil1siang,
										'harga' => $harga1,
										'subtotal' => $hariok * $harga1 * $jumlah_pil1siang,
										'tanggal' => $x,
								);
	$this->m_home->in($save,'pesandetail');			
	}}
	

	//pilihan siang
	if($pilihfasilitas_pil1malam!=''){
	for($x = $tglpakai1; $x <= $tglselesai1; $x = date('Y-m-d', strtotime('+1 days', strtotime($x)))){	
	//detail malam
	$idpesandetail = $this->m_home->getidpesandetail();		
    //$fasilitas = $pilihfasilitas[$i]; 
	$data = explode("-" , $pilihfasilitas_pil1malam);
	$idfasilitas1 =  $data[1];
	$harga1 =  $data[0];
	$save = array(
										'idpesandetail' => $idpesandetail,
										'idpesan' => $idpesan,
										'idfasilitas' => $idfasilitas1,
										'hari' => $hariok,
										'jumlah' => $jumlah_pil1malam,
										'harga' => $harga1,
										'subtotal' => $hariok * $harga1 * $jumlah_pil1malam,
										'tanggal' => $x,
								);
	$this->m_home->in($save,'pesandetail');			
	}}


	//pilihan siangmalam / full
	if($pilihfasilitas_pil1malam3!=''){
	for($x = $tglpakai1; $x <= $tglselesai1; $x = date('Y-m-d', strtotime('+1 days', strtotime($x)))){	
	//detail malam
	$idpesandetail = $this->m_home->getidpesandetail();		
    //$fasilitas = $pilihfasilitas[$i]; 
	$data = explode("-" , $pilihfasilitas_pil1malam3);
	$idfasilitas1 =  $data[1];
	$harga1 =  $data[0];
	$save = array(
										'idpesandetail' => $idpesandetail,
										'idpesan' => $idpesan,
										'idfasilitas' => $idfasilitas1,
										'hari' => $hariok,
										'jumlah' => $jumlah_pil1malam3,
										'harga' => $harga1,
										'subtotal' => $hariok * $harga1 * $jumlah_pil1malam3,
										'tanggal' => $x,
								);
	$this->m_home->in($save,'pesandetail');			
	}}
	
	
	//$cek pilih atau tidak 
	$cekpilih = $this->m_home->cekPilih($idpesan);
	if($cekpilih ==0){
		//delete
		$where = array(
		'idpesan' => $idpesan
		);
		$this->m_home->hapus($where,'pesan');
		session_destroy();
		echo '<script language="javascript">alert("Maaf, Anda Belum Memilih Fasilitas. Silahakan Cek Kembali !"); document.location="https://gedungdiklat.madiunkota.go.id/pesan";</script>';	
	}else{
		$data['alert'] = 'success';
		$data['pesan'] = 'Berhasil di simpan';
		//template
		//siang
		$data['fasilitas'] = $this->m_home->fasilitas();
		$data['fasilitas2'] = $this->m_home->fasilitas2();
		//malam
		$data['fasilitasmalam'] = $this->m_home->fasilitasmalam();
		$data['fasilitasmalam2'] = $this->m_home->fasilitasmalam2();	
		//siangmalam
		$data['fasilitassiangmalam'] = $this->m_home->fasilitassiangmalam();
		$data['fasilitasmalam3'] = $this->m_home->fasilitasmalam3();
		$data['icon'] = $this->m_icon->tampilhome();	
		$data['infosewa'] = $this->m_infosewa->tampilhome();	
		$headeractive = $this->uri->segment(1);
		$cek = $this->m_home->getmenu($headeractive);
		$title = $cek->title;
		$description = $cek->description;
		$data['idpesan'] = $idpesan;
		$data['title'] = $title;
		$data['description'] =$description;
		$data['headeractive'] = $headeractive;
		$data_session = array(
		'idpesan' => $idpesan,
		);
		$this->session->set_userdata($data_session);
		/*
		$this->load->view('top1', $data);	
		$this->load->view('header',$data);		
		$this->load->view('pesan',$data);		
		$this->load->view('foot', $data);
		$this->load->view('footjs1');
		*/
		//echo '<pre>';
		//var_dump($x);
		//echo '</pre>';
		$data['idpesan'] = $idpesan;
		$this->load->view('top1', $data);	
		$this->load->view('header',$data);		
		$this->load->view('selesai',$data);		
		$this->load->view('foot');	
		$this->load->view('footjs1');	

		/*
		$idpesan = $this->session->userdata('idpesan');	
		$data['tampil'] = $this->m_pesan->pesandetail($idpesan);
		$data['tampil2'] = $this->m_pesan->cetakpesan($idpesan);
		//ttd 
		$bagian='cetakpesan';
		$td = $this->m_pesan->ttdpesan($bagian);
		$data['headerttd'] = $td->headerttd;
		$data['sebagai'] = $td->sebagai;
		$data['nama'] = $td->nama;
		$data['nip'] = $td->nip;
		$this->load->view('cetakpesan', $data);
		*/
	}
	}else{
	$data['alert'] = 'danger';
	$data['pesan'] = $pesan;
	$data['detail'] =  'ada';	
	//template
	//siang
	$data['fasilitas'] = $this->m_home->fasilitas();
	$data['fasilitas2'] = $this->m_home->fasilitas2();
	//malam
	$data['fasilitasmalam'] = $this->m_home->fasilitasmalam();
	$data['fasilitasmalam2'] = $this->m_home->fasilitasmalam2();	
	//siangmalam
	$data['fasilitassiangmalam'] = $this->m_home->fasilitassiangmalam();
	$data['fasilitasmalam3'] = $this->m_home->fasilitasmalam3();
	$data['icon'] = $this->m_icon->tampilhome();	
	$data['infosewa'] = $this->m_infosewa->tampilhome();		
	$headeractive = $this->uri->segment(1);
	$cek = $this->m_home->getmenu($headeractive);
	$title = $cek->title;
	$description = $cek->description;
	$data['title'] = $title;
	$data['description'] =$description;
	$data['headeractive'] = $headeractive;
	$this->load->view('top1', $data);	
	$this->load->view('header',$data);		
	$this->load->view('pesan',$data);		
	$this->load->view('foot', $data);
	$this->load->view('footjs1');	
	}
	
	}
	}
	
	function cetakpesan(){
	//$idpesan = $this->session->userdata('idpesan');	
	$idpesan = $this->uri->segment(3);
	$data['tampil'] = $this->m_pesan->pesandetail($idpesan);
	$data['tampil2'] = $this->m_pesan->cetakpesan($idpesan);
	//ttd 
	$bagian='cetakpesan';
	$td = $this->m_pesan->ttdpesan($bagian);
	$data['headerttd'] = $td->headerttd;
	$data['sebagai'] = $td->sebagai;
	$data['nama'] = $td->nama;
	$data['nip'] = $td->nip;
	$this->load->view('cetakpesan', $data);	
	}	
	
	function cetaknota(){
	$idpesan = $this->session->userdata('idpesan');	
	$data['tampil'] = $this->m_pesan->pesandetail($idpesan);
	$data['tampil2'] = $this->m_pesan->cetakpesan($idpesan);
	//ttd 
	$bagian='cetakpesan';
	$td = $this->m_pesan->ttdpesan($bagian);
	$data['headerttd'] = $td->headerttd;
	$data['sebagai'] = $td->sebagai;
	$data['nama'] = $td->nama;
	$data['nip'] = $td->nip;
	$this->load->view('cetaknota', $data);	
	}		
}
