function confirmDelete(message) {
    return confirm(message || 'Hapus data ini?');
}

function confirmHapusSemua() {
    var text = prompt("Ketik 'hapus semua' untuk menghapus seluruh data penduduk:");
    var input = document.getElementById('confirmHapusSemuaInput');
    if (input) input.value = text || '';
    return (text || '').toLowerCase().trim() === 'hapus semua';
}

document.addEventListener('DOMContentLoaded', function () {
    var clock = document.getElementById('adminClock');
    var dateText = document.getElementById('adminDateText');
    var sidebar = document.getElementById('adminSidebar');
    var toggle = document.getElementById('adminMenuToggle');
    var overlay = document.getElementById('sidebarOverlay');

    function formatWitaDate(now) {
        return new Intl.DateTimeFormat('id-ID', {
            weekday: 'long', day: 'numeric', month: 'long', year: 'numeric',
            hour: '2-digit', minute: '2-digit', hour12: false,
            timeZone: 'Asia/Makassar'
        }).format(now).replace('.', ':') + ' WITA';
    }

    function updateTime() {
        var now = new Date();
        if (clock) {
            clock.textContent = new Intl.DateTimeFormat('id-ID', {
                hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false,
                timeZone: 'Asia/Makassar'
            }).format(now).replaceAll('.', ':');
        }
        if (dateText) dateText.textContent = formatWitaDate(now);
    }

    function closeSidebar() {
        if (sidebar) sidebar.classList.remove('open');
        if (overlay) overlay.classList.remove('show');
        if (toggle) toggle.setAttribute('aria-label', 'Buka menu');
    }

    function openSidebar() {
        if (sidebar) sidebar.classList.add('open');
        if (overlay) overlay.classList.add('show');
        if (toggle) toggle.setAttribute('aria-label', 'Tutup menu');
    }

    updateTime();
    window.setInterval(updateTime, 1000);

    if (toggle) {
        toggle.addEventListener('click', function () {
            if (sidebar && sidebar.classList.contains('open')) closeSidebar();
            else openSidebar();
        });
    }
    if (overlay) overlay.addEventListener('click', closeSidebar);
    if (sidebar) {
        sidebar.querySelectorAll('a').forEach(function (link) {
            link.addEventListener('click', function () {
                if (window.innerWidth <= 940) closeSidebar();
            });
        });
    }

    document.querySelectorAll('[data-counter]').forEach(function (el) {
        var target = parseInt(el.getAttribute('data-counter') || el.textContent || '0', 10);
        if (!Number.isFinite(target)) return;
        var start = performance.now();
        var duration = 800;
        function step(now) {
            var p = Math.min(1, (now - start) / duration);
            var eased = 1 - Math.pow(1 - p, 3);
            el.textContent = Math.round(target * eased).toLocaleString('id-ID');
            if (p < 1) requestAnimationFrame(step);
        }
        requestAnimationFrame(step);
    });

    document.querySelectorAll('[data-reset-form]').forEach(function (btn) {
        btn.addEventListener('click', function () {
            var form = btn.closest('form');
            if (!form) return;
            form.querySelectorAll('input:not([type=hidden]), select').forEach(function (field) { field.value = ''; });
        });
    });
});
