<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Customer extends CI_Controller {

	/**
	 * @keterangan : Controller untuk halaman profil
	 **/
	
	public function index()
	{
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
			$cari = $this->input->post('txt_cari');
			if(empty($cari)){
				$where = ' ';
			}else{
				$where = " WHERE kode_customer LIKE '%$cari%' OR nama_customer LIKE '%$cari%'";
			}
			
			$d['prg']= $this->config->item('prg');
			$d['web_prg']= $this->config->item('web_prg');
			
			$d['nama_program']= $this->config->item('nama_program');
			$d['instansi']= $this->config->item('instansi');
			$d['usaha']= $this->config->item('usaha');
			$d['alamat_instansi']= $this->config->item('alamat_instansi');

			
			$d['judul']="Pelanggan";
			
			//paging
			$page=$this->uri->segment(3);
			$limit=$this->config->item('limit_data');
			if(!$page):
			$offset = 0;
			else:
			$offset = $page;
			endif;
			
			$text = "SELECT * FROM customer $where ";		
			$tot_hal = $this->app_model->manualQuery($text);		
			
			$d['tot_hal'] = $tot_hal->num_rows();
			
			$config['base_url'] = site_url() . '/customer/index/';
			$config['total_rows'] = $tot_hal->num_rows();
			$config['per_page'] = $limit;
			$config['uri_segment'] = 3;
			$config['next_link'] = 'Lanjut &raquo;';
			$config['prev_link'] = '&laquo; Kembali';
			$config['last_link'] = '<b>Terakhir &raquo; </b>';
			$config['first_link'] = '<b> &laquo; Pertama</b>';
			$this->pagination->initialize($config);
			$d["paginator"] =$this->pagination->create_links();
			$d['hal'] = $offset;
			

			$text = "SELECT * FROM customer $where 
					ORDER BY kode_customer ASC 
					LIMIT $limit OFFSET $offset";
			$d['data'] = $this->app_model->manualQuery($text);
			
			
			$d['content'] = $this->load->view('customer/view', $d, true);		
			$this->load->view('home',$d);
		}else{
			header('location:'.base_url());
		}
	}
	
	public function tambah()
	{
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
			$d['prg']= $this->config->item('prg');
			$d['web_prg']= $this->config->item('web_prg');
			
			$d['nama_program']= $this->config->item('nama_program');
			$d['instansi']= $this->config->item('instansi');
			$d['usaha']= $this->config->item('usaha');
			$d['alamat_instansi']= $this->config->item('alamat_instansi');

			$d['judul']="Pelanggan";
			$kode	= $this->app_model->MaxKodeCust();
			
			$d['kode']			=$kode;
			$d['nama_customer']	='';
			$d['tempat_lahir']	='';
			$d['tgl_lahir']		='';
			$d['telp']			='';
			$d['alamat']		='';
			
			$text1 = "SELECT * FROM tbcomponent where Kode_Component='Jk'";
			$d['l_jk'] = $this->app_model->manualQuery($text1);
			$d['content'] = $this->load->view('customer/form', $d, true);		
			$this->load->view('home',$d);
		}else{
			header('location:'.base_url());
		}
	}
	
	public function edit()
	{
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
			
			$d['prg']= $this->config->item('prg');
			$d['web_prg']= $this->config->item('web_prg');
			
			$d['nama_program']= $this->config->item('nama_program');
			$d['instansi']= $this->config->item('instansi');
			$d['usaha']= $this->config->item('usaha');
			$d['alamat_instansi']= $this->config->item('alamat_instansi');
			
			$d['judul']="Pelanggan";
			
			$id = $this->uri->segment(3);
			$text = "SELECT * FROM customer WHERE kode_customer='$id'";
			$data = $this->app_model->manualQuery($text);
			if($data->num_rows() > 0){
				foreach($data->result() as $db){
					$d['kode']		=$id;
					$d['nama_customer']	=$db->nama_customer;
					$d['jk']		=$db->jk;
					$d['telp']		=$db->telp;
					$d['tempat_lahir']	=$db->tempat_lahir;
					$d['tgl_lahir']	=$this->app_model->tgl_str($db->tgl_lahir);
					$d['alamat']	=$db->alamat;
				}
			}else{
					$d['kode']		=$id;
					$d['nama_customer']	='';
					$d['alamat']	='';
			}
			
			$text1 = "SELECT * FROM tbcomponent where Kode_Component='Jk'";
			$d['l_jk'] = $this->app_model->manualQuery($text1);		
			$d['content'] = $this->load->view('customer/form', $d, true);		
			$this->load->view('home',$d);
		}else{
			header('location:'.base_url());
		}
	}
	
	public function hapus()
	{
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){			
			$id = $this->uri->segment(3);
			$this->app_model->manualQuery("DELETE FROM customer WHERE kode_customer='$id'");
			echo "<meta http-equiv='refresh' content='0; url=".base_url()."index.php/customer'>";			
		}else{
			header('location:'.base_url());
		}
	}
	
	public function simpan()
	{
		
		$cek = $this->session->userdata('logged_in');
		if(!empty($cek)){
				
				
				$up['kode_customer']	= $this->input->post('kode');
				$up['nama_customer']	= $this->input->post('nama_customer');
				$up['alamat']			= $this->input->post('alamat');
				$up['telp']				= $this->input->post('telp');
				$up['jk']				= $this->input->post('jk');
				$up['tempat_lahir']		= $this->input->post('tempat_lahir');
				$up['tgl_lahir']		= $this->app_model->tgl_sql($this->input->post('tgl_lahir'));
				
				$id['kode_customer']=$this->input->post('kode');
				
				$data = $this->app_model->getSelectedData("customer",$id);
				if($data->num_rows()>0){
					$this->app_model->updateData("customer",$up,$id);
					echo 'Update data Sukses';
				}else{
					$this->app_model->insertData("customer",$up);
					echo 'Simpan data Sukses';		
				}
		}else{
				header('location:'.base_url());
		}
	
	}
	
}

/* End of file profil.php */
/* Location: ./application/controllers/profil.php */