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



<div class="mb-12">
    <div class="flex space-x-2 space-x-reverse mb-4 overflow-x-auto custom-scrollbar pb-2">
        <button onclick="switchTab('history-receipts')" class="tab-btn active px-5 py-2.5 bg-blue-600 text-white rounded-xl font-bold shadow-sm whitespace-nowrap transition">فیش‌های واریزی</button>
        <button onclick="switchTab('history-manual')" class="tab-btn px-5 py-2.5 bg-white text-gray-600 dark:bg-slate-800 dark:text-gray-300 hover:bg-gray-50 border border-gray-200 dark:border-slate-700 rounded-xl font-bold shadow-sm whitespace-nowrap transition">شارژ و کسر دستی</button>
        <button onclick="switchTab('history-credit')" class="tab-btn px-5 py-2.5 bg-white text-gray-600 dark:bg-slate-800 dark:text-gray-300 hover:bg-gray-50 border border-gray-200 dark:border-slate-700 rounded-xl font-bold shadow-sm whitespace-nowrap transition">تخصیص اعتبار</button>
    </div>

    <div id="history-receipts" class="tab-content bg-white dark:bg-slate-800 rounded-3xl shadow-sm border border-gray-100 dark:border-slate-700 p-8 border-t-8 border-t-blue-500">
        <h2 class="text-lg font-bold mb-6 text-gray-800 dark:text-white">تاریخچه رسیدها و فیش‌های بانکی</h2>
        <div class="overflow-x-auto">
            <table id="adminHistoryTable" class="w-full text-sm text-right text-gray-600 dark:text-gray-400">
                <thead class="bg-gray-50 dark:bg-slate-700/50 text-gray-700 dark:text-gray-300">
                    <tr>
                        <th class="px-4 py-3 rounded-tr-xl">شماره ثبت</th>
                        <th class="px-4 py-3">تاریخ ایجاد</th>
                        <th class="px-4 py-3">نماینده</th>
                        <th class="px-4 py-3">مبلغ (تومان)</th>
                        <th class="px-4 py-3">توضیحات فیش</th>
                        <th class="px-4 py-3 text-center">وضعیت</th>
                        <th class="px-4 py-3 text-center rounded-tl-xl">مشاهده</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100 dark:divide-slate-700">
                    <?php foreach($allSettlementsAdmin as $h): 
                        $repName = !empty($h['fullname']) ? htmlspecialchars($h['fullname']) : htmlspecialchars($h['username']);
                    ?>
                        <tr class="hover:bg-gray-50 dark:hover:bg-slate-700/30 transition">
                            <td class="px-4 py-4 font-mono text-xs text-gray-400">#REQ-<?php echo $h['id']; ?></td>
                            <td class="px-4 py-4 font-mono font-bold text-gray-800 dark:text-gray-200" dir="ltr"><?php echo format_shamsi($h['created_at']); ?></td>
                            <td class="px-4 py-4 font-bold text-gray-800 dark:text-gray-200"><?php echo $repName; ?></td>
                            <td class="px-4 py-4 font-black text-gray-800 dark:text-gray-200" dir="ltr"><?php echo number_format($h['amount']); ?></td>
                            <td class="px-4 py-4 text-xs truncate max-w-[200px]" title="<?php echo htmlspecialchars($h['receipt_text']); ?>"><?php echo htmlspecialchars($h['receipt_text']) ?: '-'; ?></td>
                            <td class="px-4 py-4 text-center">
                                <?php 
                                    if($h['status'] == 'approved') echo '<span class="bg-emerald-100 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-400 px-3 py-1 rounded-lg text-[10px] font-bold">تایید شده</span>';
                                    elseif($h['status'] == 'rejected') echo '<span class="bg-rose-100 text-rose-700 dark:bg-rose-900/30 dark:text-rose-400 px-3 py-1 rounded-lg text-[10px] font-bold">رد شده</span>';
                                    else echo '<span class="bg-amber-100 text-amber-700 dark:bg-amber-900/30 dark:text-amber-400 px-3 py-1 rounded-lg text-[10px] font-bold">در انتظار</span>';
                                ?>
                            </td>
                            <td class="px-4 py-4 text-center">
                                <?php if($h['receipt_file']): ?>
                                    <a href="../../uploads/receipts/<?php echo $h['receipt_file']; ?>" target="_blank" class="text-blue-600 hover:underline font-bold text-xs bg-blue-50 dark:bg-blue-900/30 px-2 py-1 rounded">پیوست 🖼️</a>
                                <?php else: ?>
                                    <span class="text-gray-400 text-xs">-</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

    <div id="history-manual" class="tab-content hidden bg-white dark:bg-slate-800 rounded-3xl shadow-sm border border-gray-100 dark:border-slate-700 p-8 border-t-8 border-t-emerald-500">
        <h2 class="text-lg font-bold mb-6 text-gray-800 dark:text-white">تاریخچه شارژ، کسر دستی و هدایای بونوس</h2>
        <div class="overflow-x-auto">
            <table id="manualHistoryTable" class="w-full text-sm text-right text-gray-600 dark:text-gray-400">
                <thead class="bg-gray-50 dark:bg-slate-700/50 text-gray-700 dark:text-gray-300">
                    <tr>
                        <th class="px-4 py-3 rounded-tr-xl">شناسه</th>
                        <th class="px-4 py-3">تاریخ و زمان</th>
                        <th class="px-4 py-3">کاربر هدف</th>
                        <th class="px-4 py-3">نوع عملیات</th>
                        <th class="px-4 py-3">مبلغ (تومان)</th>
                        <th class="px-4 py-3 rounded-tl-xl">توضیحات</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100 dark:divide-slate-700">
                    <?php foreach($manualLogsAdmin as $log): 
                        $targetName = ($log['user_id'] == 0) ? '👥 همه اپراتورها' : (!empty($log['fullname']) ? htmlspecialchars($log['fullname']) : htmlspecialchars($log['username']));
                    ?>
                        <tr class="hover:bg-gray-50 dark:hover:bg-slate-700/30 transition">
                            <td class="px-4 py-4 font-mono text-xs text-gray-400">#LOG-<?php echo $log['id']; ?></td>
                            <td class="px-4 py-4 font-mono font-bold text-gray-800 dark:text-gray-200" dir="ltr"><?php echo format_shamsi($log['created_at']); ?></td>
                            <td class="px-4 py-4 font-bold text-gray-800 dark:text-gray-200"><?php echo $targetName; ?></td>
                            <td class="px-4 py-4">
                                <?php if($log['type'] == 'manual_add'): ?>
                                    <span class="text-emerald-600 dark:text-emerald-400 text-xs font-bold">➕ افزایش</span>
                                <?php elseif($log['type'] == 'bonus_add'): ?>
                                    <span class="text-indigo-600 dark:text-indigo-400 text-xs font-bold">🎁 بونوس</span>
                                <?php else: ?>
                                    <span class="text-rose-600 dark:text-rose-400 text-xs font-bold">➖ کسر</span>
                                <?php endif; ?>
                            </td>
                            <td class="px-4 py-4 font-black <?php echo ($log['type'] == 'manual_sub') ? 'text-rose-600 dark:text-rose-400' : 'text-emerald-600 dark:text-emerald-400'; ?>" dir="ltr"><?php echo number_format($log['amount']); ?></td>
                            <td class="px-4 py-4 text-xs text-gray-500 dark:text-gray-400"><?php echo htmlspecialchars($log['description']); ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

    <div id="history-credit" class="tab-content hidden bg-white dark:bg-slate-800 rounded-3xl shadow-sm border border-gray-100 dark:border-slate-700 p-8 border-t-8 border-t-rose-500">
        <h2 class="text-lg font-bold mb-6 text-gray-800 dark:text-white">تاریخچه تخصیص اعتبار (ایجاد بدهی)</h2>
        <div class="overflow-x-auto">
            <table id="creditHistoryTable" class="w-full text-sm text-right text-gray-600 dark:text-gray-400">
                <thead class="bg-gray-50 dark:bg-slate-700/50 text-gray-700 dark:text-gray-300">
                    <tr>
                        <th class="px-4 py-3 rounded-tr-xl">شناسه</th>
                        <th class="px-4 py-3">تاریخ و زمان</th>
                        <th class="px-4 py-3">نماینده</th>
                        <th class="px-4 py-3">مبلغ اعتبار</th>
                        <th class="px-4 py-3 rounded-tl-xl">توضیحات</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100 dark:divide-slate-700">
                    <?php foreach($creditLogsAdmin as $log): 
                        $targetName = !empty($log['fullname']) ? htmlspecialchars($log['fullname']) : htmlspecialchars($log['username']);
                    ?>
                        <tr class="hover:bg-gray-50 dark:hover:bg-slate-700/30 transition">
                            <td class="px-4 py-4 font-mono text-xs text-gray-400">#LOG-<?php echo $log['id']; ?></td>
                            <td class="px-4 py-4 font-mono font-bold text-gray-800 dark:text-gray-200" dir="ltr"><?php echo format_shamsi($log['created_at']); ?></td>
                            <td class="px-4 py-4 font-bold text-gray-800 dark:text-gray-200"><?php echo $targetName; ?></td>
                            <td class="px-4 py-4 font-black text-rose-600 dark:text-rose-400" dir="ltr"><?php echo number_format($log['amount']); ?></td>
                            <td class="px-4 py-4 text-xs text-gray-500 dark:text-gray-400"><?php echo htmlspecialchars($log['description']); ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<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 ($('#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 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>