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

class Admin extends CI_Controller {
	function __construct() {
		parent::__construct();
		
	}
	
	public function index() {
		if ($this->session->userdata('admin_valid') == FALSE ) {
			redirect("admin/login");
		}
		if(!empty($_SERVER['HTTP_CLIENT_IP'])){
				  $ip=$_SERVER['HTTP_CLIENT_IP'];
				}
				elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
				  $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
				}
				else{
				  $ip=$_SERVER['REMOTE_ADDR'];
				}
				$hostname=$_SERVER['HTTP_USER_AGENT'];
			 $hostnamezzz = gethostbyaddr($_SERVER['REMOTE_ADDR']);
			
			$cek = $this->db->query("SELECT * FROM tb_kelurahan_komdanas LIMIT 1");
		   	foreach ($cek->result_array() as $row){
			$eselon		= $row['satker_code'];
				
				}
			if (empty($eselon)){
				$this->session->set_flashdata("k", "<div class=\"alert alert-warning\" id=\"alert\">Ups, Silahkan cek radius untuk tarik data komdanas.</div>");
				redirect('hitung_panjar/json_radius');
			}else{
				$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data Komdanas tersedia,sebaiknya sering update tarik data komdanas pada menu pengaturan -->cek komdanas</div>");
				//redirect('kewenangan/list_data_');
			}	 
			
		$data['page']	= "halaman_utama";
		$data['record_instansi'] = $this->app_model->get_identitas()->row_array();
		$this->load->view('admin/home_menu', $data);
	}

	//login
	public function panjar() {
			$a['versi'] = '3.4';
			$cek = $this->db->query("SELECT * FROM tb_kelurahan_komdanas LIMIT 1");
		   	foreach ($cek->result_array() as $row){
			$eselon		= $row['satker_code'];
				
				}
			if (empty($eselon)){
				$this->session->set_flashdata("k", "<div class=\"alert alert-warning\" id=\"alert\">Ups, Silahkan cek radius untuk tarik data komdanas.</div>");
				$this->load->view('admin/webpanjar_radius_nihil');
			}else{
				$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data Komdanas tersedia,sebaiknya sering update tarik data komdanas pada menu pengaturan -->cek komdanas</div>");
				$this->load->view('admin/webpanjar', $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 		= hash("sha512",md5($this->security->xss_clean($this->input->post('p'))));
         
		$q_cek	= $this->db->query("SELECT * FROM tb_pengguna WHERE username = '".$u."' AND password = '".$p."' AND blokir = 'N'");
		$j_cek	= $q_cek->num_rows();
		$d_cek	= $q_cek->row();
		//echo $this->db->last_query();
		
        if($j_cek == 1) {
            $data = array(
                    'user_id' => $d_cek->user_id,
					 'admin_id' => $d_cek->user_id,
                    'admin_user' => $d_cek->username,
					'username' => $d_cek->username,
					'pengguna_id' => $d_cek->pengguna_id,
                    'admin_nama' => $d_cek->nama_lengkap,
                    'admin_ta' => $ta,
                    'admin_level' => $d_cek->level,
					'admin_valid' => TRUE
                    );
					//echo "Data : ".$d_cek->level;exit;
            $this->session->set_userdata($data);
            redirect('admin');
        } else {	
			$this->session->set_flashdata("k", "<div id=\"alert\" class=\"alert alert-error\">username atau password tidak dikenal</div>");
			redirect('admin/login');
		}
	}
	//login
	public function ubahpassword() {
		$this->load->view('admin/login');
	}
	public function logout(){
        $this->session->sess_destroy();
		redirect('admin/panjar');
    }
}
