2022-12-10 05:57:57 +00:00
|
|
|
<script setup lang="ts">
|
|
|
|
import DatePicker from "~~/components/Form/DatePicker.vue";
|
2022-12-30 01:19:15 +00:00
|
|
|
import { StatsFormat } from "~~/components/global/StatCard/types";
|
2023-01-22 06:15:23 +00:00
|
|
|
import { ItemOut, MaintenanceEntry } from "~~/lib/api/types/data-contracts";
|
2022-12-10 05:57:57 +00:00
|
|
|
|
|
|
|
const props = defineProps<{
|
|
|
|
item: ItemOut;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const api = useUserApi();
|
|
|
|
const toast = useNotifier();
|
|
|
|
|
2023-02-27 03:42:23 +00:00
|
|
|
const scheduled = ref(true);
|
|
|
|
|
|
|
|
watch(
|
|
|
|
() => scheduled.value,
|
|
|
|
() => {
|
|
|
|
refreshLog();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-12-10 05:57:57 +00:00
|
|
|
const { data: log, refresh: refreshLog } = useAsyncData(async () => {
|
2023-02-27 03:42:23 +00:00
|
|
|
const { data } = await api.items.maintenance.getLog(props.item.id, {
|
|
|
|
scheduled: scheduled.value,
|
|
|
|
completed: !scheduled.value,
|
|
|
|
});
|
2022-12-10 05:57:57 +00:00
|
|
|
return data;
|
|
|
|
});
|
|
|
|
|
2022-12-30 01:19:15 +00:00
|
|
|
const count = computed(() => {
|
|
|
|
if (!log.value) return 0;
|
|
|
|
return log.value.entries.length;
|
|
|
|
});
|
2022-12-10 05:57:57 +00:00
|
|
|
const stats = computed(() => {
|
|
|
|
if (!log.value) return [];
|
|
|
|
|
|
|
|
return [
|
2022-12-30 01:19:15 +00:00
|
|
|
{
|
|
|
|
id: "count",
|
|
|
|
title: "Total Entries",
|
|
|
|
value: count.value || 0,
|
|
|
|
type: "number" as StatsFormat,
|
|
|
|
},
|
2022-12-10 05:57:57 +00:00
|
|
|
{
|
|
|
|
id: "total",
|
|
|
|
title: "Total Cost",
|
2022-12-30 01:19:15 +00:00
|
|
|
value: log.value.costTotal || 0,
|
|
|
|
type: "currency" as StatsFormat,
|
2022-12-10 05:57:57 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "average",
|
|
|
|
title: "Monthly Average",
|
2022-12-30 01:19:15 +00:00
|
|
|
value: log.value.costAverage || 0,
|
|
|
|
type: "currency" as StatsFormat,
|
2022-12-10 05:57:57 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
});
|
|
|
|
|
|
|
|
const entry = reactive({
|
2023-01-22 06:15:23 +00:00
|
|
|
id: null as string | null,
|
2022-12-10 05:57:57 +00:00
|
|
|
modal: false,
|
|
|
|
name: "",
|
2023-02-27 03:42:23 +00:00
|
|
|
completedDate: null as Date | null,
|
|
|
|
scheduledDate: null as Date | null,
|
2022-12-10 05:57:57 +00:00
|
|
|
description: "",
|
|
|
|
cost: "",
|
|
|
|
});
|
|
|
|
|
|
|
|
function newEntry() {
|
|
|
|
entry.modal = true;
|
|
|
|
}
|
|
|
|
|
2023-01-22 06:15:23 +00:00
|
|
|
function resetEntry() {
|
2023-07-31 16:22:08 +00:00
|
|
|
console.log("Resetting entry");
|
2023-01-22 06:15:23 +00:00
|
|
|
entry.id = null;
|
|
|
|
entry.name = "";
|
2023-02-27 03:42:23 +00:00
|
|
|
entry.completedDate = null;
|
|
|
|
entry.scheduledDate = null;
|
2023-01-22 06:15:23 +00:00
|
|
|
entry.description = "";
|
|
|
|
entry.cost = "";
|
|
|
|
}
|
|
|
|
|
2023-07-31 16:22:08 +00:00
|
|
|
watch(
|
|
|
|
() => entry.modal,
|
|
|
|
(v, pv) => {
|
|
|
|
if (pv === true && v === false) {
|
|
|
|
resetEntry();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Calls either edit or create depending on entry.id being set
|
|
|
|
async function dispatchFormSubmit() {
|
2023-01-22 06:15:23 +00:00
|
|
|
if (entry.id) {
|
2023-07-31 16:22:08 +00:00
|
|
|
await editEntry();
|
2023-01-22 06:15:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-31 16:22:08 +00:00
|
|
|
await createEntry();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createEntry() {
|
2022-12-10 05:57:57 +00:00
|
|
|
const { error } = await api.items.maintenance.create(props.item.id, {
|
|
|
|
name: entry.name,
|
2023-02-27 03:42:23 +00:00
|
|
|
completedDate: entry.completedDate ?? "",
|
|
|
|
scheduledDate: entry.scheduledDate ?? "",
|
2022-12-10 05:57:57 +00:00
|
|
|
description: entry.description,
|
2023-02-27 03:42:23 +00:00
|
|
|
cost: parseFloat(entry.cost) ? entry.cost : "0",
|
2022-12-10 05:57:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
toast.error("Failed to create entry");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.modal = false;
|
|
|
|
|
|
|
|
refreshLog();
|
2023-01-22 06:15:23 +00:00
|
|
|
resetEntry();
|
2022-12-10 05:57:57 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 01:19:15 +00:00
|
|
|
const confirm = useConfirm();
|
|
|
|
|
2022-12-10 05:57:57 +00:00
|
|
|
async function deleteEntry(id: string) {
|
2022-12-30 01:19:15 +00:00
|
|
|
const result = await confirm.open("Are you sure you want to delete this entry?");
|
|
|
|
if (result.isCanceled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-10 05:57:57 +00:00
|
|
|
const { error } = await api.items.maintenance.delete(props.item.id, id);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
toast.error("Failed to delete entry");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
refreshLog();
|
|
|
|
}
|
2023-01-22 06:15:23 +00:00
|
|
|
|
|
|
|
function openEditDialog(e: MaintenanceEntry) {
|
|
|
|
entry.id = e.id;
|
|
|
|
entry.name = e.name;
|
2023-02-27 03:42:23 +00:00
|
|
|
entry.completedDate = new Date(e.completedDate);
|
|
|
|
entry.scheduledDate = new Date(e.scheduledDate);
|
2023-01-22 06:15:23 +00:00
|
|
|
entry.description = e.description;
|
|
|
|
entry.cost = e.cost;
|
2023-07-31 16:22:08 +00:00
|
|
|
entry.modal = true;
|
2023-01-22 06:15:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function editEntry() {
|
|
|
|
if (!entry.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { error } = await api.items.maintenance.update(props.item.id, entry.id, {
|
|
|
|
name: entry.name,
|
2023-02-27 03:42:23 +00:00
|
|
|
completedDate: entry.completedDate ?? "null",
|
|
|
|
scheduledDate: entry.scheduledDate ?? "null",
|
2023-01-22 06:15:23 +00:00
|
|
|
description: entry.description,
|
|
|
|
cost: entry.cost,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
toast.error("Failed to update entry");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.modal = false;
|
|
|
|
refreshLog();
|
|
|
|
}
|
2022-12-10 05:57:57 +00:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<div v-if="log">
|
|
|
|
<BaseModal v-model="entry.modal">
|
2023-02-27 03:42:23 +00:00
|
|
|
<template #title>
|
|
|
|
{{ entry.id ? "Edit Entry" : "New Entry" }}
|
|
|
|
</template>
|
2023-07-31 16:22:08 +00:00
|
|
|
<form @submit.prevent="dispatchFormSubmit">
|
2022-12-10 05:57:57 +00:00
|
|
|
<FormTextField v-model="entry.name" autofocus label="Entry Name" />
|
2023-02-27 03:42:23 +00:00
|
|
|
<DatePicker v-model="entry.completedDate" label="Completed Date" />
|
|
|
|
<DatePicker v-model="entry.scheduledDate" label="Scheduled Date" />
|
2022-12-10 05:57:57 +00:00
|
|
|
<FormTextArea v-model="entry.description" label="Notes" />
|
|
|
|
<FormTextField v-model="entry.cost" autofocus label="Cost" />
|
|
|
|
<div class="py-2 flex justify-end">
|
2023-01-22 06:15:23 +00:00
|
|
|
<BaseButton type="submit" class="ml-2 mt-2">
|
2022-12-10 05:57:57 +00:00
|
|
|
<template #icon>
|
|
|
|
<Icon name="mdi-post" />
|
|
|
|
</template>
|
2023-01-22 06:15:23 +00:00
|
|
|
{{ entry.id ? "Update" : "Create" }}
|
2022-12-10 05:57:57 +00:00
|
|
|
</BaseButton>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</BaseModal>
|
|
|
|
|
2022-12-30 01:19:15 +00:00
|
|
|
<section class="space-y-6">
|
|
|
|
<div class="grid grid-cols-1 md:grid-cols-3 gap-6">
|
|
|
|
<StatCard
|
|
|
|
v-for="stat in stats"
|
|
|
|
:key="stat.id"
|
|
|
|
class="stats block shadow-xl border-l-primary"
|
|
|
|
:title="stat.title"
|
|
|
|
:value="stat.value"
|
|
|
|
:type="stat.type"
|
|
|
|
/>
|
|
|
|
</div>
|
2023-01-19 05:44:06 +00:00
|
|
|
<div class="flex">
|
2023-02-27 03:42:23 +00:00
|
|
|
<div class="btn-group">
|
|
|
|
<button class="btn btn-sm" :class="`${scheduled ? 'btn-active' : ''}`" @click="scheduled = true">
|
|
|
|
Scheduled
|
|
|
|
</button>
|
|
|
|
<button class="btn btn-sm" :class="`${scheduled ? '' : 'btn-active'}`" @click="scheduled = false">
|
|
|
|
Completed
|
|
|
|
</button>
|
|
|
|
</div>
|
2023-01-19 05:44:06 +00:00
|
|
|
<BaseButton class="ml-auto" size="sm" @click="newEntry()">
|
|
|
|
<template #icon>
|
2023-02-27 03:42:23 +00:00
|
|
|
<Icon name="mdi-plus" />
|
2023-01-19 05:44:06 +00:00
|
|
|
</template>
|
2023-02-27 03:42:23 +00:00
|
|
|
New
|
2023-01-19 05:44:06 +00:00
|
|
|
</BaseButton>
|
|
|
|
</div>
|
2022-12-30 01:19:15 +00:00
|
|
|
<div class="container space-y-6">
|
2022-12-10 05:57:57 +00:00
|
|
|
<BaseCard v-for="e in log.entries" :key="e.id">
|
|
|
|
<BaseSectionHeader class="p-6 border-b border-b-gray-300">
|
|
|
|
<span class="text-base-content">
|
|
|
|
{{ e.name }}
|
|
|
|
</span>
|
|
|
|
<template #description>
|
2023-02-27 03:42:23 +00:00
|
|
|
<div class="flex flex-wrap gap-2">
|
|
|
|
<div v-if="validDate(e.completedDate)" class="badge p-3">
|
|
|
|
<Icon name="mdi-check" class="mr-2" />
|
2023-02-28 04:52:56 +00:00
|
|
|
<DateTime :date="e.completedDate" format="human" datetime-type="date" />
|
2023-02-27 03:42:23 +00:00
|
|
|
</div>
|
|
|
|
<div v-else-if="validDate(e.scheduledDate)" class="badge p-3">
|
2022-12-10 05:57:57 +00:00
|
|
|
<Icon name="mdi-calendar" class="mr-2" />
|
2023-02-28 04:52:56 +00:00
|
|
|
<DateTime :date="e.scheduledDate" format="human" datetime-type="date" />
|
2022-12-10 05:57:57 +00:00
|
|
|
</div>
|
|
|
|
<div class="tooltip tooltip-primary" data-tip="Cost">
|
|
|
|
<div class="badge badge-primary p-3">
|
|
|
|
<Currency :amount="e.cost" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
</BaseSectionHeader>
|
|
|
|
<div class="p-6">
|
|
|
|
<Markdown :source="e.description" />
|
|
|
|
</div>
|
2023-01-22 06:15:23 +00:00
|
|
|
<div class="flex justify-end p-4 gap-1">
|
|
|
|
<BaseButton size="sm" @click="openEditDialog(e)">
|
|
|
|
<template #icon>
|
|
|
|
<Icon name="mdi-edit" />
|
|
|
|
</template>
|
|
|
|
Edit
|
|
|
|
</BaseButton>
|
2022-12-10 05:57:57 +00:00
|
|
|
<BaseButton size="sm" @click="deleteEntry(e.id)">
|
|
|
|
<template #icon>
|
|
|
|
<Icon name="mdi-delete" />
|
|
|
|
</template>
|
|
|
|
Delete
|
|
|
|
</BaseButton>
|
|
|
|
</div>
|
|
|
|
</BaseCard>
|
2023-01-22 06:15:23 +00:00
|
|
|
<div class="hidden first:block">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="relative block w-full rounded-lg border-2 border-dashed border-base-content p-12 text-center"
|
|
|
|
@click="newEntry()"
|
|
|
|
>
|
|
|
|
<Icon name="mdi-wrench-clock" class="h-16 w-16"></Icon>
|
|
|
|
<span class="mt-2 block text-sm font-medium text-gray-900"> Create Your First Entry </span>
|
|
|
|
</button>
|
|
|
|
</div>
|
2022-12-10 05:57:57 +00:00
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
</div>
|
|
|
|
</template>
|