<?php
include_once('includes/system_settings/system_settings_functions.php');

if(!SITEADMIN) {
	return false;
}

function system_settings_main_page() {
	include(LANGUAGEFILE);
	$returndata = '';
	
	$system_setting_categories = get_system_setting_categories();
	
	$returndata .= '<div class="row">';
		// Heading
		$returndata .= '<div class="col-12 text-center font-bold font-150">';
			$returndata .= $l_system;
		$returndata .= '</div>';
		
		// Categories
		foreach($system_setting_categories as $this_cat_id => $this_cat_data) {
			$returndata .= '<div class="col-lg-4 col-6 text-center">';
				$returndata .= "<div class='thumb_container' onClick='".$this_cat_data['onClick']."'>";
					$returndata .= "<div class='thumb_icon_container'>";
						$returndata .= "<img src='".$this_cat_data['icon']."' class='thumb_icon_container'>";
					$returndata .= '</div>';
					$returndata .= "<div class='thumb_name_container'>";
						$returndata .= $this_cat_data['name'];
					$returndata .= "</div>";					
				$returndata .= '</div>';
			$returndata .= '</div>';
		}
	$returndata .= '</div>';
	
	return $returndata;
}

function system_settings_users_main_page() {
	include(LANGUAGEFILE);
	$returndata = '';

	$returndata .= '<div class="row">';
		// Heading
		$returndata .= '<div class="col-12 text-center font-bold font-150">';
			$returndata .= '<img src="images/back_arrow.png" class="icon mr-4"'." onClick='getSystemSettingsPage(\"system_settings_main_page\");'>";
			$returndata .= $l_users;
		$returndata .= '</div>';
		
		// Controlls
		$returndata .= '<div class="col-lg-3 col-12">';
			$returndata .= "<img src='images/add-user.png' class='iconLarge border-2 pt-2 pb-2 pl-2 pr-2 ' style='border-radius:8px;' onClick='editSystemUser(\"0\");'>";
		$returndata .= '</div>';
		$returndata .= '<div class="col-lg-9 col-12 text-right">';
			$returndata .= "";
		$returndata .= '</div>';
	$returndata .= '</div>';

	$returndata .= '<div class="row mt-4 text-center pl-3">';
		$returndata .= '<div class="col-12 text-center">';
			$returndata .= '<div class="list-group" id="system_user_list" style="margin:auto;">';
				$returndata .= print_system_user_list();
			$returndata .= '</div>';
		$returndata .= '</div>';
	$returndata .= '</div>';
	
	return $returndata;
}

function print_system_user_list() {
	global $system_user;
	$returndata = '';

	$all_users = $system_user->get_all_users();
	
	foreach($all_users as $this_user_id => $this_user_data) {
		$edit_user_js = "editSystemUser(\"".$this_user_id."\")";
		
		$returndata .= "<div class='list-group-item list-group-item-action'>";
			$returndata .= '<div class="row">';
				$returndata .= "<div class='col-6 text-left' onClick='".$edit_user_js."'>";
					$returndata .= $this_user_data['user_name'].'';
				$returndata .= '</div>';
				$returndata .= "<div class='col-3' onClick='".$edit_user_js."'>";
					$returndata .= $this_user_data['last_login'].'';
				$returndata .= '</div>';
				$returndata .= '<div class="col-3 text-right">';
				if($this_user_data['disabled']) {
					$enable_disable_button = "<img src='images/enable_user.png' class='icon'>";
				} else {
					$enable_disable_button = "<img src='images/disable_user.png' class='icon'>";
				}
					$returndata .= $enable_disable_button;
				$returndata .= '</div>';
			$returndata .= '</div>';
		$returndata .= '</div>';
	}
	
	// $returndata .= json_encode($system_user->users_data);
	
	return $returndata;
}

