<?php

namespace App\Controllers;

class Pages extends BaseController {
	
	public function dashboard_index() {
		return view('dashboard');
	}
	
	public function userroles_index(): string {
        return view('userroles_index');
    }
	
	public function users_index(): string {
		$db = \Config\Database::connect();
		$sql = "select * from cmod.dbo.CM_USERROLES";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['userroles'] = $results;
        return view('users_index', $data);
    }
	
	public function changePass() {
		if ($this->request->getMethod() === 'POST') {
			$password1 = $this->request->getVar('password1');
			$password2 = $this->request->getVar('password2');
			$data['password1'] = $password1;
			$data['password2'] = $password2;
			if($password1 == $password2) {	
				$password = password_hash($password1,PASSWORD_DEFAULT);
				$db = \Config\Database::connect();
				$sql = "update cmod.dbo.CM_USERS set PASSWORD='$password' where USERID='$userid'";
				$db->query($sql);
				return redirect()->to("/");
			} else {
				return redirect()->to("/auth/setpass/$userid")->with('flash_error', 'password is not the same.');
			}
		}
		return view('changePass');
    }
	
	public function dictTests_index() {
		return view('dictTests_index');
    }
}
