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

class Admin extends CI_Controller {
	function __construct() {
		parent::__construct();
		$this->load->library('encryption'); //in controller
		$this->load->library('MyPHPMailer');
		$this->load->helper('url');
	}
	
	public function index() {
		if ($this->session->userdata('admin_valid') == FALSE && $this->session->userdata('admin_id') == "") {
			redirect("index.php/admin/login");
		}

		redirect('index.php/admin/dda/');
		//$a['page']	= "l_oi";
		
		//$this->load->view('admin/index', $a);
	}
	
	public function kalender() {
		if ($this->session->userdata('admin_valid') == FALSE && $this->session->userdata('admin_id') == "") {
			redirect("index.php/admin/login");
		}

		$a['page']	= "f_kalender";
		
		$this->load->view('admin/index', $a);
	}

	public function pengguna() {
		if ($this->session->userdata('admin_valid') == FALSE && $this->session->userdata('admin_id') == "") {
			redirect("index.php/admin/login");
		}		
		
		//ambil variabel URL
		$mau_ke					= $this->uri->segment(3);
		
		//ambil variabel Postingan
		$idp					= addslashes($this->input->post('idp'));
		$nama					= addslashes($this->input->post('nama'));
		$alamat					= addslashes($this->input->post('alamat'));
		$kepsek					= addslashes($this->input->post('kepsek'));
		$nip_kepsek				= addslashes($this->input->post('nip_kepsek'));
		
		$cari					= addslashes($this->input->post('q'));

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

		$this->load->library('upload', $config);
		
		if ($mau_ke == "act_edt") {
			if ($this->upload->do_upload('logo')) {
				$up_data	 	= $this->upload->data();
				
				$this->db->query("UPDATE tr_instansi SET nama = '$nama', alamat = '$alamat', kepsek = '$kepsek', nip_kepsek = '$nip_kepsek', logo = '".$up_data['file_name']."' WHERE id = '$idp'");

			} else {
				$this->db->query("UPDATE tr_instansi SET nama = '$nama', alamat = '$alamat', kepsek = '$kepsek', nip_kepsek = '$nip_kepsek' WHERE id = '$idp'");
			}		

			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been updated</div>");			
			redirect('index.php/admin/pengguna');
		} else {
			$a['data']		= $this->db->query("SELECT * FROM tr_instansi WHERE id = '1' LIMIT 1")->row();
			$a['page']		= "f_pengguna";
		}
		
		$this->load->view('admin/index', $a);	
	}
	
