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

class Manajemen_user extends CI_Controller {
	function __construct(){
		parent::__construct();
        $this->load->library('datatables');
        $this->master_m->HarusLogin();
	}
	
	public function index()
	{
		$q=$this->db->get('jenis_user')->result();
		foreach($q as $val){
			$jns_user[$val->id_jenis_user]=$val->level;
		}
		$q=$this->db->get('data_kelurahan')->result();
		foreach($q as $val){
			$data_kelurahan[$val->id_data_kelurahan]=$val->nama_kelurahan;
		}
		$data['jns_user']=$jns_user;
		$data['data_kelurahan']=$data_kelurahan;
		$data['konten']='manajemen_user_v';
		$this->load->view('index_v',$data);
	}
	
	public function json()
	{
		$q=$this->db->get('jenis_user')->result();
		foreach($q as $val){
			$jns_user[$val->id_jenis_user]=$val->level;
		}
		header('Content-Type: application/json');
		$this->datatables->select('user.id_user as ID,username,jenis_user.level as jenis_user,data_kelurahan.nama_kelurahan');
        $this->datatables->from('user');
		
		$this->datatables->join('jenis_user', 'user.id_jenis_user = jenis_user.id_jenis_user');
		$this->datatables->join('data_kelurahan', 'user.id_kelurahan = data_kelurahan.id_data_kelurahan','left');
		
		if($this->session->userdata('id_jenis_user')==1){
			$this->datatables->add_column('view', '<button id="$1" class="btn btn-info btn-xs edit" username="$2" hak_akses="$3" >&nbsp;&nbsp;Edit&nbsp;&nbsp;</button> | <a href="manajemen_user/hapus/$1"  class="btn btn-danger btn-xs hapus"   onclick="return confirm(\'Apakah anda yakin untuk menghapus user ini?\');">Hapus</a>', 'ID');
		}else{
			$this->datatables->add_column('view', '<button id="$1" class="btn btn-info btn-xs edit" username="$2" hak_akses="$3" >&nbsp;&nbsp;Edit&nbsp;&nbsp;</button>', 'ID');
		}
		
		if($this->session->userdata('id_jenis_user')!=1)
			$this->datatables->where(array('id_user'=>$this->session->userdata('id_user')));

        echo $this->datatables->generate();
		/*
        $data=json_decode($this->datatables->generate(), true);

		foreach($data['data'] as $key=>$val){
			$data['data'][$key]['jenis_user']=$jns_user[$val['jenis_user']];
		}
		echo json_encode($data);
		*/
	}
	
	function get_data_user(){
		$id_user=$this->master_m->InputPost('id_user');
		$q=$this->db->get_where('user',array('id_user'=>$id_user))->row();
		echo json_encode(array('id_user'=>$q->id_user,'username'=>$q->username,'password'=>$q->password,'id_jenis_user'=>$q->id_jenis_user,'id_kelurahan'=>$q->id_kelurahan));
	}
	
	function simpan_user(){
		$username=$this->master_m->InputPost('username');
		$q=$this->db->get_where('user',array('username'=>$username))->num_rows();
		if($q==0){
			if($this->input->post('id_jenis_user')!=2)
				$_POST['id_kelurahan']=0;
			
			$this->master_m->smart_save('user',array('key'=>rand(99999,99999999),'password'=>$this->master_m->getHashedPassword($this->master_m->InputPost('password'))));
			echo 1;
		}else
			echo 0;
	}
	
	function update_user(){
		$username=$this->master_m->InputPost('username_edit');
		$id_user=$this->master_m->InputPost('hidden_id_user_edit');
		$q=$this->db->get_where('user',array('username'=>$username));
		$boleh_simpan=0;
		if($q->num_rows()==1){//jika data ada
			if($q->row('id_user')==$id_user)//jika usernya dia sendiri
				$boleh_simpan=1;
			
		}else{
			$boleh_simpan=1;
		}
		
		if($boleh_simpan==1){
			if($this->master_m->InputPost('password_edit')!='' && $this->master_m->InputPost('aktifkan_form_password')=='password_ganti'){
				if($this->session->userdata('id_jenis_user')==1){
					$data = array(
						   'username' => $this->master_m->InputPost('username_edit'),
						   'password' => $this->master_m->getHashedPassword($this->master_m->InputPost('password_edit')),
						   'id_jenis_user' => $this->master_m->InputPost('id_jenis_user_edit')
					);
				}else{
					$data = array(
						   'username' => $this->master_m->InputPost('username_edit'),
						   'password' => $this->master_m->getHashedPassword($this->master_m->InputPost('password_edit'))
					);

				}
			}else{
				if($this->session->userdata('id_jenis_user')==1){
					$data = array(
						   'username' => $this->master_m->InputPost('username_edit'),
						   'id_jenis_user' => $this->master_m->InputPost('id_jenis_user_edit')
					);
				}else{
					$data = array(
					   'username' => $this->master_m->InputPost('username_edit')
				);
				}	
			}
			if($this->input->post('id_jenis_user_edit')==2){
				$data['id_kelurahan']=$this->master_m->InputPost('id_kelurahan');
			}else{
				$data['id_kelurahan']='';
			}
				
			$this->db->where('id_user', $this->master_m->InputPost('hidden_id_user_edit'));
			$this->db->update('user', $data);
			echo 1;
		}else
			echo 0;
	}
	
	function hapus(){
		$id_user=$this->uri->segment(3);
		$this->db->delete('user', array('id_user' => $id_user)); 
		redirect(base_url('manajemen_user'));
	}

}
