jws-frontend/src/pages/02_personnel-management/MainPage.vue

557 lines
16 KiB
Vue
Raw Normal View History

2024-04-03 18:28:59 +07:00
<script setup lang="ts">
2024-04-10 14:00:55 +07:00
import { ref, onMounted, watch } from 'vue';
import { api } from 'src/boot/axios';
2024-04-11 09:37:18 +07:00
import { useRouter } from 'vue-router';
2024-04-05 17:26:40 +07:00
import { storeToRefs } from 'pinia';
2024-04-10 14:00:55 +07:00
import useUserStore from 'stores/user';
import useBranchStore from 'src/stores/branch';
2024-04-11 00:37:07 +07:00
import { dialog } from 'src/stores/utils';
2024-04-10 14:00:55 +07:00
2024-04-12 08:02:01 +07:00
import { UserCreate, UserTypeStats } from 'src/stores/user/types';
2024-04-10 14:00:55 +07:00
import { BranchUserStats } from 'src/stores/branch/types';
2024-04-04 15:03:39 +07:00
2024-04-05 17:36:54 +07:00
import PersonCard from 'components/home/PersonCard.vue';
2024-04-03 18:28:59 +07:00
import AppBox from 'components/app/AppBox.vue';
2024-04-05 17:36:54 +07:00
import StatCardComponent from 'components/StatCardComponent.vue';
import SelectorList from 'components/SelectorList.vue';
import AddButton from 'components/AddButton.vue';
2024-04-05 17:43:16 +07:00
import TooltipComponent from 'components/TooltipComponent.vue';
2024-04-05 19:05:51 +07:00
import FormDialog from 'src/components/FormDialog.vue';
import FormTop from 'src/components/02_personnel-management/FormTop.vue';
import FormName from 'src/components/02_personnel-management/FormName.vue';
import FormByType from 'src/components/02_personnel-management/FormByType.vue';
2024-04-04 15:03:39 +07:00
2024-04-11 09:37:18 +07:00
const router = useRouter();
2024-04-05 17:26:40 +07:00
const userStore = useUserStore();
const branchStore = useBranchStore();
2024-04-05 17:26:40 +07:00
const { data: userData } = storeToRefs(userStore);
2024-04-03 18:28:59 +07:00
2024-04-10 14:00:55 +07:00
const defaultFormData = {
provinceId: null,
districtId: null,
subDistrictId: null,
telephoneNo: '',
email: '',
zipCode: '',
gender: '',
addressEN: '',
address: '',
trainingPlace: null,
importNationality: null,
sourceNationality: null,
licenseExpireDate: null,
licenseIssueDate: null,
licenseNo: null,
discountCondition: '',
retireDate: null,
startDate: null,
registrationNo: null,
lastNameEN: '',
lastName: '',
firstNameEN: '',
firstName: '',
userRole: '',
userType: '',
profileImage: null,
birthDate: null,
2024-04-10 14:00:55 +07:00
responsibleArea: '',
};
const urlProfile = ref<string>();
const isEdit = ref(false);
2024-04-05 19:05:51 +07:00
const modal = ref(false);
2024-04-10 14:00:55 +07:00
const status = ref(false);
const userId = ref('');
const code = ref('');
2024-04-10 14:00:55 +07:00
const selectorLabel = ref('');
2024-04-05 19:05:51 +07:00
const hqId = ref('');
2024-04-10 14:00:55 +07:00
const brId = ref('');
const username = ref('');
const formDialogRef = ref();
const userStats = ref<BranchUserStats[]>();
const typeStats = ref<UserTypeStats>();
const formData = ref<UserCreate>({
provinceId: null,
districtId: null,
subDistrictId: null,
telephoneNo: '',
email: '',
zipCode: '',
gender: '',
addressEN: '',
address: '',
trainingPlace: null,
importNationality: null,
sourceNationality: null,
licenseExpireDate: null,
licenseIssueDate: null,
licenseNo: null,
discountCondition: '',
retireDate: null,
startDate: null,
registrationNo: null,
lastNameEN: '',
lastName: '',
firstNameEN: '',
firstName: '',
userRole: '',
userType: '',
profileImage: null,
birthDate: null,
responsibleArea: '',
});
2024-04-05 19:05:51 +07:00
2024-04-10 19:32:15 +07:00
const profileFile = ref<File | undefined>(undefined);
const inputFile = document.createElement('input');
inputFile.type = 'file';
inputFile.accept = 'image/*';
2024-04-17 11:32:18 +07:00
const reader = new FileReader();
reader.addEventListener('load', () => {
if (typeof reader.result === 'string') {
urlProfile.value = reader.result;
}
});
watch(profileFile, () => {
if (profileFile.value) reader.readAsDataURL(profileFile.value);
});
2024-04-10 19:32:15 +07:00
inputFile.addEventListener('change', (e) => {
profileFile.value = (e.currentTarget as HTMLInputElement).files?.[0];
});
2024-04-04 15:03:39 +07:00
const selectorList = [
{ label: 'USER', count: 0 },
{ label: 'MESSENGER', count: 0 },
{ label: 'DELEGATE', count: 0 },
{ label: 'AGENCY', count: 0 },
2024-04-10 14:00:55 +07:00
];
2024-04-11 15:05:01 +07:00
async function openDialog(idEdit?: string) {
2024-04-05 19:05:51 +07:00
modal.value = true;
userStore.userOption.brOpts = [];
userStore.userOption.hqOpts.length === 0
? await userStore.fetchHqOption()
: '';
userStore.userOption.roleOpts.length === 0
? await userStore.fetchRoleOption()
: '';
2024-04-11 15:05:01 +07:00
if (idEdit && userData.value) {
const foundUser = userData.value.result.find((user) => user.id === idEdit);
2024-04-10 20:28:12 +07:00
if (foundUser) {
2024-04-10 20:28:12 +07:00
formData.value = {
2024-04-11 00:37:07 +07:00
provinceId: foundUser.provinceId,
districtId: foundUser.districtId,
subDistrictId: foundUser.subDistrictId,
telephoneNo: foundUser.telephoneNo,
email: foundUser.email,
zipCode: foundUser.zipCode,
gender: foundUser.gender,
addressEN: foundUser.addressEN,
address: foundUser.address,
trainingPlace: foundUser.trainingPlace,
importNationality: foundUser.importNationality,
sourceNationality: foundUser.sourceNationality,
licenseNo: foundUser.licenseNo,
discountCondition: foundUser.discountCondition,
registrationNo: foundUser.registrationNo,
lastNameEN: foundUser.lastNameEN,
lastName: foundUser.lastName,
firstNameEN: foundUser.firstNameEN,
firstName: foundUser.firstName,
userRole: foundUser.userRole,
userType: foundUser.userType,
responsibleArea: foundUser.responsibleArea,
2024-04-10 20:28:12 +07:00
licenseExpireDate:
(foundUser.licenseExpireDate &&
new Date(foundUser.licenseExpireDate)) ||
null,
licenseIssueDate:
(foundUser.licenseIssueDate &&
new Date(foundUser.licenseIssueDate)) ||
null,
retireDate:
(foundUser.retireDate && new Date(foundUser.retireDate)) || null,
startDate:
(foundUser.startDate && new Date(foundUser.startDate)) || null,
birthDate:
(foundUser.birthDate && new Date(foundUser.birthDate)) || null,
};
foundUser.status === 'ACTIVE'
? (status.value = true)
: (status.value = false);
userId.value = foundUser.id;
hqId.value = foundUser.branch[0].headOfficeId as string;
brId.value = foundUser.branch[0].id;
code.value = foundUser.code;
urlProfile.value = foundUser.profileImageUrl;
isEdit.value = true;
await userStore.fetchBrOption(hqId.value);
await formDialogRef.value.fetchSubDistrict(formData.value.districtId);
await formDialogRef.value.fetchDistrict(formData.value.provinceId);
}
}
2024-04-05 19:05:51 +07:00
}
2024-04-10 14:00:55 +07:00
function onClose() {
code.value = '';
2024-04-10 14:00:55 +07:00
hqId.value = '';
brId.value = '';
userId.value = '';
username.value = '';
urlProfile.value = '';
modal.value = false;
status.value = false;
isEdit.value = false;
2024-04-11 00:37:07 +07:00
mapUserType(selectorLabel.value);
Object.assign(formData.value, defaultFormData);
2024-04-10 14:00:55 +07:00
}
async function onSubmit() {
2024-04-10 19:32:15 +07:00
formData.value.profileImage = profileFile.value as File;
if (isEdit.value === true && userId.value) {
2024-04-11 00:37:07 +07:00
dialog({
color: 'primary',
icon: 'mdi-pencil-outline',
title: 'ยืนยันการแก้ไขข้อมูล',
actionText: 'ตกลง',
persistent: true,
message: 'คุณต้องการแก้ไขข้อมูล ใช่หรือไม่',
action: async () => {
const formDataEdit = { ...formData.value };
await userStore.editById(userId.value, formDataEdit);
onClose();
userStore.fetchList({ includeBranch: true });
},
});
} else {
2024-04-11 00:37:07 +07:00
dialog({
color: 'primary',
icon: 'mdi-account',
title: 'ยืนยันการเพิ่มบุคลากร',
actionText: 'ตกลง',
persistent: true,
message: 'คุณต้องการเพิ่มบุคลากร ใช่หรือไม่',
action: async () => {
2024-04-17 11:34:14 +07:00
const result = await userStore.create(formData.value);
if (result) {
await branchStore.addUser(brId.value, result.id);
2024-04-11 00:37:07 +07:00
}
onClose();
userStore.fetchList({ includeBranch: true });
},
});
2024-04-10 14:00:55 +07:00
}
}
async function onDelete(id: string) {
2024-04-11 00:37:07 +07:00
dialog({
color: 'negative',
icon: 'mdi-trash-can-outline',
title: 'ยืนยันการลบข้อมูล',
actionText: 'ตกลง',
persistent: true,
message: 'คุณต้องการลบข้อมูล ใช่หรือไม่',
action: async () => {
await userStore.deleteById(id);
await userStore.fetchList({ includeBranch: true });
},
});
2024-04-10 14:00:55 +07:00
}
2024-04-11 09:37:18 +07:00
function cardClick(id: string) {
router.push({ name: 'PersonnelInfo', params: { id } });
}
2024-04-10 14:00:55 +07:00
function mapUserType(label: string) {
if (label === 'personnelSelector1') {
formData.value.userType = 'USER';
} else if (label === 'personnelSelector2') {
formData.value.userType = 'MESSENGER';
} else if (label === 'personnelSelector3') {
formData.value.userType = 'DELEGATE';
} else if (label === 'personnelSelector4') {
formData.value.userType = 'AGENCY';
}
}
onMounted(async () => {
await userStore.fetchList({ includeBranch: true });
userStore.userOption.roleOpts.length === 0
? await userStore.fetchRoleOption()
: '';
2024-04-10 14:00:55 +07:00
typeStats.value = await userStore.typeStats();
const res = await branchStore.userStats(formData.value.userType);
if (res) {
userStats.value = res;
2024-04-10 14:00:55 +07:00
}
});
watch(
() => selectorLabel.value,
async (label) => {
mapUserType(label);
const res = await branchStore.userStats(label);
if (res) {
userStats.value = res;
2024-04-10 14:00:55 +07:00
}
},
);
2024-04-03 18:28:59 +07:00
</script>
<template>
2024-04-04 15:03:39 +07:00
<div class="column q-pb-lg">
<div class="text-h6 text-weight-bold q-mb-md">
{{ $t('personnelManagement') }}
</div>
2024-04-04 15:03:39 +07:00
<div class="row full-width q-mb-md no-wrap">
<!-- selector -->
<SelectorList
:list="selectorList"
v-model:selector="selectorLabel"
class="q-mr-md col-4"
/>
2024-04-04 15:03:39 +07:00
<!-- stat -->
2024-04-05 10:37:57 +07:00
<AppBox bordered class="column full-width">
2024-04-05 19:05:51 +07:00
<div class="row q-pb-lg justify-between items-center">
<div class="text-weight-bold text-subtitle1">
{{ $t('personnelStatTitle') }}
{{ selectorLabel === '' ? '' : $t(selectorLabel) }}
2024-04-05 19:05:51 +07:00
</div>
<q-btn
dense
unelevated
label="+ เพิ่มบุคลากร"
padding="4px 16px"
@click="openDialog()"
2024-04-05 19:05:51 +07:00
style="background-color: var(--cyan-6); color: white"
/>
</div>
2024-04-12 08:02:01 +07:00
<div class="row full-width q-py-md" style="overflow-x: auto">
2024-04-10 14:00:55 +07:00
<StatCardComponent
v-if="userStats"
:branch="
userStats.map((v) => ({
2024-04-16 18:34:01 +07:00
count: v.count,
2024-04-10 14:00:55 +07:00
label: $i18n.locale === 'en-US' ? v.nameEN : v.name,
}))
"
2024-04-11 11:46:26 +07:00
:dark="$q.dark.isActive"
2024-04-10 14:00:55 +07:00
class="no-wrap"
/>
</div>
2024-04-04 15:03:39 +07:00
</AppBox>
</div>
2024-04-03 18:28:59 +07:00
2024-04-04 15:03:39 +07:00
<!-- main -->
2024-04-16 18:34:01 +07:00
<AppBox bordered style="width: 100%; height: 70vh; overflow-y: auto">
2024-04-05 17:26:40 +07:00
<PersonCard
:list="
userData?.result.map((v) => ({
2024-04-10 14:00:55 +07:00
id: v.id,
2024-04-05 17:26:40 +07:00
img: `${v.profileImageUrl}`,
2024-04-11 15:05:01 +07:00
name:
$i18n.locale === 'en-US'
? `${v.firstNameEN} ${v.lastNameEN}`
: `${v.firstName} ${v.lastName}`,
2024-04-05 17:26:40 +07:00
male: v.gender === 'male',
female: v.gender === 'female',
detail: [
{ label: 'ประเภท', value: $t(v.userType) },
{
label: 'ตำแหน่ง',
value: v.userRole
? userStore.userOption.roleOpts.find(
(r) => r.value === v.userRole,
)?.label || ''
: '',
},
2024-04-10 14:00:55 +07:00
{ label: 'โทรศัพท์', value: v.telephoneNo },
{
label: 'อายุ',
value: userStore.calculateAge(v.birthDate as Date),
},
2024-04-05 17:26:40 +07:00
],
2024-04-10 14:00:55 +07:00
badge: v.code,
2024-04-05 17:26:40 +07:00
disabled: v.status === 'INACTIVE',
})) || []
"
@updateCard="openDialog"
2024-04-10 14:00:55 +07:00
@deleteCard="onDelete"
2024-04-11 09:37:18 +07:00
@enterCard="cardClick"
2024-04-05 17:26:40 +07:00
/>
<div
class="column"
style="height: 100%"
v-if="userData && userData.total === 0"
>
<div class="col-1 self-end">
<div class="row">
2024-04-05 17:43:16 +07:00
<TooltipComponent
title="personnelTooltipTitle"
caption="personnelTooltipCaption"
imgSrc="personnel-table-"
/>
</div>
</div>
<div class="col self-center" style="display: flex; align-items: center">
<AddButton
2024-04-04 18:08:37 +07:00
:label="'personnelAdd'"
2024-04-05 09:24:10 +07:00
:cyanOn="true"
2024-04-05 19:05:51 +07:00
@trigger="openDialog"
/>
</div>
</div>
2024-04-03 18:28:59 +07:00
</AppBox>
</div>
2024-04-05 19:05:51 +07:00
2024-04-10 14:00:55 +07:00
<!-- form -->
2024-04-05 19:05:51 +07:00
<FormDialog
ref="formDialogRef"
2024-04-05 19:05:51 +07:00
v-model:modal="modal"
2024-04-10 14:00:55 +07:00
v-model:address="formData.address"
v-model:addressEN="formData.addressEN"
v-model:provinceId="formData.provinceId"
v-model:districtId="formData.districtId"
v-model:subDistrictId="formData.subDistrictId"
2024-04-10 14:00:55 +07:00
v-model:zipCode="formData.zipCode"
2024-04-05 19:05:51 +07:00
title="เพิ่มบุคลากร"
2024-04-10 14:00:55 +07:00
addressTitle="ที่อยู่พนักงาน"
addressENTitle="ที่อยู่พนักงาน ENG"
:submit="() => onSubmit()"
2024-04-10 14:00:55 +07:00
:close="() => onClose()"
2024-04-05 19:05:51 +07:00
>
<template #top>
<FormTop
2024-04-10 14:00:55 +07:00
dense
outlined
v-model:hqId="hqId"
v-model:brId="brId"
v-model:userType="formData.userType"
v-model:userRole="formData.userRole"
v-model:userName="username"
v-model:userCode="code"
2024-04-10 14:00:55 +07:00
/>
2024-04-05 19:05:51 +07:00
</template>
<template #prepend>
2024-04-10 14:00:55 +07:00
<div class="q-pl-md text-center">
<div class="upload-img-preview">
<q-img
v-if="urlProfile"
:src="urlProfile"
style="object-fit: cover; width: 100%; height: 100%"
/>
2024-04-10 14:00:55 +07:00
<q-icon
v-else
2024-04-10 14:00:55 +07:00
name="mdi-account full-height"
size="3vw"
style="color: var(--border-color)"
/>
</div>
<q-btn
v-if="urlProfile"
dense
unelevated
outlined
padding="8px"
icon="mdi-pencil-outline"
class="edit-img-btn q-my-md full-width"
label="แก้ไขโปรไฟล์"
@click="inputFile.click()"
/>
<q-btn
v-else
2024-04-10 14:00:55 +07:00
dense
unelevated
outlined
padding="8px"
class="upload-img-btn q-my-md full-width"
label="อัปโหลดรูปภาพ"
:class="{ dark: $q.dark.isActive }"
2024-04-10 19:32:15 +07:00
@click="inputFile.click()"
2024-04-10 14:00:55 +07:00
/>
<div class="text-left">
<q-toggle
dense
size="md"
color="primary"
v-model="status"
padding="none"
class="q-pr-md"
/>
<span>สถานะผใชงาน</span>
</div>
</div>
2024-04-05 19:05:51 +07:00
</template>
<template #midTop>
<FormName
2024-04-10 14:00:55 +07:00
dense
outlined
v-model:firstName="formData.firstName"
v-model:lastName="formData.lastName"
v-model:firstNameEN="formData.firstNameEN"
v-model:lastNameEN="formData.lastNameEN"
2024-04-10 14:00:55 +07:00
/>
2024-04-05 19:05:51 +07:00
</template>
<template #midBottom v-if="formData.userType">
<FormByType
2024-04-05 19:05:51 +07:00
dense
outlined
v-model:userType="formData.userType"
v-model:telephoneNo="formData.telephoneNo"
v-model:gender="formData.gender"
v-model:email="formData.email"
v-model:registrationNo="formData.registrationNo"
v-model:startDate="formData.startDate"
v-model:retireDate="formData.retireDate"
v-model:birthDate="formData.birthDate"
v-model:responsibleArea="formData.responsibleArea"
v-model:discountCondition="formData.discountCondition"
v-model:sourceNationality="formData.sourceNationality"
v-model:importNationality="formData.importNationality"
v-model:trainingPlace="formData.trainingPlace"
2024-04-05 19:05:51 +07:00
/>
2024-04-10 14:00:55 +07:00
</template>
2024-04-05 19:05:51 +07:00
</FormDialog>
2024-04-03 18:28:59 +07:00
</template>
2024-04-05 19:05:51 +07:00
<style>
.bg-white {
background-color: var(--surface-1) !important;
}
2024-04-10 14:00:55 +07:00
.upload-img-preview {
border: 1px solid var(--border-color);
border-radius: var(--radius-2);
height: 12vw;
}
.upload-img-btn {
color: hsl(var(--info-bg));
border: 1px solid hsl(var(--info-bg));
background-color: hsla(var(--info-bg) / 0.1);
border-radius: var(--radius-2);
&.dark {
background-color: transparent;
}
}
.edit-img-btn {
color: hsl(var(--info-bg));
border: 1px solid hsl(var(--info-bg));
background-color: transparent;
border-radius: var(--radius-2);
}
2024-04-05 19:05:51 +07:00
</style>