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

class Auth extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		/*
        if (isset($this->session->id_usr) == false) {
            redirect(base_url('Auth'));
        }
*/
		$this->load->model('M_auth');
	}

	public function index()
	{
		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'];
		}
		$cariip = $this->db->get_where('tpc_ip_allow', ['strIPv4' => $ip]);
		$ipinfo = $cariip->num_rows();
		$this->load->view('auth', ["ip" => $ip, 'ipinfo' => $ipinfo]);
	}

	public function Cauth()
	{
		$this->load->view('auth');
	}

	public function masuk()
	{
		$nama = $this->input->POST('strUsername');
		$sandi = $this->input->POST('strPassword');
		//$cari = $this->M_auth->auth($nama, $sandi);
		$cari = $this->db->get_where('employee', array('Email' => $nama, 'stsAktif' => '1'))->row_array();

		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'];
		}

		$ip_allow = $this->db->get_where('tpc_ip_allow', array('strIPv4' => $ip))->row_array();

		if (isset($cari['strPassword'])) {
			$pass = $cari['strPassword'];
		} else {
			$pass = 0;
		}

		if (password_verify($sandi, $pass)) {

			$usr = $this->M_auth->cari($nama, $cari['Email']);
			if ($usr['Level'] == 1 || $usr['Level'] == 2) {
				$sesi = [
					'id_user' => $usr['emp_id'],
					'strEmail' => $usr['Email'],
					'usrLevel' => $usr['Level'],
					'strNama' => $usr['FullName'],
					'strDivision' => $usr['Division'],
					'strKdLokasi' => $usr['strKdLoc'],
					'strJobPosition' => $usr['strJobPosition']
				];
				$this->session->set_userdata($sesi);
				echo "1";
			}

			if ($usr['Level'] == 3 || $usr['Level'] == 4) {
				if (isset($ip_allow['ID']) > 0) {
					$sesi = [
						'id_user' => $usr['emp_id'],
						'strEmail' => $usr['Email'],
						'usrLevel' => $usr['Level'],
						'strNama' => $usr['FullName'],
						'strDivision' => $usr['Division'],
						'strKdLokasi' => $usr['strKdLoc'],
						'strJobPosition' => $usr['strJobPosition']
					];
					$this->session->set_userdata($sesi);
					echo "1";
				}

				if (isset($ip_allow['ID']) == 0) {
					echo "100";
				}
			}
		} else {
			echo "0";
		}
	}

	public function signout()
	{
		$this->session->unset_userdata('id_user');
		$this->session->unset_userdata('strEmail');
		$this->session->unset_userdata('usrLevel');
		$this->session->unset_userdata('strNama');
		$this->session->unset_userdata('strDivision');
		$this->session->unset_userdata('strKdLokasi');
		$this->session->unset_userdata('strJobPosition');
		redirect(base_url('Auth'));
	}

	public function IP()
	{
		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'];
		}
		//return $ip;
		$ip_allow = $this->db->get_where('tpc_ip_allow', array('strIPv4' => $ip))->row_array();
		if (isset($ip_allow['ID'])) {
			echo $ip_allow['ID'];
		} else {
			echo "0";
		}
	}

	public function enkrip()
	{
		$txt = $this->input->get('t');
		$enkrip = password_hash($txt, PASSWORD_BCRYPT);
		echo $enkrip;
	}

	public function login()
	{
		$user_input_password = 'admin8';

		$hashedPasswordFromDatabase = '$2y$10$Cb5XDHnbGgqEm1musLecEus53qSq/x1eCsv6hxP9idoShluvytQPK';

		if (password_verify($user_input_password, $hashedPasswordFromDatabase)) {
			echo "Password benar!";
		} else {
			echo "Password salah!";
		}
	}

	public function myip()
	{
		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'];
		}

		echo $ip;
	}
}
