<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Slide extends CI_Controller {
	function __construct(){
		parent::__construct();	
			$this->load->library('session');
			$this->load->library('form_validation');
			//$this->load->model('m_kategori');
			$this->load->model('m_slide');
			if($this->session->userdata('username') =='' OR $this->session->userdata('token') !=md5('100%Madiun'.date('Ymd')) ){
				redirect(base_url("login"));
			}			
	}
	public function index()
	{
	$data['slide'] = $this->m_slide->tampil();
	$this->load->view('admin/slide', $data);			
	}
	
	function upload()
	{
	if(isset($_POST['simpan'])){
	$namalist=$this->security->xss_clean($this->input->post('namalist'));		
	$deskripsi=$this->security->xss_clean($this->input->post('deskripsi'));		
	$idlist=$this->security->xss_clean($this->input->post('idlist'));		
	$idfile=$this->security->xss_clean($this->input->post('idfile'));		
	$idslide=$this->security->xss_clean($this->input->post('idslide'));		
	$status=$this->security->xss_clean($this->input->post('status'));		
	$act=$this->security->xss_clean($this->input->post('act'));		
	$tipe='slide';

	if($act=='save' ){
		$upload = $this->m_slide->upload($tipe);		
		if($upload['result'] == "success"){ // Jika proses upload sukses
		$namafilebaru = $upload['namafilebaru'];	
		$data_save = array(
					'namalist' => $namalist,			
					'deskripsi' => $deskripsi,			
					'fungsi' =>$tipe,									
					'status' =>$status,									
					);	
		$this->m_slide->in($data_save,'list');
		//idlist terakhir 
		$cek = $this->m_slide->cekidlist($tipe);
		$idlist = $cek->idlist;
		$data_save = array(
					'nmtable' =>'list',
					'nmfield' => 'idlist',
					'nmvalue' =>$idlist,			
					'filename' =>$namafilebaru,		
					'deskripsi' =>$tipe,							
					);	
		$this->m_slide->in($data_save,'file');	
		$data['alert']='success';
		$data['pesan']='Upload berhasil';	
		$data['slide'] = $this->m_slide->tampil();
		$this->load->view('admin/slide', $data);			
	}else{
		$data['alert']='danger';
		$data['pesan']=$upload['error'];	
		$data['cek'] ='';	
		$data['slide'] = $this->m_slide->tampil();	
		$this->load->view('admin/slide', $data);
	}		
	}
	//update
	if($act=='edit' ){
		$upload = $this->m_slide->upload($tipe);
		if($_FILES['input_gambar']['name'] !=''){	
			if($upload['result'] == "success"){ // Jika proses upload sukses
			//delete file lama 
			$cek= $this->m_slide->filelama($idfile);
			$namafilelama = $cek->filename;
			if(file_exists('images/'.$namafilelama)){		
			unlink('images/'.$namafilelama); 		
			}	

			$data_save = array(
						'namalist' => $namalist,			
						'deskripsi' => $deskripsi,
						'status' =>$status,
						);
			$where = array(
							'idlist' => $idlist
					);					
			$this->m_slide->update($where,$data_save,'list');		
			$namafilebaru = $upload['namafilebaru'];	
			$data_save = array(
						'nmtable' =>'list',
						'nmfield' => 'idlist',
						'nmvalue' =>$idlist,		
						'filename' =>$namafilebaru,		
						'deskripsi' =>$deskripsi,									
						);
			$where = array(
							'idfile' => $idfile
					);					
			$this->m_slide->update($where,$data_save,'file');		
			$data['alert']='success';
			$data['pesan']='Upload berhasil';	
			$data['slide'] = $this->m_slide->tampil();	
			$this->load->view('admin/slide', $data);	
		}else{
			$data['alert']='danger';
			$data['pesan']=$upload['error'];	
			$data['cek'] ='';	
			$data['slide'] = $this->m_slide->tampil();	
			$this->load->view('admin/slide', $data);
		}							
		}else{
			$data_save = array(
						'namalist' => $namalist,			
						'deskripsi' => $deskripsi,
						'status' =>$status,
						);
			$where = array(
							'idlist' => $idlist
					);					
			$this->m_slide->update($where,$data_save,'list');
			$data['alert']='success';
			$data['pesan']='Upload berhasil';	
			$data['slide'] = $this->m_slide->tampil();	
			$this->load->view('admin/slide', $data);	
		}
	}
	
	}else{		
		$data['alert']='danger';
		$data['pesan']='Ada kesalahan';	
		$data['cek'] ='';	
		$data['slide'] = $this->m_slide->tampil();	
		$this->load->view('admin/slide', $data);
	}				
	}


	function delimage()
	{
	$idlist =intval($this->uri->segment(4));
	$idfile =intval($this->uri->segment(5));
	//delete file lama 
	$cek= $this->m_slide->filelama($idfile);
	$namafilelama = $cek->filename;
	if(file_exists('images/'.$namafilelama)){		
	unlink('images/'.$namafilelama); 		
	}	
	//list
	$where = array(
					'idlist' => $idlist
			);
	$this->m_slide->hapus($where,'list');	
	//file	
	$where = array(
					'idfile' => $idfile
			);
	$this->m_slide->hapus($where,'file');	
	$data['alert']='danger';
	$data['pesan']='Data berhasil di hapus';	
	$data['cek'] ='';	
	$data['slide'] = $this->m_slide->tampil();	
	$this->load->view('admin/slide', $data);
	}	
	
}