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

class Kategori extends CI_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('Kategori_model');
	}

	public function index($page = 0){
		if(!$this->session->userdata('sesilogin')) {
			redirect('login','refresh');
		} else {
			// cek if page isset
			if(isset($page)) {
				if(is_numeric($page)) {
					$start = $page;
				} else {
					$start = 0;
				}
			}

			if($this->input->post('cari')) {
				$search 		= $this->input->post('cari');
				$kategori 		= $this->Kategori_model->get_search_kategori($search, 0, 18446744073709551615);
				$jumlah			= $kategori['jumlah'];
				$cari 			= $search;
			} else {
				$kategori 		= $this->Kategori_model->get_semua_kategori($start);
				$semua_kategori 	= $this->Kategori_model->get_semua_kategori(0, 18446744073709551615);
				$jumlah			= $semua_kategori['jumlah'];
				$cari 			= '';
			}

			$this->load->library('pagination');
			$config['base_url'] = base_url('kategori/list');
			$config['total_rows'] = $jumlah;
			$config['per_page'] = 10;
			$config['uri_segment'] = 3;
			$config['num_links'] = 3;
			$config['full_tag_open'] = '<ul class="pagination">';
			$config['full_tag_close'] = '</ul>';
			$config['next_link'] = '»';
			$config['next_tag_open'] = '<li>';
			$config['next_tag_close'] = '</li>';
			$config['prev_link'] = '«';
			$config['prev_tag_open'] = '<li>';
			$config['prev_tag_close'] = '</li>';
			$config['cur_tag_open'] = '<li class="active"><a href="#">';
			$config['cur_tag_close'] = '</a></li>';
			$config['num_tag_open'] = '<li>';
			$config['num_tag_close'] = '</li>';

			$this->pagination->initialize($config);

			if($cari != '') {
				$paginasi		= '';
			} else {
				$paginasi		= $this->pagination->create_links();
			}

			$_data = array(
				'sesilogin'	=> $this->session->userdata('sesilogin'),
				'halaman'	=> 'kategori',
				'kategori'	=> $kategori['hasil'],
				'page'		=> $start + 1,
				'paginasi'	=> $paginasi,
				'jumlah'	=> $jumlah,
				'cari'		=> $cari
			);

			$_views['title']	= 'Daftar kategori';
			$_views['sidemenu'] = $this->load->view('include/menu/side_menu', $_data, TRUE);
			$_views['header'] 	= $this->load->view('include/header/header', $_data, TRUE);
			$_views['content'] 	= $this->load->view('include/content/kategori/kategori_list', $_data, TRUE);
			$_views['modal'] 	= $this->load->view('include/modal/modal_content', $_data, TRUE);
			$_views['footer'] 	= $this->load->view('include/footer/footer', $_data, TRUE);
			$_views['jscript'] 	= $this->load->view('include/ekstensi/javascript', $_data, TRUE);

			$this->load->view('template/cms_template', $_views);
		}
	}

	public function add(){
		if(!$this->session->userdata('sesilogin')) {
			redirect('login','refresh');
		} else {
			$_data = array(
				'sesilogin'	=> $this->session->userdata('sesilogin'),
				'halaman'	=> 'kategori'
			);

			$_views['title']	= 'Tambah kategori';
			$_views['sidemenu'] = $this->load->view('include/menu/side_menu', $_data, TRUE);
			$_views['header'] 	= $this->load->view('include/header/header', $_data, TRUE);
			$_views['content'] 	= $this->load->view('include/content/kategori/kategori_add', $_data, TRUE);
			$_views['modal'] 	= $this->load->view('include/modal/modal_content', $_data, TRUE);
			$_views['footer'] 	= $this->load->view('include/footer/footer', $_data, TRUE);
			$_views['jscript'] 	= $this->load->view('include/ekstensi/javascript', $_data, TRUE);

			$this->load->view('template/cms_template', $_views);
		}
	}

	public function add_save(){
		if(!$this->session->userdata('sesilogin')) {
			redirect('login','refresh');
		} else {
			if (!empty($this->input->post('nama'))) { $nama = $this->input->post('nama'); } else { $nama = ""; };

	    	$_data			= array(
				'kategori_nama'				=> $nama,
				'kategori_tanggalpembuatan'	=> tanggalhariini(),
				'kategori_terakhirupdate'	=> tanggalhariini(),
				'kategori_updateoleh'		=> sesilogin('id')
			);

	    	$masukan_kategori = $this->Kategori_model->insert_kategori($_data);
	    	if($masukan_kategori['status'] == "success") {
	    		$pesannya =
	    		"
	    			<script type='text/javascript'>
	    				successtoastr('" .$masukan_kategori['hasil']. "');
	    			</script>
	    		";
	    		$this->session->set_flashdata('success', $pesannya);
	    		redirect(base_url('kategori/list'),'refresh');
	    	} else {
	    		$pesannya =
	    		"
	    			<script type='text/javascript'>
	    				errortoastr('" .$masukan_kategori['hasil']. "');
	    			</script>
	    		";
	    		$this->session->set_flashdata('error', $pesannya);
	    		redirect(base_url('kategori/tambah'),'refresh');
	    	}
		}
	}

	public function update($id){
		if(!$this->session->userdata('sesilogin')) {
			redirect('login','refresh');
		} else {
			// get data kategori
			$kategori = $this->Kategori_model->get_spesifik_kategori($id);

			$_data = array(
				'sesilogin'	=> $this->session->userdata('sesilogin'),
				'halaman'	=> 'kategori',
				'kategori'	=> $kategori['hasil']
			);

			$_views['title']	= 'Edit kategori';
			$_views['sidemenu'] = $this->load->view('include/menu/side_menu', $_data, TRUE);
			$_views['header'] 	= $this->load->view('include/header/header', $_data, TRUE);
			$_views['content'] 	= $this->load->view('include/content/kategori/kategori_edit', $_data, TRUE);
			$_views['modal'] 	= $this->load->view('include/modal/modal_content', $_data, TRUE);
			$_views['footer'] 	= $this->load->view('include/footer/footer', $_data, TRUE);
			$_views['jscript'] 	= $this->load->view('include/ekstensi/javascript', $_data, TRUE);

			$this->load->view('template/cms_template', $_views);
		}
	}

	public function update_save(){
		if(!$this->session->userdata('sesilogin')) {
			redirect('login','refresh');
		} else {
			$id = $this->input->post('id');
			if (!empty($this->input->post('nama'))) { $nama = $this->input->post('nama'); } else { $nama = ""; };

	    	$_data			= array(
				'kategori_nama'				=> $nama,
				'kategori_terakhirupdate'	=> tanggalhariini(),
				'kategori_updateoleh'		=> sesilogin('id')
			);

	    	$updatean_kategori = $this->Kategori_model->update_kategori($id, $_data);
	    	if($updatean_kategori['status'] == "success") {
	    		$pesannya =
	    		"
	    			<script type='text/javascript'>
	    				successtoastr('" .$updatean_kategori['hasil']. "');
	    			</script>
	    		";
	    		$this->session->set_flashdata('success', $pesannya);
	    		redirect(base_url('kategori/list'),'refresh');
	    	} else {
	    		$pesannya =
	    		"
	    			<script type='text/javascript'>
	    				errortoastr('" .$updatean_kategori['hasil']. "');
	    			</script>
	    		";
	    		$this->session->set_flashdata('error', $pesannya);
	    		redirect(base_url('kategori/tambah'),'refresh');
	    	}
		}
	}

	public function view($id){
		if(!$this->session->userdata('sesilogin')) {
			redirect('login','refresh');
		} else {
			// get data kategori
			$kategori = $this->Kategori_model->get_spesifik_kategori($id);

			$_data = array(
				'sesilogin'	=> $this->session->userdata('sesilogin'),
				'halaman'	=> 'kategori',
				'kategori'	=> $kategori['hasil']
			);

			$_views['title']	= 'Dashboard';
			$_views['sidemenu'] = $this->load->view('include/menu/side_menu', $_data, TRUE);
			$_views['header'] 	= $this->load->view('include/header/header', $_data, TRUE);
			$_views['content'] 	= $this->load->view('include/content/kategori/kategori_view', $_data, TRUE);
			$_views['modal'] 	= $this->load->view('include/modal/modal_content', $_data, TRUE);
			$_views['footer'] 	= $this->load->view('include/footer/footer', $_data, TRUE);
			$_views['jscript'] 	= $this->load->view('include/ekstensi/javascript', $_data, TRUE);

			$this->load->view('template/cms_template', $_views);
		}
	}

	public function delete($id){
		if(!$this->session->userdata('sesilogin')) {
			redirect('login','refresh');
		} else {
			$hapus_kategori = $this->Kategori_model->delete_kategori($id);

			if($hapus_kategori['status'] == "success") {
				$pesannya =
	    		"
	    			<script type='text/javascript'>
	    				successtoastr('" .$hapus_kategori['hasil']. "');
	    			</script>
	    		";
	    		$this->session->set_flashdata('success', $pesannya);
	    		redirect(base_url('kategori/list'),'refresh');
			} else {
				$pesannya =
	    		"
	    			<script type='text/javascript'>
	    				errortoastr('" .$hapus_kategori['hasil']. "');
	    			</script>
	    		";
	    		$this->session->set_flashdata('error', $pesannya);
	    		redirect(base_url('kategori/list'),'refresh');
			}
		}
	}

}

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