<?php
include_once('includes/projects/projects_functions.php');

function project_main_page() {
	include(LANGUAGEFILE);
	global $fyrk_projects;
	$returndata = '';
	
	$projects = get_projects();
	
	$returndata .= '<div class="row">';
	if($fyrk_projects->my_permission >= 50) {
		$returndata .= '<div class="col-12">';
			$returndata .= '<img src="images/create_project.png" class="iconMedium margin-auto"'." onClick='editProject(\"0\")'>";
		$returndata .= '</div>';
	}
		
		foreach($projects as $this_project_id => $this_project_data) {
			$returndata .= print_project_select_button($this_project_data);
		}
	$returndata .= '</div>';
	
	return $returndata;
}

function print_project_select_button($variables) {
	$returndata = '';
	
	$set_class = '';
	
	$returndata .= '<div class="col-lg-10 offset-lg-1 col-12 mt-2 mb-2"'." onClick='editProject(\"".$variables['id']."\")'>";
		$returndata .= '<div class="project_select_button">';
			$returndata .= '<div class="row">';
				$returndata .= '<div class="col-lg-8 col-12">';
					$returndata .= $variables['name'];
				$returndata .= '</div>';
				$returndata .= '<div class="col-lg-4 col-12 center-text font-85">';
					$returndata .= $variables['date'];
				$returndata .= '</div>';
			$returndata .= '</div>';
		$returndata .= '</div>';
	$returndata .= '</div>';
	
	return $returndata;
}

function edit_project($variables) {
	/*
		$variables['id']
	*/
	include(LANGUAGEFILE);
	global $fyrk_projects, $sop;
	$returndata = '';
	
	$project_id = 0;
	$project_name = '';
	$project_date = date('Y-m-d', time());
	$project_owner = 0;
	$project_status = 0;
	$variables['depth'] = 1;
	if($project_data = get_projects($variables)) {
		$project_data = $project_data[$variables['id']];
		
		$project_id = $variables['id'];
		$project_name = $project_data['name'];
		$project_date = $project_data['date'];
		$project_owner = $project_data['owner'];
		
	} else {
		
	}
	
	$returndata .= '<div class="col-12 font-bold font-125 text-center pt-1 pb-1">';
		$returndata .= $project_name;
	$returndata .= '</div>';
	
	if($fyrk_projects->my_permission >= 50) {
		// Edit project form
		$returndata .= '<div class="col-12">';
			$returndata .= '<form action="index.php?projects=edit_project" method="post">';
				$returndata .= '<input type="hidden" name="id" id="id" value="'.$project_id.'">';
				$returndata .= '<input type="text" name="name" id="name" value="'.$project_name.'">';
				$returndata .= '<input type="text" name="date" id="date" size="7" class="datepickerbasic ml-1" value="'.$sop->date_to_print($project_date).'">';
				$this_var = array('name' => 'owner', 'id' => 'owner', 'selected' => $project_owner, 'class' => 'ml-2');
				if($fyrk_projects->my_permission <= 50) $this_var['disabled'] = true;
				$returndata .= fyrk_project_user_select($this_var);
				$returndata .= '<input type="submit" name="submit" id="fyrk_project_submit" class="ml-2" value="'.$l_save.'">';
			$returndata .= '</form>';
			$returndata .= '<hr>';
		$returndata .= '</div>';
	}
	
	if($project_id < 1) {
		return $returndata;
	}

	$returndata .= '<div class="col-12">';
		$returndata .= '<div class="row">';
			// Add task
			$returndata .= '<div class="col-3">';
				$returndata .= '<img src="images/new_task.200.png" class="icon"'." onClick='editProjectTask(\"0\", \"".$project_id."\")'>";
			$returndata .= '</div>';
			
			// Filter tasks
			$returndata .= '<div class="col-9 text-right">';
			$task_status_types = project_task_status_types();
			foreach($task_status_types as $status_id => $status_data) {
				$returndata .= '<div class="inline-block bgc-'.$status_data['color'].' ml-1 mr-1 pl-1">'.$status_data['name'].':<input type="checkbox" name="statusFilter'.$status_id.'" id="statusFilter'.$status_id.'" class="taskFilters mr-1" value="taskStatus'.$status_id.'" CHECKED'." onChange='filerProjectTasks()'></div>";
			}
				$returndata .= 'ME: <input type="checkbox" name="myProjects" id="myProjects" class="taskFilters taskFilteronly mr-1" value="owner'.$_SESSION['id'].'"'." onChange='filerProjectTasks()'>";
			$returndata .= '</div>';
			
			$returndata .= '<hr class="mt-2">';
		$returndata .= '</div>';
	$returndata .= '</div>';
	
	$returndata .= '<div class="col-12 bgc-light-gray pt-3 pb-3" id="project_task_list">';
		$this_var = array('project_id' => $variables['id']);
		$returndata .= print_project_task_list($this_var);
	$returndata .= '</div>';

	
	return $returndata;
}

