<?php
namespace src\controllers;

use \core\Controller;
use \src\handlers\LoginHandler;
use \src\handlers\PermissionHandler;
use \src\models\Deliverie;
use \src\models\Request;

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

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

    public function index() {
        $months = self::getMonths();
        // Gráfico Vendas Retiradas (Entregas)
        $this_year = array();
        $last_year = array();

        $now = date('Y');
        $this_year_init = date($now.'-0'. 1 .'-0'. 1);
        $last_year_init = date('Y-m-d', strtotime('- 1 Year', strtotime($this_year_init)));
        
        $this_year = Deliverie::getSalesThisYear($this_year_init, count($months));
        $last_year = Deliverie::getSalesThisYear($last_year_init, count($months));

        $arrayData1 = json_encode(self::tranformToCharts($this_year, $months));
        $arrayData2 = json_encode(self::tranformToCharts($last_year, $months));

        // $max_this_year = max($this_year);
        // $max_last_year = max($last_year);
        // $this->arrayInfo['max'] = max($max_this_year, $max_last_year);
        // $this->arrayInfo['months'] = $months;

        // // Ano atual        
        // foreach ($this_year as $key => $value) {            
        //     $this->arrayInfo['graphic_this_year'][$key]['percente'] = ($value / $this->arrayInfo['max']) * 100;
        //     $this->arrayInfo['graphic_this_year'][$key]['value'] = $value;
        // }        

        // // Ultimo ano
        // foreach ($last_year as $key => $v_last) {           
        //     $this->arrayInfo['graphic_last_year'][$key]['percente'] = ($v_last / $this->arrayInfo['max']) * 100;
        //     $this->arrayInfo['graphic_last_year'][$key]['value'] = $v_last;
        // }

        // Gráfico Vendas (Pedidos)

        $sales_last_year = array();

        $sales_this_year = Request::getSalesThisYear($this_year_init, count($months));
        $sales_last_year = Request::getSalesThisYear($last_year_init, count($months));

        $arrayData3 = json_encode(self::tranformToCharts($sales_this_year, $months));
        $arrayData4 = json_encode(self::tranformToCharts($sales_last_year, $months));

        // $max_sale_this_year = max($sales_this_year);
        // $max_sale_last_year = max($sales_last_year);


        // $this->arrayInfo['max_sale'] = max($max_sale_this_year, $max_sale_last_year);
        // $this->arrayInfo['months'] = $months;

        // // Ano atual        
        // foreach ($sales_this_year as $key => $value) {          
        //     $this->arrayInfo['graphic_sale_this_year'][$key]['percente'] = ($value / $this->arrayInfo['max_sale']) * 100;
        //     $this->arrayInfo['graphic_sale_this_year'][$key]['value'] = $value;
        // }        

        // // Ultimo ano
        // foreach ($sales_last_year as $key => $v_last) {         
        //     $this->arrayInfo['graphic_sale_last_year'][$key]['percente'] = ($v_last / $this->arrayInfo['max_sale']) * 100;
        //     $this->arrayInfo['graphic_sale_last_year'][$key]['value'] = $v_last;
        // }

        $this->render('dashboard', [
            'loggedUser' => $this->loggedUser,
            'pageActive' => $this->pageActive,
            'permissions' => $this->paramsName,
            'data' => $this->arrayInfo,
            'arrayData1' => $arrayData1,
            'arrayData2' => $arrayData2,
            'arrayData3' => $arrayData3,
            'arrayData4' => $arrayData4,
            'titlePage' => 'Dashboard - Supporte Agronegócios'
        ]);
    }

    private static function getMonths() {
        $months = [
            0 => 'Janeiro',
            1 => 'Fevereiro',
            2 => 'Março',
            3 => 'Abril',
            4 => 'Maio',
            5 => 'Junho',
            6 => 'Julho',
            7 => 'Agosto',
            8 => 'Setembro',
            9 => 'Outubro',
            10 => 'Novembro',
            11 => 'Dezembro'
        ];

        return $months;
    }


    private function tranformToCharts($data, $labels) {
        $response = array();

        if($labels) {
            foreach ($labels as $key => $item) {
                $response[$key]['label'] = $item;
                $response[$key]['y'] = $data[$key];
            }
        }

        return $response;
    }
}