inventory/inventory/templates/worklog.html
2025-07-15 14:26:08 -05:00

253 lines
9.8 KiB
HTML

<!-- templates/worklog.html -->
{% extends "layout.html" %}
{% block title %}{{ title }}{% endblock %}
{% block precontent %}
{% set saveLogic %}
e.preventDefault();
const updateTextareas = Array.from(document.querySelectorAll("textarea[name^='editor']"));
const updates = updateTextareas
.map(el => {
const content = el.value.trim();
if (!content) return null;
const id = el.dataset.noteId;
return id ? { id, content } : { content };
})
.filter(u => u !== null);
const payload = {
start_time: document.querySelector("input[name='start']").value,
end_time: document.querySelector("input[name='end']").value,
complete: document.querySelector("input[name='complete']").checked,
analysis: document.querySelector("input[name='analysis']").checked,
followup: document.querySelector("input[name='followup']").checked,
contact_id: parseInt(document.querySelector("select[name='contact']").value) || null,
work_item_id: parseInt(document.querySelector("select[name='item']").value) || null,
updates: updates
};
try {
const id = document.querySelector("#logId").value;
const isEdit = id && id !== "None";
const endpoint = isEdit ? `/api/worklog/${id}` : "/api/worklog";
const method = isEdit ? "PUT" : "POST";
const response = await fetch(endpoint, {
method,
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
const result = await response.json();
if (result.success) {
localStorage.setItem("toastMessage", JSON.stringify({
message: isEdit ? "Work Log entry updated!" : "Work Log entry created!",
type: "success"
}));
window.location.href = `/worklog/${result.id}`;
} else {
renderToast({ message: `Error: ${result.error}`, type: "danger" });
}
} catch (err) {
console.error(err)
renderToast({ message: `Error: ${err}`, type: "danger" });
}
{% endset %}
{% set deleteLogic %}
const id = document.querySelector("#logId").value;
if (!id || id === "None") {
renderToast({ message: "No item ID found to delete.", type: "danger" });
return;
}
if (!confirm("Are you sure you want to delete this work log entry? This action cannot be undone.")) {
return;
}
try {
const response = await fetch(`/api/worklog/${id}`, {
method: "DELETE"
});
const result = await response.json();
if (result.success) {
localStorage.setItem("toastMessage", JSON.stringify({
message: "Work log entry deleted.",
type: "success"
}));
window.location.href = "/worklog";
} else {
renderToast({ message: `Error: ${result.error}`, type: "danger" });
}
} catch (err) {
console.log(err);
renderToast({ message: `Error: ${err}`, type: "danger" });
}
{% endset %}
{% set iconBar %}
<div class="btn-group">
{{ buttons.render_button(
id='save',
icon='floppy',
logic=saveLogic,
style='outline-primary rounded-start'
) }}
{% if log.id != None %}
{{ buttons.render_button(
id='delete',
icon='trash',
style='outline-danger rounded-end',
logic=deleteLogic
) }}
{% endif %}
</div>
{% endset %}
{{ toolbars.render_toolbar(
id='newWorklog',
left=breadcrumbs.breadcrumb_header(
breadcrumbs=[
{'label': 'Work Log', 'url': url_for('main.list_worklog')}
],
title=title
),
right=iconBar
) }}
{% if log.complete %}
<div class="alert alert-success">
This work item is complete. You cannot make any further changes.
</div>
{% endif %}
{% endblock %}
{% block content %}
<input type="hidden" id="logId" value="{{ log.id }}">
<div class="container">
<div class="row">
<div class="col-6">
<label for="start" class="form-label">Start Timestamp</label>
<input type="date" class="form-control" name="start" placeholder="-"
value="{{ log.start_time.date().isoformat() if log.start_time }}"{% if log.complete %} disabled{% endif %}>
</div>
<div class="col-6">
<label for="end" class="form-label">End Timestamp</label>
<input type="date" class="form-control" name="end" placeholder="-"
value="{{ log.end_time.date().isoformat() if log.end_time }}"{% if log.complete %} disabled{% endif %}>
</div>
</div>
<div class="row">
<div class="col-4">
<label for="contact" class="form-label">
Contact
{% if log.contact_id %}
{{ links.entry_link('user', log.contact_id) }}
{% endif %}
</label>
<select class="form-select" name="contact" id="contact"{% if log.complete %} disabled{% endif %}>
<option value="">-</option>
{% for contact in users %}
<option value="{{ contact.id }}" {% if contact.id==log.contact_id %} selected{% endif %}>{{
contact.full_name }}
</option>
{% endfor %}
</select>
</div>
<div class="col-4">
<label for="item" class="form-label">
Work Item
{% if log.work_item_id %}
{{ links.entry_link('inventory_item', log.work_item_id) }}
{% endif %}
</label>
<select id="item" name="item" class="form-select"{% if log.complete %} disabled{% endif %}>
<option value="">-</option>
{% for item in items %}
<option value="{{ item.id }}" {% if item.id==log.work_item_id %} selected{% endif %}>{{ item.identifier
}}</option>
{% endfor %}
</select>
</div>
<div class="col-4">
<div class="row">
<div class="col">
<input type="checkbox" id="complete" class="form-check-input" name="complete" {% if log.complete %}
checked{% endif %}>
<label for="complete" class="form-check-label">
Complete?
</label>
</div>
</div>
<div class="row">
<div class="col">
<input type="checkbox" id="followup" class="form-check-input" name="followup" {% if log.followup %}
checked{% endif %}{% if log.complete %} disabled{% endif %}>
<label for="followup" class="form-check-label">
Follow Up?
</label>
</div>
</div>
<div class="row">
<div class="col">
<input type="checkbox" id="analysis" class="form-check-input" name="analysis" {% if log.analysis %}
checked{% endif %}{% if log.complete %} disabled{% endif %}>
<label for="analysis" class="form-check-label">
Quick Analysis?
</label>
</div>
</div>
</div>
</div>
<div class="container" id="updates-container">
<div class="row">
<div class="col-11">
<label class="form-label">Updates</label>
</div>
<div class="col">
{#
<button class="btn btn-primary mb-3" id="addUpdateButton"{% if log.complete %} disabled{% endif %}>
{{ icons.render_icon('plus-lg', 16) }}
</button>
#}
{% set addUpdateLogic %}
function formatDate(date) {
const pad = (n) => String(n).padStart(2, '0');
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} `
+ `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
}
const template = document.getElementById("editor-template");
const newEditor = EditorWidget.createEditorWidget(template, EditorWidget.createTempId("new"), formatDate(new Date()));
const updatesContainer = document.getElementById("updates-container");
updatesContainer.appendChild(newEditor);
{% endset %}
{{ buttons.render_button(
id='addUpdate',
icon='plus-lg',
logic=addUpdateLogic
) }}
</div>
</div>
{% for update in log.updates %}
{{ editor.render_editor(
id = update.id,
title = update.timestamp.strftime('%Y-%m-%d %H:%M:%S'),
mode = 'view',
content = update.content,
enabled = not log.complete
) }}
{% endfor %}
<template id="editor-template">
{{ editor.render_editor('__ID__', '__TIMESTAMP__', 'edit', '') }}
</template>
</div>
</div>
{% endblock %}