<?php
namespace App\Controllers\Admin;
use App\Controllers\BaseController;

use App\Models\LoginModel; // Import the model



class Login extends BaseController {

	protected $session;
	protected $loginmodel;
	// or within a controller that extends BaseController

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/userguide3/general/urls.html
	 */
	public function __construct() {
		$this->session = session();
		$this->loginmodel = new LoginModel();
	} 
	public function index()
	{	
		echo view('admin/login');
	}
	public function loginCheck()
	{	
		$username =  $this->request->getPost('username');
		$password =  $this->request->getPost('password');
		$status=$this->loginmodel->loginCheck($username,$password);
		if(!empty($status)){
			$this->session->set('username', $status);
			exit('1');
	}
		else{
			exit('0');
		}
	}
	public function changePassword()
	{	
		$id=$this->session->get('username')[0]->id;
		$password = encrypt_password($this->request->getPost('password'));
		$status=$this->loginmodel->changePassword($id,$password);
		if($status==1){ echo 1;}
		else{echo 0;}
		
	}
	public function logout()
	{	
		this->session->destroy();
		//redirect("admin/login");
		return redirect()->to('admin/login');
	}
}
