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

class Login extends CI_Controller {

	public function __construct()
    {
        parent::__construct();
        $this->load->model("UserModel");
        $this->load->library('form_validation');
		
    }

	public function index()
	{
		if($this->session->userdata('user_logged')!==null){	
			if($this->session->userdata('user_logged')->level=='admin'){
				redirect(site_url('admin/dashboard'));
			} else {
				redirect(site_url('user/dashboard'));
			}

		}
		
		if($this->input->post()){
            if($this->UserModel->doLogin()) {
				if($this->UserModel->isPemilik()){
					redirect(site_url('user/dashboard'));
				} else {
					redirect(site_url('admin/dashboard'));
				}
			} else {
				$data['error']="Username atau Password salah !";
				$this->load->view('login',$data);
			}
        } else {
			$this->load->view('login');
		}
	}

	public function logout()
    {
        // hancurkan semua sesi
        $this->session->sess_destroy();
        redirect(site_url('login'));
    }
}
