545 lines
20 KiB
Vue
545 lines
20 KiB
Vue
<script setup lang="ts">
|
|
import { ref, reactive, watch } from "vue";
|
|
import { useQuasar } from "quasar";
|
|
|
|
import http from "@/plugins/http";
|
|
import config from "@/app.config";
|
|
import { useCounterMixin } from "@/stores/mixin";
|
|
|
|
/**
|
|
* importType
|
|
*/
|
|
import type {
|
|
DataOption,
|
|
FormPositionSelect,
|
|
} from "@/modules/02_organization/interface/index/Main";
|
|
import type {
|
|
DataPosType,
|
|
DataLevel,
|
|
} from "@/modules/02_organization/interface/response/Main";
|
|
import type {
|
|
OptionType,
|
|
OptionExecutive,
|
|
} from "@/modules/02_organization/interface/response/organizational";
|
|
|
|
/**
|
|
* importComponents
|
|
*/
|
|
import DialogHeader from "@/components/DialogHeader.vue";
|
|
|
|
/**
|
|
* use
|
|
*/
|
|
const $q = useQuasar();
|
|
const mixin = useCounterMixin();
|
|
const { dialogConfirm, showLoader, hideLoader, messageError, success } = mixin;
|
|
|
|
/**
|
|
* props
|
|
*/
|
|
const modal = defineModel<boolean>("modalAdd", { required: true }); // เปิด,ปิด popup
|
|
const isEditCheck = defineModel<boolean>("isEdit", { required: true }); // สถานะแก้ไข
|
|
const props = defineProps({
|
|
emitSearch: Function,
|
|
getData: Function,
|
|
data: Object,
|
|
levelOp: Object,
|
|
});
|
|
|
|
const isReadonly = ref<boolean>(false); // อ่านได้อย่างเดียว
|
|
|
|
const dataLevel = ref<DataPosType[]>([]); // ข้อมูลประเภทตำแหน่ง
|
|
const posExecutive = ref<string>(""); // ชื่อตำแหน่งทางการบริหาร
|
|
const isSpecial = ref<boolean>(false); // เฉพาะสายงานที่กำหนด
|
|
const shape = ref<string>("false"); // ตำแหน่งทางการบริหาร
|
|
const isDisValidate = ref<boolean>(false);
|
|
|
|
const typeOpsMain = ref<DataOption[]>([]); //ตัวเลือกประเภทตำแหน่ง
|
|
const levelOpsMain = ref<DataOption[]>([]); //ตัวเลือกระดับตำแหน่ง
|
|
const executiveOpsMain = ref<DataOption[]>([]); //ตัวเลือกรายการตำแหน่งทางการบริหาร
|
|
const typeOps = ref<DataOption[]>([]); //ตัวเลือกประเภทตำแหน่ง
|
|
const levelOps = ref<DataOption[]>([]); //ตัวเลือกระดับตำแหน่ง
|
|
const executiveOps = ref<DataOption[]>([]); //ตัวเลือกรายการตำแหน่งทางการบริหาร
|
|
const shapeOp = ref<DataOption[]>([
|
|
{
|
|
id: "false",
|
|
name: "เลือกจากรายการ",
|
|
},
|
|
{
|
|
id: "true",
|
|
name: "เพิ่มใหม่",
|
|
},
|
|
]);
|
|
|
|
//ฟอร์มข้อมูลตำแหน่ง
|
|
const formPositionSelect = reactive<FormPositionSelect>({
|
|
positionId: "",
|
|
positionName: "",
|
|
positionField: "",
|
|
positionType: "",
|
|
positionLevel: "",
|
|
positionExecutive: "",
|
|
positionExecutiveField: "",
|
|
positionArea: "",
|
|
});
|
|
|
|
/**
|
|
* ฟังก์ชั่นตรวจสอบความถูกต้องของข้อมูลในฟอร์ม
|
|
*/
|
|
function validateFormPositionEdit() {
|
|
isDisValidate.value = false;
|
|
if (isEditCheck.value == true) {
|
|
saveSelectEdit();
|
|
} else {
|
|
onSubmitSelectEdit();
|
|
}
|
|
// }
|
|
}
|
|
|
|
/**
|
|
* ฟังก์ชั่นบันทึกการแก้ไขตำแหน่ง
|
|
*/
|
|
function saveSelectEdit() {
|
|
dialogConfirm(
|
|
$q,
|
|
async () => {
|
|
const dataExecutive =
|
|
shape.value == "false"
|
|
? formPositionSelect.positionExecutive
|
|
: posExecutive.value;
|
|
const url =
|
|
shape.value == "false"
|
|
? config.API.orgPosPositionById(formPositionSelect.positionId)
|
|
: config.API.orgPosPositionExecutive() +
|
|
`/${formPositionSelect.positionId}`;
|
|
showLoader();
|
|
const body =
|
|
shape.value == "false"
|
|
? {
|
|
posDictName: formPositionSelect.positionName,
|
|
posDictField: formPositionSelect.positionField, //สายงาน
|
|
posTypeId: formPositionSelect.positionType, //*ประเภทตำแหน่ง
|
|
posLevelId: formPositionSelect.positionLevel, //*ระดับตำแหน่ง
|
|
posExecutiveId: dataExecutive ?? "", //ตำแหน่งทางการบริหาร
|
|
posDictExecutiveField:
|
|
formPositionSelect.positionExecutiveField ?? "", //ด้านทางการบริหาร
|
|
posDictArea: formPositionSelect.positionArea, //ด้าน/สาขา
|
|
isSpecial: isSpecial.value,
|
|
}
|
|
: {
|
|
posDictName: formPositionSelect.positionName,
|
|
posDictField: formPositionSelect.positionField, //สายงาน
|
|
posTypeId: formPositionSelect.positionType, //*ประเภทตำแหน่ง
|
|
posLevelId: formPositionSelect.positionLevel, //*ระดับตำแหน่ง
|
|
posExecutive: dataExecutive ?? "", //ตำแหน่งทางการบริหาร
|
|
posDictExecutiveField: formPositionSelect.positionExecutiveField, //ด้านทางการบริหาร
|
|
posDictArea: formPositionSelect.positionArea, //ด้าน/สาขา
|
|
isSpecial: isSpecial.value,
|
|
};
|
|
await http
|
|
.put(url, body)
|
|
.then(async () => {
|
|
await props.getData?.();
|
|
await success($q, "เพิ่มข้อมูลสำเร็จ");
|
|
close();
|
|
})
|
|
.catch((err) => {
|
|
messageError($q, err);
|
|
})
|
|
.finally(() => {
|
|
hideLoader();
|
|
});
|
|
},
|
|
"ยืนยันการแก้ไขตำแหน่ง",
|
|
"ต้องการยืนยันแก้ไขตำแหน่งนี้ใช่หรือไม่?"
|
|
);
|
|
}
|
|
/**
|
|
* ฟังชั่นบันทึกการเพิ่มข้อมูลตำแหน่ง
|
|
*/
|
|
function onSubmitSelectEdit() {
|
|
dialogConfirm(
|
|
$q,
|
|
async () => {
|
|
const dataExecutive =
|
|
shape.value == "false"
|
|
? formPositionSelect.positionExecutive
|
|
: posExecutive.value;
|
|
const url =
|
|
shape.value == "false"
|
|
? config.API.orgPosPosition
|
|
: config.API.orgPosPositionExecutive();
|
|
showLoader();
|
|
|
|
const body =
|
|
shape.value == "false"
|
|
? {
|
|
posDictName: formPositionSelect.positionName,
|
|
posDictField: formPositionSelect.positionField, //สายงาน
|
|
posTypeId: formPositionSelect.positionType, //*ประเภทตำแหน่ง
|
|
posLevelId: formPositionSelect.positionLevel, //*ระดับตำแหน่ง
|
|
posExecutiveId: dataExecutive ?? "", //ตำแหน่งทางการบริหาร
|
|
posDictExecutiveField: formPositionSelect.positionExecutiveField, //ด้านทางการบริหาร
|
|
posDictArea: formPositionSelect.positionArea, //ด้าน/สาขา
|
|
isSpecial: isSpecial.value,
|
|
}
|
|
: {
|
|
posDictName: formPositionSelect.positionName,
|
|
posDictField: formPositionSelect.positionField, //สายงาน
|
|
posTypeId: formPositionSelect.positionType, //*ประเภทตำแหน่ง
|
|
posLevelId: formPositionSelect.positionLevel, //*ระดับตำแหน่ง
|
|
posExecutive: dataExecutive ?? "", //ตำแหน่งทางการบริหาร
|
|
posDictExecutiveField: formPositionSelect.positionExecutiveField, //ด้านทางการบริหาร
|
|
posDictArea: formPositionSelect.positionArea, //ด้าน/สาขา
|
|
isSpecial: isSpecial.value,
|
|
};
|
|
|
|
await http
|
|
.post(url, body)
|
|
.then(async () => {
|
|
await props.emitSearch?.(
|
|
formPositionSelect.positionName,
|
|
"positionName"
|
|
);
|
|
await success($q, "เพิ่มข้อมูลสำเร็จ");
|
|
close();
|
|
})
|
|
.catch((err) => {
|
|
messageError($q, err);
|
|
})
|
|
.finally(() => {
|
|
hideLoader();
|
|
});
|
|
},
|
|
"ยืนยันการเพิ่มตำแหน่ง",
|
|
"ต้องการยืนยันการเพิ่มตำแหน่งนี้ใช่หรือไม่?"
|
|
);
|
|
}
|
|
|
|
/**
|
|
* ฟังชั่นเคลียร์ฟอร์มข้อมูล
|
|
*
|
|
* กำหนดค่าฟอร์มข้อมูลเป็นค่า Default
|
|
*/
|
|
async function clearFormPositionSelect() {
|
|
isEditCheck.value = false;
|
|
isDisValidate.value = true;
|
|
formPositionSelect.positionId = "";
|
|
formPositionSelect.positionName = "";
|
|
formPositionSelect.positionField = "";
|
|
formPositionSelect.positionType = "";
|
|
formPositionSelect.positionLevel = "";
|
|
formPositionSelect.positionExecutive = "";
|
|
formPositionSelect.positionExecutiveField = "";
|
|
formPositionSelect.positionArea = "";
|
|
shape.value = "false";
|
|
posExecutive.value = "";
|
|
isSpecial.value = false;
|
|
setTimeout(async () => {
|
|
isDisValidate.value = false;
|
|
}, 1000);
|
|
}
|
|
|
|
/**
|
|
* ฟังก์เรียกข้อมูลรายการประเภทตำแหน่ง
|
|
*/
|
|
async function fetchType() {
|
|
await http
|
|
.get(config.API.orgPosType)
|
|
.then(async (res) => {
|
|
dataLevel.value = await res.data.result;
|
|
typeOpsMain.value = res.data.result.map((e: OptionType) => ({
|
|
id: e.id,
|
|
name: e.posTypeName,
|
|
}));
|
|
typeOps.value = typeOpsMain.value;
|
|
})
|
|
.catch((err) => {
|
|
messageError($q, err);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* ฟังก์ชันกำหนดตัวเลือกประเภทตำแหน่งตาม id ประเภทตำแหน่ง
|
|
* @param val id ประเภทตำแหน่ง
|
|
*/
|
|
async function updateSelectType(val: string) {
|
|
// หาระดับตำแหน่ง
|
|
const listLevel = dataLevel.value.find((e: DataPosType) => e.id === val);
|
|
levelOpsMain.value =
|
|
listLevel?.posLevels.map((e: DataLevel) => ({
|
|
id: e.id,
|
|
name: e.posLevelName ? e.posLevelName.toString() : "",
|
|
})) || [];
|
|
|
|
levelOps.value = levelOpsMain.value;
|
|
formPositionSelect.positionLevel = "";
|
|
}
|
|
|
|
/**
|
|
* ฟังก์เรียกข้อมูลรายการตำแหน่งทางการบริหาร
|
|
*/
|
|
async function fetchExecutive() {
|
|
await http
|
|
.get(config.API.orgPosExecutive)
|
|
.then(async (res) => {
|
|
executiveOpsMain.value = await res.data.result.map(
|
|
(e: OptionExecutive) => ({
|
|
id: e.id,
|
|
name: e.posExecutiveName,
|
|
})
|
|
);
|
|
executiveOps.value = executiveOpsMain.value;
|
|
})
|
|
.catch((err) => {
|
|
messageError($q, err);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* ฟังก์ชันปิด Popup
|
|
*
|
|
* และเรียก clearFormPositionSelect() เพื่อกำหนดฟอร์มข่้อมูลเป็น Default
|
|
*/
|
|
function close() {
|
|
modal.value = false;
|
|
clearFormPositionSelect();
|
|
}
|
|
|
|
/**
|
|
* ส่งค่า css ออกไปตามเงื่อนไข
|
|
* @param val true/false
|
|
*/
|
|
function inputEdit(val: boolean) {
|
|
return {
|
|
"full-width cursor-pointer inputgreen ": val,
|
|
"full-width cursor-pointer inputgreen": !val,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* ดูการเปลี่ยนแปลงของ modal.value
|
|
*
|
|
* ถ้าเป็น true ทำการเรียกข้อมูลรายการประเภทตำแหน่ง และ ข้อมูลรายการตำแหน่งทางการบริหาร
|
|
* และถ้ามี props.data จะกำหนด formPositionSelect เป็นตาม props.data
|
|
*/
|
|
watch(
|
|
() => modal.value,
|
|
async () => {
|
|
if (modal.value === true) {
|
|
showLoader();
|
|
await Promise.all([fetchType(), fetchExecutive()]);
|
|
hideLoader();
|
|
if (props.data) {
|
|
const dataList = props.data;
|
|
await updateSelectType(dataList.posTypeId);
|
|
formPositionSelect.positionId = dataList.id;
|
|
formPositionSelect.positionName = dataList.positionName;
|
|
formPositionSelect.positionField = dataList.positionField;
|
|
formPositionSelect.positionType = dataList.posTypeId;
|
|
formPositionSelect.positionLevel = dataList.posLevelId;
|
|
formPositionSelect.positionExecutive = dataList.posExecutiveId;
|
|
formPositionSelect.positionExecutiveField =
|
|
dataList.positionExecutiveField;
|
|
formPositionSelect.positionArea = dataList.positionArea;
|
|
isSpecial.value = dataList.isSpecial;
|
|
}
|
|
}
|
|
}
|
|
);
|
|
</script>
|
|
<template>
|
|
<q-dialog v-model="modal" persistent>
|
|
<q-card style="min-width: 50vw">
|
|
<DialogHeader
|
|
:tittle="`${isEditCheck ? `แก้ไขข้อมูลตำแหน่ง` : `เพิ่มข้อมูลตำแหน่ง`}`"
|
|
:close="close"
|
|
/>
|
|
<q-separator />
|
|
|
|
<q-card-section class="q-pa-none">
|
|
<q-form
|
|
greedy
|
|
@submit.prevent
|
|
@validation-success="validateFormPositionEdit"
|
|
>
|
|
<!-- @submit.prevent="validateFormPositionEdit" -->
|
|
<div class="row q-col-gutter-sm col-12 q-pa-sm">
|
|
<div class="col-12">
|
|
<q-input
|
|
ref="positionNameRef"
|
|
v-model="formPositionSelect.positionName"
|
|
:class="inputEdit(isReadonly)"
|
|
dense
|
|
outlined
|
|
for="#positionName"
|
|
label="ตำแหน่งในสายงาน"
|
|
lazy-rules
|
|
hide-bottom-space
|
|
:rules="
|
|
!isDisValidate
|
|
? [(val:string) => !!val || `${'กรุณากรอกตำแหน่งในสายงาน'}`]
|
|
: []
|
|
"
|
|
/>
|
|
</div>
|
|
<div class="col-12 col-sm-6 col-md-6">
|
|
<q-input
|
|
ref="positionAreaRef"
|
|
v-model="formPositionSelect.positionArea"
|
|
:class="inputEdit(isReadonly)"
|
|
dense
|
|
outlined
|
|
for="#positionArea"
|
|
label="ด้าน/สาขา"
|
|
lazy-rules
|
|
hide-bottom-space
|
|
/>
|
|
</div>
|
|
<div class="col-12 col-sm-6 col-md-6">
|
|
<q-input
|
|
ref="positionFieldRef"
|
|
v-model="formPositionSelect.positionField"
|
|
:class="inputEdit(isReadonly)"
|
|
dense
|
|
outlined
|
|
for="#positionField"
|
|
label="สายงาน"
|
|
lazy-rules
|
|
hide-bottom-space
|
|
:rules="
|
|
!isDisValidate
|
|
? [(val:string) => !!val || `${'กรุณากรอกสายงาน'}`]
|
|
: []
|
|
"
|
|
/>
|
|
</div>
|
|
<div class="col-12 col-sm-6 col-md-6">
|
|
<q-select
|
|
ref="positionTypeRef"
|
|
:class="inputEdit(isReadonly)"
|
|
label="ประเภทตำแหน่ง"
|
|
v-model="formPositionSelect.positionType"
|
|
:options="typeOps"
|
|
emit-value
|
|
dense
|
|
@update:model-value="updateSelectType"
|
|
map-options
|
|
outlined
|
|
option-label="name"
|
|
option-value="id"
|
|
lazy-rules
|
|
hide-bottom-space
|
|
:rules="
|
|
!isDisValidate
|
|
? [(val:string) => !!val || `${'กรุณาเลือกประเภทตำแหน่ง'}`]
|
|
: []
|
|
"
|
|
/>
|
|
</div>
|
|
<div class="col-12 col-sm-6 col-md-6">
|
|
<q-select
|
|
ref="positionLevelRef"
|
|
:class="inputEdit(isReadonly)"
|
|
label="ระดับตำแหน่ง"
|
|
v-model="formPositionSelect.positionLevel"
|
|
:disable="formPositionSelect.positionType === ''"
|
|
:options="levelOps"
|
|
emit-value
|
|
dense
|
|
map-options
|
|
outlined
|
|
option-label="name"
|
|
option-value="id"
|
|
lazy-rules
|
|
hide-bottom-space
|
|
:rules="
|
|
!isDisValidate
|
|
? [(val:string) => !!val || `${'กรุณาเลือกระดับตำแหน่ง'}`]
|
|
: []
|
|
"
|
|
/>
|
|
</div>
|
|
<div class="col-12 col-sm-6 col-md-6 col-lg-3">
|
|
<q-select
|
|
ref="positionExecutiveRef"
|
|
:class="inputEdit(isReadonly)"
|
|
label="ตำแหน่งทางการบริหาร"
|
|
v-model="shape"
|
|
:options="shapeOp"
|
|
emit-value
|
|
dense
|
|
map-options
|
|
outlined
|
|
option-label="name"
|
|
option-value="id"
|
|
lazy-rules
|
|
hide-bottom-space
|
|
/>
|
|
</div>
|
|
<div class="col-12 col-sm-6 col-md-6 col-lg-3">
|
|
<q-select
|
|
v-if="shape == 'false'"
|
|
ref="positionExecutiveRef"
|
|
:class="inputEdit(isReadonly)"
|
|
label="รายการตำแหน่งทางการบริหาร"
|
|
v-model="formPositionSelect.positionExecutive"
|
|
:options="executiveOps"
|
|
emit-value
|
|
dense
|
|
map-options
|
|
outlined
|
|
option-label="name"
|
|
option-value="id"
|
|
lazy-rules
|
|
hide-bottom-space
|
|
clearable
|
|
/>
|
|
<q-input
|
|
v-else
|
|
ref="positionExecutiveFieldRef"
|
|
v-model="posExecutive"
|
|
:class="inputEdit(isReadonly)"
|
|
dense
|
|
outlined
|
|
for="#positionExecutiveField"
|
|
label="ชื่อตำแหน่งทางการบริหาร"
|
|
lazy-rules
|
|
hide-bottom-space
|
|
/>
|
|
</div>
|
|
<div class="col-12 col-sm-6 col-md-6">
|
|
<q-input
|
|
ref="positionExecutiveFieldRef"
|
|
v-model="formPositionSelect.positionExecutiveField"
|
|
:class="inputEdit(isReadonly)"
|
|
dense
|
|
outlined
|
|
for="#positionExecutiveField"
|
|
label="ด้านทางการบริหาร"
|
|
lazy-rules
|
|
hide-bottom-space
|
|
/>
|
|
</div>
|
|
|
|
<div class="col-6 self-center">
|
|
<q-checkbox
|
|
size="md"
|
|
v-model="isSpecial"
|
|
label="เฉพาะสายงานที่กำหนด"
|
|
/>
|
|
</div>
|
|
</div>
|
|
<q-separator />
|
|
<q-card-actions align="right" class="bg-white text-teal q-pa-sm">
|
|
<q-btn
|
|
type="submit"
|
|
:label="`${isEditCheck ? 'แก้ไขตำแหน่ง' : 'เพิ่มตำแหน่ง'}`"
|
|
color="public"
|
|
/>
|
|
</q-card-actions>
|
|
</q-form>
|
|
</q-card-section>
|
|
</q-card>
|
|
</q-dialog>
|
|
</template>
|