	public function manage_admin() {
		if ($this->session->userdata('admin_valid') == FALSE && $this->session->userdata('admin_id') == "") {
			redirect("index.php/admin/login");
		}
		
		/* pagination */	
		$total_row		= $this->db->query("SELECT * FROM t_admin")->num_rows();
		$per_page		= 10;
		
		$awal	= $this->uri->segment(4); 
		$awal	= (empty($awal) || $awal == 1) ? 0 : $awal;
		
		//if (empty($awal) || $awal == 1) { $awal = 0; } { $awal = $awal; }
		$akhir	= $per_page;
		
		$a['pagi']	= _page($total_row, $per_page, 4, base_url()."index.php/admin/manage_admin/p");
		
		//ambil variabel URL
		$mau_ke					= $this->uri->segment(3);
		$idu					= $this->uri->segment(4);
		
		$cari					= addslashes($this->input->post('q'));

		//ambil variabel Postingan
		$idp					= addslashes($this->input->post('idp'));
		$username				= addslashes($this->input->post('username'));
		$password				= md5(addslashes($this->input->post('password')));
		$nama					= addslashes($this->input->post('nama'));
		$nip					= addslashes($this->input->post('nip'));
		$level					= 'Admin';
		$unitkerja				= addslashes($this->input->post('unitkerja'));
		$email					= addslashes($this->input->post('email'));
		$cari					= addslashes($this->input->post('q'));

		
		if ($mau_ke == "del") {
			$this->db->query("DELETE FROM t_admin WHERE id = '$idu'");
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been deleted </div>");
			redirect('index.php/admin/manage_admin');
		} else if ($mau_ke == "cari") {
			$a['data']		= $this->db->query("SELECT * FROM t_admin WHERE nama LIKE '%$cari%' ORDER BY id DESC")->result();
			$a['page']		= "l_manage_admin";
		} else if ($mau_ke == "add") {
			$a['page']		= "f_manage_admin";
		} else if ($mau_ke == "edt") {
			$a['datpil']	= $this->db->query("SELECT * FROM t_admin WHERE id = '$idu'")->row();	
			$a['page']		= "f_manage_admin";
		} else if ($mau_ke == "act_add") {	
			$cek_user_exist = $this->db->query("SELECT username FROM t_admin WHERE username = '$username'")->num_rows();

			if (strlen($username) < 4) {
				$this->session->set_flashdata("k", "<div class=\"alert alert-danger\" id=\"alert\">Username minimal 4 huruf</div>");
				redirect('index.php/admin/manage_admin');
			} else if ($cek_user_exist > 0) {
				$this->session->set_flashdata("k", "<div class=\"alert alert-danger\" id=\"alert\">Username telah dipakai. Ganti yang lain..!</div>");
				redirect('index.php/admin/manage_admin');	
			} else {
				$this->db->query("INSERT INTO t_admin VALUES (NULL, '$username', '$password', '$nama', '$nip', '$level','$unitkerja','$email')");
				$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been added</div>");
			}
			
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been added</div>");
			redirect('index.php/admin/manage_admin');
		} else if ($mau_ke == "act_edt") {
			if ($password = md5("-")) {
				$this->db->query("UPDATE t_admin SET username = '$username', nama = '$nama', nip = '$nip', level = '$level', email='$email' WHERE id = '$idp'");
			} else {
				$this->db->query("UPDATE t_admin SET username = '$username', password = '$password', nama = '$nama', nip = '$nip', level = '$level', email='$email' WHERE id = '$idp'");
			}
			
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been updated </div>");			
			redirect('index.php/admin/manage_admin');
		} else {
			$a['data']		= $this->db->query("SELECT * FROM t_admin LIMIT $awal, $akhir ")->result();
			$a['page']		= "l_manage_admin";
		}
		
		$this->load->view('admin/index', $a);
	}

	public function get_klasifikasi() {
		$kode 				= $this->input->post('kode',TRUE);
		
		$data 				=  $this->db->query("SELECT id, kode, nama FROM ref_klasifikasi WHERE kode LIKE '%$kode%' ORDER BY id ASC")->result();
		
		$klasifikasi 		=  array();
        foreach ($data as $d) {
			$json_array				= array();
            $json_array['value']	= $d->kode;
			$json_array['label']	= $d->kode." - ".$d->nama;
			$klasifikasi[] 			= $json_array;
		}
		
		echo json_encode($klasifikasi);
	}
	
	//tambahan
	public function get_bidang() {
		$kode 				= $this->input->post('kpd_yth',TRUE);
		
		$data 				=  $this->db->query("SELECT * FROM m_bidang WHERE nama_bidang LIKE '%$kode%' ORDER BY id_bidang ASC")->result();
		
		$klasifikasi 		=  array();
        foreach ($data as $d) {
			$json_array				= array();
            $json_array['value']	= $d->id_bidang." - ".$d->nama_bidang;
			$json_array['label']	= $d->id_bidang." - ".$d->nama_bidang;
			$klasifikasi[] 			= $json_array;
		}
		
		echo json_encode($klasifikasi);
	}
	
	public function get_instansi_lain() {
		$kode 				= $this->input->post('dari',TRUE);
		
		$data 				=  $this->db->query("SELECT dari FROM t_surat_masuk WHERE dari LIKE '%$kode%' GROUP BY dari")->result();
		
		$klasifikasi 		=  array();
        foreach ($data as $d) {
			$klasifikasi[] 	= $d->dari;
		}
		
		echo json_encode($klasifikasi);
	}
	
