<?php namespace App\Controllers;
/**
 * File			: Login.php
 * Description  : Controller untuk halaman login admin
 * Created By	: Hendi
 * Created Date : 15 Mar 2021
 * Last Update  : 06 Apr 2021
**/
use App\Models\UserModel;

class Login extends MyBaseController
{
	public function index()
	{
		# validation sudah diload di MyBaseController
		$data = [
			'title' => 'Login',
		];
		return view('templates/login', $data);
	}
	
	public function doLogin() 
	{
		$user_id = $this->request->getPost('user_id');
        $password = $this->request->getPost('password');
		
		$data = [
            'user_id'  => $user_id,
            'password'  => $password
        ];
		
		# roles 'login' sudah didefinisikan di Config/Validation.php
		if ($this->form_validation->run($data, 'login') === FALSE) 
        {
			# mengembalikan nilai input yang sudah diinput sebelumnya
            $this->session->setFlashdata('inputs', $this->request->getPost());
            # memberikan pesan error 
			$this->session->setFlashdata('errors', $this->form_validation->getErrors());
           	return redirect()->to(base_url('/login'));
        }
        else
        {
			$UserModel = new UserModel();
			$data = $UserModel->where('user_id', $user_id)
					->first();
			if ($data) 
			{
				if ($data['is_active'] == 'F')
				{
					# mengembalikan nilai input yang sudah diinput sebelumnya
					$this->session->setFlashdata('inputs', $this->request->getPost());
					# memberikan pesan error
					$this->session->setFlashdata('errors', ['User sudah tidak aktif']);
					return redirect()->to(base_url('login'));
				}
				$pass = $data['password'];
				//echo password_hash("a", PASSWORD_DEFAULT)."\n"; 
				$verify_pass = password_verify($password, $pass);
				if($verify_pass)
				{
					$ses_data = [
						'user_id' => $data['user_id'],
						'full_name' => $data['full_name'],
						'email' => $data['email'],
						'is_loggedin' => TRUE,
						'level_id' => $data['level_id'],
                        'level_name' => $UserModel->db->query("SELECT description AS level_name FROM level WHERE level_id = '".$data['level_id']."'")->getRow()->level_name
					];
					$this->session->set($ses_data);
					return redirect()->to(base_url('dashboard'));
				}
				else
				{
					# mengembalikan nilai input yang sudah diinput sebelumnya
					$this->session->setFlashdata('inputs', $this->request->getPost());
					# memberikan pesan error
					$this->session->setFlashdata('errors', ['Password salah']);
					return redirect()->to(base_url('login'));
				}
			}
			else
			{
				# mengembalikan nilai input yang sudah diinput sebelumnya
				$this->session->setFlashdata('inputs', $this->request->getPost());
				# memberikan pesan error
				$this->session->setFlashdata('errors', ['Username tidak ditemukan']);
				return redirect()->to(base_url('login'));
			}
			
			# memberikan pesan success
			//$this->session->setFlashdata('success', 'Registration successfully');
            //return redirect()->to(base_url('login'));
        }
	}
	
	public function doLogout() 
	{
		$this->session->destroy();
		return redirect()->to(base_url('login'));
	}
}
