<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//ini_set('display_errors', 1);
//ini_set('display_startup_errors', 1);
//error_reporting(E_ALL);
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()
	{
	$this->session->unset_userdata('idpesan');
	$status='1';
	$data['tampilpesan'] = $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['tampilpesan'] = $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['tampilpesan'] = $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['tampilpesan'] = $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['tampilpesan'] = $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'])){
	$this->session->unset_userdata('idpesan');
	 $status=$this->security->xss_clean($this->input->post('status'));
	 $data['status_pilih'] = $status;	
	 $data['tampilpesan'] = $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 kosongnotif()
	{
	$data_save = array(
							'notif' => 1
					);			
	$where = array(
					'notif' => 0
			);
	$this->m_pesan->update($where,$data_save,'pesan');		
	$this->session->unset_userdata('idpesan');
	$status='1';
	$data['tampilpesan'] = $this->m_pesan->pesan($status);	
	$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['tampilpesan'] = $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['tampilpesan'] = $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['tampilpesan'] = $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'])){
	*/	
	if(isset($_POST['detail'])){
	$this->session->unset_userdata('idpesan');
	$idpesan=$this->security->xss_clean($this->input->post('idpesan'));		
	$data_session = array(
	'idpesan' => $idpesan,
	);
	$this->session->set_userdata($data_session);		
	$data['tampilpesandetail']= $this->m_pesan->pesandetail($idpesan);	
	$data['cetakpesan']= $this->m_pesan->cetakpesan($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);	
	}elseif(isset($_POST['lunas'])){
	$idpesan=$this->security->xss_clean($this->input->post('idpesan'));		
	$tglpesan = $this->m_pesan->tglpesan($idpesan);
	$totalharga = $this->m_pesan->totalharga_lunas($idpesan);
	$pamkot = $this->m_pesan->pamkot($idpesan);
	$date=date_create($tglpesan);
	$tgldp1 = date_format($date,"Y-m-d");
	
	if($pamkot==1){
		$total = $totalharga / 2;
		$data_save = array(					
											'dp1' => $total,										
											'tgldp1' => $tgldp1,										
											'statusdp' => 1,																			
									);
			$where = array(
							'idpesan' => $idpesan
					);
		$this->m_pesan->update($where,$data_save,'pesan');			
	}else{
		$data_save = array(					
											'dp1' => $totalharga,										
											'tgldp1' => $tgldp1,										
											'statusdp' => 1,																			
									);
			$where = array(
							'idpesan' => $idpesan
					);
		$this->m_pesan->update($where,$data_save,'pesan');				
	}

	$status='1';
	$data['tampilpesan'] = $this->m_pesan->pesan($status);	
	$data['alert']='success';
	$data['pesan']= 'No.Reg '.$idpesan.' Sudah Lunas';
	$this->load->view('admin/pesan', $data);
	
	
	}else{
	$status='1';
	$data['tampilpesan'] = $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'));		
	$disetujui = $this->security->xss_clean($this->input->post('disetujui'));		
	$noskrd = $this->security->xss_clean($this->input->post('noskrd'));		
	if($disetujui==1){
		if($noskrd==0 OR $noskrd ==''){
			$noskrdbaru = $this->m_pesan->noskrdbaru();
		}else{
			$noskrdbaru = $noskrd;
		}
	}else{
		$noskrdbaru = 0;
	}
	//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);	
	if($dp1 >= $validgrandtotal){
		$statusdp = 1;
		}else{
		$statusdp =0;
		}
	$hariok  = 1;
	
		//getidpesan
		$data_save = array(
										
											'ktp' => $ktp,
											'namapesan' => $namapesan,
											'hppesan' => $hppesan,
											'namapemakai' => $namapemakai,
											'emailpemesan' => $emailpemesan,
											'hppakai' => $hppakai,
											'alamat' => $alamat,
											'jampakai' => $jampakai,
											'keperluan' => $keperluan,
											'keterangan' => $keterangan,
											'dp1' => $dp1,										
											'tgldp1' => $tgldp1,										
											//'status' => $statusacara,	
											'statusdp' => $statusdp,										
											'pemkot' => $pemkot,	
											'siangmalam' =>$waktu,										
											'disetujui' =>$disetujui,										
											'noskrd' =>$noskrdbaru,										
									);
			$where = array(
							'idpesan' => $idpesan
					);
		$this->m_pesan->update($where,$data_save,'pesan');	
	
		$data['alert']='success';
		$data['pesan']='No.reg:'.$idpesan.', Data disimpan, '.$namastatusdp;
		$idpesan=$this->security->xss_clean($this->input->post('idpesan'));	
		$data['tampilpesandetail']= $this->m_pesan->pesandetail($idpesan);	
		$data['cetakpesan']= $this->m_pesan->cetakpesan($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['tampilpesan'] = $this->m_pesan->pesan($status);	
	$data_save = array(
							'status' => $status,
							'noskrd' => 0
					);			
	$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['button4'])){
	//batalkan	
	$idpesan=$this->security->xss_clean($this->input->post('idpesan'));		
	$status='1';	
	$data['tampilpesan'] = $this->m_pesan->pesan($status);	
	$data_save = array(
						'statusdp' => 0,	
						'noskrd' => 0
					);			
	$where = array(
					'idpesan' => $idpesan
			);
	$this->m_pesan->update($where,$data_save,'pesan');		
	echo '<script>window.location.href = "'.base_url().'admin/pesan/ ";</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['tampilpesan'] = $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['tampilpesandetail']= $this->m_pesan->pesandetail($idpesan);	
	$data['cetakpesan']= $this->m_pesan->cetakpesan($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['tampilpesandetail']= $this->m_pesan->pesandetail($idpesan);	
	$data['cetakpesan']= $this->m_pesan->cetakpesan($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);	
	}
	
	
	
	}
	}

	function hapuspesan(){
	$idpesan = $this->uri->segment('4');		
	//delete
	$where = array(
	'idpesan' => $idpesan
	);
	$this->m_pesan->hapus($where,'pesan');	
	//delete
	$where = array(
	'idpesan' => $idpesan
	);
	$this->m_pesan->hapus($where,'pesandetail');	
	$this->session->unset_userdata('idpesan');
	$status='1';
	$data['tampilpesan'] = $this->m_pesan->pesan($status);	
	$data['alert']='danger';
	$data['pesan']='data berhasil di hapus';
	$this->load->view('admin/pesan', $data);				
	}
	function ceknotif()
	{
	$idpesan = $this->uri->segment('4');
	$this->session->unset_userdata('idpesan');
	$status='1';
	$data['tampilpesan'] = $this->m_pesan->pesan($status);	
	$data['alert']='';
	$data['pesan']='';
	//getidpesan
	$data_save = array(										
			'notif' => 1,										
	);
	$where = array(
	'idpesan' => $idpesan
	);
	$this->m_pesan->update($where,$data_save,'pesan');		
	$this->load->view('admin/pesan', $data);	
	}	

	function cetakskrd(){
	$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('cetakskrd', $data);	
	}		
	
}
