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

class User_siakad 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');
	   $this->load->model('model_users');
	}
	
	public function list_data() {
		
		$ta = $this->session->userdata('admin_ta');
		   
		$total_row		= $this->db->query("SELECT * FROM tb_pengguna ")->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()."user_siakad/list_data/p");
		
		$mau_ke					= $this->uri->segment(3);
		$postaktif				= $this->uri->segment(4);
		$idu					= $this->uri->segment(5);
		$resetpassw				= hash("sha512", md5('123'));
		$id_user		= $this->session->userdata('admin_id');
		$cari					= addslashes($this->input->post('q'));
		
		
		//ambil variabel post
		$idp					= addslashes($this->input->post('idp'));
		$no_agenda				= addslashes($this->input->post('no_agenda'));
		$indek_berkas			= addslashes($this->input->post('indek_berkas'));
		$kode					= addslashes($this->input->post('kode'));
		$dari					= addslashes($this->input->post('dari'));
		$no_surat				= addslashes($this->input->post('no_surat'));
		$tgl_surat				= addslashes($this->input->post('tgl_surat'));
		$uraian					= addslashes($this->input->post('uraian'));
		$ket					= addslashes($this->input->post('ket'));
		$sifat					= addslashes($this->input->post('sifat')); 
		$kepada					= addslashes($this->input->post('kepada'));
		$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") {
			
			$this->db->query("DELETE FROM t_surat_masuk WHERE id = '$idu'");
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data berhasil dihapus </div>");
			redirect('masuk/surat_masuk');
		} else if ($mau_ke == "cari") {
			$a['data']		= $this->db->query("SELECT A.*
								FROM tb_pengguna A
									WHERE nama_lengkap LIKE '%$cari%' ORDER BY nama_lengkap ASC")->result();
			$a['page']		= "user/list_";
		} else if ($mau_ke == "add") {
			$a['data']		= $this->db->query("SELECT * FROM tb_group_users ORDER BY level ASC");
			$a['page']		= "user/form_";
		} else if ($mau_ke == "edt") {
			$a['datpil']	= $this->db->query("SELECT * FROM t_surat_masuk WHERE id = '$idu'")->row();	
			$a['page']		= "f_surat_masuk";
		} else if ($mau_ke == "act_add") {	
			if ($this->upload->do_upload('file_surat')) {
				$up_data	 	= $this->upload->data();
				//simpan surat masuk
				$this->db->query("INSERT INTO t_surat_masuk (id,kode,no_agenda,indek_berkas,isi_ringkas,dari,kepada,no_surat,tgl_surat,tgl_diterima,keterangan,file,sifat_surat,pengolah) 
										VALUES (NULL, '$kode', '	$no_agenda', '$indek_berkas', '$uraian', '$dari', '$kepada','$no_surat', '$tgl_surat', NOW(), '$ket', '".$up_data['file_name']."', '$sifat', '".$this->session->userdata('admin_id')."')");
				$idmakx		= $this->db->query("SELECT MAX(id) AS id FROM t_surat_masuk")->result();
					foreach ($idmakx as $b) {
						$idsurat = $b->id;
					}
				
			
			} else {
					$this->db->query("INSERT INTO t_surat_masuk (id,kode,no_agenda,indek_berkas,isi_ringkas,dari,kepada,no_surat,tgl_surat,tgl_diterima,keterangan,file,sifat_surat,pengolah) VALUES (NULL, '$kode', '$no_agenda', '$indek_berkas', '$uraian', '$dari', '$kepada','$no_surat', '$tgl_surat', NOW(), '$ket', '', '$sifat', '".$this->session->userdata('admin_id')."')");
				
					$idmakx		= $this->db->query("SELECT MAX(id) AS id FROM t_surat_masuk")->result();
						foreach ($idmakx as $b) {
							$idsurat = $b->id;
						}
				}	
			
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data berhasil disimpan. ".$this->upload->display_errors()."</div>");
			redirect('masuk/surat_masuk');
		} else if ($mau_ke == "aktif") {
				$this->db->query("UPDATE tb_pengguna SET blokir = '$postaktif' WHERE user_id = '$idu'");
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data berhasil di update. ".$this->upload->display_errors()."</div>");			
			redirect('user_siakad/list_data');
		} else if ($mau_ke == "reset") {
				$this->db->query("UPDATE tb_pengguna SET password = '$resetpassw' WHERE user_id = '$idu'");
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Username telah diubah menjadi default. ".$this->upload->display_errors()."</div>");			
			redirect('user_siakad/list_data');
		} else if ($mau_ke == "ubahpassword") {
					$this->model_users->users_update($idu);
				
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Username telah diubah menjadi default. ".$this->upload->display_errors()."</div>");			
			redirect('user_siakad/list_data');
		} else if ($mau_ke == "form_ubahpassword") {
			$a['datpil']	= $this->db->query("SELECT * FROM tb_pengguna WHERE user_id = $id_user")->row();
			//$a['datpil']	= $this->db->query("SELECT * FROM tb_pengguna WHERE user_id = '$idu'")->row();
			$a['page']		= "user/form_ubahpassword";
		} else {
			$a['data']		= $this->db->query("SELECT A.*
								FROM tb_pengguna A
										ORDER BY A.level ASC,A.nama_lengkap ASC LIMIT $awal, $akhir ")->result();
			$a['page']		= "user/list_";
		}
		
		$this->load->view('admin/home_menu', $a);
	}
	public function password_save() {
		if ($this->session->userdata('admin_valid') == FALSE && $this->session->userdata('admin_id') == "") {
			redirect("admin/login");
		}
		
		$ke				= $this->uri->segment(4);
		$id_user		= $this->session->userdata('admin_id');
		$tombol 		= $this->input->post('act');
		//var post hash("sha512", md5('123'));
		$p1				= hash("sha512", md5($this->input->post('passw1')));
		$p2				= hash("sha512", md5($this->input->post('passw2')));
		$p3				= hash("sha512", md5($this->input->post('passw3')));
		
		if ($tombol == "simpan") {
			$cek_password_lama	= $this->db->query("SELECT password FROM tb_pengguna WHERE user_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('user_siakad/password_save/act_ubah');
			} else if ($p3 != $p2) {
				$this->session->set_flashdata('k_passwod', '<div id="alert" class="alert alert-error">Password Baru dan Ulangi Password baru tidak cocok</div>');
				redirect('user_siakad/password_save/act_ubah');
			} else {
				$this->db->query("UPDATE tb_pengguna SET password = '$p3' WHERE user_id = ".$id_user."");
				$this->session->set_flashdata('k_passwod', '<div id="alert" class="alert alert-success">Password berhasil diperbaharui</div>');
				redirect('user_siakad/password_save/act_ubah');
			}
		} else {
			$a['datpil']	= $this->db->query("SELECT * FROM tb_pengguna WHERE user_id = $id_user")->row();
			$a['page']		= "user/form_ubahpassword";
		}
		
		$this->load->view('admin/home_menu', $a);
	}
	function tambah_(){
		if (isset($_POST['submit'])){
			
			$cek = $this->db->query("SELECT * FROM tb_pengguna WHERE username ='".$this->input->post('a')."'");
		    $row = $cek->row_array();
		    $total = $cek->num_rows();
			if ($total > 0){
				$this->session->set_flashdata("k", "<div class=\"alert alert-warning\" id=\"alert\">Ups, Nama User Sudah ada [".$this->input->post('a')."],ulangi lagi.</div>");
				redirect('user_siakad/tambah_');
			}else{
				$this->model_users->users_tambah();
				
				$this->session->set_flashdata("k", "<div class=\"alert alert-info\" id=\"alert\">Data berhasil ditambah</div>");
				redirect('user_siakad/tambah_');
			}
			
		}else{
			$a['data']		= $this->db->query("SELECT * FROM tb_group_users ORDER BY level ASC");
			$a['page']		= "user/form_";
		}
		$this->load->view('admin/home_menu', $a);
	}
	public function list_data_pegawai() {
		
		$ta = $this->session->userdata('admin_ta');
		   
		$total_row		= $this->db->query("SELECT * FROM pegawai")->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()."user_siakad/list_data_pegawai/p");
		
		$mau_ke					= $this->uri->segment(3);
		$postaktif				= $this->uri->segment(4);
		$idu					= $this->uri->segment(5);
		$resetpassw				= hash("sha512", md5('123'));
		$id_user		= $this->session->userdata('admin_id');
		$cari					= addslashes($this->input->post('q'));
		
		
		//ambil variabel post
		$idp					= addslashes($this->input->post('idp'));
				
		if ($mau_ke == "del") {
			$id = $this->encrypt->decode(base64_decode($this->uri->segment(4)));
				$this->db->query("DELETE FROM pegawai WHERE id = '$id'");
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data berhasil dihapus </div>");
			redirect('user_siakad/list_data_pegawai');
		} else if ($mau_ke == "cari") {
			$a['data']		= $this->db->query("SELECT * FROM pegawai
									WHERE nama_pegawai LIKE '%$cari%' ORDER BY nama_pegawai ASC")->result();
			$a['page']		= "user/list_pegawai";
		} else if ($mau_ke == "aktif") {
			//echo " ID :".$postaktif;exit;
				$this->db->query("UPDATE pegawai SET blokir = '$postaktif' WHERE id = '$idu'");
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data berhasil di update. </div>");			
			redirect('user_siakad/list_data_pegawai');
		
		} else {
			$a['data']		= $this->db->query("SELECT * FROM pegawai ORDER BY nama_pegawai ASC LIMIT $awal, $akhir ")->result();
			$a['page']		= "user/list_pegawai";
		}
		
		$this->load->view('admin/home_menu', $a);
	}
	
}
