<?php namespace App\Controllers;

class Auth extends BaseController
{
	public function index()
	{
		// If already logged in, redirect to admin dashboard
		if (session()->get('admin_session_final')) {
			return redirect()->to(base_url('admin/activities'));
		}
		
		return view('admin/login');
	}

	public function login()
	{
		$session = session();
		$username = $this->request->getPost('username');
		$password = $this->request->getPost('password');
		
		// Hardcoded credentials as requested
		if ($username === 'admin' && $password === 'admin123') {
			$sessData = [
				'username'  => $username,
				'admin_session_final' => TRUE
			];
			$session->set($sessData);
			return redirect()->to(base_url('admin/activities'));
		} else {
			$session->setFlashdata('msg', 'Username atau Password salah');
			return redirect()->to(base_url('login'));
		}
	}

	public function logout()
	{
		$session = session();
		$session->remove('admin_session_final');
		$session->destroy();
		return redirect()->to(base_url('/'));
	}
}
