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

class Catalog_login extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->library('catalog_auth');
		$this->load->helper('url');
	}

	public function index()
	{
		if ($this->catalog_auth->is_logged_in())
		{
			redirect('catalog/admin');
			return;
		}

		$this->load->view('catalog/login', array('error' => NULL));
	}

	public function submit()
	{
		$username = trim((string) $this->input->post('username'));
		$password = (string) $this->input->post('password');

		$result = $this->catalog_auth->attempt_login($username, $password);
		if (!$result['ok'])
		{
			$this->load->view('catalog/login', array('error' => $result['message']));
			return;
		}

		redirect('catalog/admin');
	}

	public function logout()
	{
		$this->catalog_auth->logout();
		redirect('catalog/login');
	}
}
