var sheetName = 'AntrianData';
var previousNumberKey = 'previousNumber';
var lastCalledNumberKey = 'lastCalledNumber';
var updateRequiredKey = 'updateRequired';
var lastDateKey = 'lastDate';
function doGet(e) {
return HtmlService.createTemplateFromFile('index').evaluate();
}
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename).getContent();
}
function antrian() {
return HtmlService.createHtmlOutputFromFile('antrian').getContent();
}
function display() {
return HtmlService.createHtmlOutputFromFile('display').getContent();
}
function admin() {
return HtmlService.createHtmlOutputFromFile('admin').getContent();
}
function ambilNomorAntrian() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var lastRow = sheet.getLastRow();
var today = new Date();
var currentDateString = Utilities.formatDate(today, Session.getScriptTimeZone(), 'yyyy-MM-dd');
// Periksa tanggal terakhir yang disimpan
var lastDate = PropertiesService.getScriptProperties().getProperty(lastDateKey);
// Jika tanggal saat ini berbeda dengan tanggal terakhir, reset nomor ke 1
if (lastDate !== currentDateString) {
sheet.clear(); // Opsional: Hapus data jika ingin mulai dari awal setiap hari
sheet.appendRow(['Nomor', 'Tanggal', 'Status']); // Header
PropertiesService.getScriptProperties().setProperty(lastDateKey, currentDateString);
var newNumber = 1;
} else {
// Jika tanggal sama, gunakan nomor terakhir + 1
var newNumber = lastRow > 1 ? sheet.getRange(lastRow, 1).getValue() + 1 : 1;
}
var waktuAntrian = new Date();
sheet.appendRow([newNumber, waktuAntrian, 'waiting']);
return newNumber;
}
function getNomorAntrianSaatIni() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var range = sheet.getRange('A:C');
var values = range.getValues();
for (var i = values.length - 1; i >= 1; i--) {
if (values[i][2] === 'called') {
return values[i][0];
}
}
return 'Belum ada nomor antrian yang dipanggil';
}
function panggilNomorBerikut() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var range = sheet.getRange('A:C');
var values = range.getValues();
var previousNumber = PropertiesService.getScriptProperties().getProperty(previousNumberKey);
previousNumber = previousNumber ? parseInt(previousNumber, 10) : null;
if (semuaNomorDipanggil()) {
return 'Semua nomor sudah dipanggil';
}
for (var i = 1; i < values.length; i++) {
if (values[i][2] === 'waiting') {
if (previousNumber !== null) {
sheet.getRange(previousNumber + 1, 3).setValue('called');
}
values[i][2] = 'called';
sheet.getRange(i + 1, 3).setValue('called');
PropertiesService.getScriptProperties().setProperty(previousNumberKey, i);
PropertiesService.getScriptProperties().setProperty(lastCalledNumberKey, values[i][0]);
PropertiesService.getScriptProperties().setProperty(updateRequiredKey, 'true');
return values[i][0];
}
}
return 'Tidak ada nomor antrian yang menunggu';
}
function semuaNomorDipanggil() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var values = sheet.getRange('A:C').getValues();
for (var i = 1; i < values.length; i++) {
if (values[i][2] === 'waiting') {
return false; // Masih ada nomor yang menunggu
}
}
return true; // Semua nomor sudah dipanggil
}
function getNomorBelumDipanggil() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var values = sheet.getRange('A:C').getValues();
var waitingNumbers = [];
for (var i = 1; i < values.length; i++) {
if (values[i][2] === 'waiting') {
waitingNumbers.push(values[i][0]);
}
}
return waitingNumbers.length > 0 ? waitingNumbers.join(', ') : 'Semua nomor sudah dipanggil';
}
function ulangiNomor() {
var previousNumber = PropertiesService.getScriptProperties().getProperty(previousNumberKey);
if (previousNumber !== null) {
previousNumber = parseInt(previousNumber, 10);
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var values = sheet.getRange('A:C').getValues();
var currentStatus = sheet.getRange(previousNumber + 1, 3).getValue();
if (currentStatus === 'called') {
sheet.getRange(previousNumber + 1, 3).setValue('waiting');
PropertiesService.getScriptProperties().setProperty(lastCalledNumberKey, values[previousNumber][0]);
PropertiesService.getScriptProperties().setProperty(updateRequiredKey, 'true');
return values[previousNumber][0];
} else {
return 'Tidak ada nomor yang dipanggil sebelumnya';
}
}
return 'Tidak ada nomor yang dipanggil sebelumnya';
}
function getLastCalledNumber() {
return PropertiesService.getScriptProperties().getProperty(lastCalledNumberKey) || 'Belum ada nomor antrian yang dipanggil';
}
function isUpdateRequired() {
return PropertiesService.getScriptProperties().getProperty(updateRequiredKey) === 'true';
}
function setUpdateComplete() {
PropertiesService.getScriptProperties().setProperty(updateRequiredKey, 'false');
}