hrms-mgt/src/modules/05_placement/components/Main.vue
setthawutttty c296bdfc48 no message
2023-07-04 09:41:24 +07:00

502 lines
14 KiB
Vue

<script setup lang="ts">
import { onMounted, reactive, ref, useAttrs } from "vue";
import type { QTableProps } from "quasar";
import { useQuasar } from "quasar";
import type { FormPlacementMainData } from "@/modules/05_placement/interface/request/Main";
import type { DataOption } from "@/modules/05_placement/interface/index/Main";
import { useCounterMixin } from "@/stores/mixin";
import { usePlacementDataStore } from "@/modules/05_placement/store";
import router from "@/router";
import http from "@/plugins/http";
import config from "@/app.config";
const DataStore = usePlacementDataStore();
const $q = useQuasar();
const mixin = useCounterMixin();
const { dateText, messageError } = mixin;
// แปลงเวลา ค.ศ ให้เป็น พ.ศ
const textDate = (value: Date) => {
return dateText(value);
};
// หัวตาราง
const columns = ref<QTableProps["columns"]>([
{
name: "id",
align: "left",
label: "ลำดับ",
sortable: true,
field: "id",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "name",
align: "left",
label: "รอบการสอบ",
sortable: true,
field: "name",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
sort: (a: string, b: string) =>
a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
},
{
name: "round",
align: "left",
label: "ครั้งที่",
sortable: true,
field: "round",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "year",
align: "left",
label: "ปีงบประมาณ",
sortable: true,
field: "year",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
},
{
name: "number",
align: "left",
label: "จำนวนผู้สอบได้",
sortable: false,
field: "number",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
sort: (a: string, b: string) =>
a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
},
{
name: "placementType",
align: "left",
label: "ประเภทการสอบ",
sortable: false,
field: "placementType",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
sort: (a: string, b: string) =>
a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
},
{
name: "startDate",
align: "left",
label: "วันที่เริ่มคำสั่ง",
sortable: true,
field: "startDate",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
sort: (a: string, b: string) =>
a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
},
{
name: "endDate",
align: "left",
label: "วันที่บัญชีหมดอายุ",
sortable: true,
field: "endDate",
headerStyle: "font-size: 14px",
style: "font-size: 14px",
sort: (a: string, b: string) =>
a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" }),
},
]);
// ข้อมูลตาราง (จำลอง)
const rows = ref<FormPlacementMainData[]>([
{
id: 0,
name: "",
round:0 ,
year: 0,
number: 0,
placementType: 0,
startDate: "",
endDate: "",
},
]);
let OriginalData = ref<FormPlacementMainData[]>([]);
let UpdataData = ref<FormPlacementMainData[]>([]);
const OriginalDataFetch = async () => {
await http
.get(config.API.MainDetail())
.then(async (res: any) => {
console.log("🚀 ~ file: Main.vue:160 ~ .then ~ res:", res.data.result);
await DataStore.DataMain(res.data.result);
OriginalData.value = await DataStore.DataMainOrig;
UpdataData.value = OriginalData.value;
})
.catch((e: any) => {
messageError($q, e);
})
.finally(async () => {});
};
onMounted(async () => {
await OriginalDataFetch();
fiscalYearFilter();
examTimeFilter();
examTypeFilter();
await expiredAccountFilter();
searchFilterTable();
});
// ดูรายการสอบแข่งขัน หน้าต่อไป
const redirectToPage = (examId?: number) => {
// router.push({ name: 'placementDetail'});
router.push(`/placement/pass/${examId}`);
};
// เลือกปีงบประมาณ
const fiscalyear = ref<number | null>(0);
const fiscalyearOP = reactive<DataOption[]>([{ id: 0, name: "ทั้งหมด" }]);
const addedfiscalYearValues: number[] = [];
const fiscalYearFilter = async () => {
// API
await http
.get(config.API.yearOptions())
.then((res: any) => {
console.log("🚀 ", res.data.result);
for (let data of res.data.result) {
fiscalyearOP.push(data);
}
// DataStore.DataMainYearSet(rows.value);
// fiscalyearOP.value = DataStore.DataMainYearGet;
})
.catch((e: any) => {
messageError($q, e);
})
.finally(async () => {});
};
// เลือกปีงบประมาณตาม API
const searchfiscalyear = async () => {
console.log("Input value changed:", fiscalyear.value);
// API
await http
.get(config.API.yearOptions())
.then((res: any) => {
console.log("🚀date", res);
})
.catch((e: any) => {
messageError($q, e);
})
.finally(async () => {});
};
// ค้นหาในตาราง
const filterKeyword = ref<string>("");
const filterRef = ref<any>(null);
const resetFilter = () => {
filterKeyword.value = "";
filterRef.value.focus();
};
const attrs = ref<any>(useAttrs());
const visibleColumns = ref<any[]>([
"id",
"name",
"round",
"year",
"number",
"placementType",
"startDate",
"endDate",
]); //ค้นหา คอลัมน์ คอลัมน์ที่แสดง
// ครั้งที่สอบ
const examTime = ref<number | null>(null);
const examTimeOP = ref<number[]>([]);
const examTimeFilter = async () => {
for (let data of OriginalData.value) {
if (!examTimeOP.value.includes(data.round)) {
examTimeOP.value.push(data.round);
}
}
examTimeOP.value.sort((a, b) => a - b); // เรียงลำดับ
};
// ประเภทการสอบ
const examType = ref<number | null>(0);
const examTypeOP = reactive<DataOption[]>([{ id: 0, name: "ทั้งหมด" }]);
const addedexamTypeValues: number[] = [];
const examTypeFilter = async () => {
for (let data of OriginalData.value) {
const examTypeValue = data.placementType;
if (examTypeValue == 1 && !addedexamTypeValues.includes(1)) {
examTypeOP.push({ id: 1, name: "คัดเลือก" });
addedexamTypeValues.push(1);
} else if (examTypeValue == 2 && !addedexamTypeValues.includes(2)) {
examTypeOP.push({ id: 2, name: "สอบแข่งขัน" });
addedexamTypeValues.push(2);
}
}
};
const searchFilterTable = async () => {
// console.log('Input value changed:', examTime.value, examType.value, expiredAccount.value);
await DataStore.DataUpdateMain(
examTime.value,
examType.value,
expiredAccount.value
);
UpdataData.value = DataStore.DataMainUpdate;
};
// บัญชีหมดอายุ
const expiredAccount = ref<boolean>(false);
const expiredAccountFilter = async () => {
const currentDate = new Date();
const updatedRows = OriginalData.value.map((data) => {
let expirationDate = new Date(data.endDate);
let isExpired = expirationDate < currentDate;
return { ...data, isExpired };
});
await DataStore.DataMain(updatedRows);
};
const paging = ref<boolean>(true);
const pagination = ref({
sortBy: "desc",
descending: false,
page: 1,
rowsPerPage: 10,
});
const paginationLabel = (start: string, end: string, total: string) => {
if (paging.value == true) return " " + start + "-" + end + " ใน " + total;
else return start + "-" + end + " ใน " + total;
};
</script>
<template>
<div class="toptitle text-dark col-12 row items-center">
รายการสอบแขงข / ดเลอก
</div>
<q-card flat bordered class="col-12 q-mt-sm q-pa-md">
<div class="row q-col-gutter-md">
<div class="row col-12 q-col-gutter-sm">
<q-select
class="col-xs-12 col-sm-3 col-md-2"
v-model="fiscalyear"
label="ปีงบประมาณ"
dense
emit-value
map-options
:options="fiscalyearOP"
option-value="id"
option-label="name"
lazy-rules
hide-bottom-space
:readonly="false"
:borderless="false"
:outlined="true"
:hide-dropdown-icon="false"
@update:model-value="searchfiscalyear"
/>
<q-space />
<q-input
class="col-xs-12 col-sm-3 col-md-2"
standout
dense
v-model="filterKeyword"
ref="filterRef"
outlined
debounce="300"
placeholder="ค้นหา"
>
<template v-slot:append>
<q-icon v-if="filterKeyword == ''" name="search" />
<q-icon
v-if="filterKeyword !== ''"
name="clear"
class="cursor-pointer"
@click="resetFilter"
/>
</template>
</q-input>
<q-select
class="col-xs-12 col-sm-3 col-md-2"
v-model="visibleColumns"
multiple
outlined
dense
options-dense
:display-value="$q.lang.table.columns"
emit-value
map-options
:options="columns"
option-value="name"
options-cover
/>
</div>
<div class="col-12">
<q-card bordered class="col-12 filter-card q-pa-sm">
<div class="row col-12 q-col-gutter-sm">
<q-select
class="col-xs-12 col-sm-3 col-md-2"
v-model="examTime"
label="ครั้งที่"
dense
emit-value
map-options
:options="examTimeOP"
option-value="id"
option-label="name"
lazy-rules
hide-bottom-space
:readonly="false"
:borderless="false"
:outlined="true"
:hide-dropdown-icon="false"
@update:model-value="searchFilterTable"
/>
<q-select
class="col-xs-12 col-sm-3 col-md-2"
v-model="examType"
label="ประเภทการสอบ"
dense
emit-value
map-options
option-label="name"
:options="examTypeOP"
option-value="id"
lazy-rules
hide-bottom-space
:readonly="false"
:borderless="false"
:outlined="true"
:hide-dropdown-icon="false"
@update:model-value="searchFilterTable"
/>
<q-toggle
class="col-xs-12 col-sm-5 col-md-5 toggle-expired-account"
v-model="expiredAccount"
color="blue"
label="แสดงบัญชีหมดอายุ"
@update:model-value="searchFilterTable"
/>
</div>
</q-card>
</div>
<div class="col-12">
<q-table
ref="table"
:columns="columns"
:rows="UpdataData"
:filter="filterKeyword"
row-key="id"
flat
bordered
dense
class="custom-header-table"
v-bind="attrs"
:visible-columns="visibleColumns"
:pagination-label="paginationLabel"
v-model:pagination="pagination"
>
<template v-slot:pagination="scope">
<q-pagination
v-model="pagination.page"
color="primary"
:max="scope.pagesNumber"
:max-pages="5"
size="sm"
boundary-links
direction-links
></q-pagination>
</template>
<template v-slot:body="props">
<q-tr
:props="props"
class="cursor-pointer"
@click="redirectToPage(props.row.id)"
>
<q-td key="id" :props="props">
{{ props.rowIndex+1 }}
</q-td>
<q-td key="name" :props="props">
{{ props.row.name }}
</q-td>
<q-td key="round" :props="props">
{{ props.row.round }}
</q-td>
<q-td key="year" :props="props">
{{ props.row.year + 543 }}
</q-td>
<q-td key="number" :props="props">
{{ props.row.number }}
</q-td>
<q-td key="placementType" :props="props">
{{ props.row.placementType == 1 ? "คัดเลือก" : "สอบแข่งขัน" }}
</q-td>
<q-td key="startDate" :props="props">
{{ textDate(props.row.startDate) }}
</q-td>
<q-td key="endDate" :props="props">
{{ textDate(props.row.endDate) }}
</q-td>
</q-tr>
</template>
</q-table>
</div>
</div>
</q-card>
</template>
<style lang="scss" scope>
.filter-card {
background-color: #f1f1f1b0;
}
.toggle-expired-account {
font-size: 12px;
font-weight: 400;
font-size: 15px;
line-height: 150%;
color: #35373c;
}
.icon-color {
color: #4154b3;
}
.custom-header-table {
max-height: 64vh;
.q-table tr:nth-child(odd) td {
background: white;
}
.q-table tr:nth-child(even) td {
background: #f8f8f8;
}
.q-table thead tr {
background: #ecebeb;
}
.q-table thead tr th {
position: sticky;
z-index: 1;
}
/* this will be the loading indicator */
.q-table thead tr:last-child th {
/* height of all previous header rows */
top: 48px;
}
.q-table thead tr:first-child th {
top: 0;
}
}
</style>