<?php
// modules/wallet/widgets/scripts.php
if (basename($_SERVER['PHP_SELF']) == 'scripts.php') { die('Access Denied'); }
?>

<script>
function switchTab(tabId) {
    document.querySelectorAll('.tab-content').forEach(el => el.classList.add('hidden'));
    document.querySelectorAll('.tab-btn').forEach(el => {
        el.classList.remove('bg-blue-600', 'text-white', 'active');
        el.classList.add('bg-white', 'text-gray-600', 'dark:bg-slate-800', 'dark:text-gray-300');
    });
    
    document.getElementById(tabId).classList.remove('hidden');
    const activeBtn = event.currentTarget;
    activeBtn.classList.remove('bg-white', 'text-gray-600', 'dark:bg-slate-800', 'dark:text-gray-300');
    activeBtn.classList.add('bg-blue-600', 'text-white', 'active');
}
</script>

<?php if ($isBonusSystemActive && count($activeBonuses) > 0 && $bonusTimerActive && !empty($bonusTimerDate) && !$timerExpired): ?>
<script>
document.addEventListener("DOMContentLoaded", function() {
    const formatNum = (num) => num < 10 ? "0" + num : num;
    const timers = document.querySelectorAll('.wallet-bonus-timer');
    if (timers.length > 0) {
        timers.forEach(function(el) {
            const targetDate = new Date(el.getAttribute('data-target')).getTime();
            
            const timerInterval = setInterval(function() {
                const now = new Date().getTime();
                const distance = targetDate - now;
                
                if (distance < 0) {
                    clearInterval(timerInterval);
                    el.innerHTML = "پایان زمان";
                    setTimeout(() => location.reload(), 2000);
                    return;
                }
                
                const days = Math.floor(distance / (1000 * 60 * 60 * 24));
                const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
                const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
                const seconds = Math.floor((distance % (1000 * 60)) / 1000);
                
                if (days > 0) {
                    el.innerHTML = days + " روز و " + formatNum(hours) + ":" + formatNum(minutes) + ":" + formatNum(seconds);
                } else {
                    el.innerHTML = formatNum(hours) + ":" + formatNum(minutes) + ":" + formatNum(seconds);
                }
            }, 1000);
        });
    }
});
</script>
<?php endif; ?>

<script src="/vpnet-panel/assets/js/jquery-3.7.0.min.js"></script>
<script src="/vpnet-panel/assets/js/jquery.dataTables.min.js"></script>
<script src="/vpnet-panel/assets/js/dataTables.buttons.min.js"></script>
<script src="/vpnet-panel/assets/js/jszip.min.js"></script>
<script src="/vpnet-panel/assets/js/buttons.html5.min.js"></script>

<script>
$(document).ready(function() {
    const dtLang = {
        "sEmptyTable": "هیچ داده‌ای یافت نشد",
        "sSearch": "جستجوی سریع:",
        "sInfo": "نمایش _START_ تا _END_ از _TOTAL_ رکورد",
        "oPaginate": { "sNext": "بعدی", "sPrevious": "قبلی" },
        "sLengthMenu": "نمایش _MENU_ سطر"
    };

    if ($('#operatorHistoryTable').length) {
        $('#operatorHistoryTable').DataTable({
            language: dtLang, order: [], pageLength: 10,
            dom: '<"flex flex-col md:flex-row justify-between items-center mb-4 gap-4"lf>rt<"flex flex-col md:flex-row justify-between items-center mt-4 gap-4"ip>'
        });
    }

    if ($('#adminHistoryTable').length) {
        $('#adminHistoryTable').DataTable({
            language: dtLang, order: [], pageLength: 10,
            dom: '<"flex flex-col md:flex-row justify-between items-center mb-4 gap-4"<"flex items-center gap-4"lB>f>rt<"flex flex-col md:flex-row justify-between items-center mt-4 gap-4"ip>',
            buttons: [{ extend: 'excelHtml5', text: '📥 خروجی Excel', className: 'btn-export' }]
        });
    }

    if ($('#manualHistoryTable').length) {
        $('#manualHistoryTable').DataTable({
            language: dtLang, order: [], pageLength: 10,
            dom: '<"flex flex-col md:flex-row justify-between items-center mb-4 gap-4"<"flex items-center gap-4"lB>f>rt<"flex flex-col md:flex-row justify-between items-center mt-4 gap-4"ip>',
            buttons: [{ extend: 'excelHtml5', text: '📥 خروجی Excel', className: 'btn-export' }]
        });
    }

    if ($('#creditHistoryTable').length) {
        $('#creditHistoryTable').DataTable({
            language: dtLang, order: [], pageLength: 10,
            dom: '<"flex flex-col md:flex-row justify-between items-center mb-4 gap-4"<"flex items-center gap-4"lB>f>rt<"flex flex-col md:flex-row justify-between items-center mt-4 gap-4"ip>',
            buttons: [{ extend: 'excelHtml5', text: '📥 خروجی Excel', className: 'btn-export' }]
        });
    }
});

function formatCurrency(input) {
    let value = input.value.replace(/,/g, '').replace(/\D/g, '');
    if (value) { input.value = Number(value).toLocaleString('en-US'); }
    else { input.value = ''; }
}
</script>

<style>
    .dt-buttons .dt-button {
        background-color: #f3f4f6 !important; border: 1px solid #d1d5db !important; border-radius: 8px !important;
        color: #374151 !important; font-family: 'Vazirmatn', sans-serif !important; font-weight: bold !important;
        padding: 6px 12px !important; transition: all 0.2s !important;
    }
    .dt-buttons .dt-button.buttons-excel { background-color: #ecfdf5 !important; border-color: #10b981 !important; color: #047857 !important; }
    .dt-buttons .dt-button.buttons-excel:hover { background-color: #10b981 !important; color: white !important; }
    html.dark .dt-buttons .dt-button { background-color: #1e293b !important; border-color: #334155 !important; color: #cbd5e1 !important; }
    html.dark .dt-buttons .dt-button.buttons-excel { background-color: #064e3b !important; border-color: #065f46 !important; color: #34d399 !important; }
</style>