function edit_system_user($variables) {
	/*
		$variables['user_id']
	*/
	include(LANGUAGEFILE);
	global $system_user;
	$returndata = '';
	
	if($variables['user_id']) {
		if(!$user_data = $system_user->get_user_data($variables['user_id'])) {
			$GLOBALS['errors'][] = __FUNCTION__.' user not found in database';
			error_log(__FUNCTION__.' user not found in database');
			return false;
		}

		$heading = $l_edit_user.' <u class="font-125">'.$user_data['user_name'].'</u>';
		
	} else {
		$heading = $l_create_new_user;

		// Set default values for new user
		$user_data = array(
			'user_name' => '',
			'email' => '',
			'login_name' => '',
			'last_login' => '',
			'disabled' => 0,
			'admin' => 0
		);
	}

	$returndata .= '<div class="row">';
		// Heading
		$returndata .= '<div class="col-12 text-center font-bold font-150">';
			$returndata .= '<img src="images/back_arrow.png" class="icon mr-4"'." onClick='getSystemSettingsPage(\"users\");'>";
			$returndata .= $heading;
			if($variables['user_id']) {
				$returndata .= '<br><span style="font-size:45%;">(Last Login: '.$user_data['last_login'].')</span>';
			}

		$returndata .= '</div>';

		// Form
		$returndata .= '<div class="col-12 my-3">';
			$returndata .= '<form action="#" method="POST"'." onSubmit='saveUserData(this);return false;'>";
				$returndata .= '<input type="hidden" id="id" name="id" value="'.$variables['user_id'].'">';
			$returndata .= '<div class="row">';
				$returndata .= '<div class="col-6">';
					$returndata .= 'Name: <input type="text" id="user_name" name="user_name" value="'.$user_data['user_name'].'">';
				$returndata .= '</div>';
				// email
				$returndata .= '<div class="col-6">';
					$returndata .= 'Email: <input type="text" id="user_email" name="email" value="'.$user_data['email'].'">';
				$returndata .= '</div>';
				// Login name
				$returndata .= '<div class="col-6 my-1">';
					$returndata .= 'Login Name: <input type="text" id="user_login_name" name="login_name" value="'.$user_data['login_name'].'">';
				$returndata .= '</div>';
				// Disabled
				$returndata .= '<div class="col-6 my-1">';
					// Set default value to 0
					$returndata .= '<input type="hidden" id="user_disabled"  name="disabled" value="0">';
					$returndata .= 'Disabled: <input type="checkbox" id="user_disabled" value="1"';
					if($user_data['disabled']) {
						$returndata .= ' checked';
					}
					$returndata .= '>';
				$returndata .= '</div>';

				// If editing user is admin, they can set admin flag
				$returndata .= '<input type="hidden" id="user_admin" name="admin" value="0">';
				if(is_system_admin()) {
					$returndata .= '<div class="col-4 my-1 text-left">';
						// Set default value to 0
						$returndata .= 'Admin: <input type="checkbox" id="user_admin" name="admin" value="1"';
						if($user_data['admin']) {
							$returndata .= ' checked';
						}
						$returndata .= '>';
					$returndata .= '</div>';
				} else {
					$returndata .= '<div class="col-4 my-1"></div>';
				}

				// Password
				$returndata .= '<div class="col-8 my-1">';
					$returndata .= 'Password: <input type="password" id="password" name="password" value="">';
				$returndata .= '</div>';
				// Repeat Password
				$returndata .= '<div class="offset-4 col-8 my-1">';
					$returndata .= 'Confirm Password: <input type="password" id="password_confirm" name="password_confirm" value="">';
				$returndata .= '</div>';

				// Save Button
				$returndata .= '<div class="col-12 text-center my-3">';
					$returndata .= '<input type="submit" class="btn btn-primary" name="submit" value="'.$l_save.'">';
				$returndata .= '</div>';
			$returndata .= '</div>';
			$returndata .= '</form>';
		$returndata .= '</div>';

		if($variables['user_id']) {
			// List user function permissions
			$user_permissions = $system_user->get_user_permissions($variables['user_id']);

			$all_functions = get_system_functions();

			$returndata .= '<div class="col-12">';
				$returndata .= '<div class="row">';
					$returndata .= '<div class="col-12 text-center font-bold font-150">';
						$returndata .= $l_user_permissions;
					$returndata .= '</div>';
				$returndata .= '</div>';
				$returndata .= '<div class="row">';
					$returndata .= '<div class="col-12">';
						$returndata .= '<div class="list-group">';
							foreach($all_functions as $this_function_id => $this_function_data) {
								$returndata .= '<div class="list-group-item list-group-item-action">';
									$returndata .= '<div class="row">';
										$returndata .= '<div class="col-6 text-left">';
											$returndata .= $this_function_data['name'];
										$returndata .= '</div>';
										// Permission level dropdown, 0, 10 : user, 50 : supervisor, 100 : admin
										$returndata .= '<div class="col-6 text-right">';
											$permission_levels = [0 => 'None', 10 => 'User', 50 => 'Supervisor', 100 => 'Admin'];
											$returndata .= '<select id="permission_'.$this_function_id.'" name="permission_'.$this_function_id.'"'." onChange='updateUserPermission(\"".$variables['user_id']."\", \"".$this_function_id."\", this.value);'>";
												foreach($permission_levels as $this_permission_level => $this_permission_name) {
													$returndata .= '<option value="'.$this_permission_level.'"';
													if((isset($user_permissions[$this_function_id]) ? $user_permissions[$this_function_id] : 0) == $this_permission_level) {
														$returndata .= ' selected';
													}
													$returndata .= '>'.$this_permission_name.'</option>';
												}
											$returndata .= '</select>';
										$returndata .= '</div>';
									$returndata .= '</div>';
								$returndata .= '</div>';
							}
						$returndata .= '</div>';
					$returndata .= '</div>';
				$returndata .= '</div>';
		}
	$returndata .= '</div>';

	return $returndata;
}

function system_settings_system_test_page() {
	include_once('includes/projects/projects_components.php');
	include(LANGUAGEFILE);
	$returndata = '';

	$returndata .= '<div class="row">';
		// Heading
		$returndata .= '<div class="col-12 text-center font-bold font-150">';
			$returndata .= '<img src="images/back_arrow.png" class="icon mr-4"'." onClick='getSystemSettingsPage(\"system_settings_main_page\");'>";
			$returndata .= $l_system.' '.$l_test;
		$returndata .= '</div>';
	$returndata .= '</div>';
	
	$returndata .= page_test_project_reminder_function();
	
	return $returndata;
}
?>