<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Class Auth
 * @property Ion_auth|Ion_auth_model $ion_auth        The ION Auth spark
 * @property CI_Form_validation      $form_validation The form validation library
 */
class Login extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('User_model');

	}
	function index(){
		if($this->session->userdata('status') == "login"){
			redirect(base_url("dashboard"));
		}
		$this->data['title'] = 'Login';
		$this->data['message'] ="";
		$this->load->view('auth/login', $this->data);
	}
 	
	function log_in(){
		$username = $this->input->post('username');
			$user = $this->User_model->get_by_username($username);
		if(empty($user)){
			$this->session->set_flashdata('message', '<div class="box-body">
					<div class="alert alert-block alert-danger"><button type="button" class="close" data-dismiss="alert"><i class="ace-icon fa fa-times"></i></button>
					<i class="ace-icon fa fa-bullhorn red"></i> Login Gagal. Data tidak ditemukan.
					</div></div>'); 
 			echo "0";
			//redirect(base_url('login'));
		}
		else{
			$hash=$user->password;
		if (password_verify($this->input->post('password'), $hash)){
			$id_user = $this->User_model->get_by_username($username)->id_user;
			$nama_user = $this->User_model->get_by_username($username)->nama_user;
			$level = $this->User_model->get_by_username($username)->level;


			$data_session = array(
				'id_user' => $id_user,
				'nama_user' => $nama_user,
				'level' => $level,
				'status' => "login"
				);
			$this->session->set_userdata($data_session); 
			$this->session->set_flashdata('message', '<div class="box-body">
					<div class="alert alert-block alert-success"><button type="button" class="close" data-dismiss="alert"><i class="ace-icon fa fa-times"></i></button>
					<i class="ace-icon fa fa-bullhorn green"></i> Login Berhasil
					</div></div>'); 
 			echo "1";
 
		}else{
			$this->session->set_flashdata('message', '<div class="box-body">
					<div class="alert alert-block alert-danger"><button type="button" class="close" data-dismiss="alert"><i class="ace-icon fa fa-times"></i></button>
					<i class="ace-icon fa fa-bullhorn red"></i> Login Gagal
					</div></div>'); 
  			echo "0";

			//redirect(base_url('login'));
		}
		}
	}
	public function register(){
	  $nik = $this->User_model->get_by_nik($this->input->post('nik'));
		if(!(empty($nik))){
			echo 0;
			die();
		}
	  $username = $this->User_model->get_by_username($this->input->post('username'));
		if(!(empty($username))){
			echo 0;
			die();
		}
		if (!($this->input->post('password')==$this->input->post('repassword'))){
			echo 2;
			die();

		}
	      $data = array(
	        'nik'          => $this->input->post('nik'),
	        'nama_user'          => $this->input->post('nama_user'),
	        'username'          => $this->input->post('username'),
	        'password'          => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
	        'alamat_user'          => $this->input->post('alamat_user'),
	        'level'          => 1,
	        'status'          => 0,
	        'tanggal_daftar'          => date('Y-m-d H:i:s'),
	        'jenis_kelamin'          => $this->input->post('jenis_kelamin'),
	        'pendidikan'          => $this->input->post('pendidikan'),
	        'pekerjaan'          => $this->input->post('pekerjaan'),
	        'id_kabupaten'          => $this->input->post('id_kabupaten'),
	      );
	      // eksekusi query INSERT
	      $insert = $this->User_model->insert($data);
	  
      // set pesan data berhasil dibuat
        $this->session->set_flashdata('message', '
        <div class="alert alert-block alert-success"><button type="button" class="close" data-dismiss="alert"><i class="ace-icon fa fa-times"></i></button>
          <i class="ace-icon fa fa-bullhorn green"></i> Data berhasil disimpan
        </div>');
		echo "1";
	  }
 
	function logout(){
		$this->session->sess_destroy();
		redirect(base_url('login'));
	}

}
