$(document).ready(function () { const alertBox = $('#loginAlert'); const submitBtn = $('#primartom'); const origBtnHtml = submitBtn.html(); function showAlert(msg, isSuccess = false) { alertBox.removeClass('alert-error alert-success') .addClass(isSuccess ? 'alert-success' : 'alert-error') .html(msg) .fadeIn(); } $('#loginForm').on('submit', function (e) { e.preventDefault(); const username = $('#username').val().trim(); const password = $('#password').val().trim(); if (!username || !password) { showAlert('Username dan password wajib diisi.'); return; } submitBtn.prop('disabled', true).html(' Memverifikasi...'); alertBox.hide(); const urlParams = new URLSearchParams(window.location.search); const serviceParam = urlParams.get('service') || ''; const redirectParam = urlParams.get('redirect') || ''; $.ajax({ type: 'POST', url: 'login/proses', data: { username: username, password: password, service: serviceParam, redirect: redirectParam }, success: function (response) { let resData = null; try { resData = typeof response === 'object' ? response : JSON.parse(response); } catch (e) { resData = { status: parseInt(response, 10) }; } if (resData.status === 1) { if (resData.warning) { showAlert(resData.warning, true); setTimeout(function () { window.location.href = resData.redirect || redirectParam || 'main'; }, 1300); } else { showAlert('Login berhasil. Mengalihkan ke layanan...', true); setTimeout(function () { const target = resData.redirect || redirectParam || ''; if (target && target !== '') { window.location.href = target; } else { window.location.reload(); } }, 400); } } else if (resData.status === 2) { showAlert('Pengguna saat ini masih aktif pada sesi lain.'); submitBtn.prop('disabled', false).html(origBtnHtml); } else { let errMsg = resData.message || 'Username atau kata sandi tidak sesuai.'; if (resData.portal_link) { errMsg += '
'; } showAlert(errMsg); submitBtn.prop('disabled', false).html(origBtnHtml); } }, error: function () { showAlert('Gagal terhubung ke server autentikasi.'); submitBtn.prop('disabled', false).html(origBtnHtml); } }); }); });