<?php
require_once APPPATH . "/helpers/SimpleXLSXGen.php";

class Trialusage extends Controller
{
	private $ctrID;
	private $partner;

	function __construct()
	{
		parent::__construct();

		$this->load->library('session');
		$this->load->model('mtrialusage');
		$this->load->model('mweborder');
		$this->lang->load('site');

		if ($this->phpsession->get('access_id')) {
			if ($this->phpsession->get('additional_access_id') != '')
				$access_id = $this->phpsession->get('additional_access_id');
			else
				$access_id = $this->phpsession->get('access_id');

			$this->ctrID = $this->mweborder->getCtrID($access_id);
			$this->partner = $this->mtrialusage->getPartner($access_id);
		}
	}

	function index()
	{
		if (!$this->mweborder->weborderAccess()) {
			$this->mweborder->loadLoginTemplate();
			return;
		}

		$data = array();

		$partner_template 	= $this->mtrialusage->getPartnerTemplate($this->partner->id);
		$temp 				= json_decode($partner_template->template);
		$data['templates'] 	= $temp->templates;
		$data['versions']	= $this->mtrialusage->getProgramVersions();
		$data['types'] 		= $this->mtrialusage->getProgramTypes();
		$data['ctrID']		= $this->ctrID;
		$data['spec']		= $this->mtrialusage->getSpecEditonForCountry(substr($this->ctrID, 0, 2));
		$data['appearance'] = $_COOKIE["appearance"] != '' ? $_COOKIE["appearance"] : 1;
		$data['permission']	= $this->mtrialusage->getPermission($this->partner->permission_id);
		$data['crm_url']	= $data['permission']->is_admin == 1 ? 'https://crm.cadline.hu/hu/users/show/' : $partner_template->crm_url;
		$data['dists']		= $this->mtrialusage->getDistributors($data['permission']->is_admin, $this->ctrID);

		if ($data['permission']->is_admin)
			$data['schools'] = $this->mtrialusage->getSchools();

		$this->mweborder->loadTemplate('trialusage/index.php', $data, 'Program Usage', 'trialusage', false);
	}

	function trialrequests()
	{
		$data = array();

		$data['ctrID']		= $this->ctrID;
		$data['permission']	= $this->mtrialusage->getPermission($this->partner->permission_id);
		$data['dists'] 		= $this->mtrialusage->getDistributors($data['permission']->is_admin, $this->ctrID);
		$data['versions']	= $this->mtrialusage->getProgramVersions();

		$this->mweborder->loadTemplate('trialusage/trialrequests.php', $data, 'Trial Requests', 'trialrequests', false);
	}

	function getTrialStarts()
	{
		$response = array();

		$response = $this->mtrialusage->getTrialStarts($this->ctrID, $_POST['distID'], $this->partner->permission_id, $this->partner->id, $_POST['fromDate'], $_POST['endDate'], $_POST['typeID'], $_POST['versionID']);

		echo json_encode($response);
	}

	function exportUsers()
	{
		$users = json_decode($_POST['users']);

		$userArray = array(
			array('<b>Name</b>', '<b>Email</b>', '<b>Serial</b>', '<b>Number of starts</b>', '<b>Working hour(s)</b>')
		);


		foreach ($users as $user) {
			$workingHour = round($user->workingHour);
			$userDatas = array($user->userName, $user->email, $user->serial, $user->startSum, $workingHour);
			array_push($userArray, $userDatas);
		}

		$xlsx = Shuchkin\SimpleXLSXGen::fromArray($userArray);
		$xlsx->setDefaultFontSize(14);
		$xlsx->downloadAs($_POST['selected_template'] . '.xlsx');
	}

	function getProgramStarts()
	{
		$response 	= array();
		$courses 	= json_decode($_POST['courses']);

		$response = $this->mtrialusage->getProgramStarts($this->ctrID, $_POST['tmpID'], $_POST['distID'], $this->partner->permission_id, $this->partner->id, $_POST['fromDate'], $_POST['endDate'], $courses);

		echo json_encode($response);
	}

	function getUsersFromPassword()
	{
		$response = array();

		$passwords = json_decode(stripslashes($_POST['data']));

		$response = $this->mtrialusage->getUsersFromPassword($passwords, substr($this->ctrID, 0, 2), $_POST['tmpID'], $this->partner->id);

		echo json_encode($response);
	}

	function getTemplate()
	{
		$response = array();

		$tmp 		= $this->mtrialusage->getPartnerTemplate($this->partner->id);
		$templates = json_decode($tmp->template);
		$templates = (array)$templates->templates;

		$response['template'] 	= $templates[$_POST['id']];
		$response['tmp_id']		= $_POST['id'];

		echo json_encode($response);
	}

	function saveTemplate()
	{
		$response = array();

		$data 		= json_decode($_POST['data']);
		$template	= $data->template;

		$response = $this->mtrialusage->saveTemplate($template, $this->partner->id);

		echo json_encode($response);
	}

	function deleteTemplate()
	{
		$response = array();

		$response = $this->mtrialusage->deleteTemplate($_POST['id'], $this->partner->id);

		echo json_encode($response);
	}

	function getCourses()
	{
		$response = array();

		$selectedSchools = json_decode($_POST['selectedSchools']);

		$response = $this->mtrialusage->getCourses($selectedSchools);

		echo json_encode($response);
	}

	function getNameMask()
	{
		echo json_encode($this->partner->program_usage_name_mask);
	}

	function saveNameMask()
	{
		$response = array();

		$response = $this->mtrialusage->saveNameMask($this->partner->id, $_POST['nameMask']);

		echo json_encode($response);
	}

	function createAdminAccess()
	{
		if (isset($_POST['admin']) && $_POST['admin'] == 'true') {
			$this->phpsession->save('bugtracker_access', true);
			$this->phpsession->save('access_id', '36');
		}

		redirect('/trialusage');
	}
}
