<?php
defined('BASEPATH') OR exit('No direct script access allowed');
error_reporting(0);
class Pesan extends CI_Controller {

	function __construct(){
		parent::__construct();
			$this->load->library('session');
			$this->load->library('form_validation');
			$this->load->model('m_home');
			$this->load->model('m_pesan');
			if($this->session->userdata('username') =='' OR $this->session->userdata('token') !=md5('100%Madiun'.date('Ymd')) ){
				redirect(base_url("login"));
			}			
	} 
	public function index()
	{
	$status='0';
	$data['tampil'] = $this->m_pesan->pesan($status);	
	$data['alert']='';
	$data['pesan']='';
	
	$this->load->view('admin/pesan', $data);			
		
	}

	function up()
	{
	//pesan jika di update
	 $status = $this->uri->segment(4);
	 $idpesan = $this->uri->segment(5);
	 $alert = $this->uri->segment(6);
	if($status==0 AND $alert=='p' AND $idpesan!=''){
	$data['tampil'] = $this->m_pesan->pesan($status);
	//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['alert']='warning';
	$data['pesan']='No.reg:'.$idpesan.', Data Baru';
	$this->load->view('admin/pesan', $data);			
	}elseif($status==1 AND $alert=='s' AND $idpesan!=''){		
	$data['tampil'] = $this->m_pesan->pesan($status);
	//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['alert']='success';
	$data['pesan']='No.reg:'.$idpesan.', Data berhasil diterima';
	$this->load->view('admin/pesan', $data);					
	}elseif($status==2 AND $alert=='d' AND $idpesan!=''){		
	$data['tampil'] = $this->m_pesan->pesan($status);	
	//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['alert']='danger';
	$data['pesan']='No.reg:'.$idpesan.', Data berhasil ditolak';
	$this->load->view('admin/pesan', $data);					
	}else{
	$status='0';
	$data['tampil'] = $this->m_pesan->pesan($status);
	//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();	
	$data['alert']='';
	$data['pesan']='';
	$this->load->view('admin/pesan', $data);	
	}
	}
	
	function status()
	{
	if(isset($_POST['status'])){
	$status=$this->security->xss_clean($this->input->post('status'));	
	$data['tampil'] = $this->m_pesan->pesan($status);	
	//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['alert']='';
	$data['pesan']='';
	$this->load->view('admin/pesan', $data);
	}	
	}


