<?php
	
	class dashboardController extends mainController {
		
		private $dashboardPageLimit;
				
		function __construct() {
			parent::__construct();
			
			$this->dashboardPageLimit = 5;
			
			//To change for every Controller
			$this->viewDir = 'Dashboard';			
			//$this->allow = [];
		}
		
		public function index($args=null) {
			
			if (!$this->user->isLogged()) {
				return $this->redirect('login', 'permissionDenied');
			}
			
			$this->view->userData = $this->user->getUser();
			$this->view->showRoleLabel = count($this->view->userData['userRolesLocale']) > 1 ? true : false; //Statistics
			$this->showBreadcrumbs = false;
			
			return $this->setJsonView('index');
		}
		
		public function indexNoPrivacy() {
			return $this->setJsonView('indexNoPrivacy');
		}
		
		//Last sessions on Dashboard for administrator
		public function loadLastSessions() {

			if(!$this->checkPermissions([ADMIN_ROLE_ID])) {
				return $this->redirect('login', 'permissionDenied');
			}
			
			$userGroupId = $this->user->getUserField('userGroupId');
			$page = 1;
			$this->db->pageLimit = $this->dashboardPageLimit;
			$sessions = $this->db
				->join('users u', 'u.id=s.user_id', 'INNER')
				->where('s.user_id', 0, '>')
				->where('u.group_id', $userGroupId)
				->orderBy('s.session_updated_at', 'DESC')
				->paginate('sessions s', $page, ['s.*', 'u.surname', 'u.name', 'u.updated_at']);
			
			$this->view->sessions = $sessions;
			return $this->setJsonView('loadLastSessions');
		}
		
		public function loadLastSubscriptions() {
			if(!$this->checkPermissions([ADMIN_ROLE_ID])) {
				return $this->redirect('login', 'permissionDenied');
			}
			
			$userGroupId = $this->user->getUserField('userGroupId');
			
			$page = 1;
			$this->db->pageLimit = $this->dashboardPageLimit;
			$this->view->usersList = $this->db->where('group_id', $userGroupId)->orderBy('created_at', 'DESC')->paginate('users', $page);
			return $this->setJsonView('loadLastSubscriptions');
		}
		
		public function loadLastAccesses() {
			if(!$this->checkPermissions([ADMIN_ROLE_ID])) {
				return $this->redirect('login', 'permissionDenied');
			}
			
			$userGroupId = $this->user->getUserField('userGroupId');
			
			$page = 1;
			$this->db->pageLimit = $this->dashboardPageLimit;
			$accesses = $this->db
				->join('users u', 'u.id=a.user_id', 'INNER')
				->where('a.user_id', 0, '>')
				->where('u.group_id', $userGroupId)
				->orderBy('a.created_at', 'DESC')
				->paginate('log_access a', $page, ['a.*', 'a.created_at AS access_date', 'u.username', 'u.surname', 'u.name', 'u.updated_at AS user_updated_at']);
			
			$this->view->accesses = $accesses;
			return $this->setJsonView('loadLastAccesses');
		}
		
		public function loadStatistics() {
			
			$roleId = $this->getPost('roleId', 0);
			
			if(!$this->checkPermissions([ADMIN_ROLE_ID, MODERATOR_ROLE_ID, REFERRER_ROLE_ID, APPLICANT_ROLE_ID, GUEST_ROLE_ID])) {
				return $this->redirect('login', 'permissionDenied');
			}
			
			$handleRequest = new HandleRequest();
			$triageData = $handleRequest->getStatistics('triage_color', $roleId, $this);
			$statusData = $handleRequest->getStatistics('request_status', $roleId, $this);
			
			$this->view->statData = $statusData;
			$this->view->roleId = $roleId;
			
			$this->view->triageQty = array_sum($triageData['values']);
			$this->view->statusQty = array_sum($statusData['values']);
			
			//return $this->setJsonView('loadStatistics');
			$html = $this->partial('Dashboard/statistics-charts');
			return $this->setRawJsonResponse('ok', '', ['html'=>$html, 'chartTitle'=>'', 
			
				'barTriageLabels'=>$triageData['labelsValues'],
				'pieTriageLabels'=>$triageData['labelsPerc'],
				'TriageColors'=>$triageData['colors'],
				'TriageBorders'=>$triageData['borders'],
				'TriageValues'=>$triageData['values'],
				'TriagePerc'=>$triageData['perc'],
				
				'barStatusLabels'=>$statusData['labelsValues'],
				'pieStatusLabels'=>$statusData['labelsPerc'],
				'StatusColors'=>$statusData['colors'],
				'StatusBorders'=>$statusData['borders'],
				'StatusValues'=>$statusData['values'],
				'StatusPerc'=>$statusData['perc'],
			]);
		}
		
		public function allowAccess() {
			if (!$this->user->isLogged()) {
				return $this->redirect('login', 'index', ['jsRedirect'=>'/']);
			}
			
			return false;
		}
		
	}