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

class Login extends CI_Controller 
{

	public function __construct() 
	{
		parent::__construct();
		$this->load->model('M_home');
		//$this->load->model('Admin_m');
		//$this->load->model('Content_m');
		$this->load->model('Content_m');
		$this->load->model( array( 'Crud_m','M_home') );
		$data['get_kontak_kami'] = $this->Content_m->select_db('global_kontak_kami')->row();	

		$this->load->vars($data);
	}

	public function index()
	{
		$data['mainView'] = 'content/login';
		$data['sideBar'] = 'sidebar/blog_detail';
 		$this->load->view('template',$data);
	}

	public function logged_in_as($gelombang, $username ,$password)
	{
		$value = $this->M_home->cek_login_guest_double(urldecode($username),$password ,$gelombang)->row();
		//echo $this->db->last_query();

		$data=array(
			"login_guest"=>TRUE,
			"email"=>$username,
			//"prodi_pilihan"=>$value->prodi_pilihan,
			"userid"=>$value->id_permohonan_daftar_mhs
		);
		
		//level 1 : admin, level 2 : dosen, level 3 : mhs
		$this->session->set_userdata($data);
	
		if($this->session->userdata('login_guest')==TRUE){
			if($value->bukti_transfer == '' || $value->bukti_transfer == null){
				redirect("daftar/proses1");	
			}else if($value->bukti_transfer != '' || $value->bukti_transfer != null){
				redirect("daftar/proses2");	
			}
			
		}

	}

	public function auntentifikasi()
	{
		extract($_POST);
		$cek=$this->M_home->cek_login_guest($username,md5($password));
		$value = $this->M_home->cek_login_guest($username,md5($password))->row();

		
		if($cek->num_rows()==1){
			//$this->session->sess_destroy();

			if($value->deleted != '0'){
				echo "<center>User anda di delete oleh admin, silahkan konfirmasi ke admin untuk mendapatkan informasi</center>";
			}else{
				$data=array(
						"login_guest"=>TRUE,
						'logged_in' => FALSE , 
						"email"=>$username,
						//"prodi_pilihan"=>$value->prodi_pilihan,
						"userid"=>$value->id_permohonan_daftar_mhs
					);
			
				//level 1 : admin, level 2 : dosen, level 3 : mhs
				$this->session->set_userdata($data);
			
				if($this->session->userdata('login_guest')==TRUE){
					if($value->bukti_transfer == '' || $value->bukti_transfer == null){
						redirect("daftar/proses1");	
					}else if($value->bukti_transfer != '' || $value->bukti_transfer != null){
						redirect("daftar/proses2");	
					}
					
				}
			}
			
		}
		else if($cek->num_rows()>1){
			redirect("daftar/pilih_akun/".urlencode($username));	
		}
		else if($cek->num_rows() == 0)
		{
			$cek_dim=$this->M_home->cek_login_guest_dim($username,md5($password));
			$value_dim = $this->M_home->cek_login_guest_dim($username,md5($password))->row();

			$cek_other=$this->M_home->cek_login_guest_other($username,md5($password));
			$value_other=$this->M_home->cek_login_guest_other($username,md5($password))->row();
			
			if($cek_dim->num_rows()==1)
			{
				//$this->session->sess_destroy();
				$data=array(
							"login_guest_dim"=>TRUE,
							'logged_in' => FALSE, 
							"email"=>$username,
							//"prodi_pilihan"=>$value->prodi_pilihan,
							"useruuid"=>$value_dim->id_cust,
							"userid"=>$value_dim->id_permohonan_daftar_dim
						);
				//level 1 : admin, level 2 : dosen, level 3 : mhs
				$this->session->set_userdata($data);
				redirect("daftar_dim/homepage");	
					
			}
			else if($cek_other->num_rows()==1)
			{
				$data=array(
							"login_guest_other"=>TRUE,
							'logged_in' => FALSE, 
							"email"=>$username,
							//"prodi_pilihan"=>$value->prodi_pilihan,
							"useruuid"=>$value_other->id_cust,
							"userid"=>$value_other->id_permohonan_daftar_other
						);
				//level 1 : admin, level 2 : dosen, level 3 : mhs
				$this->session->set_userdata($data);
				redirect("daftar_other/homepage");	

			}
			else
			{
				$this->session->set_flashdata('login_error',TRUE);
				redirect("login");	
			}




		}
		else{
			$this->session->set_flashdata('login_error',TRUE);
			redirect("login");	
		}

		

		//echo $this->db->last_query();

	}




}

