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

class Masuk_sistem extends MBELINK_Controller {

	public function __construct()
	{
		parent::__construct();
		
		$this->load->library(array('session'));
		$this->load->helper(array('rsonline'));
				 
		// User Login Information
		$this->data['user'] = user_info();
		$this->data['instansi'] = orgInfo();
		
		//Ngaeng-ngaeng
		$this->load->model('masuk_model');
		$this->load->model('inform_model');
	}
	public function index()
	{
		// Cek Sesi
		 if($this->session->userdata('userid')):
			redirect(base_url('home'), 'refresh');
		endif; 
		
		$form = array(
			'label'=>'Login Pendaftaran',  
			'action'=>base_url('login-system.html'), 			
			'atr'=>array(
				'style'=>'font-size:0.9em', 
				'autocomplete'=>'off', 
			), 
			'set_rules'=>array(
				array(
					'field'=>'user', 
					'type'=>'text', 
					'label'=>'Masukan Username', 
					//'rules'=>'required|valid_user', 
					'rules'=>'required', 
					'placeholder'=>'Insert Username ', 
					'fas'=>'fa fa-user', 
				), 
				array(
					'field'=>'passwd', 
					'type'=>'password', 
					'label'=>'Password', 
					'rules'=>'required|callback_cek_login', 
					'placeholder'=>'Insert password',
					'fas'=>'fa fa-lock',					
				), 
			), 
			'tombol'=>array(
				'next'=>array(
					'class'=>'primary', 
					'label'=>'Login'
				), 
			), 
		);
		
		$this->form_validation->set_rules($form['set_rules']);
		//$this->form_validation->set_rules('g-recaptcha-response', '<strong>Captcha</strong>', 'callback_getResponseCaptcha');
		$this->form_validation->set_message('required', '%s Tidak Boleh Kosong...!');
		//$this->form_validation->set_message('valid_user', 'User ID Tidak Valid.');//<br>&nbsp;Contoh Valid: <strong>ardi.setia@gmail.com</strong>!');
		
		//$this->form_validation->set_message('numeric', '%s Hanya boleh diisi dengan angka.<br>&nbsp;Contoh Valid: <strong>087870965442</strong>!');
		$this->form_validation->set_message('cek_login', 'Username dan/atau Password Tidak Sesuai!');
		//$this->form_validation->set_message('callback_getResponseCaptcha', '{field} {g-recaptcha-response} harus diisi. ');
		$this->form_validation->set_error_delimiters('<p style="color:#DE1D2D;font-weight:bold;">', '</p>');
		//$recaptcha = $this->input->post('g-recaptcha-response');
       // $response = $this->recaptcha->verifyResponse($recaptcha);
		$this->data['form'] = $form;
		
		if($this->form_validation->run() == FALSE):			
			$this->load->view('login_system', $this->data);
		else:
			$paswd = $this->input->post('passwd', TRUE);
			$email = $this->input->post('user', TRUE);
			
			/* if(substr($email,0,1)=='2'){
				$profilp = $this->masuk_model->getDataUserPerujuk($email, $paswd);
				$data_sesi = array(
					'userid'=>$email,
					'nik'=>$profilp->nik, 				
					'nama'=>$profilp->nama, 
					'grup'=>$this->inform_model->get_grupuser($email),
				); 
			}else{ */
				$profil = $this->masuk_model->getDataUser($email, $paswd);
				/* $data_sesi = array(
					'userid'=>$email,
					'nik'=>$profil->nik, 				
					'nama'=>$profil->nama, 				
					'tgl_lahir'=>$profil->tgl_lahir, 				
					'alamat'=>$profil->alamat, 				
					'notelp'=>$profil->notelp,
					'photo'=>$profil->foto, 				
					'jkel'=>$profil->jkel, 
					'grup'=>$this->inform_model->get_grupuser($email),
					
				); */
				
				$data_sesi = array(
					'userid'=>$email,
					//'nik'=>$profil->nik, 				
					'nama'=>$profil->nama, 				
					//'login'=>$profil->login, 				
					//'jenis_user'=>$profil->jenis_user, 				
					'deptid'=>$profil->kd_jbtn,
					'status'=>$profil->status, 				
					//'dokterid'=>$profil->DokterID, 				
					//'jkel'=>$profil->jkel, 
					'grup'=>$this->inform_model->get_grupuser($email),				
				);
			//}
			
			$this->session->set_userdata($data_sesi);
			//$this->update_lastlogin($email, $hp);
			
			redirect(base_url('home.html'));
		endif;
	}
		
	public function cek_login($passwd)
	{
		$email = $this->input->post('user', TRUE);
		//return $this->masuk_model->cek_login($email, $passwd);
		$loghosp=$this->masuk_model->cek_login($email, $passwd);
		$logperujuk=$this->masuk_model->cek_loginPerujuk($email, $passwd);
		if($loghosp){
			return $loghosp;
		}elseif($logperujuk){
			return $logperujuk;
		}
	}
	
	public function logout()
	{
		$this->session->sess_destroy();		
		redirect(base_url('login-system.html'));
	}
	
	
}