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

class Welcome extends CI_Controller {
    
    public function __construct()
	{
		parent::__construct();
		$this->load->model('user_model');
	}

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		$this->load->view('login');
	}
	
	public function periksa_sandi()
	{
	    //echo "<pre>";
	    //print_r($_POST);
	    $username = $this->input->post('username');
	    $password = $this->input->post('password');
	    
	    $user = $this->user_model->select_by_username_password($username, $password)->row();
	    
	    if($user) {
	        $newData = array(
	           'username' => $user->username,
	           'nama' => $user->nama_lengkap,
	           'state' => $user->role,
	           'logged_in' => TRUE
	        );
	        $this->session->set_userdata($newData);
	        if($user->role == 'Pimpinan') {
	            redirect(site_url("pimpinan"));
	        } else {
	            redirect(site_url("data-peserta"));
	        }
	    } else {
	        redirect(site_url("?login=gagal"));
	    }
	}
	
	public function logout() 
	{
	    $this->session->sess_destroy();
	    redirect(site_url());
	}
}