function print_project_task_list($variables) {
	/*
		$variables['task_id']
		$variables['project_id']
	*/
	global $sop, $fyrk_projects;
	
	$variables['order_by'] = 'end_date asc';
	if(!$result = get_project_tasks($variables)) {
		$GLOBALS['errors'][] = __FUNCTION__.' could not retrieve project tasks';
		return false;
	}
	$returndata = '';
	
	$project_users = get_project_users();
	$project_status_types = project_task_status_types();
	
	if(isset($_SESSION['undo']['project_task_delete']) AND $fyrk_projects->my_permission > 50) {
		if((strtotime($_SESSION['undo']['project_task_delete']['delet_ts'])+300) > time() AND $_SESSION['undo']['project_task_delete']['project_id'] == $variables['project_id']) {
			// 5 minute window to undo a deleted task for this project
			$returndata .= '<div class="row mt-2 mb-2 ml-1 pt-1 pb-1 border-2 border-radius-20 projectTaskListRow  hover-highlight-blue owner'.$_SESSION['undo']['project_task_delete']['owner'].' " style="border-color:black;" id="taskRow'.$_SESSION['undo']['project_task_delete']['task_id'].'" '." >";
				$returndata .= '<div class="col-12 text-center font-bold" >';
					$returndata .= "<img src='images/Undo.png' class='icon mr-2' onClick='undoProjectTaskDelete(\"".$_SESSION['undo']['project_task_delete']['task_id']."\", \"".$_SESSION['undo']['project_task_delete']['project_id']."\")'>";
					$returndata .= '<span class="">UNDO DELETE - </span>'.$_SESSION['undo']['project_task_delete']['name'].' <span class="font-85">('.$sop->date_to_print($_SESSION['undo']['project_task_delete']['end_date']).')</span>';
				$returndata .= '</div>';
				
				// Owner
				$returndata .= '<div class="col-3 text-left">';
					$returndata .= (isset($project_users[$_SESSION['undo']['project_task_delete']['owner']]['user_name']) ? $project_users[$_SESSION['undo']['project_task_delete']['owner']]['user_name'] : "");
				$returndata .= '</div>';
				
				// Task description
				$returndata .= '<div class="col-9 text-center ">';
					$returndata .= strip_tags(substr($_SESSION['undo']['project_task_delete']['description'], 0, 55)).'...';
				$returndata .= '</div>';
			$returndata .= '</div>';			
		} elseif((strtotime($_SESSION['undo']['project_task_delete']['delet_ts'])+300) < time()) {
			// Reset undo variable
			unset($_SESSION['undo']['project_task_delete']);
		}
	}
	
	foreach($result as $this_task_id => $this_task_data) {
		$set_row_class = '';
		
		if(isset($project_status_types[$this_task_data['status']])) {
			$set_row_class = 'bgc-'.$project_status_types[$this_task_data['status']]['color'];
		}
		
		if($this_task_data['status'] < 2 AND (strtotime($this_task_data['end_date']) < strtotime(date('Y-m-d', time())))) {
			$set_row_class = 'bg-danger';
		}
		
		$returndata .= '<div class="row mt-2 mb-2 ml-1 pt-1 pb-1 border-2 border-radius-20 projectTaskListRow  hover-highlight-blue taskStatus'.$this_task_data['status'].' owner'.$this_task_data['owner'].' '.$set_row_class.' " style="border-color:black;" id="taskRow'.$this_task_id.'" '." onClick='editProjectTask(\"".$this_task_id."\", \"".$this_task_data['project_id']."\")'>";
			$returndata .= '<div class="col-12 text-center font-bold">';
				$returndata .= $this_task_data['name'].' <span class="font-85">('.$sop->date_to_print($this_task_data['end_date']).')</span>';
			$returndata .= '</div>';
			
			// Owner
			$returndata .= '<div class="col-3 text-left">';
				$returndata .= (isset($project_users[$this_task_data['owner']]['user_name']) ? $project_users[$this_task_data['owner']]['user_name'] : "");
			$returndata .= '</div>';
			
			// Task description
			$returndata .= '<div class="col-9 text-center ">';
				$returndata .= strip_tags(substr($this_task_data['description'], 0, 55)).'...';
			$returndata .= '</div>';
		$returndata .= '</div>';
	}
	
	return $returndata;
}

