169 lines
5.2 KiB
Vue
169 lines
5.2 KiB
Vue
<script setup lang="ts">
|
|
import { useForm } from '@inertiajs/vue3';
|
|
import { Save } from '@lucide/vue';
|
|
import { computed, ref } from 'vue';
|
|
import { toast } from 'vue-sonner';
|
|
import { ImageUploadField } from '@/components/form/image-upload-field';
|
|
import { RupiahInput } from '@/components/form/rupiah-input';
|
|
import { Button } from '@/components/ui/button';
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from '@/components/ui/dialog';
|
|
import {
|
|
Field,
|
|
FieldError,
|
|
FieldGroup,
|
|
FieldLabel,
|
|
FieldSet,
|
|
} from '@/components/ui/field';
|
|
import { Textarea } from '@/components/ui/textarea';
|
|
import { useFormDialog } from '@/composables/useFormDialog';
|
|
import { FIELD_LIMITS } from '@/lib/field-limits';
|
|
import { formErrors } from '@/lib/form';
|
|
import { parseRupiah } from '@/lib/rupiah';
|
|
import type { ExpenseFormData, ExpenseListItem } from '@/types/expense';
|
|
import { appendPhotosToFormData } from '@/types/media';
|
|
|
|
const open = defineModel<boolean>('open', { default: false });
|
|
|
|
const props = defineProps<{
|
|
expense?: ExpenseListItem | null;
|
|
}>();
|
|
|
|
const isEditing = computed(() => props.expense != null);
|
|
|
|
const existingPhotoId = ref<number | null>(null);
|
|
|
|
const currentPhotoUrl = computed(() => props.expense?.photos?.[0]?.url ?? null);
|
|
|
|
const form = useForm<ExpenseFormData>({
|
|
amount: '',
|
|
description: '',
|
|
photos: [],
|
|
remove_media_ids: [],
|
|
});
|
|
|
|
const photoFile = computed<File | null>({
|
|
get: () => form.photos[0] ?? null,
|
|
set: (file) => {
|
|
form.photos = file ? [file] : [];
|
|
},
|
|
});
|
|
|
|
function resetForm() {
|
|
form.reset();
|
|
form.photos = [];
|
|
form.remove_media_ids = [];
|
|
existingPhotoId.value = null;
|
|
form.clearErrors();
|
|
}
|
|
|
|
function populateForm(expense: ExpenseListItem | null | undefined) {
|
|
resetForm();
|
|
|
|
if (!expense) {
|
|
return;
|
|
}
|
|
|
|
form.amount = String(expense.amount);
|
|
form.description = expense.description;
|
|
existingPhotoId.value = expense.photos?.[0]?.id ?? null;
|
|
}
|
|
|
|
useFormDialog({
|
|
open,
|
|
source: () => props.expense,
|
|
populate: populateForm,
|
|
reset: resetForm,
|
|
});
|
|
|
|
function buildFormData(forUpdate: boolean): FormData {
|
|
const formData = new FormData();
|
|
|
|
if (forUpdate) {
|
|
formData.append('_method', 'PUT');
|
|
}
|
|
|
|
formData.append('amount', parseRupiah(form.amount));
|
|
formData.append('description', form.description);
|
|
|
|
const removeMediaIds = [...form.remove_media_ids];
|
|
|
|
if (form.photos.length > 0 && existingPhotoId.value !== null) {
|
|
removeMediaIds.push(existingPhotoId.value);
|
|
}
|
|
|
|
appendPhotosToFormData(formData, form.photos, removeMediaIds);
|
|
|
|
return formData;
|
|
}
|
|
|
|
function submit() {
|
|
const payload = buildFormData(isEditing.value);
|
|
|
|
const options = {
|
|
forceFormData: true,
|
|
preserveScroll: true,
|
|
onSuccess: () => {
|
|
open.value = false;
|
|
},
|
|
onError: () => {
|
|
toast.error('Gagal menyimpan data. Periksa kembali formulir.');
|
|
},
|
|
};
|
|
|
|
if (isEditing.value && props.expense) {
|
|
form.transform(() => payload).put(`/admin/finance/expenses/${props.expense.id}`, options);
|
|
|
|
return;
|
|
}
|
|
|
|
form.transform(() => payload).post('/admin/finance/expenses', options);
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<Dialog v-model:open="open">
|
|
<DialogContent class="sm:max-w-md">
|
|
<DialogHeader>
|
|
<DialogTitle>{{ isEditing ? 'Ubah Pengeluaran' : 'Tambah Pengeluaran' }}</DialogTitle>
|
|
</DialogHeader>
|
|
|
|
<form @submit.prevent="submit">
|
|
<FieldGroup>
|
|
<FieldSet class="grid gap-4">
|
|
<Field>
|
|
<FieldLabel for="expense-amount" required>Jumlah</FieldLabel>
|
|
<RupiahInput id="expense-amount" v-model="form.amount" autofocus />
|
|
<FieldError :errors="formErrors(form, 'amount')" />
|
|
</Field>
|
|
<Field>
|
|
<FieldLabel for="expense-description" required>Keterangan</FieldLabel>
|
|
<Textarea id="expense-description" v-model="form.description"
|
|
placeholder="Contoh: Pembelian perlengkapan toko" rows="3"
|
|
:maxlength="FIELD_LIMITS.description" />
|
|
<FieldError :errors="formErrors(form, 'description')" />
|
|
</Field>
|
|
<ImageUploadField id="expense-photos" v-model="photoFile" label="Foto Bukti" required
|
|
:current-url="currentPhotoUrl" :errors="formErrors(form, 'photos')" />
|
|
</FieldSet>
|
|
</FieldGroup>
|
|
|
|
<DialogFooter class="mt-6">
|
|
<Button type="button" variant="outline" :disabled="form.processing" @click="open = false">
|
|
Batal
|
|
</Button>
|
|
<Button type="submit" :disabled="form.processing">
|
|
<Save class="size-4" />
|
|
{{ form.processing ? 'Menyimpan...' : 'Simpan' }}
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
</template>
|