	public function passwod() {
		if ($this->session->userdata('admin_valid') == FALSE && $this->session->userdata('admin_id') == "") {
			redirect("index.php/admin/login");
		}
		
		$ke				= $this->uri->segment(3);
		$id_user		= $this->session->userdata('admin_id');
		
		//var post
		$p1				= md5($this->input->post('p1'));
		$p2				= md5($this->input->post('p2'));
		$p3				= md5($this->input->post('p3'));
		
		if ($ke == "simpan") {
			$cek_password_lama	= $this->db->query("SELECT password FROM t_admin WHERE id = $id_user")->row();
			//echo 
			
			if ($cek_password_lama->password != $p1) {
				$this->session->set_flashdata('k_passwod', '<div id="alert" class="alert alert-error">Password Lama tidak sama</div>');
				redirect('index.php/admin/passwod');
			} else if ($p2 != $p3) {
				$this->session->set_flashdata('k_passwod', '<div id="alert" class="alert alert-error">Password Baru 1 dan 2 tidak cocok</div>');
				redirect('index.php/admin/passwod');
			} else {
				$this->db->query("UPDATE t_admin SET password = '$p3' WHERE id = ".$id_user."");
				$this->session->set_flashdata('k_passwod', '<div id="alert" class="alert alert-success">Password berhasil diperbaharui</div>');
				redirect('index.php/admin/passwod');
			}
		} else {
			$a['page']	= "f_passwod";
		}
		
		$this->load->view('admin/index', $a);
	}
	
	//login
	public function login() {
		$this->load->view('admin/login');
	}
	
	public function do_login() {
		$u 		= $this->security->xss_clean($this->input->post('u'));
		$ta 	= $this->security->xss_clean($this->input->post('ta'));
        $p 		= md5($this->security->xss_clean($this->input->post('p')));
         
		$q_cek	= $this->db->query("SELECT * FROM t_admin WHERE username = '".$u."' AND password = '".$p."'");
		$j_cek	= $q_cek->num_rows();
		$d_cek	= $q_cek->row();
		//echo $this->db->last_query();
		
        if($j_cek == 1) {
            $data = array(
                    'admin_id' => $d_cek->id,
                    'admin_user' => $d_cek->username,
                    'admin_nama' => $d_cek->nama,
                    'admin_ta' => $ta,
                    'admin_level' => $d_cek->level,
					'admin_nip' => $d_cek->nip,
					'admin_unitkerja' => $d_cek->id_unitkerja,
					'admin_email' => $d_cek->email,
					'admin_valid' => true
                    );
            $this->session->set_userdata($data);
            redirect('index.php/admin');
        } else {	
			$this->session->set_flashdata("k", "<div id=\"alert\" class=\"alert alert-error\">username or password is not valid</div>");
			redirect('index.php/admin/login');
		}
	}
	
	public function logout(){
        $this->session->sess_destroy();
		redirect('index.php/admin/login');
    }
	