function fyrk_project_user_select($variables) {
	/*
		$variables['name']
		$variables['id']
		$variables['class']
		$variables['selected']
		$variables['disabled'] = true / false (default)
	*/
	global $sop;
	$returndata = '';
	
	if(!isset($variables['selected'])) {
		$variables['selected'] = 0;
	}
	
	$available_users = get_project_users();
	
	$returndata .= '<select';
	
	if(isset($variables['name'])) {
		$returndata .= ' name="'.$variables['name'].'"';
	}
	
	if(isset($variables['id'])) {
		$returndata .= ' id="'.$variables['id'].'"';
	}
	
	if(isset($variables['class'])) {
		$returndata .= ' class="'.$variables['class'].'"';
	}
	
	if(isset($variables['disabled']) ? $variables['disabled'] : false) {
		$returndata .= ' DISABLED';
	}
	
	$returndata .= '>';
	$returndata .= '<option value="0"></option>';
	
	foreach($available_users as $this_user_id => $this_user_data) {
		
		$set_selected = '';
		if($variables['selected'] == $this_user_id) {
			$set_selected = 'SELECTED';
		}
		
		$returndata .= '<option value="'.$this_user_id.'" '.$set_selected.'>';
		if($this_user_data['permission_lvl'] >= FYRK_MANAGER) {
			$returndata .= '<span stye="font-size: .5em;">*</span>';
		}
			$returndata .= $this_user_data['user_name'];
		$returndata .= '</option>';
	}
	
	$returndata .= '</select>';
	
	return $returndata;
}


function fyrk_project_status_select($variables) {
	/*
		$variables['name']
		$variables['id']
		$variables['class']
		$variables['selected']
	*/
	global $sop;
	$returndata = '';
	
	if(!isset($variables['selected'])) {
		$variables['selected'] = 0;
	}
	
	$available_users = project_task_status_types();
	
	$returndata .= '<select';
	
	if(isset($variables['name'])) {
		$returndata .= ' name="'.$variables['name'].'"';
	}
	
	if(isset($variables['id'])) {
		$returndata .= ' id="'.$variables['id'].'"';
	}
	
	if(isset($variables['class'])) {
		$returndata .= ' class="'.$variables['class'].'"';
	}
	
	$returndata .= '>';
	$returndata .= '<option value="0"></option>';
	
	foreach($available_users as $this_status_id => $this_status_data) {
		
		$set_selected = '';
		if($variables['selected'] == $this_status_id) {
			$set_selected = 'SELECTED';
		}
		
		$returndata .= '<option value="'.$this_status_id.'" '.$set_selected.' style="background-color:'.$this_status_data['color'].';">';
			$returndata .= $this_status_data['name'];
		$returndata .= '</option>';
	}
	
	$returndata .= '</select>';
	
	return $returndata;
}

