<?php namespace Pratik\Controllers;
use CodeIgniter\Controller;
use Pratik\Models\Pratik_Model;
class Login extends Controller
{
	public $data = array();
	public $model = array();
    function __construct()
	{
        $this->model = new Pratik_Model();
        $this->session = \Config\Services::session();
		$this->request = \Config\Services::request();
    }
	public function index()
	{	
		return view('Admin\login', array('data'=>$this->data));
	}
	public function checkLogin()
	{	
        if(!isset($_SESSION)) session()->start();
		if(isset($_GET['lang']))
		{
			define('LANG',$_GET['lang']);
			$this->session->remove('lang');
			$this->session->remove('text');
		} else {
			if(isset($_SESSION['lang']))
			{
				define('LANG',$_SESSION['lang']);
				
			} else {
				define('LANG','tr_TR');
				$_SESSION['lang'] = LANG;
			}
		}
		$logged = $this->session->get('logged');
		if(!$logged)
		{
			$path = $_SERVER['PATH_INFO']??'/';
			$freePath=array('/login','/login/out','/login/checkUser','/site');
			if(!in_array($path, $freePath))
			{
				redirect2(site_url('login')); /* Redirect browser */
			}
		} else {
			$user = $this->session->get('user');
			$sessiondata['user']['basic'] = $this->model->getTableRow( 'users',"id='".$user['basic']['id']."'");
			$this->session->set('logged', TRUE);
			$this->session->set('ALC', $sessiondata['user']['basic']['alc']);
			$elements = new \Pratik\Config\Users_Config();
			$sessiondata['user']['metas'] = $this->model->getMetas($sessiondata['user']['basic']['id'], 'users', "",$elements->metas, 'tr_TR', 'meta', false, false);
			$this->session->set($sessiondata);		
		}
	}
	public function checkUser()
	{	
		$logged = $this->session->get('logged');
		if($logged)
		{
			redirect2(getSubUrl('dashboard').'?padmin'); /* Redirect browser */
			$sessiondata['user']['basic'] = $this->model->getTableRow( 'users',"id='".$sessiondata['user']['basic']['id']."'");
		} else {
			
			$email = $this->request->getGetpost('email');
			$password = $this->request->getGetpost('Password');
			$sessiondata['user']['basic'] = $this->model->getTableRow( 'users',"email='".$email."' AND `password`='".$password."'");
		}
	
		if($sessiondata['user']['basic'])
		{
			$this->session->set('logged', TRUE);
			$this->session->set('ALC', $sessiondata['user']['basic']['alc']);
			$elements = new \Pratik\Config\Users_Config();
			$sessiondata['user']['metas'] = $this->model->getMetas($sessiondata['user']['basic']['id'], 'users', "",$elements->metas, 'tr_TR', 'meta', false, false);
			$this->session->set($sessiondata);
			redirect2(site_url('dashboard')); /* Redirect browser */			
		} else {
			redirect2(site_url('login')); /* Redirect browser */		
		}
	}
	public function out()
	{	
		$this->session->set('logged',FALSE);
		$this->session->set('ALC',FALSE);
		$this->session->remove('user');
		$this->session->remove('lang');
		$this->session->remove('text');
		return view('Admin\login', array('data'=>$this->data));
	}
	//--------------------------------------------------------------------
}
