<html>
    <head>
    <title>Ansicht Aufgaben</title>
    
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
     <meta name="author" content="Stefan Pichel">
	<link rel="stylesheet" type="text/css" href="css/bestformat.css?v=2.0.11">
	<link rel="stylesheet" href="php/contenthandler.php?module=admin&command=getmobilecss" />
 	<link rel="stylesheet" href="php/3rdparty/orderstyle/jquery.mobile.icons.min.css" />
  
   <link rel="stylesheet" href="php/3rdparty/jquery.mobile-1.4.0.min.css" type="text/css" />
   <script src="php/3rdparty/jquery-1.11.3.min.js"></script> 
   <script src="php/3rdparty/jquery.mobile-1.4.5.min.js"></script>

   <script src="utilities.js?v=2.0.11"></script>

    <link rel="stylesheet" href="php/3rdparty/jqueryui1-11-4/jquery-ui.min.css" />
	<script src="php/3rdparty/jqueryui1-11-4/jquery-ui.min.js"></script>
	<script src="php/3rdparty/jqueryui1-11-4/jquery-ui-i18n.min.js"></script>

	<link rel="stylesheet" href="php/3rdparty/jquerymobiledatepicker/jquery.mobile.datepicker.css" />
	<script src="php/3rdparty/jquerymobiledatepicker/jquery.mobile.datepicker.js"></script>
	
</head>

<body>


<script>

var TASKS_UPDATE = ["Aktualisieren","Update","Actualizar"];
var TASK_OVERVIEW_TXT = ["Übersicht","Overview","Resumen"];
var TASKS_NEW_TASK = ["Neue Aufgabe","New task","Tarea nueva"];
var TASK_CHANGE = ["Ändern","Change","Modificar"];
var TASK_DELETE = ["Löschen","Delete","Removar"];
var TASKS_CREATE = ["Erstellen","Create","Crear"];
var TASKS_SUMMARY = ["Thema","Topic","Tema"];
var TASKS_DESCRIPTION = ["Beschreibung","Description","Descripcion"];
var TASK_PRIO = ["Priorität","Priroty","Prio"];
var TASK_PRIO_LEVELS = [
	["1 - sehr wichtig","2 - wichtig", "3 - normal","4 - unwichtig","5 - sehr unwichtig"],
	["1 - very important","2 - important","3 - normal","4 - unimportant","5 - irrelevant"],
	["1 - muy importante","2 - importante","3 - normal", "4 - sin importancia","5 - irrelevante"]
];
var TASK_OWNER = ["Verantwortlich","Owner","Responsible"];
var TASK_SUBMITDATE = ["Erstellung","Creation","Creado"];
var TASK_LASTDATE = ["Letzte Änderung","Last change","Modificado"];
var TASK_SUBMITTER = ["Ersteller","Creator","Creador"];

var TASK_STATUS = ["Status","Status","Status"];
var TASK_STATUS_LEVELS = [
	["Neu","In Arbeit","Erledigt"],
	["New","Open","Closed"],
	["Nuevo","Abuerto","Completado"]
];
var TASKS_HISTORY = ["Historie","History","Historia"];
var TASKS_DATE = ["Datum","Date","Fecha"];
var TASKS_USER = ["Benutzer","User","Usario"];
var TASKS_ACTION = ["Aktion","Action","Evento"];
var TASKS_FIELDS = ["Felder","Fields","Propriedades"];

var TASKS_FILTER = ["Filter","Filter","Filtro"];
var TASK_FILTER_ALL = ["Zeige alle","Show all","Mostrar toto"];
var TASK_FILTER_OWN = ["Nur eigene","Only own items","Solo para mi"];
var TASK_FILTER_ALL_NON_COMPLETED = ["Alle nicht erledigten","All non completed","Todo las tareas que no son completadas"];
var TASK_FILTER_OWN_NON_COMPLETED = ["Nur eigene nicht erledigte","Only own incomplete tasks","Tareas no complatedas para mi"];
var TASK_FILTER_SYSTEM_AND_OPEN = ["Nicht erledigt mit Verantwortung: System","Not completed with responsibility: System","No completada, para System"];

var lang = 0;
var activeusers = [];
var taskallassign = 0;
var curuser = null;
var selectedfilter = 1;

function setLanguage(language) {
	lang = language;
	
	$("#overviewtxt").html(TASK_OVERVIEW_TXT[lang]);
	
	initNewTaskArea();
	initFilter();
	gettasks();
}

