Integrasi Menu Laporan Operator
This commit is contained in:
@@ -662,6 +662,118 @@ Alpine.data('satuTable', (config) => ({
|
||||
},
|
||||
}));
|
||||
|
||||
/*
|
||||
* Laporan berdata asli: setiap perubahan parameter menjalankan laporan ke server (JSON) dan hasilnya tampil
|
||||
* berhalaman; kolom bisa berbeda per parameter, jadi tabel dirender dinamis. Tautan Unduh/Cetak memuat parameter
|
||||
* terkini. Alamat halaman ikut diperbarui (replaceState) agar bisa dibagikan.
|
||||
* config: { url, downloadUrl, printUrl, params: [{name, type, required, reload}], values: { nama: '' | bool }, perPage, per }.
|
||||
*/
|
||||
Alpine.data('laporanReport', (config) => ({
|
||||
values: { ...config.values },
|
||||
params: config.params,
|
||||
per: config.per,
|
||||
result: null,
|
||||
error: null,
|
||||
loading: false,
|
||||
page: 1,
|
||||
controller: null,
|
||||
timer: null,
|
||||
|
||||
init() {
|
||||
this.load();
|
||||
},
|
||||
|
||||
// Semua parameter wajib sudah terisi
|
||||
get ready() {
|
||||
return this.params.every((p) => !p.required || (this.values[p.name] !== '' && this.values[p.name] !== null && this.values[p.name] !== undefined));
|
||||
},
|
||||
|
||||
get query() {
|
||||
const params = new URLSearchParams();
|
||||
Object.entries(this.values).forEach(([name, value]) => {
|
||||
// Kotak centang hanya dikirim saat dicentang
|
||||
if (value === '' || value === null || value === undefined || value === false) return;
|
||||
params.set(name, value === true ? '1' : String(value));
|
||||
});
|
||||
return params.toString();
|
||||
},
|
||||
|
||||
get downloadHref() {
|
||||
return this.ready ? `${config.downloadUrl}?${this.query}` : '#';
|
||||
},
|
||||
|
||||
get printHref() {
|
||||
return this.ready ? `${config.printUrl}?${this.query}` : '#';
|
||||
},
|
||||
|
||||
get window() {
|
||||
const pages = this.result?.last_page ?? 1;
|
||||
const page = this.result?.page ?? 1;
|
||||
const start = Math.max(1, Math.min(page - 2, pages - 4));
|
||||
return Array.from({ length: Math.min(5, pages) }, (_, i) => start + i);
|
||||
},
|
||||
|
||||
cell(value) {
|
||||
return value === null || value === undefined || value === '' ? '–' : value;
|
||||
},
|
||||
|
||||
// Parameter atau jumlah per halaman berubah: kembali ke halaman 1
|
||||
refresh() {
|
||||
this.page = 1;
|
||||
this.load();
|
||||
},
|
||||
|
||||
go(page) {
|
||||
this.page = Math.min(Math.max(1, page), this.result?.last_page ?? 1);
|
||||
this.load();
|
||||
},
|
||||
|
||||
async load() {
|
||||
clearTimeout(this.timer);
|
||||
if (!this.ready) {
|
||||
this.controller?.abort();
|
||||
this.result = null;
|
||||
this.error = null;
|
||||
this.loading = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Permintaan lama dibatalkan agar hasil yang terlambat tidak menimpa yang terbaru
|
||||
this.controller?.abort();
|
||||
const controller = new AbortController();
|
||||
this.controller = controller;
|
||||
this.loading = true;
|
||||
const params = `${this.query}${this.query ? '&' : ''}page=${this.page}&per=${this.per}`;
|
||||
const started = performance.now();
|
||||
|
||||
try {
|
||||
const response = await fetch(`${config.url}?${params}`, {
|
||||
headers: { Accept: 'application/json', 'X-Requested-With': 'XMLHttpRequest' },
|
||||
signal: controller.signal,
|
||||
});
|
||||
const data = await response.json().catch(() => null);
|
||||
if (!response.ok || !data) {
|
||||
this.result = null;
|
||||
this.error = data?.message || 'Laporan tidak dapat dimuat. Muat ulang halaman lalu coba lagi.';
|
||||
return;
|
||||
}
|
||||
|
||||
this.error = null;
|
||||
this.result = data;
|
||||
this.page = data.page;
|
||||
window.history.replaceState(null, '', `${window.location.pathname}${this.query ? `?${this.query}` : ''}`);
|
||||
console.info(`[SIAKAD] GET ${config.url}?${params}`, { total: data.total, halaman: `${data.page}/${data.last_page}`, waktu: `${Math.round(performance.now() - started)} ms` });
|
||||
} catch (error) {
|
||||
if (error.name === 'AbortError') return;
|
||||
console.error('[SIAKAD] Gagal memuat laporan', error);
|
||||
this.result = null;
|
||||
this.error = 'Laporan tidak dapat dimuat. Periksa koneksi lalu coba lagi.';
|
||||
} finally {
|
||||
if (this.controller === controller) this.loading = false;
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
/*
|
||||
* Tabel SATU berisi data asli: tampilan sama dengan satuTable, tetapi filter, pencarian, dan halaman diminta
|
||||
* ke server lewat fetch (JSON), sehingga alamat halaman tidak berubah. Ringkasan permintaan dicatat di konsol.
|
||||
@@ -793,6 +905,10 @@ Alpine.data('satuRemoteTable', (config) => ({
|
||||
Object.assign(this, { view: data.rows, page: data.page, pages: data.last_page, total: data.total, per: data.per_page });
|
||||
this.picked = [];
|
||||
console.info(`[SIAKAD] GET ${this.url}?${params}`, { total: data.total, halaman: `${data.page}/${data.last_page}`, waktu: `${Math.round(performance.now() - started)} ms` });
|
||||
// Halaman terakhir baru saja kosong (mis. setelah hapus): pindah ke halaman terakhir yang masih ada
|
||||
if (data.rows.length === 0 && data.page > Math.max(1, data.last_page)) {
|
||||
queueMicrotask(() => this.load(Math.max(1, data.last_page)));
|
||||
}
|
||||
} catch (error) {
|
||||
if (error.name === 'AbortError') return;
|
||||
console.error('[SIAKAD] Gagal memuat data', error);
|
||||
|
||||
Reference in New Issue
Block a user