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

class Auto_login extends CI_Controller{

  function index($username, $token) {
		$this->db->from('users');
		$this->db->join('token_auto_login', 'token_auto_login.id_company = users.id_company');
		$this->db->where('username', $username);
		$this->db->where('token', $token);
		$this->db->where('waktu >= now()');
		$query = $this->db->get();
		if($query->num_rows() > 0) {
			$this->db->select("users.id_user,
								users.id_karyawan,
								users.username,
								data_karyawan.nik,
								data_karyawan.nama_lengkap as nama_user,
								data_karyawan.id_cabang,
								data_karyawan.id_departemen,
								data_karyawan.id_jabatan,
								data_karyawan.foto,
								users.id_company,
								master_jabatan.level_user,
								IF(p_notification.notif_hrd = '1','1',IF(p_notification.notif_finance = '1','6','0')) AS level_khusus")
						->from("users")
						->join("data_karyawan","data_karyawan.id_karyawan = users.id_karyawan")
						->join("master_jabatan","master_jabatan.id_jabatan = data_karyawan.id_jabatan")
						->join("p_notification","p_notification.id_karyawan = data_karyawan.id_karyawan","LEFT")
						->where("users.username",$username)
						->where("data_karyawan.status IN ('1','2','3','4')");
			$select_user = $this->db->get();
      $rows = $select_user->row();
			$id_user 			= $rows->id_user; # untuk v2
			$level_user 		= $rows->level_user;
			$id_karyawan 		= $rows->id_karyawan;
			$id_departemen_arr 	= array();
			$id_cabang_arr 		= array();
			$multi_departemen = false;
			$multi_cabang = false;

			$data = array('id_karyawan'			=>$id_karyawan,
							'username'			=>$rows->username,
							'nik'				=>$rows->nik,
							'nama_user'			=>$rows->nama_user,
							'foto_user'			=>$rows->foto,
							'id_company'		=>$rows->id_company,
							'multi_cabang'		=>$multi_cabang,
							'id_cabang'			=>$rows->id_cabang,
							'id_cabang_arr'		=>$id_cabang_arr,
							'multi_departemen'	=>$multi_departemen,
							'id_departemen'		=>$rows->id_departemen,
							'id_departemen_arr'	=>$id_departemen_arr,
							'id_jabatan'		=>$rows->id_jabatan,
							'level_user'		=>$level_user,
							'level_khusus'		=>$rows->level_khusus);

			$session_v2 = array('id_user' 		=> $id_user,
								'usergroup' 	=> $level_user,
								'nama' 			=> $rows->nama_user,
								'divisi' 		=> $rows->id_departemen,
								'cabang' 		=> $rows->id_cabang,
								'role_input' 	=> "1",
								'zona_waktu' 	=> "1",
								'flag' 			=> "0",
								'is_absenku_karyawan_new' => true);
			$data_session = array_merge($data, $session_v2);
			$this->session->set_userdata($data_session);
			redirect('wizard');
		} else {
			redirect('login');
		}
	}

}