function initFilter() {
	var txt = "";
	var values = [
		{id:0, text:TASK_FILTER_ALL[lang]},
		{id:1, text:TASK_FILTER_OWN[lang]},
		{id:2, text:TASK_FILTER_ALL_NON_COMPLETED[lang]},
		{id:3, text:TASK_FILTER_OWN_NON_COMPLETED[lang]},
		{id:4, text:TASK_FILTER_SYSTEM_AND_OPEN[lang]}
	];
	txt += createLabelWithOption("","taskoverviewfilter","",TASKS_FILTER[lang],values,selectedfilter);
	$("#filterarea").html(txt);
	$("#filterarea").trigger("create");
	bindFilterChange();
}

function bindFilterChange() {
	$("#taskoverviewfilter").off("change").on("change", function (e) {
		e.stopImmediatePropagation();
		e.preventDefault();
		selectedfilter = $("#taskoverviewfilter").find(":selected").val();
		gettasks();
		initFilter();
	});
}

function getGeneralConfigItems() {
		doAjax("GET", "php/contenthandler.php?module=admin&command=getGeneralConfigItemsAndUsers", null, insertGenConfigStartRest, "Fehler Konfigurationsdaten");
	}
	
function insertGenConfigStartRest(configResult) {
	if (configResult.status == "OK") {
		var values = configResult.msg;
		activeusers = values.activeusers;
		setLanguage(values.userlanguage);
	} else {
		setTimeout(function(){document.location.href = "index.html";},250); // not logged in
	}
}

function createNewTaskArea(id,name) {
	var collapsiblePart = '<div data-role="collapsible" id="' + id + '"';
	
	collapsiblePart += ' data-theme="c" data-content-theme="c">';
	
	collapsiblePart += '<h3>'+ toHtml(name) + '</h3>';
	collapsiblePart += '<form>';
	
	collapsiblePart += createLabelWithTextField("userlabel_summary_new",TASKS_SUMMARY[lang],TASKS_SUMMARY[lang]);
	collapsiblePart += createLabelWithTextArea("userlabel_description_new",TASKS_DESCRIPTION[lang]);
	collapsiblePart += createLabelWithOption("","userlabel_prio_new","",TASK_PRIO[lang],getPrioArray(),3);
	collapsiblePart += createLabelWithOption("","userlabel_owner_new","",TASK_OWNER[lang],createTaskOwnerList(),curuser.id);
	
	collapsiblePart += '<button data-theme="f" data-icon="plus" id="CreateNewTask">' + TASKS_CREATE[lang] + '</button>';
	
	collapsiblePart += '</form>';
	collapsiblePart += '</div>';
	return collapsiblePart;
}

function getPrioArray() {
	var rets = [];
	for (var i=0;i<5;i++) {
		rets[rets.length] = {id: (i+1), text: TASK_PRIO_LEVELS[lang][i] };
	}
	return rets;
}

function getStatusArray() {
	var rets = [];
	for (var i=0;i<3;i++) {
		rets[rets.length] = {id: (i+1), text: TASK_STATUS_LEVELS[lang][i] };
	}
	return rets;
}

function createTaskOwnerList() {
	var allValues = [];
	
	if (taskallassign == 0) {
		allValues[0] = {
			id:curuser.id,
			text: curuser.username
		};
	} else {
		allValues[0] = {
			id:0,
			text: "System"
		};
		for (var i=0;i<activeusers.length;i++) {
			var anActiveUser = activeusers[i];
			allValues[allValues.length] = {
				id:anActiveUser.id,
				text:anActiveUser.username
			};
		}
	}
	return allValues;
}

function initNewTaskArea() {
	doAjax("GET", "php/contenthandler.php?module=tasks&command=gettaskinfoforuser", null, continueNewTaskArea, null);
}

function continueNewTaskArea(answer) {
	if (answer.status == "OK") {
		taskallassign = answer.msg.taskallassign;
		curuser = answer.msg.curuser;
		var txt = createNewTaskArea("newtask",TASKS_NEW_TASK[lang]);
		$("#newtaskarea").html(txt);
		$("#newtaskarea").trigger("create");
		bindNewTaskBtn();
	}
}

function bindNewTaskBtn() {
	$("#CreateNewTask").off("click").on("click", function (e) {
		e.stopImmediatePropagation();
		e.preventDefault();
		
		var summary = $("#userlabel_summary_new").val();
		var description = $("#userlabel_description_new").val();
		var prio = $("#userlabel_prio_new").find(":selected").val();
		var owner = $("#userlabel_owner_new").find(":selected").val();
		
		var data = {
			summary: summary,
			description: description,
			prio: prio,
			owner: owner
		};
		doAjax("POST", "php/contenthandler.php?module=tasks&command=createtask", data, handleNewTask, null);
	});
}

function handleNewTask(answer) {
	if (answer.status != "OK") {
		alert("Fehler: " + answer.msg);
	} else {
		initNewTaskArea();
		gettasks();
	}
}

