<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Client 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_client');
			if($this->session->userdata('username') =='' OR $this->session->userdata('token') !=md5('100%Madiun'.date('Ymd')) ){
				redirect(base_url("login"));
			}			
	}
	public function index()
	{
	$data['client'] = $this->m_client->tampil();
	$this->load->view('admin/client', $data);			
	}
	
	function upload()
	{
	if(isset($_POST['simpan'])){
	$namalist=$this->security->xss_clean($this->input->post('namalist'));				
	$idlist=$this->security->xss_clean($this->input->post('idlist'));		
	$idfile=$this->security->xss_clean($this->input->post('idfile'));		
	$status=$this->security->xss_clean($this->input->post('status'));		
	$act=$this->security->xss_clean($this->input->post('act'));		
	$tipe='client';

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

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


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