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

class Bantuan extends CI_Controller {
	function __construct() {
		parent::__construct();
		if($this->session->userdata('admin_valid') == FALSE && $this->session->userdata('admin_id') == ""){
          redirect("admin/login");
       }
	   $this->load->model('crud_model');
	}
	
	public function list_data() {
		
		$ta = $this->session->userdata('admin_ta');
		   
		$total_row		= $this->db->query("SELECT * FROM tb_panduan ")->num_rows();
		$per_page		= 10;
		
		$awal	= $this->uri->segment(4); 
		$awal	= (empty($awal) || $awal == 1) ? 0 : $awal;
		
		$akhir	= $per_page;
		
		$a['pagi']	= _page($total_row, $per_page, 4, base_url()."perkara/list_data/p");
		
		$mau_ke					= $this->uri->segment(3);
		$idu					= $this->uri->segment(4);
		
		$cari					= addslashes($this->input->post('q'));
		
		
		//ambil variabel post
		//$data['id'] 			= addslashes($this->input->post('idp'));
		$data['keterangan'] 		= addslashes($this->input->post('jawaban'));
		$data['kategori'] 	= addslashes($this->input->post('pertanyaan'));
		//ambil variabel post pencarian
		$cari					= addslashes($this->input->post('q'));

		//upload config 
		$config['upload_path'] 		= './upload/surat_masuk';
		$config['allowed_types'] 	= 'gif|jpg|png|pdf|doc|docx|rtf';
		$config['max_size']			= '123000';
		$config['max_width']  		= '3000';
		$config['max_height'] 		= '3000';

		$this->load->library('upload', $config);
		
		if ($mau_ke == "del") {
			$data['id'] = $this->uri->segment(4);
			//echo "id : ".$mau_ke;exit;
			$this->app_model->deleteData("tb_panduan",array('id' => $this->uri->segment(4)));
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data berhasil dihapus </div>");
			redirect('bantuan/list_data');
		} else if ($mau_ke == "cari") {
			$a['data']		= $this->db->query("SELECT * FROM tb_panduan WHERE keterangan LIKE '%$cari%' OR kategori LIKE '%$cari%' ORDER BY id ASC")->result();
			$a['page']		= "bantuan/list_";
		} else if ($mau_ke == "add") {
			$a['page']		= "bantuan/form_";
		} else if ($mau_ke == "edt") {
			$a['datpil'] = $this->app_model->getSelectedData('tb_panduan', array('id' => $idu))->row();
			
			//$a['datpil']	= $this->db->query("SELECT * FROM tb_panduan WHERE id = '$idu'")->row();	
			$a['page']		= "bantuan/form_";
		} else if ($mau_ke == "act_add") {	
			//simpan_data
			$this->app_model->insertData("tb_panduan",$data);
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data berhasil disimpan. ".$this->upload->display_errors()."</div>");
			redirect('bantuan/list_data');
		} else if ($mau_ke == "act_edt") {
			
			$where = array('id' => $this->input->post('idp'));
            $this->app_model->updateData('tb_panduan', $data, $where);
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data berhasil di update. ".$this->upload->display_errors()."</div>");			
			redirect('bantuan/list_data');
		} else {
			$a['data']		= $this->db->query("SELECT * FROM tb_panduan ORDER BY id ASC LIMIT $awal, $akhir ")->result();
			$a['page']		= "bantuan/list_";
		}
		
		$this->load->view('admin/home_menu', $a);
	}
	function delete_data(){
		$this->app_model->deleteData('temp_panjar');
		//$this->app_model->deleteData('temp_pos');
		//$this->db->query("DELETE FROM temp_pos");
			
			//$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data berhasil dihapus </div>");
		
			redirect('dashboard/index');
		
	}
}