function edit_project_task($variables) {
	/*
		$variables['task_id']
		$variables['project_id']
	*/
	
	if(!isset($variables['project_id'])) {
		$GLOBALS['errors'][] = __FUNCTION__.' not enought parameters to continue, exit!';
		return false;
	}

	include(LANGUAGEFILE);
	global $fyrk_projects, $sop;
	$returndata = '';
	
	$task_status_types = project_task_status_types();
	$project_users = get_project_users();
	$project_task_status_types = project_task_status_types();
	
	$project_task_name = 'New Task';
	$project_task_project_id = (isset($variables['project_id']) ? $variables['project_id'] : 0);
	$project_task_owner = 0;
	$project_task_end_date = date('Y-m-d', time()+604800);
	$project_task_status = 0;
	$project_task_description = '';
	$project_task_set_reminder = ' CHECKED';
	if(isset($variables['task_id']) ? $variables['task_id'] : 0) {
		if(!$project_task_data = get_project_tasks($variables)) {
			$GLOBALS['errors'][] = __FUNCTION__.' could not retrieve project task data exit';
			return false;
		}
		
		$project_task_data = $project_task_data[$variables['task_id']];

		$project_task_name = $project_task_data['name'];
		$project_task_project_id = $project_task_data['project_id'];
		$project_task_owner = $project_task_data['owner'];
		$project_task_end_date = $project_task_data['end_date'];;
		$project_task_status = $project_task_data['status'];
		$project_task_description = $project_task_data['description'];
		if(!$project_task_data['reminder_active']) {
			$project_task_set_reminder = '';
		}
		
		// $returndata .= json_encode($project_task_data);
	}
	
	$set_edit = false;
	if($fyrk_projects->my_permission >= 50) {
		$set_edit = true;
	}
	
	// Heading
	$returndata .= '<div class="row">';
		// Backbutton
		$returndata .= '<div class="col-3 text-center">';
			$returndata .= '<img src="images/back_arrow.png" class="icon"'." onClick='editProject(\"".$variables['project_id']."\")'>";
		$returndata .= '</div>';
		
		$returndata .= '<div class="col-6 text-center font-bold font-125">';
			$returndata .= $project_task_name;
		$returndata .= '</div>';
		
		// Dummy field
		$returndata .= '<div class="col-3 text-center">';
		if($variables['task_id'] AND $fyrk_projects->my_permission > 50) {
			$returndata .= "<img src='images/trash.png' class='icon' style='width:35px;' onClick='deleteTask(\"".$variables['task_id']."\")'>";
		}
		$returndata .= '</div>';
	$returndata .= '</div>';
	
	// fields
	$returndata .= '<div class="row">';
	if($set_edit) {
		$returndata .= '<form action="#" method="POST" id="project_task_form">';
		$returndata .= '<input type="hidden" name="task_id" id="task_id" value="'.$variables['task_id'].'">';
		$returndata .= '<input type="hidden" name="project_id" id="project_id" value="'.$project_task_project_id.'">';
		$returndata .= '<div class="col-lg-6 col-12">';
			$returndata .= '<div class="inline-block" style="width:50%">';
				$returndata .= $l_task;
			$returndata .= '</div>';
			$returndata .= '<div class="inline-block text-center" style="width:50%">';
				$returndata .= '<input type="text" size="" style="width:96%;" name="name" id="name" value="'.$project_task_name.'" '."onFocus='this.select()'>";
			$returndata .= '</div>';
		$returndata .= '</div>';
	}
	
		// Task owner
		$returndata .= '<div class="col-lg-6 col-12 mt-1 mb-1">';
			$returndata .= '<div class="inline-block" style="width:50%">';
				$returndata .= $l_owner;
			$returndata .= '</div>';
			$returndata .= '<div class="inline-block text-center" style="width:50%">';
			if($set_edit) {
				$this_var = array('id' => 'owner', 'name' => 'owner', 'selected' => $project_task_owner);
				if($fyrk_projects->my_permission <= 50) $this_var['disabled'] = true;
				$returndata .= fyrk_project_user_select($this_var);
			} elseif(isset($project_users[$project_task_owner])) {
				$returndata .= $project_users[$project_task_owner]['user_name'];
			}
			$returndata .= '</div>';
		$returndata .= '</div>';	
		
		// task end date
		$returndata .= '<div class="col-lg-6 col-12 mt-1 mb-1">';
			$returndata .= '<div class="inline-block" style="width:50%">';
				$returndata .= $l_deadline;
			$returndata .= '</div>';
			$returndata .= '<div class="inline-block text-center" style="width:50%">';
			if($set_edit) {
				$returndata .= '<input type="text" name="end_date" id="end_date" size="7" class="datepickerbasic ml-1" value="'.$sop->date_to_print($project_task_end_date).'">';
			} else {
				$returndata .= $sop->date_to_print($project_task_end_date);
			}
			$returndata .= '</div>';
		$returndata .= '</div>';
	
		// task status
		$set_style = 'background-color:'.$project_task_status_types[$project_task_status]['color'].';';
		$returndata .= '<div class="col-lg-6 col-12 mt-1 mb-1" style="'.$set_style.'">';
			$returndata .= '<div class="inline-block" style="width:50%">';
				$returndata .= $l_status;
			$returndata .= '</div>';
			$returndata .= '<div class="inline-block text-center" style="width:50%">';

			if($set_edit) {
				$this_var = array('id' => 'status', 'name' => 'status', 'selected' => $project_task_status);
				$returndata .= fyrk_project_status_select($this_var);
			} else {
				$returndata .= $project_task_status_types[$project_task_status]['name'];
			}
			$returndata .= '</div>';
		$returndata .= '</div>';	
		
		// task reminder
		if($set_edit) {
		$returndata .= '<div class="col-lg-6 col-12 mt-1 mb-1" style="">';
			$returndata .= '<div class="inline-block" style="width:50%">';
				$returndata .= $l_reminder;
			$returndata .= '</div>';
			$returndata .= '<div class="inline-block text-center" style="width:50%">';
				$returndata .= '<input type="checkbox" name="reminder_active" id="reminder_active" value="1" '.$project_task_set_reminder.'>';
			$returndata .= '</div>';
		$returndata .= '</div>';	
		}
		
		// task description
		$returndata .= '<div class="col-lg-6 col-12 mt-1 mb-1 text-left" style="">';
			$returndata .= '<div class="inline-block" style="width:90%">';
				$returndata .= $l_description;
			$returndata .= '</div>';
			$returndata .= '<div class="inline-block" style="width:10%">';
			if($set_edit) {
				$returndata .= "<img src='images/Edit-512.png' class='icon' onClick='toggleEditTask()'>";
			}
			$returndata .= '</div>';
			
			$set_editable = '';
			if($set_edit) {
				$set_editable = 'contenteditable="true"';
			}
			$returndata .= '<div class="block border" style="width:100%;height:250px;overflow-y:scroll;" '.$set_editable.' id="taskDescription">';
				$returndata .= $project_task_description;
				//$returndata .= '<a href="https://www.google.com" target="_blank">Google</a>';
			$returndata .= '</div>';
		$returndata .= '</div>';

		// Save task
		$returndata .= '<div class="col-12 mt-1 mb-1 text-center" style="">';
			$returndata .= "<button type='button' onClick='saveProjectTask()'>".$l_save.'</button>';
		$returndata .= '</div>';
	
		$returndata .= '</form>';
		
		// Store pictures and links
		$returndata .= '<div class="col-12 mt-1 mb-1 text-center" style="">';
			$returndata .= '<div class="row">';
				$returndata .= '<div class="col-12 text-center">';
					$returndata .= '<input type="text" name="add_link_url" id="add_link_url" value="" placeholder="Url/link">';
					$returndata .= '<input type="text" name="add_link_name" id="add_link_name" value=""  placeholder="Link name" size="15">';
					$returndata .= "<button type='button' class='ml-2' onClick='addLinkToDescription()'>".$l_add."</button>";
				$returndata .= '</div>';
			$returndata .= '</div>';
			$returndata .= '<div class="row" id="attachments_container">';
			$returndata .= '</div>';
		$returndata .= '</div>';
		
	$returndata .= '</div>';
	
	// $returndata .= json_encode($variables);
	
	return $returndata;
}

