<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Fasilitas 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');			
			$this->load->model('m_fasilitas');			
			$this->load->model('m_kategori');			
			if($this->session->userdata('username') =='' OR $this->session->userdata('token') !=md5('100%Madiun'.date('Ymd')) ){
				redirect(base_url("login"));
			}			
	}
	public function index()
	{	
	$data['alert']='';
	$data['pesan']='';
	$waktu = $this->session->userdata('waktu');	
	if($waktu!=''){
	$waktu =$waktu;
	}else{
	$waktu = '0';
	}
	$data['fasilitas'] = $this->m_fasilitas->tampilwaktu($waktu);	
	$this->load->view('admin/fasilitas', $data);			
	}
	function waktu()
	{	
	if(isset($_POST['waktu'])){
	$waktu=$this->security->xss_clean($this->input->post('waktu'));
	$data['alert']='';
	$data['pesan']='';
	$waktu = $waktu;
	$data_session = array(
	'waktu' => $waktu,
	);
	$this->session->set_userdata($data_session);			
	$data['fasilitas'] = $this->m_fasilitas->tampilwaktu($waktu);	
	$this->load->view('admin/fasilitas', $data);		
	}	
	}	
	function add()
	{
	$data['cek']='';
	$data['kategori'] = $this->m_kategori->tampil();
	$this->load->view('admin/fasilitas-add', $data);				
	}	
	function edit()
	{	
	if(isset($_POST['edit'])){
	$idfasilitas=$this->security->xss_clean($this->input->post('idfasilitas'));
	$data['alert']='';
	$data['pesan']='';	
	$data['cek'] ='edit';	
	$data['idfasilitas'] =$idfasilitas;
	$data['edit'] = $this->m_fasilitas->edit($idfasilitas);
	$data['kategori'] = $this->m_kategori->tampil();
	$data['fileupload'] = $this->m_fasilitas->fileupload($idfasilitas);	
	$this->load->view('admin/fasilitas-add', $data);		
	}	
	}	
	function save()
	{
	$act=$this->security->xss_clean($this->input->post('act'));
	$idfasilitas=$this->security->xss_clean($this->input->post('idfasilitas'));	
	$namafasilitas=$this->security->xss_clean($this->input->post('namafasilitas'));	
	$harga=$this->security->xss_clean($this->input->post('harga'));	
	$deskripsi=$this->security->xss_clean($this->input->post('deskripsi'));	
	$status=$this->security->xss_clean($this->input->post('status'));	
	$idkategori=$this->security->xss_clean($this->input->post('idkategori'));	
	$umum=$this->security->xss_clean($this->input->post('umum'));	
	$waktu=$this->security->xss_clean($this->input->post('waktu'));	
	$batasmax=$this->security->xss_clean($this->input->post('batasmax'));	
	if(isset($_POST['button']) AND $act=='save'){
	$data_save = array(
								'namafasilitas' => ucwords($namafasilitas),
								'harga' =>$harga,
								'deskripsi' => $deskripsi,
								'status' => $status,
								'idkategori' => $idkategori,
								'umum' => $umum,
								'waktu' => $waktu,
								'batasmax' => $batasmax
						);	
	$this->m_fasilitas->in($data_save,'fasilitas');		
	$data['alert']='success';
	$data['pesan']='Data berhasil di simpan';	
	$data['cek'] ='edit';		
	$cekok =  $this->m_fasilitas->idfasilitasdesc();
	$idfasilitas = $cekok->idfasilitas;
	$data['edit'] = $this->m_fasilitas->edit($idfasilitas);
	$data['fileupload'] = $this->m_fasilitas->fileupload($idfasilitas);		
	$data['kategori'] = $this->m_kategori->tampil();
	$this->load->view('admin/fasilitas-add', $data);			
	}else if(isset($_POST['button']) AND  $act=='edit'){
		$data_save = array(
								'namafasilitas' => ucwords($namafasilitas),
								'harga' =>$harga,
								'deskripsi' => $deskripsi,
								'status' => $status,
								'idkategori' => $idkategori,
								'umum' => $umum,
								'waktu' => $waktu,
								'batasmax' => $batasmax
						);			
		$where = array(
						'idfasilitas' => $idfasilitas
				);
		$this->m_fasilitas->update($where,$data_save,'fasilitas');	
	$data['alert']='success';
	$data['pesan']='Data berhasil di ubah';	
	$data['cek'] ='edit';		
	$cekok =  $this->m_fasilitas->idfasilitasdesc();
	$idfasilitas = $cekok->idfasilitas;
	$data['edit'] = $this->m_fasilitas->edit($idfasilitas);
	$data['fileupload'] = $this->m_fasilitas->fileupload($idfasilitas);		
	$data['kategori'] = $this->m_kategori->tampil();
	$this->load->view('admin/fasilitas-add', $data);		
	}else{
	$data['alert']='';
	$data['pesan']='';	
	$data['cek'] ='';	
	$waktu = $this->session->userdata('waktu');	
	if($waktu!=''){
	$waktu =$waktu;
	}else{
	$waktu = '0';
	}
	$data['fasilitas'] = $this->m_fasilitas->tampilwaktu($waktu);	
	$this->load->view('admin/fasilitas', $data);			
	}		
	}	
	function upload()
	{
	if(isset($_POST['simpan'])){
	$deskripsi=$this->security->xss_clean($this->input->post('deskripsi'));		
	$idfasilitas=$this->security->xss_clean($this->input->post('idfasilitas'));		
	$idfile=$this->security->xss_clean($this->input->post('idfile'));		
	$tipe='fasilitas';
	$upload = $this->m_fasilitas->upload($tipe);
	if($idfile==''){
		if($upload['result'] == "success"){ // Jika proses upload sukses
		$namafilebaru = $upload['namafilebaru'];
		$data_save = array(
					'nmtable' =>'fasilitas',
					'nmfield' => 'idfasilitas',
					'nmvalue' =>$idfasilitas,			
					'filename' =>$namafilebaru,		
					'deskripsi' =>$deskripsi,							
					);	
		$this->m_fasilitas->in($data_save,'file');	
		$data['alert']='success';
		$data['pesan']='Upload berhasil';	
		$data['cek'] ='edit';	
		$data['idfasilitas'] =$idfasilitas;
		$data['edit'] = $this->m_fasilitas->edit($idfasilitas);
		$data['fileupload'] = $this->m_fasilitas->fileupload($idfasilitas);
		$data['kategori'] = $this->m_kategori->tampil();		
		$this->load->view('admin/fasilitas-add', $data);		
	}else{
		$data['alert']='danger';
		$data['pesan']=$upload['error'];	
		$data['cek'] ='';	
		$data['fasilitas'] = $this->m_fasilitas->tampil();	
		$this->load->view('admin/fasilitas', $data);
	}		
	}
	//update
	if($idfile!=''){
		if($upload['result'] == "success"){ // Jika proses upload sukses
		//delete file lama 
		$cek= $this->m_fasilitas->filelama($idfile);
		$namafilelama = $cek->filename;
		if(file_exists('images/'.$namafilelama)){		
		unlink('images/'.$namafilelama); 		
		}		
		$namafilebaru = $upload['namafilebaru'];	
		$data_save = array(
					'nmtable' =>'fasilitas',
					'nmfield' => 'idfasilitas',
					'nmvalue' =>$idfasilitas,			
					'filename' =>$namafilebaru,		
					'deskripsi' =>$deskripsi,									
					);
		$where = array(
						'idfile' => $idfile
				);					
		$this->m_fasilitas->update($where,$data_save,'file');		
		$data['alert']='success';
		$data['pesan']='Upload berhasil';	
		$data['cek'] ='edit';	
		$data['idfasilitas'] =$idfasilitas;
		$data['edit'] = $this->m_fasilitas->edit($idfasilitas);
		$data['fileupload'] = $this->m_fasilitas->fileupload($idfasilitas);
		$data['kategori'] = $this->m_kategori->tampil();		
		$this->load->view('admin/fasilitas-add', $data);		
	}else{		
		$data['alert']='danger';
		$data['pesan']=$upload['error'];	
		$data['cek'] ='';	
		$data['fasilitas'] = $this->m_fasilitas->tampil();	
		$this->load->view('admin/fasilitas', $data);
	}				
	}
	}
	}
	function delimage()
	{
	$idfasilitas =intval($this->uri->segment(4));
	$idfile =intval($this->uri->segment(5));
	//delete file lama 
	$cek= $this->m_fasilitas->filelama($idfile);
	$namafilelama = $cek->filename;
	if(file_exists('images/'.$namafilelama)){		
	unlink('images/'.$namafilelama); 		
	}	
	$where = array(
					'idfile' => $idfile
			);
	$this->m_fasilitas->hapus($where,'file');	
	$data['alert']='success';
	$data['pesan']='Upload berhasil di hapus';	
	$data['cek'] ='edit';	
	$data['idfasilitas'] =$idfasilitas;
	$data['edit'] = $this->m_fasilitas->edit($idfasilitas);
	$data['fileupload'] = $this->m_fasilitas->fileupload($idfasilitas);	
	$data['kategori'] = $this->m_kategori->tampil();
	$this->load->view('admin/fasilitas-add', $data);	
	}		
}