	function cek()
	{
	if(isset($_POST['button'])){
	$idpesan=$this->security->xss_clean($this->input->post('idpesan'));		
	$status='0';	
	$data['tampil'] = $this->m_pesan->pesan($status);	
	$data_save = array(
							'status' => $status
					);			
	$where = array(
					'idpesan' => $idpesan
			);
	$this->m_pesan->update($where,$data_save,'pesan');		
	
	echo '<script>window.location.href = "'.base_url().'admin/pesan/up/'.$status.'/'.$idpesan.'/p ";</script>';				
	}elseif(isset($_POST['button1'])){
	$idpesan=$this->security->xss_clean($this->input->post('idpesan'));		
	$status='1';	
	$data['tampil'] = $this->m_pesan->pesan($status);	
	$data_save = array(
							'status' => $status
					);			
	$where = array(
					'idpesan' => $idpesan
			);
	$this->m_pesan->update($where,$data_save,'pesan');		
	echo '<script>window.location.href = "'.base_url().'admin/pesan/up/'.$status.'/'.$idpesan.'/s ";</script>';			
		
	}elseif(isset($_POST['button2'])){
	$idpesan=$this->security->xss_clean($this->input->post('idpesan'));		
	$status='2';	
	$data['tampil'] = $this->m_pesan->pesan($status);	
	$data_save = array(
							'status' => $status
					);			
	$where = array(
					'idpesan' => $idpesan
			);
	$this->m_pesan->update($where,$data_save,'pesan');		
	echo '<script>window.location.href = "'.base_url().'admin/pesan/up/'.$status.'/'.$idpesan.'/d ";</script>';					
	}elseif(isset($_POST['detail'])){
	$idpesan=$this->security->xss_clean($this->input->post('idpesan'));	
	$data_session = array(
	'idpesan' => $idpesan,
	);
	$this->session->set_userdata($data_session);		
	$data['tampil']= $this->m_pesan->pesandetail($idpesan);	
	//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['idpesan']= $idpesan;	
	$tot= $this->m_pesan->totalharga($idpesan);	
	$totalharga = $tot->totalharga;
	$data['totalharga']=$totalharga;
	$data['alert']='';
	$data['pesan']='';
	$this->load->view('admin/pesandetail', $data);				
	}else{
	$status='0';
	$data['tampil'] = $this->m_pesan->pesan($status);	
	$data['alert']='';
	$data['pesan']='';
	$this->load->view('admin/pesan', $data);			
	}
	}	
	function simpandetail()
	{
	if(isset($_POST['button'])){

	$idpesan = $this->security->xss_clean($this->input->post('idpesan'));
	$ktp = $this->security->xss_clean($this->input->post('ktp'));
	$namapesan = $this->security->xss_clean($this->input->post('namapesan'));
	$hppesan = $this->security->xss_clean($this->input->post('hppesan'));
	$namapemakai = $this->security->xss_clean($this->input->post('namapemakai'));
	$emailpemesan = $this->security->xss_clean($this->input->post('emailpemesan'));
	$hppakai = $this->security->xss_clean($this->input->post('hppakai'));
	$alamat = $this->security->xss_clean($this->input->post('alamat'));	
	$tglpakai1 = $this->security->xss_clean($this->input->post('tglpakai'));
	$tglselesai1 = $this->security->xss_clean($this->input->post('tglselesai'));
	$jampakai = $this->security->xss_clean($this->input->post('jampakai'));
	$keperluan = $this->security->xss_clean($this->input->post('keperluan'));
	$keterangan = $this->security->xss_clean($this->input->post('keterangan'));	
	$pilihfasilitas = $this->security->xss_clean($this->input->post('pilihfasilitas'));	
	$pilihfasilitas2 = $this->security->xss_clean($this->input->post('pilihfasilitas2'));	
	$pilihfasilitas3 = $this->security->xss_clean($this->input->post('pilihfasilitas3'));	

	$tgldp1 = $this->security->xss_clean($this->input->post('tgldp1'));	
	$tgldp2 = $this->security->xss_clean($this->input->post('tgldp2'));	
	//bayar 
	$a = array(',','.00');
	$b = array('','');
	$statusdp = $this->security->xss_clean($this->input->post('statusdp'));	
	$validgrandtotal = str_replace($a,$b, $this->security->xss_clean($this->input->post('validgrandtotal')));	
	$dp1 = str_replace($a,$b, $this->security->xss_clean($this->input->post('dp1')));		
	$dp2 = str_replace($a,$b, $this->security->xss_clean($this->input->post('dp2')));		
	$statusacara = $this->security->xss_clean($this->input->post('statusacara'));	
	$pemkot = $this->security->xss_clean($this->input->post('pemkot'));	
	$waktu = $this->security->xss_clean($this->input->post('waktu'));	

	//pil siang
	$jumlah_pil1siang = $this->security->xss_clean($this->input->post('jumlah_pil1siang'));	
	$pilihfasilitas_pil1siang = $this->security->xss_clean($this->input->post('pilihfasilitas_pil1siang'));
	//pil malam
	$jumlah_pil1malam = $this->security->xss_clean($this->input->post('jumlah_pil1malam'));	
	$pilihfasilitas_pil1malam = $this->security->xss_clean($this->input->post('pilihfasilitas_pil1malam'));		
	//pil full
	$jumlah_pil1full = $this->security->xss_clean($this->input->post('jumlah_pil1full'));	
	$pilihfasilitas_pil1full = $this->security->xss_clean($this->input->post('pilihfasilitas_pil1full'));		
	//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  = (($selisihhari->d)+1);


		//cek idkategori 
		echo $cekkategori = $this->m_pesan->ambilidkategori($idfasilitas);
		echo $idkategori = $cekkategori->idkategori;	
		//cek apakah di tanggal pakai ini sudah ada  acara ?
		$cektgl = $this->m_pesan->cektanggal($tglpakai, $tglselesai, $waktu, $idkategori);
		if($cektgl==0){
		
		$totalbayar =$dp1+$dp2;
		if($totalbayar>=$validgrandtotal){
			//lunas
			//$statusacara ='1';
			$statusdp ='1';
			$namastatusdp = '<strong>Status Pembayaran : Sudah Lunas</strong>';		
		}else{
			//belum
			$statusdp='0';
			$namastatusdp = '<strong style="color:red">Status Pembayaran :Belum Lunas</strong>';		
		}
		 if($dp1!='0' ){
			//$statusacara ='1';
		 }
		//getidpesan
		$data_save = array(
										
											'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,
											'dp1' => $dp1,										
											'tgldp1' => $tgldp1,										
											'dp2' => $dp2,										
											'tgldp2' => $tgldp2,	
											'status' => $statusacara,	
											'statusdp' => $statusdp,										
											'pemkot' => $pemkot,	
											'siangmalam' =>$waktu,										
									);
			$where = array(
							'idpesan' => $idpesan
					);
		$this->m_pesan->update($where,$data_save,'pesan');	
		//Pilih Waktu
		//SIANG 1
		if($waktu==0  AND $pilihfasilitas!='' ){
		//delete
		$where = array(
						'idpesan' => $idpesan
				);
		$this->m_pesan->hapus($where,'pesandetail');			
		for ($i=0; $i < count($pilihfasilitas) ; $i++){
		$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, 
									);
		$this->m_home->in($save,'pesandetail');			
		}	
		}
		
		//SIANG 1
		if($waktu==0  AND $pilihfasilitas_pil1siang!='' ){		
		//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,
									);
		$this->m_home->in($save,'pesandetail');			
		}
		
		//MALAM
		if($waktu==1 AND $pilihfasilitas2!=''){
		//delete
		$where = array(
						'idpesan' => $idpesan
				);
		$this->m_pesan->hapus($where,'pesandetail');		
		for ($i=0; $i < count($pilihfasilitas2) ; $i++){
		$idpesandetail = $this->m_home->getidpesandetail();		
		$fasilitas = $pilihfasilitas2[$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, 
									);
		$this->m_home->in($save,'pesandetail');	
		}
		}
		
		if($waktu==1 AND $pilihfasilitas_pil1malam!=''){		
		//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,
									);
		$this->m_home->in($save,'pesandetail');			
		}
		//FULL
		if($waktu==2 AND $pilihfasilitas_pil1full!=''){
		//delete
		$where = array(
						'idpesan' => $idpesan
				);
		$this->m_pesan->hapus($where,'pesandetail');		
		for ($i=0; $i < count($pilihfasilitas3) ; $i++){
		$idpesandetail = $this->m_home->getidpesandetail();		
		$fasilitas = $pilihfasilitas3[$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, 
									);
		$this->m_home->in($save,'pesandetail');	
		}
		}
		//FULL
		if($waktu==2 AND $pilihfasilitas_pil1full!=''){		

		//detail malam
		$idpesandetail = $this->m_home->getidpesandetail();		
		//$fasilitas = $pilihfasilitas[$i]; 
		$data = explode("-" , $pilihfasilitas_pil1full);
		$idfasilitas1 =  $data[1];
		$harga1 =  $data[0];
		$save = array(
											'idpesandetail' => $idpesandetail,
											'idpesan' => $idpesan,
											'idfasilitas' => $idfasilitas1,
											'hari' => $hariok,
											'jumlah' => $jumlah_pil1full,
											'harga' => $harga1,
											'subtotal' => $hariok * $harga1 * $jumlah_pil1full,
									);
		$this->m_home->in($save,'pesandetail');			
		}
		
