jws-frontend/src/pages/03_customer-management/components/employer/EmployerFormBasicInfo.vue

469 lines
12 KiB
Vue
Raw Normal View History

2024-08-05 15:09:36 +07:00
<script lang="ts" setup>
import { ref, watch, capitalize } from 'vue';
2024-08-05 15:09:36 +07:00
import { QSelect } from 'quasar';
import { selectFilterOptionRefMod } from 'stores/utils';
import { getRole } from 'src/services/keycloak';
2024-08-20 18:04:27 +07:00
import useOptionStore from 'stores/options';
2024-08-05 15:09:36 +07:00
import { onMounted } from 'vue';
2024-08-20 18:04:27 +07:00
import { useI18n } from 'vue-i18n';
const { locale } = useI18n();
import {
dateFormat,
calculateAge,
parseAndFormatDate,
disabledAfterToday,
} from 'src/utils/datetime';
2024-08-07 17:56:59 +07:00
import {
SaveButton,
EditButton,
DeleteButton,
2024-08-09 13:20:38 +07:00
UndoButton,
2024-08-09 14:02:40 +07:00
} from 'components/button';
2024-08-05 15:09:36 +07:00
defineProps<{
prefixId?: string;
outlined?: boolean;
readonly?: boolean;
2024-08-07 17:56:59 +07:00
create?: boolean;
actionDisabled?: boolean;
2024-08-05 15:09:36 +07:00
customerType?: 'CORP' | 'PERS';
}>();
2024-08-07 17:56:59 +07:00
defineEmits<{
(e: 'save'): void;
(e: 'edit'): void;
(e: 'delete'): void;
(e: 'cancel'): void;
}>();
2024-08-05 15:09:36 +07:00
2024-08-20 18:04:27 +07:00
const optionStore = useOptionStore();
2024-08-13 12:52:25 +07:00
const code = defineModel<string>('code', { required: true });
2024-08-20 18:04:27 +07:00
const namePrefix = defineModel<string | null>('namePrefix');
const birthDate = defineModel<Date | string | null>('birthDate');
const gender = defineModel<string>('gender');
const firstName = defineModel<string>('firstName', { required: true });
const lastName = defineModel<string>('lastName', { required: true });
const firstNameEN = defineModel<string>('firstNameEn', { required: true });
const lastNameEN = defineModel<string>('lastNameEn', { required: true });
2024-08-05 15:09:36 +07:00
const registeredBranchId = defineModel<string>('registeredBranchId', {
required: true,
});
const branchOptions = defineModel<{ id: string; name: string }[]>(
'branchOptions',
{ default: [] },
);
const filteredBranchOptions = ref<Record<string, unknown>[]>([]);
let branchFilter: (
value: string,
update: (callbackFn: () => void, afterFn?: (ref: QSelect) => void) => void,
) => void;
onMounted(() => {
branchFilter = selectFilterOptionRefMod(
branchOptions,
filteredBranchOptions,
'name',
);
});
watch(
() => branchOptions.value,
() => {
branchFilter = selectFilterOptionRefMod(
branchOptions,
filteredBranchOptions,
'name',
);
},
);
2024-08-13 12:52:25 +07:00
2024-08-20 18:04:27 +07:00
const prefixNameOptions = ref<Record<string, unknown>[]>([]);
let prefixNameFilter: (
value: string,
update: (callbackFn: () => void, afterFn?: (ref: QSelect) => void) => void,
) => void;
const prefixNameEnOptions = ref<Record<string, unknown>[]>([]);
let prefixNameEnFilter: (
value: string,
update: (callbackFn: () => void, afterFn?: (ref: QSelect) => void) => void,
) => void;
const genderOptions = ref<Record<string, unknown>[]>([]);
let genderFilter: (
value: string,
update: (callbackFn: () => void, afterFn?: (ref: QSelect) => void) => void,
) => void;
onMounted(() => {
prefixNameFilter = selectFilterOptionRefMod(
ref(optionStore.globalOption?.prefix),
prefixNameOptions,
'label',
);
genderFilter = selectFilterOptionRefMod(
ref(optionStore.globalOption?.gender),
genderOptions,
'label',
);
});
watch(
() => optionStore.globalOption,
() => {
prefixNameFilter = selectFilterOptionRefMod(
ref(optionStore.globalOption.prefix),
prefixNameOptions,
'label',
);
genderFilter = selectFilterOptionRefMod(
ref(optionStore.globalOption.gender),
genderOptions,
'label',
);
},
);
2024-08-30 11:13:16 +07:00
watch(
() => namePrefix.value,
(v) => {
if (v === 'mr') gender.value = 'male';
else gender.value = 'female';
},
);
2024-08-13 12:52:25 +07:00
function formatCode(input: string | undefined, type: 'code' | 'number') {
if (!input) return;
return input.slice(...(type === 'code' ? [0, -6] : [-6]));
}
2024-08-05 15:09:36 +07:00
</script>
<template>
2024-08-13 12:52:25 +07:00
<div class="row q-col-gutter-sm q-mb-sm">
2024-08-05 15:09:36 +07:00
<div class="col-12 text-weight-bold text-body1 row items-center">
<q-icon
flat
size="xs"
class="q-pa-sm rounded q-mr-xs"
color="info"
name="mdi-office-building-outline"
style="background-color: var(--surface-3)"
/>
2024-08-26 16:24:08 +07:00
<span>{{ $t('form.field.basicInformation') }}</span>
2024-08-07 17:56:59 +07:00
<EditButton
2024-08-08 13:32:46 +07:00
icon-only
2024-08-07 17:56:59 +07:00
v-if="readonly && !create"
type="button"
@click="$emit('edit')"
class="q-ml-auto"
:disabled="actionDisabled"
/>
<DeleteButton
2024-08-08 13:32:46 +07:00
icon-only
2024-08-07 17:56:59 +07:00
v-if="readonly && !create"
@click="$emit('delete')"
type="button"
:disabled="actionDisabled"
/>
2024-08-09 13:20:38 +07:00
<UndoButton
2024-08-08 13:32:46 +07:00
icon-only
2024-08-09 13:20:38 +07:00
v-if="!readonly && !create"
2024-08-07 17:56:59 +07:00
class="q-ml-auto"
2024-08-09 13:20:38 +07:00
type="button"
2024-08-07 17:56:59 +07:00
:disabled="actionDisabled"
2024-08-09 13:20:38 +07:00
@click="$emit('cancel')"
2024-08-07 17:56:59 +07:00
/>
2024-08-09 13:20:38 +07:00
<SaveButton
2024-08-08 13:32:46 +07:00
icon-only
2024-08-09 13:20:38 +07:00
v-if="!readonly"
2024-08-13 12:52:25 +07:00
:class="{ 'q-ml-auto': create }"
2024-08-09 13:20:38 +07:00
@click="$emit('save')"
2024-08-14 09:43:54 +07:00
type="submit"
2024-08-07 17:56:59 +07:00
:disabled="actionDisabled"
2024-08-05 15:09:36 +07:00
/>
</div>
2024-08-20 18:04:27 +07:00
2024-08-23 09:19:23 +07:00
<div class="col-12 row q-col-gutter-sm">
2024-08-20 18:04:27 +07:00
<q-select
outlined
clearable
use-input
fill-input
emit-value
map-options
hide-selected
hide-bottom-space
dense
2024-08-23 09:19:23 +07:00
class="col-12 col-md-7"
2024-08-20 18:04:27 +07:00
option-value="id"
input-debounce="0"
option-label="name"
v-model="registeredBranchId"
:readonly="readonly"
:options="filteredBranchOptions"
:hide-dropdown-icon="readonly"
2024-08-26 16:24:08 +07:00
:label="$t('customer.form.registeredBranch')"
2024-08-20 18:04:27 +07:00
:for="`${prefixId}-input-source-nationality`"
:rules="[
(val) => {
const roles = getRole() || [];
return (
['admin', 'system', 'head_of_admin'].some((v) =>
roles.includes(v),
) ||
!!val ||
$t('form.error.required')
);
},
]"
@filter="branchFilter"
>
<template v-slot:no-option>
<q-item>
<q-item-section class="text-grey">
2024-08-26 16:24:08 +07:00
{{ $t('general.noData') }}
2024-08-20 18:04:27 +07:00
</q-item-section>
</q-item>
</template>
</q-select>
</div>
<div class="col-12 row q-col-gutter-sm">
<q-select
outlined
use-input
fill-input
emit-value
map-options
hide-selected
hide-bottom-space
input-debounce="0"
option-label="label"
option-value="value"
hide-dropdown-icon
2024-08-23 09:19:23 +07:00
class="col-12 col-md-2"
2024-08-20 18:04:27 +07:00
dense
:readonly="readonly"
:options="prefixNameOptions"
:for="`${prefixId}-select-prefix-name`"
2024-08-26 16:24:08 +07:00
:label="$t('form.prefixName')"
2024-08-20 18:04:27 +07:00
@filter="prefixNameFilter"
:model-value="readonly ? namePrefix || '-' : namePrefix"
@update:model-value="
(v) => {
typeof v === 'string' ? (namePrefix = v) : '';
}
"
@clear="namePrefix = ''"
2024-08-23 11:11:31 +07:00
:rules="[
(val) => {
const roles = getRole() || [];
return !!val || $t('form.error.required');
},
]"
2024-08-20 18:04:27 +07:00
>
<template v-slot:no-option>
<q-item>
<q-item-section class="text-grey">
2024-08-26 16:24:08 +07:00
{{ $t('general.noData') }}
2024-08-20 18:04:27 +07:00
</q-item-section>
</q-item>
</template>
</q-select>
<q-input
dense
outlined
:readonly="readonly"
hide-bottom-space
2024-08-23 09:19:23 +07:00
class="col-12 col-md-5"
2024-08-20 18:04:27 +07:00
:label="$t('customer.form.firstName')"
for="input-first-name"
v-model="firstName"
2024-08-23 11:11:31 +07:00
:rules="[
(val) => {
const roles = getRole() || [];
return !!val || $t('form.error.required');
},
]"
2024-08-20 18:04:27 +07:00
/>
<q-input
dense
outlined
:readonly="readonly"
hide-bottom-space
2024-08-23 09:19:23 +07:00
class="col-12 col-md-5"
2024-08-20 18:04:27 +07:00
:label="$t('customer.form.lastName')"
for="input-last-name"
v-model="lastName"
2024-08-23 11:11:31 +07:00
:rules="[
(val) => {
const roles = getRole() || [];
return !!val || $t('form.error.required');
},
]"
2024-08-20 18:04:27 +07:00
/>
</div>
<div class="col-12 row q-col-gutter-sm">
<q-input
dense
outlined
2024-08-23 09:51:00 +07:00
:disable="!readonly"
readonly
2024-08-20 18:04:27 +07:00
hide-bottom-space
2024-08-23 09:19:23 +07:00
class="col-12 col-md-2"
2024-08-20 18:04:27 +07:00
:label="$t('customer.form.prefixName')"
for="input-prefix-name"
:model-value="
readonly
? capitalize(namePrefix || '') || '-'
: capitalize(namePrefix || '')
2024-08-20 18:04:27 +07:00
"
/>
<q-input
dense
outlined
:readonly="readonly"
hide-bottom-space
2024-08-23 09:19:23 +07:00
class="col-12 col-md-5"
2024-08-20 18:04:27 +07:00
:label="$t('customer.form.firstNameEN')"
for="input-first-name-en"
v-model="firstNameEN"
/>
<q-input
dense
outlined
:readonly="readonly"
hide-bottom-space
2024-08-23 09:19:23 +07:00
class="col-12 col-md-5"
2024-08-20 18:04:27 +07:00
:label="$t('customer.form.lastNameEN')"
for="input-last-name-en"
v-model="lastNameEN"
/>
</div>
<div class="col-12 row q-col-gutter-sm">
<q-select
outlined
use-input
fill-input
emit-value
map-options
hide-selected
hide-bottom-space
input-debounce="0"
option-label="label"
option-value="value"
2024-08-23 09:19:23 +07:00
class="col-12 col-md-2"
2024-08-20 18:04:27 +07:00
dense
:readonly="readonly"
:options="genderOptions"
:hide-dropdown-icon="readonly"
:for="`${prefixId}-select-gender`"
2024-08-26 16:24:08 +07:00
:label="$t('form.gender')"
2024-08-20 18:04:27 +07:00
@filter="genderFilter"
:model-value="readonly ? gender || '-' : gender"
@update:model-value="(v) => (typeof v === 'string' ? (gender = v) : '')"
@clear="gender = ''"
2024-08-23 11:11:31 +07:00
:rules="[
(val) => {
const roles = getRole() || [];
return !!val || $t('form.error.required');
},
]"
2024-08-20 18:04:27 +07:00
>
<template v-slot:no-option>
<q-item>
<q-item-section class="text-grey">
2024-08-26 16:24:08 +07:00
{{ $t('general.noData') }}
2024-08-20 18:04:27 +07:00
</q-item-section>
</q-item>
</template>
</q-select>
<VueDatePicker
:id="`${prefixId}-input-birth-date`"
:for="`${prefixId}-input-birth-date`"
utc
autoApply
v-model="birthDate"
:disabled-dates="disabledAfterToday"
:teleport="true"
:dark="$q.dark.isActive"
2024-08-26 16:24:08 +07:00
:locale="$i18n.locale === 'tha' ? 'th' : 'en'"
2024-08-20 18:04:27 +07:00
:enableTimePicker="false"
:disabled="readonly"
2024-08-23 09:19:23 +07:00
class="col-12 col-md-3"
2024-08-20 18:04:27 +07:00
>
<template #year="{ value }">
2024-08-26 16:24:08 +07:00
{{ $i18n.locale === 'tha' ? value + 543 : value }}
2024-08-20 18:04:27 +07:00
</template>
<template #year-overlay-value="{ value }">
2024-08-26 16:24:08 +07:00
{{ $i18n.locale === 'tha' ? value + 543 : value }}
2024-08-20 18:04:27 +07:00
</template>
<template #trigger>
<q-input
:for="`${prefixId}-input-birth-date`"
hide-bottom-space
placeholder="DD/MM/YYYY"
2024-08-26 16:24:08 +07:00
:label="$t('form.birthDate')"
2024-08-20 18:04:27 +07:00
dense
outlined
:readonly="readonly"
:rules="[
(val: string) =>
2024-08-26 16:24:08 +07:00
!!val || $t('selectValidate') + $t('form.birthDate'),
2024-08-20 18:04:27 +07:00
]"
:mask="readonly ? '' : '##/##/####'"
:model-value="
birthDate && readonly
? dateFormat(birthDate)
: dateFormat(birthDate, false, false, true)
"
@update:model-value="
(v) => {
if (v && v.toString().length === 10) {
const today = new Date();
const date = parseAndFormatDate(v, locale);
birthDate = date && date > today ? today : date;
}
}
"
>
<template v-slot:prepend>
<q-icon
size="xs"
name="mdi-calendar-blank-outline"
class="cursor-pointer"
color="primary"
/>
</template>
</q-input>
</template>
</VueDatePicker>
<q-input
:for="`${prefixId}-input-age`"
:id="`${prefixId}-input-age`"
dense
outlined
2024-09-04 16:20:22 +07:00
readonly
2024-08-26 16:24:08 +07:00
:label="$t('general.age')"
2024-08-23 09:19:23 +07:00
class="col-12 col-md-2"
2024-08-20 18:04:27 +07:00
:model-value="
birthDate?.toString() === 'Invalid Date' ||
birthDate?.toString() === undefined
? ''
: calculateAge(birthDate)
"
/>
</div>
2024-08-05 15:09:36 +07:00
</div>
</template>