	public function get_kegiatan() {
		$unitkerja=$this->input->post('unitkerja');
		$bidang = substr($unitkerja,1,4);
		$query 	=  $this->db->query("SELECT * FROM m_jeniskegiatan WHERE substring(id_jeniskegiatan,1,4)='$bidang'");
		?>
		<option value="Kosong">-- Pilih Nama Kegiatan--<?php echo $bidang;?></option>
		<?php
        foreach($query->result() as $row)
        { 
             echo "<option value='".$row->id_jeniskegiatan."'>".$row->nama_kegiatan."</option>";
        }
	}
	
	
public function konfirmasi() {
		if ($this->session->userdata('admin_valid') == FALSE && $this->session->userdata('admin_id') == "") {
			redirect("index.php/admin/login");
		}
		
		$ta = $this->session->userdata('admin_ta');
		parse_str($_SERVER['QUERY_STRING'], $_GET);  
		
		//ambil variabel URL
		$mau_ke					= $this->uri->segment(3);
		$idu					= $this->uri->segment(4);
		
		$cari					= addslashes($this->input->post('q'));

		//ambil variabel post
		$id_kontrak_konfirm		= addslashes($this->input->post('id_kontrak'));
		$cari					= addslashes($this->input->post('q'));
		
		if ($mau_ke == "edt") {
			$id_edit 		  = $this->input->get('konfirmasi_id');
			?>
			<script>
			window.alert('<?php echo $id_edit;?>');</script>
			<?php
			$a['datpil']	= $this->db->query("select * from t_kontrak where id_kontrak='$id_edit'")->row();	
			$a['page']		= "f_konfirmasi";
		}
		else if ($mau_ke == "act_edt")
		{
				$this->db->query("update t_kontrak set flag_konfirm='1' where id_kontrak='$id_kontrak_konfirm'");
				$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data sudah dikonfirmasi selesai</div>");
				redirect('index.php/admin/oi/');
		
		}
		
		$this->load->view('admin/index', $a);
	}




//dda
public function dda() {
		if ($this->session->userdata('admin_valid') == FALSE && $this->session->userdata('admin_id') == "") {
			redirect("index.php/admin/login");
		}
		
		$ta = $this->session->userdata('admin_ta');
		
		/* pagination */	
		$total_row		= $this->db->query("SELECT * FROM t_list_tabel")->num_rows();
		$per_page		= 10;
		
		$awal	= $this->uri->segment(4); 
		$awal	= (empty($awal) || $awal == 1) ? 0 : $awal;
		
		if (empty($awal) || $awal == 1) { $awal = 0; } { $awal = $awal; }
		$akhir	= $per_page;
		
		$a['pagi']	= _page($total_row, $per_page, 4, base_url()."index.php/admin/dda/p");
		
		//ambil variabel URL
		$mau_ke					= $this->uri->segment(3);
		$idu					= $this->uri->segment(4);
		$cari					= addslashes($this->input->post('q'));
	
		//ambil variabel post
		
		$idp					= addslashes($this->input->post('idp'));
		$nip					= addslashes($this->input->post('nip'));
		//$tgl					= addslashes($this->input->post('tgl'));
		//$jam_keluar			= addslashes($this->input->post('jam_keluar'));
		//$jam_masuk			= addslashes($this->input->post('jam_masuk'));
		$status					= addslashes($this->input->post('status'));
		$kepentingan_kel		= addslashes($this->input->post('kepentingan_kel'));
		$kepentingan_uraian		= addslashes($this->input->post('kepentingan_uraian'));
				
		$cari					= addslashes($this->input->post('q'));
		$nippegawai=$this->session->userdata('admin_nip');

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

		$this->load->library('upload', $config);
		
		if($mau_ke == "del")
		{
			$id_delete = $this->input->get('delete_id');;
			?>
			<script>
			//window.alert('<?php echo $id_delete;?>');</script>
			<?php
			$a['datpil']	= $this->db->query("select * from t_oi where id='$id_delete'")->row();	
			$a['page']		= "f_del";
		
		}
		else if ($mau_ke == "act_del") {
			$id_oi = addslashes($this->input->post('id'));
			$this->db->query("DELETE FROM t_oi WHERE id = '$id_oi'");
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been deleted </div>");
			redirect('index.php/admin/oi/');
			
		} 
		else if($mau_ke == "in")
		{
			$id_in = $this->input->get('in_id');;
			?>
			<script>
			//window.alert('<?php echo $id_in;?>');</script>
			<?php
			$a['datpil']	= $this->db->query("select * from t_oi where id='$id_in'")->row();	
			$a['page']		= "f_in";
		
		}
		else if ($mau_ke == "act_in") {
			$id_oi = addslashes($this->input->post('id'));
			date_default_timezone_set("Asia/Jakarta");
			$tanggal_in=date("H:i");
			$this->db->query("UPDATE t_oi SET jam_masuk='$tanggal_in' where id='$id_oi'");
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been deleted </div>");
			redirect('index.php/admin/oi/');
			
		} 
		else if($mau_ke == "konfirmasi")
		{
			$id_konfirmasi = $this->input->get('konfirmasi_id');;
			?>
			<script>
			//window.alert('<?php echo $id_konfirmasi;?>');</script>
			<?php
			$a['datpil']	= $this->db->query("select l.id, l.judul_ind, l.judul_en, l.id_unitkerja, m.unitkerja_ind from t_list_tabel l left join m_unitkerja m on l.id_unitkerja=m.id_unitkerja where l.id='$id_konfirmasi'")->row();	
			$a['page']		= "f_konfirmasi";
		
		}
		else if ($mau_ke == "act_konfirmasi") {
			$id_data = addslashes($this->input->post('id'));
			$catatan = addslashes($this->input->post('catatan'));
			
			//$confirmed_by=$this->session->userdata('admin_nama');
			$this->db->query("UPDATE t_list_tabel SET is_confirm='1', catatan='$catatan' where id='$id_data'");
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been updated</div>");
			redirect('index.php/admin/dda/');
			
		} 
		else if ($mau_ke == "cari") {

			$a['data']		= $this->db->query("SELECT * from t_oi where keterangan_uraian  LIKE '%$cari%' ")->result();
			$a['page']		= "l_oi";
			
		} else if ($mau_ke == "add") {
		
			$a['page']		= "f_oi";
			
		} else if ($mau_ke == "edt") {

			$a['datpil']	= $this->db->query("SELECT * from t_oi WHERE id = '$idu'")->row();	
			$a['page']		= "f_oi";
		} 
		else if ($mau_ke == "act_add") {	
				$hariini=date('Y-m-d');
				date_default_timezone_set("Asia/Jakarta");
				$waktusekarang= date("H:i");
			
				$this->db->query("INSERT INTO t_oi VALUES (NULL, '$nippegawai', '$hariini', '$waktusekarang','','T','$kepentingan_kel','$kepentingan_uraian','','')");
				$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been added</div>");
				redirect('index.php/admin/dda');

		}
		else if ($mau_ke == "act_edt") {
			?>
			<script>
			//window.alert('<?php echo $idu;?>');</script>
			<?php
				$this->db->query("UPDATE t_oi SET kepentingan_kel ='$kepentingan_kel', kepentingan_uraian= '$kepentingan_uraian' where id='$idp'");

				$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been updated</div>");
				redirect('index.php/admin/dda/');
		}
		else if ($mau_ke == "view_tabel")
		{
			$id_unitkerja = $this->input->get('id');
			$a['data']		= $this->db->query("SELECT * FROM t_list_tabel where id_unitkerja='$id_unitkerja' LIMIT $awal, $akhir ")->result();
			$a['page']		= "l_dda";	
		}
		else 
		{
			$unitkerjalogin=$this->session->userdata('admin_unitkerja');
			
			if ($unitkerjalogin == 'bps')
			{
			/* pagination */	
			$total_row		= $this->db->query("SELECT * FROM m_unitkerja")->num_rows();
			$per_page		= 10;
			
			$awal	= $this->uri->segment(4); 
			$awal	= (empty($awal) || $awal == 1) ? 0 : $awal;
			
			if (empty($awal) || $awal == 1) { $awal = 0; } { $awal = $awal; }
			$akhir	= $per_page;
			
			$a['pagi']	= _page($total_row, $per_page, 4, base_url()."index.php/admin/dda/p");
					
				
			 $a['data']		= $this->db->query("SELECT * FROM m_unitkerja LIMIT $awal, $akhir")->result();
			 $a['page']		= "l_dda_admin";
			}
			else
			{
			 $a['data']		= $this->db->query("SELECT * FROM t_list_tabel where id_unitkerja='$unitkerjalogin' LIMIT $awal, $akhir ")->result();
			 $a['page']		= "l_dda";	
			}
		}
		
		$this->load->view('admin/index', $a);
	}

public function forum_diskusi()
	{
		if ($this->session->userdata('admin_valid') == FALSE && $this->session->userdata('admin_id') == "") {
			redirect("index.php/admin/login");
		}
		
		//Setting Email
		$mail = new PHPMailer();
        $mail->IsSMTP(); // we are going to use SMTP
        $mail->SMTPAuth   = true; // enabled SMTP authentication
        $mail->SMTPSecure = "ssl";  // prefix for secure protocol to connect to the server
        $mail->Host       = "ssl://smtp.gmail.com";      // setting GMail as our SMTP server
        $mail->Port       = 465;                   // SMTP port to connect to GMail
        $mail->Username   = "dda.jateng@gmail.com";  // user email address
        $mail->Password   = "dd4j4t3ng3300";            // password in GMail
        $mail->SetFrom('dda.jateng@gmail.com', 'Kabupaten Kendal Dalam Angka');  //Who is sending the email
        //$mail->AddReplyTo("datakita1612@gmail.com","Data Kita");  //email address that receives the response
        $mail->Subject    = "Forum Diskusi Kabupaten Kendal Dalam Angka";
        //$mail->Body      = "<h1>Hallo, hanya memastikan saja mas :)</h1>";
        //$mail->AltBody    = "coba coba";
        //$destino = "rizchi.ew@gmail.com"; // Who is addressed the email to
        //$mail->AddAddress($destino, "Cahya Dy");

        //$mail->AddAttachment("");      // some attached files
        //$mail->AddAttachment(""); // as many as you want
        if($mail->Send()) {
		$data["message"] = "Message sent correctly!";
        } 
		else {
            $data["message"] = "Error: " . $mail->ErrorInfo;
        }
        //$this->load->view('sent_mail.php',$data);
		// end of setting email
		
		$ta = $this->session->userdata('admin_ta');
		
		/* pagination */	
		$total_row		= $this->db->query("SELECT * FROM m_unitkerja")->num_rows();
		$per_page		= 10;
		
		$awal	= $this->uri->segment(4); 
		$awal	= (empty($awal) || $awal == 1) ? 0 : $awal;
		
		if (empty($awal) || $awal == 1) { $awal = 0; } { $awal = $awal; }
		$akhir	= $per_page;
		
		$a['pagi']	= _page($total_row, $per_page, 4, base_url()."index.php/admin/forum_diskusi/p");
		
		//ambil variabel URL
		$mau_ke					= $this->uri->segment(3);
		$idu					= $this->uri->segment(4);
		$cari					= addslashes($this->input->post('q'));
		$unitkerjalogin			= $this->session->userdata('admin_unitkerja');
		$email 					= $this->session->userdata('admin_email');
		$nama 					= $this->session->userdata('admin_nama');
		
		if($mau_ke == 'view_topik')
		{	
			$id_unitkerja = $this->input->get('id');
			if($unitkerjalogin == 'bps')
			{
				$a['data']		= $this->db->query("SELECT * FROM m_forumtopic where id_unitkerja='$id_unitkerja' LIMIT $awal, $akhir ")->result();
				$a['page']		= "l_forum_topik";
			}
			else
			{
			  $a['data']		= $this->db->query("SELECT * FROM m_forumtopic where id_unitkerja='$unitkerjalogin' LIMIT $awal, $akhir ")->result();
			  $a['page']		= "l_forum_topik";
			}	
		}
		else if ($mau_ke == 'add_topik')
		{
			$a['page']		= "f_tambah_topik";	
		}
		else if ($mau_ke == 'act_add_topik')
		{
			$hariini=date('Y-m-d');
			$nama_topik					= addslashes($this->input->post('nama_topik'));
			$user_id					= $this->session->userdata('admin_user');
			
			$mail->Body      = $nama_topik." telah ditambahkan";
			//$mail->AltBody    = "coba coba";
			//$destino = "rizchi.ew@gmail.com"; // Who is addressed the email to
			$mail->AddAddress($email,$nama);
			
			$mail->Send();
			$this->db->query("INSERT INTO m_forumtopic VALUES (NULL, '$unitkerjalogin','$nama_topik','$user_id','$hariini')");
			
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Topik Telah Ditambahkan</div>");
			redirect('index.php/admin/forum_diskusi/'.$unitkerjalogin.'');
		}
		else if ($mau_ke == 'view_comment')
		{
			$id_topik					= $this->uri->segment(4);
			$a['data']					= $this->db->query("SELECT * FROM forumcomment where id_topik='$id_topik' LIMIT $awal, $akhir ")->result();
			$a['page']					= "l_forum_komen";
		}
		else if ($mau_ke == 'add_comment')
		{
			$hariini=date('Y-m-d');
			$comment					= addslashes($this->input->post('comment'));
			$id_topik					= addslashes($this->input->post('id_topik'));
			$user_id					= $this->session->userdata('admin_user');
			
			$mail->Body      = $comment." telah ditambahkan";
			$mail->AddAddress($email,$nama);
			$mail->Send();
			
			$this->db->query("INSERT INTO forumcomment VALUES (NULL,'$id_topik','$comment','$user_id','$hariini')");
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Komentar Telah Ditambahkan</div>");
			redirect('index.php/admin/forum_diskusi/view_comment/'.$id_topik.'');
		}
		else
		{
			if ($unitkerjalogin == 'bps')
			{
				$a['data']		= $this->db->query("SELECT * FROM m_unitkerja LIMIT $awal, $akhir ")->result();
				$a['page']		= "l_forum";	
			}
			else
			{
			  $a['data']		= $this->db->query("SELECT * FROM m_unitkerja where id_unitkerja='$unitkerjalogin' LIMIT $awal, $akhir ")->result();
			  $a['page']		= "l_forum";	
			}
		
		}
		$this->load->view('admin/index', $a);
	}