function page_test_project_reminder_function() {
	include(LANGUAGEFILE);
	add_js_file('includes/projects/projects.js');
	$returndata = '';
	
	$returndata .= '<div class="row">';
		$returndata .= '<div class="col-12 theme_heading_colors text-center theme_heading_spacing mt-2 font-150">';
			$returndata .= 'Test project reminder function';
		$returndata .= '</div>';
		
		// Controll
		$returndata .= '<div class="col-12">';
			$this_var = array('id' => 'reminder_function_test_date', 'name' => 'date', 'date' => date('Y-m-d'));
			$returndata .= date_input_field($this_var);
			$returndata .= "<button type='button' class='ml-3' onClick='testProjectReminderFunction()'>".ucfirst($l_test)."</button>";
		$returndata .= '</div>';
		
		// result
		$returndata .= '<div class="col-lg-10 offset-1 col-12 mt-4 border theme_border pt-1 pb-1" style="height:250px;overflow-y:scroll" id="reminder_function_test_result_container">';
		$returndata .= '</div>';
	$returndata .= '</div>';
	
	return $returndata;
}

function test_project_reminder_function($variables) {
	/*
		$variables['date']
	*/
	include_once('cronjob/auto_functions.php');
	$returndata = '';
	
	$variables['return_example'] = true;
	
	$result = send_project_task_reminder($variables);
	
	foreach($result as $this_reminder_data) {
		$returndata .= '<div class="row border-bottom pt-2 pb-2">';
			$returndata .= '<div class="col-3">';
				$returndata .= $this_reminder_data['to'];
			$returndata .= '</div>';
			$returndata .= '<div class="col-9">';
				$returndata .= $this_reminder_data['subject'];
			$returndata .= '</div>';
			$returndata .= '<div class="col-12">';
				$returndata .= strip_tags(substr($this_reminder_data['message'], 0, 100));
			$returndata .= '</div>';
			if($_SESSION['id'] == 1) {
				$returndata .= '<div class="col-12">';
					// $returndata .= json_encode($this_reminder_data['task_data']);
				$returndata .= '</div>';
			}
		$returndata .= '</div>';
	}
	
	return $returndata;
	
}

?>