<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pemakai extends CI_Controller {
	function __construct(){
		parent::__construct();	
			$this->load->library('session');
			$this->load->library('form_validation');
			//$this->load->model('m_pemakai');
			$this->load->model('m_pemakai');
			if($this->session->userdata('username') =='' OR $this->session->userdata('token') !=md5('100%Madiun'.date('Ymd')) ){
				redirect(base_url("login"));
			}			
	}
	public function index()
	{
	$data['pemakai'] = $this->m_pemakai->tampil();
	$this->load->view('admin/pemakai', $data);			
	}
	public function add()
	{
	$data['cek']='';
	$data['pemakai'] = $this->m_pemakai->tampil();
	$this->load->view('admin/pemakai-add', $data);				
	}	
	public function save()
	{
		if(isset($_POST['button'])){
		$this->form_validation->set_rules('namapemakai','namapemakai','required|max_length[100]');			
		$this->form_validation->set_rules('act','act','required');
		$act = $this->security->xss_clean($this->input->post('act'));			
		$idpemakai = $this->security->xss_clean($this->input->post('idpemakai'));			
		$namapemakai = $this->security->xss_clean($this->input->post('namapemakai'));		
		$alamat = $this->security->xss_clean($this->input->post('alamat'));			
		if($this->form_validation->run() == false){ 
				$data['alert'] = 'danger';
				$data['pesan'] = 'Ada kesalahan, mohon periksa kembali !';
				$this->load->view('admin/pemakai-add', $data);
		}else if($act =='save'){			
		$data_save = array(
								'namapemakai' => ucwords($namapemakai),
								'alamat' => ucwords($alamat),
						);	
		$this->m_pemakai->in($data_save,'pemakai');
		$data['pemakai'] = $this->m_pemakai->tampil();		
		$data['alert'] = 'success';
		$data['pesan'] = 'Berhasil disimpan';		
		$this->load->view('admin/pemakai', $data);				
		}else if($act=='edit'){
		$data_save = array(
								'namapemakai' => ucwords($namapemakai),
								'alamat' => ucwords($alamat),
						);			
		$where = array(
						'idpemakai' => $idpemakai
				);
		$this->m_pemakai->update($where,$data_save,'pemakai');		
		$data['pemakai'] = $this->m_pemakai->tampil();		
		$data['alert'] = 'success';
		$data['pesan'] = 'Berhasil diperbarui';		
		$this->load->view('admin/pemakai', $data);		
		}
		}
		if(isset($_POST['kembali'])){
		$data['alert'] = '';
		$data['pesan'] = '';				
		$data['pemakai'] = $this->m_pemakai->tampil();
		$this->load->view('admin/pemakai', $data);			
		}		
	}	
	public function edit()
	{
	if(isset($_POST['edit'])){
		$id=$this->security->xss_clean($this->input->post('id'));	
		$data['cek'] ='edit';
		$data['pemakai'] = $this->m_pemakai->tampil();		
		$data['edit'] = $this->m_pemakai->edit($id);
		$this->load->view('admin/pemakai-add', $data);					
	}else if(isset($_POST['hapus'])){
		$id=$this->security->xss_clean($this->input->post('id'));	
		$where = array(
						'idpemakai' => $id
				);
		$this->m_pemakai->hapus($where,'pemakai');
		$data['pemakai'] = $this->m_pemakai->tampil();		
		$data['alert'] = 'success';
		$data['pesan'] = 'Data berhasil dihapus';			
		$this->load->view('admin/pemakai', $data);				
	}else{
		
		$data['pemakai'] = $this->m_pemakai->tampil();
		$this->load->view('admin/pemakai', $data);		
		}	
	}
}