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

<div class="grid grid-cols-1 lg:grid-cols-2 gap-8 mb-8">
    <div class="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-amber-500 h-fit lg:col-span-2">
        <h2 class="text-xl font-bold mb-6 text-gray-800 dark:text-white flex items-center justify-between">
            <span>فیش‌های نیازمند تایید</span>
            <span class="bg-amber-100 text-amber-700 dark:bg-amber-900/50 dark:text-amber-400 text-xs px-2 py-1 rounded-lg"><?php echo count($pendingRequests); ?> مورد</span>
        </h2>
        
        <?php if(empty($pendingRequests)): ?>
            <div class="bg-gray-50 dark:bg-slate-700/50 border border-gray-200 dark:border-slate-600 text-gray-500 dark:text-gray-400 p-8 rounded-2xl text-center font-bold">درخواست بررسی نشده‌ای وجود ندارد.</div>
        <?php else: ?>
            <div class="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-6 max-h-[600px] overflow-y-auto custom-scrollbar pr-2">
                <?php foreach($pendingRequests as $req): 
                    $displayName = !empty($req['fullname']) ? htmlspecialchars($req['fullname']) : htmlspecialchars($req['username']);
                    $debt = (int)$req['credit_debt'];
                    $reqAmount = (int)$req['amount'];
                    
                    $netWallet = $reqAmount - $debt;
                    if ($netWallet < 0) $netWallet = 0;
                    
                    $potentialBonusPercent = 0;
                    $potentialBonusAmt = 0;
                    if ($isBonusSystemActive && $netWallet > 0) {
                        // بررسی مجوز بونوس برای این کاربر در پنل ادمین
                        $isTargetBonusActive = true;
                        $stmtTargetBonusCheck = $pdo->prepare("SELECT COUNT(*) FROM user_prices WHERE user_id = ? AND (allow_bonus = 0 OR allow_bonus IS NULL)");
                        $stmtTargetBonusCheck->execute([$req['user_id']]);
                        if ($stmtTargetBonusCheck->fetchColumn() > 0) {
                            $isTargetBonusActive = false;
                        }
                        
                        if ($isTargetBonusActive) {
                            foreach ($activeBonuses as $b) {
                                if ($netWallet >= $b['min_amount']) {
                                    $potentialBonusPercent = $b['bonus_percent'];
                                    $potentialBonusAmt = ($netWallet * $potentialBonusPercent) / 100;
                                    break;
                                }
                            }
                        }
                    }
                ?>
                    <div class="border-2 border-amber-200 dark:border-amber-700 bg-amber-50 dark:bg-amber-900/20 rounded-3xl p-6 flex flex-col shadow-sm">
                        <div class="flex justify-between items-start mb-4 border-b border-amber-100 dark:border-amber-800/50 pb-3">
                            <div>
                                <span class="font-black text-gray-800 dark:text-gray-200 block text-base"><?php echo $displayName; ?></span>
                                <div class="text-left text-[10px] text-gray-500 dark:text-gray-400 font-mono mt-1" dir="ltr"><?php echo format_shamsi($req['created_at']); ?></div>
                            </div>
                            <div class="text-amber-700 dark:text-amber-400 font-black text-xl" dir="ltr"><?php echo number_format($reqAmount); ?> <span class="text-[10px] font-normal">T</span></div>
                        </div>
                        
                        <div class="flex-1 space-y-3 mb-4">
                            <?php if($debt > 0): ?>
                                <div class="bg-rose-100 dark:bg-rose-900/40 text-rose-700 dark:text-rose-300 text-xs font-bold px-3 py-2 rounded-xl border border-rose-200 dark:border-rose-800">
                                    ⚠️ <b><?php echo number_format($debt); ?> تومان</b> بدهی تسویه‌نشده دارد.
                                </div>
                                <div class="text-[11px] text-gray-600 dark:text-gray-300 px-1 font-bold">
                                    <?php if($netWallet > 0): ?>
                                        مبلغ خالص قابل شارژ: <span class="text-emerald-600 dark:text-emerald-400 font-black" dir="ltr"><?php echo number_format($netWallet); ?> T</span>
                                    <?php else: ?>
                                        <span class="text-rose-500">کل فیش صرف تسویه بدهی می‌شود.</span>
                                    <?php endif; ?>
                                </div>
                            <?php endif; ?>

                            <?php if($potentialBonusPercent > 0): ?>
                                <div class="bg-emerald-100 dark:bg-emerald-900/40 text-emerald-700 dark:text-emerald-300 text-xs font-bold px-3 py-2.5 rounded-xl border border-emerald-200 dark:border-emerald-800 flex justify-between items-center">
                                    <span>🎁 <b><?php echo $potentialBonusPercent; ?>٪</b> بونوس</span>
                                    <span class="font-black" dir="ltr">+ <?php echo number_format($potentialBonusAmt); ?> T</span>
                                </div>
                            <?php endif; ?>
                            
                            <div class="bg-white dark:bg-slate-800 p-3 rounded-xl border border-amber-100 dark:border-slate-700 text-xs">
                                <p class="text-gray-700 dark:text-gray-300 font-bold mb-1">یادداشت فیش:</p>
                                <p class="text-gray-500 dark:text-gray-400"><?php echo htmlspecialchars($req['receipt_text']) ?: 'بدون توضیحات'; ?></p>
                                <?php if($req['receipt_file']): ?>
                                    <a href="../../uploads/receipts/<?php echo $req['receipt_file']; ?>" target="_blank" class="text-blue-600 dark:text-blue-400 font-black underline mt-2 block w-max bg-blue-50 dark:bg-blue-900/30 px-2 py-1 rounded">مشاهده تصویر فیش 🖼️</a>
                                <?php endif; ?>
                            </div>
                        </div>
                        
                        <div class="flex gap-3 mt-auto">
                            <form method="POST" class="flex-1">
                                <input type="hidden" name="action" value="approve_charge"><input type="hidden" name="req_id" value="<?php echo $req['id']; ?>">
                                <button type="submit" onclick="return confirm('تایید این فیش؟ محاسبات بدهی و بونوس به طور خودکار انجام می‌شود.');" class="w-full bg-green-500 text-white py-2.5 rounded-xl font-bold hover:bg-green-600 shadow-md transition text-xs">✅ تایید هوشمند</button>
                            </form>
                            <form method="POST" class="w-1/3">
                                <input type="hidden" name="action" value="reject_charge"><input type="hidden" name="req_id" value="<?php echo $req['id']; ?>">
                                <button type="submit" onclick="return confirm('آیا از رد کردن این فیش مطمئن هستید؟');" class="w-full bg-red-50 text-red-600 border border-red-200 dark:bg-red-900/20 dark:border-red-800 dark:text-red-400 py-2.5 rounded-xl font-bold hover:bg-red-100 shadow-sm transition text-xs">❌ رد فیش</button>
                            </form>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</div>