2024-05-30 14:04:48 +07:00
|
|
|
<script setup lang="ts">
|
|
|
|
|
import { ref, reactive, watch } from "vue";
|
|
|
|
|
import { useQuasar } from "quasar";
|
|
|
|
|
import http from "@/plugins/http";
|
|
|
|
|
import config from "@/app.config";
|
|
|
|
|
|
2024-05-31 15:08:11 +07:00
|
|
|
import type {
|
|
|
|
|
FormUser,
|
|
|
|
|
Roles,
|
|
|
|
|
} from "@/modules/02_users/interface/request/Main";
|
2024-05-30 14:04:48 +07:00
|
|
|
|
|
|
|
|
/** importComponents*/
|
|
|
|
|
import DialogHeader from "@/components/DialogHeader.vue";
|
|
|
|
|
|
|
|
|
|
/** importStore*/
|
|
|
|
|
import { useCounterMixin } from "@/stores/mixin";
|
|
|
|
|
|
|
|
|
|
/** use*/
|
|
|
|
|
const $q = useQuasar();
|
|
|
|
|
const { showLoader, hideLoader, dialogConfirm, messageError, success } =
|
|
|
|
|
useCounterMixin();
|
|
|
|
|
|
|
|
|
|
/** props*/
|
|
|
|
|
const modal = defineModel<boolean>("modal", { required: true });
|
|
|
|
|
const isStatusEdit = defineModel<boolean>("isStatusEdit", { required: true });
|
|
|
|
|
const userId = defineModel<string>("userId", { required: true });
|
|
|
|
|
const props = defineProps({
|
|
|
|
|
fetchlist: {
|
|
|
|
|
type: Function,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const isPwd = ref<boolean>(true);
|
2024-05-31 15:08:11 +07:00
|
|
|
const roles = ref<Roles[]>([]);
|
2024-05-30 14:04:48 +07:00
|
|
|
const formData = reactive<FormUser>({
|
|
|
|
|
username: "",
|
|
|
|
|
password: "",
|
|
|
|
|
firstName: "",
|
|
|
|
|
lastName: "",
|
|
|
|
|
email: "",
|
2024-05-31 15:08:11 +07:00
|
|
|
roles: [],
|
2024-05-30 14:04:48 +07:00
|
|
|
});
|
2024-05-31 15:08:11 +07:00
|
|
|
const roleOptionsMain = ref([]);
|
|
|
|
|
const roleOptions = ref([]);
|
2024-05-30 14:04:48 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* function fetch ข้อมูลผู้ใช้งาน
|
|
|
|
|
* @param id ผู้ใช้งาน
|
|
|
|
|
*/
|
|
|
|
|
function fetchUserDetail(id: string) {
|
|
|
|
|
showLoader();
|
|
|
|
|
http
|
|
|
|
|
.get(config.API.managementUser + `/${id}`)
|
|
|
|
|
.then((res) => {
|
|
|
|
|
const data = res.data;
|
|
|
|
|
formData.username = data.username;
|
|
|
|
|
formData.firstName = data.firstName;
|
|
|
|
|
formData.lastName = data.lastName;
|
|
|
|
|
formData.email = data.email;
|
2024-05-31 15:08:11 +07:00
|
|
|
roles.value = data.roles;
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
messageError($q, err);
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
hideLoader();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** function fetc รายการ role*/
|
|
|
|
|
function fetchlistRole() {
|
|
|
|
|
showLoader();
|
|
|
|
|
http
|
|
|
|
|
.get(config.API.managementRole)
|
|
|
|
|
.then((res) => {
|
|
|
|
|
roleOptions.value = res.data;
|
2024-05-30 14:04:48 +07:00
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
messageError($q, err);
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
hideLoader();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** function บันทึกข้อมูลผู้ใช้งาน*/
|
|
|
|
|
function onSubmit() {
|
2024-05-31 15:08:11 +07:00
|
|
|
formData.roles = !isStatusEdit.value
|
|
|
|
|
? roles.value.map((e: { id: string }) => e.id)
|
|
|
|
|
: undefined;
|
|
|
|
|
formData.password = !isStatusEdit.value ? formData.password : undefined;
|
|
|
|
|
|
2024-05-30 14:04:48 +07:00
|
|
|
dialogConfirm($q, () => {
|
|
|
|
|
showLoader();
|
|
|
|
|
const url = isStatusEdit.value
|
|
|
|
|
? config.API.managementUser + `/${userId.value}`
|
|
|
|
|
: config.API.managementUser;
|
|
|
|
|
|
|
|
|
|
http[isStatusEdit.value ? "put" : "post"](url, formData)
|
|
|
|
|
.then(() => {
|
|
|
|
|
success($q, "บันทึกข้อมูลสำเร็จ");
|
|
|
|
|
props.fetchlist?.();
|
|
|
|
|
closeDialog();
|
|
|
|
|
})
|
|
|
|
|
.catch((e) => {
|
|
|
|
|
messageError($q, e);
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
hideLoader();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** function ปิด Dialog */
|
|
|
|
|
function closeDialog() {
|
|
|
|
|
modal.value = false;
|
|
|
|
|
formData.username = "";
|
|
|
|
|
formData.password = "";
|
|
|
|
|
formData.firstName = "";
|
|
|
|
|
formData.lastName = "";
|
|
|
|
|
formData.email = "";
|
2024-05-31 15:08:11 +07:00
|
|
|
roles.value = [];
|
2024-05-30 14:04:48 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
watch(
|
|
|
|
|
() => modal.value,
|
|
|
|
|
() => {
|
|
|
|
|
if (modal.value && isStatusEdit.value) {
|
|
|
|
|
fetchUserDetail(userId.value);
|
|
|
|
|
}
|
2024-05-31 15:08:11 +07:00
|
|
|
modal.value && fetchlistRole();
|
2024-05-30 14:04:48 +07:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
|
|
|
|
<q-dialog v-model="modal" persistent>
|
|
|
|
|
<q-card class="col-12" style="width: 25%">
|
|
|
|
|
<q-form greedy @submit.prevent @validation-success="onSubmit">
|
|
|
|
|
<DialogHeader
|
|
|
|
|
:tittle="isStatusEdit ? 'แก้ไขผู้ใช้งาน' : 'เพิ่มผู้ใช้งาน'"
|
|
|
|
|
:close="closeDialog"
|
|
|
|
|
/>
|
|
|
|
|
<q-separator />
|
|
|
|
|
<q-card-section>
|
|
|
|
|
<div class="row q-col-gutter-md">
|
|
|
|
|
<div class="col-12">
|
|
|
|
|
<q-input
|
|
|
|
|
:readonly="isStatusEdit"
|
|
|
|
|
v-model="formData.username"
|
|
|
|
|
outlined
|
|
|
|
|
dense
|
|
|
|
|
:rules="[(val:string) => !!val || `${'กรุณากรอกชื่อผู้ใช้งาน'}`,]"
|
|
|
|
|
lazy-rules
|
|
|
|
|
hide-bottom-space
|
|
|
|
|
class="inputgreen"
|
|
|
|
|
label="ชื่อผู้ใช้งาน"
|
|
|
|
|
>
|
|
|
|
|
</q-input>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="col-12" v-if="!isStatusEdit">
|
|
|
|
|
<q-input
|
|
|
|
|
v-model="formData.password"
|
|
|
|
|
outlined
|
|
|
|
|
dense
|
|
|
|
|
:type="isPwd ? 'password' : 'text'"
|
|
|
|
|
:rules="[(val:string) => !!val || `${'กรุณากรอกรหัสผู้ใช้งาน'}`,]"
|
|
|
|
|
lazy-rules
|
|
|
|
|
label="รหัสผู้ใช้งาน"
|
|
|
|
|
hide-bottom-space
|
|
|
|
|
class="inputgreen"
|
|
|
|
|
>
|
|
|
|
|
<template v-slot:append>
|
|
|
|
|
<q-icon
|
|
|
|
|
:name="isPwd ? 'visibility_off' : 'visibility'"
|
|
|
|
|
class="cursor-pointer"
|
|
|
|
|
@click="isPwd = !isPwd"
|
|
|
|
|
/>
|
|
|
|
|
</template>
|
|
|
|
|
</q-input>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="col-12">
|
|
|
|
|
<q-input
|
|
|
|
|
v-model="formData.firstName"
|
|
|
|
|
outlined
|
|
|
|
|
dense
|
|
|
|
|
:rules="[(val:string) => !!val || `${'กรุณากรอกชื่อ'}`,]"
|
|
|
|
|
lazy-rules
|
|
|
|
|
hide-bottom-space
|
|
|
|
|
class="inputgreen"
|
|
|
|
|
label="ชื่อ"
|
|
|
|
|
>
|
|
|
|
|
</q-input>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="col-12">
|
|
|
|
|
<q-input
|
|
|
|
|
v-model="formData.lastName"
|
|
|
|
|
outlined
|
|
|
|
|
dense
|
|
|
|
|
:rules="[(val:string) => !!val || `${'กรุณากรอกนามสกุล'}`,]"
|
|
|
|
|
lazy-rules
|
|
|
|
|
hide-bottom-space
|
|
|
|
|
class="inputgreen"
|
|
|
|
|
label="นามสกุล"
|
|
|
|
|
>
|
|
|
|
|
</q-input>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div class="col-12">
|
|
|
|
|
<q-input
|
|
|
|
|
v-model="formData.email"
|
|
|
|
|
outlined
|
|
|
|
|
dense
|
|
|
|
|
:rules="[(val:string) => !!val || `${'กรุณากรอก Email'}`,]"
|
|
|
|
|
lazy-rules
|
|
|
|
|
hide-bottom-space
|
|
|
|
|
class="inputgreen"
|
|
|
|
|
label="Email"
|
|
|
|
|
type="email"
|
|
|
|
|
>
|
|
|
|
|
</q-input>
|
|
|
|
|
</div>
|
2024-05-31 15:08:11 +07:00
|
|
|
<div class="col-12" v-if="!isStatusEdit">
|
|
|
|
|
<q-select
|
|
|
|
|
v-model="roles"
|
|
|
|
|
outlined
|
|
|
|
|
label="role"
|
|
|
|
|
dense
|
|
|
|
|
option-label="name"
|
|
|
|
|
option-value="id"
|
|
|
|
|
:options="roleOptions"
|
|
|
|
|
class="inputgreen"
|
|
|
|
|
map-options
|
|
|
|
|
hide-bottom-space
|
|
|
|
|
lazy-rules
|
|
|
|
|
use-chips
|
|
|
|
|
multiple
|
|
|
|
|
use-input
|
|
|
|
|
:rules="[(val:any) => !!val && val.length > 0|| `${'กรุณาเลือก Role'}`,]"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2024-05-30 14:04:48 +07:00
|
|
|
</div>
|
|
|
|
|
</q-card-section>
|
|
|
|
|
<q-separator />
|
|
|
|
|
<q-card-actions align="right">
|
|
|
|
|
<q-btn label="บันทึก" color="secondary" type="submit"
|
|
|
|
|
><q-tooltip>บันทึกข้อมูล</q-tooltip></q-btn
|
|
|
|
|
>
|
|
|
|
|
</q-card-actions>
|
|
|
|
|
</q-form>
|
|
|
|
|
</q-card>
|
|
|
|
|
</q-dialog>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<style scoped></style>
|