function gettasks() {
	doAjax("GET", "php/contenthandler.php?module=tasks&command=gettasks", {filter: selectedfilter}, handleGetTasks, null);
}

function handleGetTasks(answer) {
	if (answer.status != "OK") {
		alert("Fehler: " + answer.msg);
		return;
	}
	
	var usermaydelete = answer.usermaydelete;
	
	var txt = "";
	var tasks = answer.msg;
	tasks.forEach(function (task,index) {
		txt +=  '<div id="taskitemcollapsible_' + task.id + '" class="taskitemcollapsible" data-role="collapsible" ';
		txt += ' data-theme="e" data-content-theme="e">';
	
		txt += '<h3>'+ toHtml(task.summary) + ' (' + TASK_STATUS_LEVELS[lang][parseInt(task.status)-1] + ')</h3>';
		txt += '<form>';
		txt += '<p><b>' + TASK_SUBMITDATE[lang] + ":</b> " + task.submitdate;
		txt += '<p><b>' + TASK_LASTDATE[lang] + ":</b> <span id='lastdate_" + task.id + "'>" + task.lastdate + "</span>";
		txt += '<p><b>' + TASK_SUBMITTER[lang] + ":</b> " + task.submittername;
		
		txt += createLabelWithOption("",task.id,"taskitemstatus",TASK_STATUS[lang],getStatusArray(),task.status);
		txt += createLabelWithOption("",task.id,"taskitemowner",TASK_OWNER[lang],createTaskOwnerList(),task.owner);
		txt += createLabelWithOption("",task.id,"taskitemprio",TASK_PRIO[lang],getPrioArray(),task.prio);
		txt += createLabelWithTextFieldWithValue("taskitemsummary_" + task.id,TASKS_SUMMARY[lang],toHtml(task.summary));
		txt += createLabelWithTextAreaWithValue("taskitemdescription_" + task.id,TASKS_DESCRIPTION[lang],toHtml(task.description));
		
		txt += '<p><h4>' + TASKS_HISTORY[lang] + '</h4>';
		txt += '<div id="taskhist_' + task.id + '">';
		txt += '  <img src="php/3rdparty/images/ajax-loader.gif" />';
		txt += '</div>';
		
		txt += '<button data-theme="f" data-icon="check" class="changetask" id="Changeask_' + task.id + '">' + TASK_CHANGE[lang] + '</button>';
		if (usermaydelete == 1) {
			txt += '<button data-theme="d" data-icon="delete" class="deletetask" id="deletetask_' + task.id + '">' + TASK_DELETE[lang] + '</button>';
		}
		txt += '</form>';
		txt += "</div>";
	});
	
	txt += '<button data-theme="f" data-icon="check" id="update">' + TASKS_UPDATE[lang] + '</button>';
	
	$("#result").html(txt);
	$("#result").trigger("create");
	bindChangeBtn();
}

function bindChangeBtn() {
	$(".changetask").off("click").on("click", function (e) {
		e.stopImmediatePropagation();
		e.preventDefault();
		var id = this.id.split("_")[1];
		
		var status = $("#taskitemstatus_" + id).find(":selected").val();
		var owner = $("#taskitemowner_" + id).find(":selected").val();
		var summary = $("#taskitemsummary_" + id).val();
		var description = $("#taskitemdescription_" + id).val();
		var prio = $("#taskitemprio_" + id).val();
		
		var data = {
			id: id,
			status: status,
			owner: owner,
			prio: prio,
			summary: summary,
			description: description
		};
		doAjax("POST", "php/contenthandler.php?module=tasks&command=changetask", data, handleChangeTask, null);
	});
	
	$(".taskitemcollapsible").off("collapsibleexpand").on("collapsibleexpand", function (e) {
		var id = this.id.split("_")[1];
		requestHistForTask(id);
	});
	
	$(".deletetask").off("click").on("click", function (e) {
		e.stopImmediatePropagation();
		e.preventDefault();
		var id = this.id.split("_")[1];
		doAjax("POST", "php/contenthandler.php?module=tasks&command=delete", {id:id}, handleDeleteTask, null);
	});
	
	$("#update").off("click").on("click", function (e) {
		e.stopImmediatePropagation();
		e.preventDefault();
		gettasks();
	});
}

function requestHistForTask(taskid) {
	doAjax("GET", "php/contenthandler.php?module=tasks&command=gethistory", {id:taskid}, handleGetHistory, null);
}

function handleDeleteTask(answer) {
	if (answer.status == "OK") {
		gettasks();
	} else {
		alert("Fehler: " + answer.msg);
	}
}

