import { Dialog } from 'quasar'; import GlobalDialog from 'components/GlobalDialog.vue'; import { ComposerTranslation, useI18n } from 'vue-i18n'; import { defineStore } from 'pinia'; import { ref } from 'vue'; export function dialog(opts: { title: string; message: string; color?: string; icon?: string; persistent?: boolean; actionText?: string; cancelText?: string; enablei18n?: boolean; action?: (...args: unknown[]) => unknown; cancel?: (...args: unknown[]) => unknown; }) { Dialog.create({ component: GlobalDialog, componentProps: opts, }); } export function dialogWarningClose( t: ComposerTranslation, opts: { action?: (...args: unknown[]) => unknown; cancel?: (...args: unknown[]) => unknown; }, ) { dialog({ color: 'warning', icon: 'mdi-alert', title: t('warning'), actionText: t('ok'), message: t('warningClose'), action: async () => { if (opts.action) opts.action(); }, cancel: () => { if (opts.cancel) opts.cancel(); }, }); } export function calculateAge(birthDate: Date | null | string) { const { locale } = useI18n(); if (!birthDate) return null; const birthDateTimeStamp = new Date(birthDate).getTime(); const now = new Date(); const diff = now.getTime() - birthDateTimeStamp; const ageDate = new Date(diff); const years = ageDate.getUTCFullYear() - 1970; const months = ageDate.getUTCMonth(); const days = ageDate.getUTCDate() - 1; if (locale.value === 'th-th') { return `${years} ปี ${months !== 0 ? months + ' เดือน' : ''} ${days !== 0 ? days + ' วัน' : ''} `; } else { return `${years} years ${months !== 0 ? months + ' months' : ''} ${days !== 0 ? days + ' days' : ''} `; } } export function moveItemUp(items: unknown[], index: number) { if (index > 0) { const temp = items[index]; items[index] = items[index - 1]; items[index - 1] = temp; } } export function moveItemDown(items: unknown[], index: number) { if (index < items.length - 1) { const temp = items[index]; items[index] = items[index + 1]; items[index + 1] = temp; } } export function deleteItem(items: unknown[], index: number) { if (!items) return; if (index >= 0 && index < items.length) { items.splice(index, 1); } } export function formatNumberDecimal(num: number, point: number): string { return num.toLocaleString('en-US', { minimumFractionDigits: point, maximumFractionDigits: point, }); } const useUtilsStore = defineStore('utilsStore', () => { const currentTitle = ref<{ title: string; path: { text: string; argsi18n?: Record; handler?: () => unknown; }[]; }>({ title: '', path: [ { text: '', handler: () => {}, }, ], }); return { currentTitle, }; }); export default useUtilsStore;