<?php
namespace src\controllers;

use \core\Controller;
use \src\handlers\FiltersHandler;
use \src\handlers\LoginHandler;
use \src\handlers\PermissionHandler;
use \src\controllers\UtilsController;

class PermissionsController extends Controller {
    private $loggedUser;
    private $pageActive;
    private $paramsName;
    private $arrayInfo;

    public function __construct() {
        $this->loggedUser = LoginHandler::checkLogin();

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

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

        if(!in_array('permissions_view', $this->paramsName)) {
            $this->redirect('/');
        }        
    }

    public function index() {        
        $groupsList = PermissionHandler::getAllGroups();

        $this->render('permissions', [
            'loggedUser' => $this->loggedUser,
            'pageActive' => $this->pageActive,
            'permissions' => $this->paramsName,
            'groupsList' => $groupsList,
            'titlePage' => 'Permissões - Supporte Agronegócios'
        ]);
    }

    public function add() {        
        $permissionsList = PermissionHandler::getAllPermissions();

        $this->render('permissions_add', [
            'loggedUser' => $this->loggedUser,
            'pageActive' => $this->pageActive,
            'permissions' => $this->paramsName,
            'permissionsList' => $permissionsList,
            'titlePage' => 'Adicionar Grupo de Permissões - Supporte Agronegócios'
        ]);
    }

    public function addAction() {        
        if(!empty($_POST['name']) && !empty($_POST['permissions'])) {
            $permission['name'] = filter_input(INPUT_POST, 'name');
            $permission['params'] = filter_input(INPUT_POST, 'permissions', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);

            if($permission['name'] && $permission['params']) {
                $permission['params'] = implode(',', $permission['params']);

                PermissionHandler::saveGroup($permission);
                $this->redirect('/permissions');
            }
        }
    }

    public function edit() {        
        $permission = filter_input(INPUT_GET, 'g');

        if($permission) {
            $permissionsList = PermissionHandler::getAllPermissions();
            $permissionInfo = PermissionHandler::getPermission($permission);
            $permissionInfo['params'] = explode(',', $permissionInfo['params']);

            $this->render('permissions_edit', [
                'loggedUser' => $this->loggedUser,
                'pageActive' => $this->pageActive,
                'permissions' => $this->paramsName,
                'permissionsList' => $permissionsList,
                'permissionInfo' => $permissionInfo,
                'titlePage' => 'Editar Grupo de Permissões - Supporte Agronegócios'
            ]);
        }
    }

    public function editAction() {        
        if(!empty($_POST['name']) && !empty($_POST['permissions'])) {
            $permissionId = filter_input(INPUT_POST, 'id');
            $permission['name'] = filter_input(INPUT_POST, 'name');
            $permission['params'] = filter_input(INPUT_POST, 'permissions', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);

            if($permission['name'] && $permission['params']) {
                $permission['params'] = implode(',', $permission['params']);

                PermissionHandler::updateGroup($permissionId, $permission);
                $this->redirect('/permissions');
            }
        }
    }

    public function delete() {        
        $permission = filter_input(INPUT_GET, 'g');

        if($permission) {
            PermissionHandler::deleteGroup($permission);
            $this->redirect('/permissions');
        }
    }    
}