function handleChangeTask(answer) {
	if (answer.status != "OK") {
		alert("Fehler: " + answer.msg);
	} else {
		var lastChange = answer.lastchange;
		$("#lastdate_" + answer.taskid).html(lastChange);
		requestHistForTask(answer.taskid);
	}
}

function translateAction(action) {
	if (action == 0) {
		return TASKS_CREATE[lang];
	} else if (action == 1) {
		return TASK_CHANGE[lang];
	} else if (action == 2) {
		return TASK_DELETE[lang];
	} else {
		return "?";
	}
}

function translateFields(fields) {
	if (fields == "") {
		return "";
	}
	var farr = fields.split(",");
	var txt = [];
	farr.forEach(function(f,i) {
		var fparts = f.split(":",2);
		switch(fparts[0]) {
			case 'o':
				txt[txt.length] = TASK_OWNER[lang] + "->" + toHtml(fparts[1]);
				break;
			case 's':
				txt[txt.length] = TASK_STATUS[lang] + "->" + TASK_STATUS_LEVELS[lang][parseInt(fparts[1])-1];
				break;
			case 'p':
				txt[txt.length] = TASK_PRIO[lang] + "->" + TASK_PRIO_LEVELS[lang][parseInt(fparts[1])-1];
				break;
			case 't':
				txt[txt.length] = TASKS_SUMMARY[lang];
				break;
			case 'd':
				txt[txt.length] = TASKS_DESCRIPTION[lang];
				break;
			default:
		}
	});
	return txt.join(", ");
}

function handleGetHistory(answer) {
	if (answer.status == "OK") {
		var entries = answer.msg;
		if (entries.length > 0) {
			var taskid = entries[0]["taskid"];
			var txt = "<table class='viewtable' >";
			txt += "<tr><th>" + TASKS_DATE[lang] + "<th>" + TASKS_USER[lang] + "<th>" + TASKS_ACTION[lang] + "<th>" + TASKS_FIELDS[lang] + "</tr>";
			entries.forEach(function(entry,i) {
				txt += "<tr>";
				txt += "<td>" + entry.date;
				txt += "<td>" + entry.username;
				txt += "<td>" + translateAction(entry.action);
				txt += "<td>" + translateFields(entry.fields);
				txt += "</tr>";
			});
			txt += "</table>";
			$("#taskhist_" + taskid).html(txt);
		}
	}
}

$(document).on("pageinit", "#time-page", function () {
	initializeMainMenu("#modulemenu");
	hideMenu();
	$.ajaxSetup({ cache: false });
	getGeneralConfigItems();
});


</script>
<div data-role="page" id="time-page" data-theme="c">
	<div data-role="panel" id="modulepanel" data-position="right" data-display="overlay">
        <ul data-role="listview" id="modulemenu" data-divider-theme="a" data-inset="true">
            <li data-theme="b" data-role="heading">Hauptmenü</li>
        </ul>
    </div><!-- /panel -->
    <div data-role="header" data-theme="b" data-position="fixed" id="theheader" style="background-color:black;">
         <h1><span id="timetitletxt">Aufgaben</span> <img src="img/connection.png" class="connectionstatus" style="display:none;" /> <img src="img/printerstatus.png" class="printerstatus" style="display:none;" /> <img src="img/tsestatus.png" class="tsestatus" style="display:none;" /> <img src="img/tasksstatus.png" class="tasksstatus" style="display:none;" /></h1>
		 <div data-type="horizontal" style="top:0px;position:absolute;float:right;z-index:10;display:inline;" align="right" class="ui-btn-right"> 
			<a href="#" data-role="button" data-icon="arrow-d" data-ajax="false" id="menuswitch"><span id="modulstxt2">Hauptmenü</span></a> 
		 </div>
    </div>
    <div data-role="content" id="fullcontent">
	    
	    <div id="overviewarea">
		    <div data-role="collapsible" id="overviewcollapsibke" data-theme="c" data-content-theme="c">
	
			    <h3><span id='overviewtxt'>Übersicht</span></h3>
				<div id="filterarea"></div>
			    
			    <div id="result"></div>
		    </div>
	    </div>
	    
	    
	    <div id="newtaskarea">
		    <img src="php/3rdparty/images/ajax-loader.gif" />
	    </div>
    </div>   
   
    <div data-role="footer" data-theme="b" id="thefooterr" style="background-color:black;"> 
		<div class="ui-grid-a">
			<div class="ui-block-a userinfo" id="loggedinuser"></div>
			<div class="ui-block-b grid_right" id="versioninfo"></div>
		</div><!-- /grid-a -->
	</div> <!--  footer  -->

</div> <!--  first page -->

</body>
</html>