<?php

namespace App\Controllers;

use CodeIgniter\Controller;
use App\Models\AdminModel;

class Login extends BaseController
{
	protected $Admin_model;
     
	public function __construct()
    {
			
        $this->Admin_model = new AdminModel();
    }  
    public function user_login()
	{
		
		$this->load->view('index');
		return view('index');
		
	}
	public function index()
	{
		 if (is_numeric(session()->get('user_type'))) {
            redirect()->to(base_url('admin/dashboard'))->send();
            
        }
		return view('index');
		
	}
	public function validate_login()
	{
		$query=$this->Admin_model->login();	
		$api_data = array();	
		if(sizeof($query)>0)
		{
			$data=array(
				'user_id'=>$query['uid'],
				'user_type'=>$query['user_type'],
				'user_name'=>$query['user_name'],
				'name'=>$query['name'],
				'email'=>$query['email'],
				'mobile'=>$query['mobile'],
				'work_for'=>$query['work_for'],
				//'counselor_id'=>$query['counselor_id'],
				);
			session()->set($data);
			$this->Admin_model->update_last_login($query['uid']);
			//redirect('admin/dashboard');
			$api_data['status'] = 'success';
			$api_data['redirect_url'] = '/admin/dashboard';
		}
		else
		{
			//$this->session->set_flashdata('msg', 'Incorrect user name or password.');
			//redirect('login/user_login');
			$api_data['status'] = 'fail';
			$api_data['msg'] = 'Incorrect user name or password.';
			$api_data['redirect_url'] = '/login/user_login';
		}
		echo json_encode($api_data);
	}
	public function logout(){
		$this->session->unset_userdata('admin_email');
		$this->session->sess_destroy();
		redirect()->to(base_url('login/user_login'))->send();	
	}
	
}
