Files
FEDEO/pages/vehicles/[mode]/[[id]].vue
florianfederspiel b465f4a75a Introduced ProfileStore
Corrected All Links to DataStore
2024-12-21 22:33:42 +01:00

406 lines
10 KiB
Vue

<script setup>
import dayjs from "dayjs";
import {useSupabaseSelectSingle} from "~/composables/useSupabase.js";
definePageMeta({
middleware: "auth"
})
defineShortcuts({
'backspace': () => {
router.push("/vehicles")
},
'arrowleft': () => {
if(openTab.value > 0){
openTab.value -= 1
}
},
'arrowright': () => {
if(openTab.value < 3) {
openTab.value += 1
}
},
})
const dataStore = useDataStore()
const profileStore = useProfileStore()
const route = useRoute()
const router = useRouter()
const toast = useToast()
const id = ref(route.params.id ? route.params.id : null )
const openTab = ref(0)
//Working
const mode = ref(route.params.mode || "show")
const itemInfo = ref({
name: "",
licensePlate: "",
type: "",
driver: null,
active: true
})
const oldItemInfo = ref({})
const tabItems = [{
label: 'Informationen',
}, {
label: 'Dokumente',
}, {
label: 'Überprüfungen',
}]
const incomingInvoicesColumns = [
{
key: "state",
label: "Status",
sortable: true
},{
key: "vendor",
label: "Lieferant",
sortable: true
},{
key: "date",
label: "Datum",
sortable: true
},{
key: "description",
label: "Beschreibung",
sortable: true
},{
key: "accounts",
label: "Betrag",
sortable: true
},
]
//Functions
const setupPage = async () => {
if(mode.value === "show"){
itemInfo.value = await useSupabaseSelectSingle("vehicles",route.params.id,"*, checks(*)")
} else if(mode.value === "edit"){
itemInfo.value = await useSupabaseSelectSingle("vehicles",route.params.id,"*")
}
if(itemInfo.value) oldItemInfo.value = JSON.parse(JSON.stringify(itemInfo.value))
}
const cancelEditorCreate = () => {
if(itemInfo.value.id) {
router.push(`/vehicles/show/${itemInfo.value.id}`)
} else {
router.push(`/vehicles`)
}
}
const getRowAmount = (row) => {
let amount = 0
row.accounts.forEach(account => {
amount += account.amountNet
amount += account.amountTax
})
return amount
}
setupPage()
</script>
<template>
<UDashboardNavbar
:title="itemInfo ? itemInfo.licensePlate : (mode === 'create' ? 'Fahrzeug erstellen' : 'Fahrzeug bearbeiten')"
:ui="{center: 'flex items-stretch gap-1.5 min-w-0'}"
>
<template #left>
<UButton
icon="i-heroicons-chevron-left"
variant="outline"
@click="router.push(`/vehicles`)"
>
Fahrzeuge
</UButton>
</template>
<template #center>
<h1
v-if="itemInfo"
:class="['text-xl','font-medium']"
>{{itemInfo ? `Fahrzeug: ${itemInfo.licensePlate}` : (mode === 'create' ? 'Fahrzeug erstellen' : 'Fahrzeug bearbeiten')}}</h1>
</template>
<template #right>
<ButtonWithConfirm
color="rose"
variant="outline"
@confirmed="dataStore.updateItem('vehicles',{...itemInfo, archived: true})"
v-if="mode === 'edit'"
>
<template #button>
Archivieren
</template>
<template #header>
<span class="text-md text-black font-bold">Archivieren bestätigen</span>
</template>
Möchten Sie das Fahrzeug {{itemInfo.name}} wirklich archivieren?
</ButtonWithConfirm>
<UButton
v-if="mode === 'edit'"
@click="dataStore.updateItem('vehicles',itemInfo, oldItemInfo)"
>
Speichern
</UButton>
<UButton
v-else-if="mode === 'create'"
@click="dataStore.createNewItem('vehicles',itemInfo)"
>
Erstellen
</UButton>
<UButton
@click="cancelEditorCreate"
color="red"
class="ml-2"
v-if="mode === 'edit' || mode === 'create'"
>
Abbrechen
</UButton>
<UButton
v-if="mode === 'show'"
@click="router.push(`/vehicles/edit/${itemInfo.id}`)"
>
Bearbeiten
</UButton>
</template>
</UDashboardNavbar>
<UTabs
:items="tabItems"
v-if="mode === 'show' && itemInfo.id"
class="p-5"
v-model="openTab"
>
<template #item="{item}">
<div v-if="item.label === 'Informationen'" class="flex flex-row mt-5">
<UCard class="w-1/2 mr-5">
<UAlert
v-if="itemInfo.archived"
color="rose"
variant="outline"
title="Objekt archiviert"
icon="i-heroicons-light-bulb"
class="mb-5"
/>
<div class="text-wrap">
<table class="w-full">
<tr>
<td>Kennzeichen: </td>
<td>{{itemInfo.licensePlate}}</td>
</tr>
<tr>
<td>Typ: </td>
<td>{{itemInfo.type}}</td>
</tr>
<tr>
<td>Fahrgestellnummer: </td>
<td>{{itemInfo.vin}}</td>
</tr>
<tr>
<td>Fahrer:</td>
<td>{{profileStore.profiles.find(profile => profile.id === itemInfo.driver) ? profileStore.profiles.find(profile => profile.id === itemInfo.driver).fullName : 'Kein Fahrer gewählt'}}</td>
</tr>
<tr>
<td>Tankvolumen:</td>
<td>{{itemInfo.tankSize !== 0 ? `${itemInfo.tankSize} L` : "Kein Tank verbaut"}}</td>
</tr>
<tr>
<td>Baujahr:</td>
<td>{{itemInfo.buildYear}}</td>
</tr>
<tr>
<td>Anhänglast:</td>
<td>{{itemInfo.towingCapacity}}</td>
</tr>
<tr>
<td>Farbe:</td>
<td>{{itemInfo.color}}</td>
</tr>
<tr>
<td>Leistung:</td>
<td>{{itemInfo.powerInKW && itemInfo.powerInKW + " kW"}}</td>
</tr>
</table>
</div>
</UCard>
<UCard class="w-1/2">
<HistoryDisplay
type="vehicle"
v-if="itemInfo"
:element-id="itemInfo.id"
/>
</UCard>
</div>
<div v-else-if="item.label === 'Eingangsrechnungen'">
<UTable
:rows="dataStore.getIncomingInvoicesByVehicleId(itemInfo.id)"
:columns="incomingInvoicesColumns"
@select="(row) => router.push('/receipts/show/' + row.id)"
>
<template #vendor-data="{row}">
{{dataStore.getVendorById(row.vendor) ? dataStore.getVendorById(row.vendor).name : ""}}
</template>
<template #date-data="{row}">
{{dayjs(row.date).format("DD.MM.YYYY")}}
</template>
<template #accounts-data="{row}">
{{getRowAmount(row) ? String(getRowAmount(row).toFixed(2)).replace('.',',') + " €" : ""}}
</template>
</UTable>
</div>
<div v-else-if="item.label === 'Dokumente'">
<UCard class="mt-5">
<Toolbar>
<DocumentUpload
type="vehicle"
:element-id="itemInfo.id"
/>
</Toolbar>
<DocumentList
:documents="dataStore.getDocumentsByVehicleId(itemInfo.id)"
/>
</UCard>
</div>
<div v-else-if="item.label === 'Überprüfungen'">
<UCard class="mt-5">
<UTable
:rows="itemInfo.checks"
:columns="[{key:'name',label: 'Name'},{key:'rhythm',label: 'Rhythmus'},{key:'description',label: 'Beschreibung'}]"
class="w-full"
:ui="{ divide: 'divide-gray-200 dark:divide-gray-800' }"
@select="(i) => router.push(`/checks/show/${i.id}`) "
:empty-state="{ icon: 'i-heroicons-circle-stack-20-solid', label: 'Keine Überprüfungen anzuzeigen' }"
>
<template #rhythm-data="{row}">
{{row.distance}}
<span v-if="row.distanceUnit === 'dayjs'">Tage</span>
<span v-if="row.distanceUnit === 'years'">Jahre</span>
</template>
</UTable>
</UCard>
</div>
</template>
</UTabs>
<UForm
v-else-if="mode == 'edit' || mode == 'create'"
class="p-5"
>
<UFormGroup
label="Kennzeichen:"
>
<UInput
v-model="itemInfo.licensePlate"
/>
</UFormGroup>
<UFormGroup
label="Fahrzeug aktiv:"
>
<UCheckbox
v-model="itemInfo.active"
/>
</UFormGroup>
<UFormGroup
label="Fahrgestellnummer:"
>
<UInput
v-model="itemInfo.vin"
/>
</UFormGroup>
<UFormGroup
label="Typ:"
>
<UTextarea
v-model="itemInfo.type"
/>
</UFormGroup>
<UFormGroup
label="Fahrer:"
>
<USelectMenu
v-model="itemInfo.driver"
:options="[{id: null, fullName: 'Kein Fahrer'},...profileStore.profiles]"
option-attribute="fullName"
value-attribute="id"
>
<template #label>
{{profileStore.profiles.find(profile => profile.id === itemInfo.driver) ? profileStore.profiles.find(profile => profile.id === itemInfo.driver).fullName : 'Kein Fahrer ausgewählt'}}
</template>
</USelectMenu>
</UFormGroup>
<UFormGroup
label="Tankvolumen:"
>
<UInput
v-model="itemInfo.tankSize"
type="number"
>
<template #trailing>
L
</template>
</UInput>
</UFormGroup>
<UFormGroup
label="Baujahr:"
>
<UInput
v-model="itemInfo.buildYear"
type="number"
/>
</UFormGroup>
<UFormGroup
label="Anhängelast:"
>
<UInput
v-model="itemInfo.towingCapacity"
type="number"
>
<template #trailing>kg</template>
</UInput>
</UFormGroup>
<UFormGroup
label="Farbe:"
>
<UInput
v-model="itemInfo.color"
type="text"
/>
</UFormGroup>
<UFormGroup
label="Leistung:"
>
<UInput
v-model="itemInfo.powerInKW"
type="number"
>
<template #trailing>kW</template>
</UInput>
</UFormGroup>
</UForm>
</template>
<style scoped>
td {
border-bottom: 1px solid lightgrey;
vertical-align: top;
padding-bottom: 0.15em;
padding-top: 0.15em;
}
</style>