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

<div class="bg-gradient-to-r from-rose-600 to-red-800 dark:from-rose-900 dark:to-red-950 rounded-3xl p-6 md:p-8 mb-8 text-white flex flex-col lg:flex-row justify-between items-center shadow-lg relative overflow-hidden border border-rose-500/30">
    <div class="absolute -left-10 -bottom-10 opacity-10 text-9xl">⚠️</div>
    <div class="relative z-10 flex items-center gap-4 w-full lg:w-1/2 mb-6 lg:mb-0">
        <div class="w-16 h-16 bg-white/20 rounded-full flex items-center justify-center text-3xl shrink-0 backdrop-blur-sm">💳</div>
        <div>
            <h2 class="text-sm font-bold text-rose-100 mb-1 tracking-wider uppercase">مجموع کل بدهی فعلی شبکه (تسویه‌نشده)</h2>
            <div class="text-3xl md:text-4xl font-black tracking-widest drop-shadow-md" dir="ltr">
                <?php echo number_format($totalDebt); ?> <span class="text-base font-normal text-rose-200">تومان</span>
            </div>
        </div>
    </div>
</div>

<div class="grid grid-cols-1 lg:grid-cols-3 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-6 flex flex-col border-t-8 border-t-fuchsia-500">
        <div class="flex justify-between items-center mb-6 border-b border-gray-100 dark:border-slate-700 pb-4">
            <h2 class="text-lg font-bold text-gray-800 dark:text-white flex items-center gap-2">
                <span class="text-2xl">🛟</span> مدیریت شارژ اضطراری
            </h2>
        </div>
        
        <div class="flex-1 space-y-6">
            <div class="bg-fuchsia-50 dark:bg-fuchsia-900/20 p-4 rounded-2xl border border-fuchsia-100 dark:border-fuchsia-800">
                <h3 class="text-xs font-black text-fuchsia-800 dark:text-fuchsia-400 mb-2">سقف سراسری (پیش‌فرض سیستم)</h3>
                <form method="POST" class="flex gap-2">
                    <input type="hidden" name="action" value="update_global_emergency">
                    <input type="text" name="global_amount" required value="<?php echo $globalEmergency > 0 ? number_format($globalEmergency) : ''; ?>" placeholder="مبلغ (تومان)" class="w-full border-2 border-fuchsia-200 dark:border-fuchsia-700 p-2.5 rounded-xl outline-none font-mono font-bold text-sm text-fuchsia-700 dark:text-fuchsia-300 bg-white dark:bg-slate-800" dir="ltr" onkeyup="formatCurrency(this)">
                    <button type="submit" class="bg-fuchsia-500 hover:bg-fuchsia-600 text-white font-bold px-4 rounded-xl transition shadow-md text-xs shrink-0">ثبت</button>
                </form>
            </div>

            <div class="bg-gray-50 dark:bg-slate-700/50 p-4 rounded-2xl border border-gray-200 dark:border-slate-600">
                <h3 class="text-xs font-black text-gray-800 dark:text-gray-200 mb-2">سقف اختصاصی نماینده (VIP)</h3>
                <form method="POST" class="space-y-3">
                    <input type="hidden" name="action" value="update_user_emergency">
                    <select name="target_user_id" required class="w-full border-2 border-gray-200 dark:border-slate-600 p-2.5 rounded-xl outline-none font-bold text-xs bg-white dark:bg-slate-800 dark:text-white">
                        <option value="" disabled selected>-- انتخاب نماینده --</option>
                        <?php foreach($usersList as $u): ?>
                            <option value="<?php echo $u['id']; ?>"><?php echo !empty($u['fullname']) ? htmlspecialchars($u['fullname']) : htmlspecialchars($u['username']); ?></option>
                        <?php endforeach; ?>
                    </select>
                    <div class="flex gap-2">
                        <input type="text" name="user_amount" placeholder="خالی = حذف" class="w-full border-2 border-gray-200 dark:border-slate-600 p-2.5 rounded-xl outline-none font-mono font-bold text-sm text-gray-700 dark:text-gray-300 bg-white dark:bg-slate-800" dir="ltr" onkeyup="formatCurrency(this)">
                        <button type="submit" class="bg-gray-800 hover:bg-gray-900 dark:bg-gray-600 dark:hover:bg-gray-500 text-white font-bold px-4 rounded-xl transition shadow-md text-xs shrink-0">اعمال</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="bg-white dark:bg-slate-800 rounded-3xl shadow-sm border border-gray-100 dark:border-slate-700 p-6 flex flex-col border-t-8 border-t-rose-500">
        <div class="flex justify-between items-center mb-6 border-b border-gray-100 dark:border-slate-700 pb-4">
            <h2 class="text-lg font-bold text-gray-800 dark:text-white flex items-center gap-2">
                <span class="text-2xl">💳</span> تخصیص شارژ اعتباری
            </h2>
        </div>
        
        <p class="text-[11px] text-gray-500 dark:text-gray-400 mb-6 font-bold leading-relaxed text-justify">
            با این گزینه، کیف پول کاربر شارژ شده و معادل آن بدهی در سیستم ثبت می‌شود تا با واریز فیش بعدی، این بدهی به طور خودکار تسویه گردد.
        </p>
        
        <form method="POST" class="space-y-4 mt-auto">
            <input type="hidden" name="action" value="allocate_credit">
            <div>
                <label class="block text-xs font-bold text-gray-700 dark:text-gray-300 mb-2">انتخاب نماینده هدف</label>
                <select name="target_user_id" required class="w-full border-2 border-gray-200 dark:border-slate-600 p-3 rounded-xl focus:border-rose-500 outline-none font-bold text-sm bg-gray-50 dark:bg-slate-700 dark:text-white">
                    <option value="" disabled selected>-- لیست نمایندگان --</option>
                    <?php foreach($usersList as $u): ?>
                        <option value="<?php echo $u['id']; ?>"><?php echo !empty($u['fullname']) ? htmlspecialchars($u['fullname']) : htmlspecialchars($u['username']); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div>
                <label class="block text-xs font-bold text-gray-700 dark:text-gray-300 mb-2">مبلغ اعتبار (تومان)</label>
                <input type="text" name="credit_amount" required placeholder="مثلاً: 1,000,000" class="w-full border-2 border-gray-200 dark:border-slate-600 p-3 rounded-xl focus:border-rose-500 outline-none font-mono font-bold text-lg text-rose-700 dark:text-rose-400 bg-transparent" dir="ltr" onkeyup="formatCurrency(this)">
            </div>
            <button type="submit" onclick="return confirm('تخصیص اعتبار و ثبت بدهی انجام شود؟');" class="w-full bg-rose-500 hover:bg-rose-600 text-white font-bold py-3.5 rounded-xl transition shadow-md mt-2 text-sm">اعمال شارژ و ثبت بدهی</button>
        </form>
    </div>

    <div class="bg-white dark:bg-slate-800 rounded-3xl shadow-sm border border-gray-100 dark:border-slate-700 p-6 flex flex-col border-t-8 border-t-emerald-500">
        <div class="flex justify-between items-center mb-6 border-b border-gray-100 dark:border-slate-700 pb-4">
            <h2 class="text-lg font-bold text-gray-800 dark:text-white flex items-center gap-2">
                <span class="text-2xl">💰</span> شارژ یا کسر دستی
            </h2>
        </div>
        
        <div class="bg-blue-50 dark:bg-blue-900/20 p-3 rounded-xl border border-blue-100 dark:border-blue-800/50 mb-5">
            <p class="text-[10px] text-blue-600 dark:text-blue-400 font-bold leading-relaxed text-justify">این بخش تاثیری روی عدد بدهی کاربران ندارد و مستقیماً موجودی کیف پول را کم یا زیاد می‌کند (برای اصلاح حساب).</p>
        </div>

        <form method="POST" class="space-y-4 mt-auto">
            <input type="hidden" name="action" value="manual_charge">
            <div class="grid grid-cols-2 gap-3">
                <div>
                    <label class="block text-xs font-bold text-gray-700 dark:text-gray-300 mb-2">کاربر / گروه</label>
                    <select name="target_user_id" required class="w-full border-2 border-gray-200 dark:border-slate-600 p-2.5 rounded-xl focus:border-emerald-500 outline-none font-bold text-xs bg-gray-50 dark:bg-slate-700 dark:text-white">
                        <option value="all" class="text-blue-600 dark:text-blue-400 font-black">👥 تمام اپراتورها</option>
                        <optgroup label="لیست اپراتورها">
                            <?php foreach($usersList as $u): ?>
                                <option value="<?php echo $u['id']; ?>"><?php echo !empty($u['fullname']) ? htmlspecialchars($u['fullname']) : htmlspecialchars($u['username']); ?></option>
                            <?php endforeach; ?>
                        </optgroup>
                    </select>
                </div>
                <div>
                    <label class="block text-xs font-bold text-gray-700 dark:text-gray-300 mb-2">نوع عملیات</label>
                    <select name="charge_type" class="w-full border-2 border-gray-200 dark:border-slate-600 p-2.5 rounded-xl focus:border-emerald-500 outline-none font-bold text-xs bg-gray-50 dark:bg-slate-700 dark:text-white">
                        <option value="add">➕ افزایش</option>
                        <option value="subtract">➖ کسر</option>
                    </select>
                </div>
            </div>
            <div>
                <label class="block text-xs font-bold text-gray-700 dark:text-gray-300 mb-2">مبلغ عملیات (تومان)</label>
                <input type="text" name="manual_amount" required placeholder="100,000" class="w-full border-2 border-gray-200 dark:border-slate-600 p-3 rounded-xl focus:border-emerald-500 outline-none font-mono font-bold text-lg text-emerald-700 dark:text-emerald-400 bg-transparent" dir="ltr" onkeyup="formatCurrency(this)">
            </div>
            <button type="submit" onclick="return confirm('تایید اعمال دستی کیف پول؟');" class="w-full bg-emerald-500 hover:bg-emerald-600 text-white font-bold py-3.5 rounded-xl transition shadow-md mt-2 text-sm">ثبت تراکنش در کیف پول</button>
        </form>
    </div>
</div>