<template>
<div class="container mt-4">
<edit-modal-container v-if="editStyle === 'modal'" />
<h1 class="mb-4">KEHADIRAN (Managed by Vue)</h1>
<b-form-group label="Select Editing Style:">
<b-form-radio-group v-model="editStyle" :options="['inline', 'modal']"></b-form-radio-group>
</b-form-group>
<b-table striped hover :items="tableData" :fields="tableFields" row-key="no_absen">
<template #cell(terlambat)="data">
<in-place-edit
v-if="editStyle === 'inline'"
v-model="data.item.terlambat"
:name="'terlambat|' + data.item.no_absen"
:dbid="data.item.no_absen"
type="number"
mode="ajax"
saveurl="/api/update-kehadiran"
/>
<modal-trigger
v-if="editStyle === 'modal'"
:initial-value="data.item.terlambat"
:name="'terlambat|' + data.item.no_absen"
:dbid="data.item.no_absen"
type="number"
saveurl="/api/update-kehadiran"
/>
</template>
<template #cell(kelas_susulan)="data">
<in-place-edit
v-if="editStyle === 'inline'"
v-model="data.item.kelas_susulan"
:name="'kelas_susulan|' + data.item.no_absen"
:dbid="data.item.no_absen"
type="select"
mode="ajax"
:options="yesNoOptions"
saveurl="/api/update-kehadiran"
/>
<modal-trigger
v-if="editStyle === 'modal'"
:initial-value="data.item.kelas_susulan"
:name="'kelas_susulan|' + data.item.no_absen"
:dbid="data.item.no_absen"
type="select"
:options="yesNoOptions"
saveurl="/api/update-kehadiran"
/>
</template>
</b-table>
</div>
</template>
<script>
import InPlaceEdit from '../components/InPlaceEdit.vue';
import EditModalContainer from '../components/EditModalContainer.vue';
import ModalTrigger from '../components/ModalTrigger.vue';
import { EventBus } from '../EventBus.js';
import axios from 'axios';
export default {
name: 'AttendanceTable',
components: { InPlaceEdit, EditModalContainer, ModalTrigger },
data() {
return {
editStyle: 'inline',
tableFields: [
{ key: 'no_absen', label: 'NO ABSEN' },
{ key: 'nama', label: 'NAMA' },
{ key: 'terlambat', label: 'TERLAMBAT (MENIT)' },
{ key: 'kelas_susulan', label: 'KELAS SUSULAN' },
],
tableData: [],
yesNoOptions: [
{ value: 'YA', text: 'YA' },
{ value: 'TIDAK', text: 'TIDAK' },
],
};
},
created() {
this.fetchAttendanceData();
EventBus.$on('edit-save-success', this.updateDataFromModal);
},
beforeDestroy() {
EventBus.$off('edit-save-success', this.updateDataFromModal);
},
methods: {
async fetchAttendanceData() {
// In a real app, you would fetch data from your API
// const response = await axios.get('/api/attendance');
// this.tableData = response.data;
this.tableData = [ /* Paste mock data here for testing */ ];
},
updateDataFromModal({ name, newValue }) {
const [fieldName, id] = name.split('|');
const record = this.tableData.find(item => item.no_absen === id);
if (record) {
record[fieldName] = newValue;
}
},
},
};
</script>