		$data['alert']='success';
		$data['pesan']='No.reg:'.$idpesan.', Data disimpan, '.$namastatusdp;
		$idpesan=$this->security->xss_clean($this->input->post('idpesan'));	
		$data['tampil']= $this->m_pesan->pesandetail($idpesan);	
		//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['idpesan']= $idpesan;	
		$tot= $this->m_pesan->totalharga($idpesan);	
		$totalharga = $tot->totalharga;
		$data['totalharga']=$totalharga;
		$this->load->view('admin/pesandetail', $data);	

		}else{
		$tglpakaiada = date('d F Y', strtotime($tglpakai1));	
		$tglselesaiada = date('d F Y', strtotime($tglselesai1));	
		$data['alert']='danger';
		$data['pesan']='<strong>Gagal</strong>, Antara Tanggal Pakai: '.$tglpakaiada.' s/d Tanggal Selesai :'. $tglselesaiada.' Telah Digunakan' ;
		$idpesan=$this->security->xss_clean($this->input->post('idpesan'));	
		$data['tampil']= $this->m_pesan->pesandetail($idpesan);	
		//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['idpesan']= $idpesan;	
		$tot= $this->m_pesan->totalharga($idpesan);	
		$totalharga = $tot->totalharga;
		$data['totalharga']=$totalharga;
		$this->load->view('admin/pesandetail', $data);			
		}	
	}elseif(isset($_POST['button2'])){
	//batalkan	
	$idpesan=$this->security->xss_clean($this->input->post('idpesan'));		
	$status='2';	
	$data['tampil'] = $this->m_pesan->pesan($status);	
	$data_save = array(
							'status' => $status
					);			
	$where = array(
					'idpesan' => $idpesan
			);
	$this->m_pesan->update($where,$data_save,'pesan');		
	echo '<script>window.location.href = "'.base_url().'admin/pesan/up/'.$status.'/'.$idpesan.'/d ";</script>';					
	}elseif(isset($_POST['button3'])){
	//atur jadwal baru
	$idpesan=$this->security->xss_clean($this->input->post('idpesan'));
	$tglpakai1 = $this->security->xss_clean($this->input->post('tglpakai'));
	$tglselesai1 = $this->security->xss_clean($this->input->post('tglselesai'));
	$jampakai = $this->security->xss_clean($this->input->post('jampakai'));
	$waktu = $this->security->xss_clean($this->input->post('waktu'));
	$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");
	//cek apakah di tanggal pakai ini sudah ada  acara ?
	//cek idkategori 
	$cekkategori = $this->m_pesan->ambilidkategori($idfasilitas);
	$idkategori = $cekkategori->idkategori;	
	//cek apakah di tanggal pakai ini sudah ada  acara ?
	$cektgl = $this->m_pesan->cektanggal($tglpakai, $tglselesai, $waktu, $idkategori);
	if($cektgl==0){	
	$data['tampil'] = $this->m_pesan->pesan($status);	
	$data_save = array(
								'tglpesan' => $tglpesan,
								'tglpakai' => $tglpakai,
								'tglselesai' => $tglselesai,
								'jampakai' => $jampakai
					);			
	$where = array(
					'idpesan' => $idpesan
			);
	$this->m_pesan->update($where,$data_save,'pesan');		
	$data['alert']='success';
	$data['pesan']='No.reg:'.$idpesan.', Jadwal Berhasil di Perbarui';
	$idpesan=$this->security->xss_clean($this->input->post('idpesan'));	
	$data['tampil']= $this->m_pesan->pesandetail($idpesan);	
	//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['idpesan']= $idpesan;	
	$tot= $this->m_pesan->totalharga($idpesan);	
	$totalharga = $tot->totalharga;
	$data['totalharga']=$totalharga;
	$this->load->view('admin/pesandetail', $data);					
	}else{
	$tglpakaiada = date('d F Y', strtotime($tglpakai1));	
	$tglselesaiada = date('d F Y', strtotime($tglselesai1));	
	$data['alert']='danger';
	$data['pesan']='<strong>Gagal</strong>, Antara Tanggal Pakai: '.$tglpakaiada.' s/d Tanggal Selesai :'. $tglselesaiada.' Telah Digunakan' ;
	$idpesan=$this->security->xss_clean($this->input->post('idpesan'));	
	$data['tampil']= $this->m_pesan->pesandetail($idpesan);	
	//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['idpesan']= $idpesan;	
	$tot= $this->m_pesan->totalharga($idpesan);	
	$totalharga = $tot->totalharga;
	$data['totalharga']=$totalharga;
	$this->load->view('admin/pesandetail', $data);	
	}
	}
	}

	

}