<?php
namespace src\controllers;

use \core\Controller;
use \src\controllers\UtilsController;
use \src\handlers\LoginHandler;
use \src\handlers\PermissionHandler;
use \src\models\Bills_to_pay;
use \src\models\Bills_to_receive;

use \src\models\User;

class SetingsController 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 = 'config';
        $this->paramsName = PermissionHandler::getPermissions($this->loggedUser->group);

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

    public function index() {

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

    public function reports() {
        $error = array();
        $reportsInfo = UtilsController::getConfigReports();

        if(isset($_SESSION['msg'])) {
            $error['alertType'] = $_SESSION['alertType'];
            $error['msg'] = $_SESSION['msg'];
            unset($_SESSION['msg'], $_SESSION['alertType']);
        }

        $this->render('report_settings', [
            'loggedUser' => $this->loggedUser,
            'pageActive' => $this->pageActive,
            'permissions' => $this->paramsName,
            'reports_info' => $reportsInfo,
            'error' => $error,
            'titlePage' => 'Configurações de Impressão - Supporte Agronegócios'
        ]);
    }

    public function reportsAction() {
        if(!empty($_POST['title'])) {
            $reports_info = UtilsController::getConfigReports();
            $report['title'] = filter_input(INPUT_POST, 'title');
            $report['other_information'] = filter_input(INPUT_POST, 'other_information');
            $file = $_FILES['img_soon'];

            if($file['size'] > 0) {
                $report['img_soon'] = UtilsController::uploadAttachment($file, 'assets/images');    

                if($report['img_soon'] == false) {
                    $_SESSION['alertType'] = 'alert-warning';
                    $_SESSION['msg'] = "Arquivo não aceito e/ou muito grande!";
                    $this->redirect('/reportconfig');
                }
                
                unlink('../public/assets/images/'.$reports_info['img_soon']);
            }

            UtilsController::updateReportSetings($report, $reports_info['id']);
            $this->redirect('/reportconfig');
        }
    }

    public function btp() {

        $this->render('bp_settings', [
            'loggedUser' => $this->loggedUser,
            'pageActive' => $this->pageActive,
            'permissions' => $this->paramsName,
            'titlePage' => 'Configurações de Contas a Pagar - Supporte Agronegócios'
        ]);
    }


    public function btpType() {
        $btpList = Bills_to_pay::getAllBillsToPaytype();

        $this->render('btp_type', [
            'loggedUser' => $this->loggedUser,
            'pageActive' => $this->pageActive,
            'permissions' => $this->paramsName,
            'btpList' => $btpList,
            'titlePage' => 'Tipo de Contas a Pagar - Supporte Agronegócios'
        ]);
    }

    public function btrType() {
        $btrList = Bills_to_receive::getAllReceiverOrigin();

        $this->render('btr_type', [
            'loggedUser' => $this->loggedUser,
            'pageActive' => $this->pageActive,
            'permissions' => $this->paramsName,
            'btrList' => $btrList,
            'titlePage' => 'Tipo de Contas a Receber - Supporte Agronegócios'
        ]);
    }
}