<?php
namespace src\controllers;

use \core\Controller;
use \src\handlers\LoginHandler;
use \src\handlers\PermissionHandler;
use \src\models\User;

class HomeController extends Controller {
    private $loggedUser;
    private $pageActive;
    private $paramsName;

    public function __construct() {

        $this->loggedUser = LoginHandler::checkLogin();

        if($this->loggedUser == false) {
            $this->redirect('/login');
        }

        $this->pageActive = 'home';
        $this->paramsName = PermissionHandler::getPermissions($this->loggedUser->group);
        
    }

    public function index() {
        $this->render('home', [
            'loggedUser' => $this->loggedUser,
            'pageActive' => $this->pageActive,
            'permissions' => $this->paramsName,
            'titlePage' => 'Home - Supporte Agronegócios'
        ]);
    }


    public function setMessageRead() {
        $ref = intval(filter_input(INPUT_POST, 'ref'));

        if($ref) {
            User::updateNotification($ref, 2);
        }
        echo json_encode(true);
    }

}