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

class Login extends CI_Controller {

	function __construct(){
		parent::__construct();
		$this->load->model('m_kontak');/* sesuaikan dengan nama file pada model */
		$this->load->model('m_login'); 
		$this->load->helper('url');
		$this->load->library('session');

	}
	public function index()
	{
// 		$options = array(
//         'img_path'=>'./captcha/', #folder captcha yg sudah dibuat tadi
//         'img_url'=>base_url('captcha'), #ini arahnya juga ke folder captcha
//         'img_width'=>'165', #lebar image captcha
//         'img_height'=>'45', #tinggi image captcha
//         'expiration'=>7200, #waktu expired
//         'font_path' => FCPATH . 'assets/font/coolvetica.ttf', #load font jika mau ganti fontnya
//         'pool' => '123Q56789ABGETZAJ', #tipe captcha (angka/huruf, atau kombinasi dari keduanya)

//         # atur warna captcha-nya di sini ya.. gunakan kode RGB
//         'colors' => array(
//         	'background' => array(242, 242, 242),
//         	'border' => array(255, 255, 255),
//         	'text' => array(0, 0, 0),
//         	'grid' => array(255, 40, 40))
//     	);
// 		$cap = create_captcha($options);
// 		$data['img'] = $cap['image'];
// 		$this->session->set_userdata('mycaptcha', $cap['word']);
// 		$data['word'] = $this->session->userdata('mycaptcha'); 
		$data['kontak'] = $this->m_kontak->kontak();
		$this->load->view('login',$data);
	}
	public function login()
	{
 
		$data['kontak'] = $this->m_kontak->kontak();
		$this->load->view('login2',$data);
	}
	public function refresh(){
		// Captcha configuration
		$config = array(
			'img_path'  => './captcha/',
			'img_url'  => base_url().'captcha/',
			'img_width'     => '150',
			'img_height'    => 50,
			'word_length'   => 8,
			'font_size'     => 16
		);
		$captcha = create_captcha($config);
		
		// Unset previous captcha and store new captcha word
		$this->session->unset_userdata('captchaCode');
		$this->session->set_userdata('captchaCode',$captcha['word']);
		
		// Display captcha image
		echo $captcha['image'];
	}
	public function proses()
    {
        
        $username = $this->input->post('username', TRUE);
        $password = $this->input->post('password', TRUE);

        // select * from login where user = ?
       	$where = array(
					'username' => $username 

				);
		$cek = $this->m_login->cek_login("tb_admin",$where)->num_rows();
        
        // cek username 
        if($cek > 0)
        {
            // kita ambil isi dari record
           	$db=$this->m_login->m_cek_mail()->row();
            if(password_verify($password, $db->password))
            {
              	        $data_session = array(
							'email' => $email, 
							'username' => $username,
							'level' => $level,
							'status' => "loginbro"
						);

						$this->session->set_userdata($data_session);
						
						$ip=$_SERVER['REMOTE_ADDR'];
			            $hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
						$data=array(

							'id'=> NULL,
							'id_admin'=>   $username,
							'username'=>  $username,
							'ip'=> $ip , 
							'hostname'=> $hostname ,
							'status'=> 'login'

						);

						$this->db->insert('tb_log',$data);
                // redirect ke admin
                	$this->session->set_flashdata('msg','success-login');
               	redirect(base_url("home"));
            }else{

                // jika password salah
               	$this->session->set_flashdata('msg','<div class="alert alert-danger alert-dismissible">
							<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
							<strong> Maaf Login Gagal ,Password Salah  </strong>Ulangi Kembali
							</div> ');

                redirect(base_url('login/login'));
            }

        }else{

            // jika username salah
           	$this->session->set_flashdata('msg','<div class="alert alert-danger alert-dismissible">
						<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
						<strong> Maaf Login Gagal , username tidak dikenali </strong>Ulangi Kembali
						</div> ');
             redirect(base_url('login/login'));
        }
    }
	function cek() {
		
// 		$secutity_code = $this->input->post('secutity_code');
// 		$mycaptcha = $this->session->userdata('mycaptcha');
		
// 		if ($this->input->post() && ($secutity_code == $mycaptcha)) 
// 		{

			$ip=$_SERVER['REMOTE_ADDR'];
			$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
			$this->form_validation->set_rules('username', 'username', 'trim|required|min_length[3]|max_length[45]');
			$this->form_validation->set_rules('password', 'password', 'trim|required|min_length[5]|max_length[45]');

			if ($this->form_validation->run() == TRUE) { 
				$username = $this->input->post('username');
				$password = $this->input->post('password');
				$where = array(
					'username' => $username 

				);
				$cek = $this->m_login->cek_login("tb_admin",$where)->num_rows();
				if($cek > 0){
					$db=$this->m_login->m_cek_mail()->row();
					if(hash_verified($this->input->post('password'),$db->password)) {
						$data_session = array(
							'email' => $email, 
							'username' => $username,
							'level' => $level,
							'status' => "loginbro"
						);

						$this->session->set_userdata($data_session);

				// 		$sql_menu = "SELECT * FROM tb_admin WHERE  username = '$username' ";
				// 		$main_menu = $this->db->query($sql_menu)->result();
				// 		foreach ($main_menu as $main) {
				// 			$id_admin=$main->id;
				// 			$nama_admin=$main->username;

				// 		}

						$data=array(

							'id'=> NULL,
							'id_admin'=>   $username,
							'username'=>  $username,
							'ip'=> $ip , 
							'hostname'=> $hostname ,
							'status'=> 'login'

						);

						$this->db->insert('tb_log',$data);
						$this->session->set_flashdata('msg','success-login');
						
						redirect(base_url("home"));
						
					}else{
						$this->session->set_flashdata('msg','<div class="alert alert-danger alert-dismissible">
							<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
							<strong> Maaf Login Gagal ,Password Salah  </strong>Ulangi Kembali
							</div> ');

						redirect(base_url("login"));
					}

				}else{
					$this->session->set_flashdata('msg','<div class="alert alert-danger alert-dismissible">
						<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
						<strong> Maaf Login Gagal , username tidak dikenali </strong>Ulangi Kembali
						</div> ');

					redirect(base_url("login"));
				}
			} else { 
				$this->session->set_flashdata('msg','<div class="alert alert-warning alert-dismissible">
					<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
					<strong> Maaf Login Gagal, Krakter Tidak Sesuai </strong>Ulangi Kembali
					</div> ');
				redirect(base_url("login"));

			}

// 		}else 
// 		{ 
// 			$this->session->set_flashdata('msg','<div class="alert alert-warning alert-dismissible">
// 				<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
// 				<strong> Maaf Login Gagal, Captcha Tidak Sesuai </strong>Ulangi Kembali
// 				</div> ');
// 			redirect(base_url("login"));


// 		}  
	}
	
	function logout(){
		$ip=$_SERVER['REMOTE_ADDR'];
		$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
		$username = $this->session->userdata("username");
	 

		$data=array(

		 
			'id_admin'=>   $username,
			'username'=> $username,
			'ip'=> $ip , 
			'hostname'=> $hostname ,
			'status'=> 'logout'

		);

		$this->db->insert('tb_log',$data);

// 		$this->session->unset_userdata('login');
// 		$this->session->unset_userdata('email');
// 		$this->session->unset_userdata('level');
// 		$this->session->unset_userdata('status');     		
		$this->session->sess_destroy();
		redirect(base_url('login'));
	}
	public function login_redirect(){
		$this->session->set_userdata('login', TRUE);
		redirect(base_url());		
	}
}
