<?php

namespace App\Controllers;

use Config\Services;


class Home extends BaseController
{
    public function index()
    {
        // $segments = ['news', 'local', '123'];
        // echo site_url($segments);
        return view('welcome_message');
    }

    public function startlogin()
    {
        $request = \Config\Services::request();

        if ($request->getMethod() == 'post') {
            $out = [];
            $username = trim($request->getPost('username'));
            $password = trim($request->getPost('password'));
            if ($this->loginUser($username, $password, $out) == true) {
                $segments = ['dash'];
                return redirect()->to(site_url($segments));
            }
        } else {
            return view('welcome_message');
        }
        return view('welcome_message'); // may get here in case of error login
    }

    private function loginUser($username, $password, &$out)
    {
        $loginComplete = false;
        //entries sent
        if ($username == '' || $password == '') {
            $loginComplete = false;
            $out['error'] = 'All parameters required';
        }
        if ($username == 'ameye@chiefsoft.com' && $password == 'may12002') {
            $loginComplete = true;
            $out['username'] = 'ameye@chiefsoft.com';
            $out['firstname'] = 'Olusesan';
            $out['lastname'] = 'Ameye';
            $out['uid'] = 'gdgdgdgd-dhdhshdh-gshshshhs-hdhdhuu';
            $out['id'] = '100';
           // $_SESSION['bko_session'] = rand(10000,9999999).'-hhdsdyuyu487484398439843jjhjfhjfdhjfdhjfdhj89498498598jfjfhjf';
           // $_SESSION['bko_user'] = $out; 
            
            Services::session()->set([
                'bko_user' => $out,
                'bko_session' => rand(10000,9999999).'-hhdsdyuyu487484398439843jjhjfhjfdhjfdhjfdhj89498498598jfjfhjf',
                'lastName'=> $out['lastname'],
                'firstName'=> $out['firstname'],
                'username'=> $out['username']
            ]);
        }

        return $loginComplete;
    }
}
