<?php namespace App\Controllers;
  
use CodeIgniter\Controller;
use App\Models\UserModel;
  use CodeIgniter\Session\Session;

class Login extends Controller
{
    public function index()
    {
        helper(['form']);
                echo view('navtwo');

        echo view('login');
    } 
  
    public function auth()
    {
        $session = session();
        $model = new UserModel();
        $email = $this->request->getVar('email');
        $password = $this->request->getVar('password');
        $data = $model->where('email', $email)->first();
        if($data){
            $pass = $data['password'];
            $verify_pass = password_verify($password, $pass);
            if($verify_pass){
                $ses_data = [
                    'id'       => $data['id'],
                    'fullname'     => $data['fullname'],
                    'email'    => $data['email'],

                ];
                $session->set($ses_data);
                    $session->set('admin', $data);

                return redirect()->to('/index');
            }else{
                $session->setFlashdata('msg', 'Wrong Password');
                return redirect()->to('/login');
            }
        }else{
            $session->setFlashdata('msg', 'Email not Found');
            return redirect()->to('/login');
        }
    }
  
    public function logout()
    {
        $session = session();
        $session->destroy();
        return redirect()->to('/login');
    }
} 