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

class c_login extends CI_Controller {
	function __construct(){
		parent::__construct();		
// 		$this->load->model('m_masters',"listusers");
		$this->load->model('m_masters');
		$this->load->library('GoogleAuthenticator');
		date_default_timezone_set("Asia/Jakarta");
	}
	public function index()
	{
// 		$data['title'] = "Welcome to Telemarketing!";
// 		$this->load->view('v_login',$data);

		$data['title'] = "Welcome to Telemarketing!";
		if (empty($this->session->userdata('username')))
        { 
          $this->load->view('v_login',$data);
        }
        else
        {
            redirect('c_modules');
        }
		
	}

	public function validatelogin()
	{
		$viewing['title'] = 'Login Page';
		$viewing['txtemail']=$this->input->post('checkemail');
		$viewing['txtpassword']=$this->input->post('checkpassword');
		

		$this->form_validation->set_rules('checkemail','Email','required');
		$this->form_validation->set_rules('checkpassword','Password','required|max_length[20]');	
		
		// $this->load->view('v_testing',$viewing);
		if($this->form_validation->run()==FALSE)
		{
			$this->load->view('v_login',$viewing);
		}
		else
		{		
			    $querystring = "select * from operators where operator = '".$viewing['txtemail'] . "' and password = md5('".$viewing['txtpassword'] . "')";
				$viewing['listusersarray'] = $this->m_masters->querying($querystring);
				$viewing['listusers'] = $viewing['listusersarray']->result();
				$viewing['listuserscount'] = $viewing['listusersarray']->num_rows();

				if($viewing['listuserscount'] > 0)
				{
					$this->session->set_userdata('tempusername',$viewing['listusers'][0]->Operator);
					$this->session->set_userdata('tempprivileges',$viewing['listusers'][0]->Privilege);
					$this->session->set_userdata('tempprivilege',$viewing['listusers'][0]->OperatorGroup);
					$this->load->view('v_googleauth');
				}
				else
				{
				// 	$this->session->set_flashdata('message',"Invalid Username Or Password");
				$this->session->set_userdata('pesanerror','Invalid Username Or Password');
					redirect('c_login/errorlogin');
				}
		} 		
	}


    public function errorlogin()
	{
	   // echo $this->session->userdata('pesanerror');
	    $this->load->view('v_logon');
	}
	
	
	public function validategoogle()
	{
		$code = $this->input->post('googlecode');
		$querystring = "select gsecret from gsecret where operatorgroup = '" .$this->session->userdata('tempprivilege'). "'";
		$secret = $this->m_masters->querying($querystring)->result()[0]->gsecret;
		// echo $querystring;
		$g = new \Sonata\GoogleAuthenticator\GoogleAuthenticator();
		
		if ($g->checkCode($secret, $code)) {

			$querystring = "select loggedin from operators where operator = '".$this->session->userdata('tempusername') ."'";
			$userlogin = $this->m_masters->querying($querystring)->result();
			if($userlogin[0]->loggedin == '0')
			{	
				redirect('c_limbo/changepasswordawal');
			}
			else
			{
				$this->session->set_userdata('username',$this->session->userdata('tempusername'));
				$this->session->set_userdata('privileges',$this->session->userdata('tempprivileges'));
				$this->session->set_userdata('privilege',$this->session->userdata('tempprivilege'));
				redirect('c_modules');	
			}
		}
		else
		{
			$this->session->set_flashdata('message',"Google Authenticator Code Incorrect!");
			redirect('c_login');
		}
	}	
}

	

