Introduced Plants Some Polishing Some Resources got Query Params Extended GlobalSearch.vue Removed Jobs
185 lines
3.5 KiB
Vue
185 lines
3.5 KiB
Vue
<script setup>
|
|
definePageMeta({
|
|
middleware: "auth"
|
|
})
|
|
|
|
const dataStore = useDataStore()
|
|
const supabase = useSupabaseClient()
|
|
const route = useRoute()
|
|
const router = useRouter()
|
|
const toast = useToast()
|
|
const id = ref(route.params.id ? route.params.id : null )
|
|
|
|
let currentItem = null
|
|
|
|
//Working
|
|
const mode = ref(route.params.mode || "show")
|
|
const itemInfo = ref({
|
|
name: "",
|
|
customer: 0,
|
|
|
|
})
|
|
|
|
//Functions
|
|
const setupPage = () => {
|
|
if(mode.value === "show" || mode.value === "edit"){
|
|
currentItem = dataStore.getPlantById(Number(useRoute().params.id))
|
|
}
|
|
|
|
if(mode.value === "edit") itemInfo.value = currentItem
|
|
|
|
|
|
|
|
}
|
|
|
|
const createItem = async () => {
|
|
const {data,error} = await supabase
|
|
.from("plants")
|
|
.insert([itemInfo.value])
|
|
.select()
|
|
|
|
if(error) {
|
|
console.log(error)
|
|
} else {
|
|
mode.value = "show"
|
|
itemInfo.value = {
|
|
id: 0,
|
|
title: "",
|
|
}
|
|
toast.add({title: "Anlage erfolgreich erstellt"})
|
|
await dataStore.fetchPlants()
|
|
router.push(`/plants/show/${data[0].id}`)
|
|
setupPage()
|
|
}
|
|
}
|
|
|
|
const editItem = async () => {
|
|
router.push(`/plants/edit/${currentItem.id}`)
|
|
setupPage()
|
|
}
|
|
|
|
const cancelEditorCreate = () => {
|
|
mode.value = "show"
|
|
itemInfo.value = {
|
|
id: 0,
|
|
infoData: {}
|
|
}
|
|
}
|
|
|
|
const updateItem = async () => {
|
|
const {error} = await supabase
|
|
.from("plants")
|
|
.update(itemInfo.value)
|
|
.eq('id',itemInfo.value.id)
|
|
|
|
if(error) {
|
|
console.log(error)
|
|
}
|
|
|
|
|
|
router.push(`/plants/show/${currentItem.id}`)
|
|
toast.add({title: "Anlage erfolgreich gespeichert"})
|
|
dataStore.fetchPlants()
|
|
}
|
|
|
|
|
|
|
|
setupPage()
|
|
</script>
|
|
|
|
<template>
|
|
<UCard v-if="currentItem && mode == 'show'" class="h-full">
|
|
<template #header>
|
|
{{currentItem.name}}
|
|
</template>
|
|
|
|
<InputGroup>
|
|
<UButton
|
|
@click="router.push(`/projects/create?plant=${currentItem.id}`)"
|
|
>
|
|
+ Projekt
|
|
</UButton>
|
|
</InputGroup>
|
|
|
|
{{currentItem}}
|
|
|
|
<template #footer>
|
|
<UButton
|
|
v-if="mode == 'show' && currentItem.id"
|
|
@click="editItem"
|
|
>
|
|
Bearbeiten
|
|
</UButton>
|
|
<UButton
|
|
color="red"
|
|
class="ml-2"
|
|
disabled
|
|
>
|
|
Archivieren
|
|
</UButton>
|
|
<!-- TODO: Kunde archivieren -->
|
|
</template>
|
|
|
|
|
|
|
|
</UCard>
|
|
<UCard v-else-if="mode === 'edit' || mode === 'create'" class="h-full">
|
|
<template #header v-if="mode === 'edit'">
|
|
{{itemInfo.name}}
|
|
</template>
|
|
|
|
<UFormGroup
|
|
label="Name:"
|
|
>
|
|
<UInput
|
|
v-model="itemInfo.name"
|
|
/>
|
|
</UFormGroup>
|
|
|
|
<UFormGroup
|
|
label="Kundennummer:"
|
|
>
|
|
<USelectMenu
|
|
v-model="itemInfo.customer"
|
|
:options="dataStore.customers"
|
|
option-attribute="name"
|
|
value-attribute="id"
|
|
searchable
|
|
:search-attributes="['name']"
|
|
>
|
|
<template #label>
|
|
{{dataStore.customers.find(customer => customer.id === itemInfo.customer) ? dataStore.customers.find(customer => customer.id === itemInfo.customer).name : "Kunde auswählen"}}
|
|
</template>
|
|
</USelectMenu>
|
|
</UFormGroup>
|
|
|
|
|
|
|
|
<template #footer>
|
|
<UButton
|
|
v-if="mode == 'edit'"
|
|
@click="updateItem"
|
|
>
|
|
Speichern
|
|
</UButton>
|
|
<UButton
|
|
v-else-if="mode == 'create'"
|
|
@click="createItem"
|
|
>
|
|
Erstellen
|
|
</UButton>
|
|
<UButton
|
|
@click="cancelEditorCreate"
|
|
color="red"
|
|
class="ml-2"
|
|
>
|
|
Abbrechen
|
|
</UButton>
|
|
</template>
|
|
|
|
</UCard>
|
|
</template>
|
|
|
|
<style scoped>
|
|
|
|
</style> |