<?php
	class publicController extends mainController {
		function __construct() {
			parent::__construct();
			
			//To change for every Controller
			$this->viewDir = 'Public';
		}
		
		public function beforeRender($content=null) {
			return false;
		}
		
		public function getGloablJs() {
			return $this->setView('getGloablJs');
		}
		
		public function recoveryPassword() {
			$email = $this->getPost('email', '');
			$validate = $this->utility->validateEmail($email);
			
			if (!$validate) {
				return $this->setRawJsonResponse('err', null);
			}
			
			$user = $this->db->where('email', $email, 'like')->getOne('users');
			
			if (is_array($user) && !empty($user)) {
				
				$passwd = uniqid();
				$encrPasswd = md5($passwd);
				
				$upadate = $this->db->where('id', $user['id'])->update('users', ['password'=>$encrPasswd]);
				
				if ($upadate) {
					$recipientName = ucwords(strtolower($user['name'].' '.$user['surname']));
					$recipientUsername = $user['username'];
					$loginPage = '<a href="'.$this->config['settings']['email']['footer-home-link'].'" target="_blank">'.$this->config['settings']['email']['footer-home-link'].'</a>';
					$defaultGreeting = $this->config['settings']['email']['final-greeting'];
					$mailID = uniqid();
					
					$mailContent = _("Dear %s,<br>
					you've just requested a Temporary Password:<br><br><b>Username</b>: %s<br><b>Password</b>: %s<br><b>Login Page</b>: %s<br><br>After logging in, you can change the password in your <b>Profile</b> page.<br><br>%s");
					
					$mailSubject = _('Password recovery');
					$mailHTML = $this->partial('Message/Email/template', ['mailID'=>$mailID, 'body'=>vsprintf($mailContent, [$recipientName, $recipientUsername, $passwd, $loginPage, $defaultGreeting]), 'showTemplate'=>true]);
					
					$this->notification->sendEmail($email, $mailSubject, $mailHTML, $mailID);
				} else {
					return $this->setRawJsonResponse('err', null);
				}
				
			} else{
				return $this->setRawJsonResponse('err', null);
			}
			
			return $this->setRawJsonResponse('ok', null, []);
		}
		
		public function emailPreview() {
			$args = func_get_args();
			$emailId = isset($args[0]['id']) ? $args[0]['id'] : null;
			
			$mailResult = $this->db->where('id', $emailId)->getOne('log_notifications');
			$mailBody = isset($mailResult['mail_body']) ? $mailResult['mail_body'] : null;
			$showTemplate = is_null($mailBody) ? true : false;
			
			$mailBody = is_null($mailBody) ? _('Invalid e-mail content.') : $mailBody;
			
			$this->view->html = $this->partial('Message/Email/template', ['mailID'=>$emailId, 'body'=>$mailBody, 'showTemplate'=>$showTemplate]);
			return $this->setView('emailPreview');
		}

		public function showAttachementsInDream() {
			$request_code = $this->getPost('request_code', null);
			$handleRequest = new HandleRequest();
			
			$this->view->request = [];
			$this->view->attachments = [];
			$this->view->groupedAttachments = [];
			
			$this->actionTitle = 'Request';
			$this->view->patientInfo = '';
			
			if (!is_null($request_code)) {
				$request = $this->db
					->join('requests_registry rr', 'rr.request_id=r.id')
					->where('r.unique_code', $request_code)
					->getOne('requests r', "r.*, rr.name patient_name, rr.surname patient_surname, rr.birthdate patient_dob");
				
				if (isset($request['id'])) {
					$this->actionTitle = 'Request #'.$request['id'];
					$this->view->patientInfo = $request['patient_surname'].' '.$request['patient_name'].', '.$this->helper->getDateString($request['patient_dob'], false);
					
					$this->view->request = $request;
					
					$this->view->attachments = $handleRequest->getAttachmentsByRequestId($request['id']);
					
					//Group attachments by date
			
					if (is_array($this->view->attachments) && !empty($this->view->attachments)) {
						foreach($this->view->attachments as $attachment) {
							//$ext = $this->helper->getExtension($attachment['file_name']);
							$attachment['previewType'] = $this->helper->getPreviewType($attachment['file_name']);
							$this->view->groupedAttachments[date('Y-m-d 00:00:00', strtotime($attachment['created_at']))][] = $attachment;
						}
	
						if (!empty($this->view->groupedAttachments)) {
							foreach($this->view->groupedAttachments as $ext => $attachList) {
								sort($attachList);
								$this->view->groupedAttachments[$ext] = $attachList;
							}
						}
					}
				}
			}
			
			return $this->setJsonView('showAttachementsInDream');
		}
	}