Income/expense lists are table-or-card with detail, edit, void, and delete; edit/delete require owner, admin, or the new finance manage permission. Co-authored-by: Cursor <cursoragent@cursor.com>
90 lines
2.5 KiB
Vue
90 lines
2.5 KiB
Vue
<script setup lang="ts">
|
|
import { computed, ref } from 'vue';
|
|
import { Button as AButton, Upload as AUpload, message } from 'ant-design-vue';
|
|
import type { UploadFile, UploadProps } from 'ant-design-vue';
|
|
import { UploadOutlined } from '@ant-design/icons-vue';
|
|
import { FormFileTooLargeError, prepareFormFieldFile } from '@/lib/form-file-compress';
|
|
import type { TreasuryPendingPhoto } from '@/lib/treasury-doc-photos';
|
|
|
|
export type { TreasuryPendingPhoto };
|
|
|
|
const PHOTO_LIMIT = 8;
|
|
|
|
const props = withDefaults(
|
|
defineProps<{
|
|
disabled?: boolean;
|
|
}>(),
|
|
{ disabled: false },
|
|
);
|
|
|
|
const model = defineModel<TreasuryPendingPhoto[]>({ default: () => [] });
|
|
const busy = ref(false);
|
|
|
|
const fileList = computed<UploadFile[]>(() =>
|
|
model.value.map((photo) => ({
|
|
uid: photo.localId,
|
|
name: photo.name,
|
|
status: 'done',
|
|
url: photo.dataUrl,
|
|
})),
|
|
);
|
|
|
|
const beforeUpload: UploadProps['beforeUpload'] = async (file) => {
|
|
if (props.disabled || busy.value) return AUpload.LIST_IGNORE;
|
|
if (model.value.length >= PHOTO_LIMIT) {
|
|
message.warning(`حداکثر ${PHOTO_LIMIT} عکس برای هر سند.`);
|
|
return AUpload.LIST_IGNORE;
|
|
}
|
|
const raw = file as File;
|
|
if (!raw.type.startsWith('image/')) {
|
|
message.error('فقط فایل تصویری مجاز است.');
|
|
return AUpload.LIST_IGNORE;
|
|
}
|
|
busy.value = true;
|
|
try {
|
|
const prepared = await prepareFormFieldFile(raw);
|
|
model.value = [
|
|
...model.value,
|
|
{
|
|
localId: crypto.randomUUID(),
|
|
name: prepared.name,
|
|
dataUrl: prepared.dataUrl,
|
|
file: prepared.file,
|
|
},
|
|
];
|
|
} catch (e) {
|
|
if (e instanceof FormFileTooLargeError) {
|
|
message.error('حجم عکس باید زیر ۲۰۰ کیلوبایت باشد');
|
|
} else {
|
|
message.error('فشردهسازی عکس ناموفق بود.');
|
|
}
|
|
} finally {
|
|
busy.value = false;
|
|
}
|
|
return AUpload.LIST_IGNORE;
|
|
};
|
|
|
|
const onRemove: UploadProps['onRemove'] = (file) => {
|
|
if (props.disabled || busy.value) return false;
|
|
model.value = model.value.filter((photo) => photo.localId !== file.uid);
|
|
return true;
|
|
};
|
|
</script>
|
|
|
|
<template>
|
|
<AUpload
|
|
list-type="picture"
|
|
accept="image/*"
|
|
:file-list="fileList"
|
|
:disabled="disabled || busy || model.length >= PHOTO_LIMIT"
|
|
:before-upload="beforeUpload"
|
|
:on-remove="onRemove"
|
|
multiple
|
|
>
|
|
<AButton :disabled="disabled || busy || model.length >= PHOTO_LIMIT">
|
|
<template #icon><UploadOutlined /></template>
|
|
{{ busy ? 'آمادهسازی…' : 'افزودن عکس' }}
|
|
</AButton>
|
|
</AUpload>
|
|
</template>
|