	//master_tabel
public function master_tabel() {
		if ($this->session->userdata('admin_valid') == FALSE && $this->session->userdata('admin_id') == "") {
			redirect("index.php/admin/login");
		}
		
		$ta = $this->session->userdata('admin_ta');
		
		/* pagination */	
		$total_row		= $this->db->query("SELECT * FROM t_list_tabel")->num_rows();
		$per_page		= 10;
		
		$awal	= $this->uri->segment(4); 
		$awal	= (empty($awal) || $awal == 1) ? 0 : $awal;
		
		if (empty($awal) || $awal == 1) { $awal = 0; } { $awal = $awal; }
		$akhir	= $per_page;
		
		$a['pagi']	= _page($total_row, $per_page, 4, base_url()."index.php/admin/master_tabel/p");
		
		//ambil variabel URL
		$mau_ke					= $this->uri->segment(3);
		$idu					= $this->uri->segment(4);
		$cari					= addslashes($this->input->post('q'));
	
		//ambil variabel post
		
		$idp					= addslashes($this->input->post('idp'));
		$judul_ind				= addslashes($this->input->post('judul_ind'));
		$judul_en				= addslashes($this->input->post('judul_en'));
		$link_tabel				= addslashes($this->input->post('link_tabel'));
		$id_unitkerja			= addslashes($this->input->post('id_unitkerja'));
		$is_confirm				= '2';
		$catatan				= '-';
				
		$cari					= addslashes($this->input->post('q'));
			
		if($mau_ke == "del")
		{
			$id_delete = $this->input->get('delete_id');;
			?>
			<script>
			//window.alert('<?php echo $id_delete;?>');</script>
			<?php
			$a['datpil']	= $this->db->query("select * from t_list_tabel where id='$id_delete'")->row();	
			$a['page']		= "f_del_master";
		}
		else if ($mau_ke == "act_del") {
			$id_oi = addslashes($this->input->post('id'));
			$this->db->query("DELETE FROM t_list_tabel WHERE id = '$id_oi'");
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been deleted </div>");
			redirect('index.php/admin/master_tabel/');
		} 
		else if ($mau_ke == "cari") {

			$a['data']		= $this->db->query("SELECT * from t_list_tabel where keterangan_uraian  LIKE '%$cari%' ")->result();
			$a['page']		= "l_master_tabel";
			
		} else if ($mau_ke == "add") {
		
			$a['page']		= "f_master_tabel";
			
		} else if ($mau_ke == "edt") {

			$a['datpil']	= $this->db->query("SELECT * from t_list_tabel WHERE id = '$idu'")->row();	
			$a['page']		= "f_master_tabel";
		} 
		else if ($mau_ke == "act_add") {	
				$hariini=date('Y-m-d');
				date_default_timezone_set("Asia/Jakarta");
				$waktusekarang= date("H:i");
			
				$this->db->query("INSERT INTO t_list_tabel VALUES (NULL, '$judul_ind', '$judul_en', '$link_tabel','$id_unitkerja','$is_confirm','$catatan')");
				$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data berhasil ditambahkan</div>");
				redirect('index.php/admin/master_tabel/');
		}
		else if ($mau_ke == "act_edt") {
			
				$this->db->query("UPDATE t_list_tabel SET judul_ind ='$judul_ind', judul_en= '$judul_en', link_tabel = '$link_tabel', id_unitkerja='$id_unitkerja' where id='$idp'");
				$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data berhasil di update</div>");
				redirect('index.php/admin/master_tabel/');
		}
		else 
		{
			 $a['data']		= $this->db->query("SELECT l.*,u.unitkerja_ind FROM t_list_tabel l left join m_unitkerja u on l.id_unitkerja=u.id_unitkerja LIMIT $awal, $akhir ")->result();
			 $a['page']		= "l_master_tabel";	
		}
		$this->load->view('admin/index', $a);
	}
	
}
