<?php
$title = '用户管理';
include __DIR__ . '/head.php';
$vipnum = $DB->getColumn("SELECT count(*) FROM pre_user_vip");
$vipList = $DB->getAll("SELECT vip, name FROM pre_user_vip ORDER BY vip ASC");
?>

<div class="p-4 md:p-6">
<?php if (isset($_GET['my']) && $_GET['my'] == 'edit') {
    $zid = intval($_GET['zid']);
    $row = $DB->getRow("SELECT * FROM pre_user WHERE zid='$zid' LIMIT 1");
?>
    <!-- 编辑用户 -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div class="p-4 md:p-6 border-b border-gray-200">
            <h2 class="text-base md:text-lg font-semibold text-gray-800">编辑用户信息</h2>
        </div>
        <div class="p-4 md:p-6">
            <form onsubmit="saveSetting(this);return false;" class="max-w-2xl space-y-4">
                <input type="hidden" name="zid" value="<?php echo $zid; ?>">
                
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">用户名</label>
                    <div class="md:col-span-3">
                        <input type="text" class="w-full px-3 md:px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm" name="user" value="<?php echo htmlspecialchars($row['user']); ?>">
                        <p class="mt-1 text-xs text-red-500">非必要请勿修改用户名</p>
                    </div>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">账户余额</label>
                    <div class="md:col-span-3">
                        <input type="text" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" name="rmb" value="<?php echo $row['rmb']; ?>">
                    </div>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-sm font-medium text-gray-700">用户等级</label>
                    <div class="md:col-span-3">
                        <input type="number" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" name="vip" value="<?php echo $row['vip']; ?>" min="1" max="<?php echo $vipnum; ?>">
                        <p class="mt-1 text-xs text-gray-500">等级范围：1 - <?php echo $vipnum; ?></p>
                    </div>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-sm font-medium text-gray-700">重置密码</label>
                    <div class="md:col-span-3">
                        <input type="text" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" name="pwd" placeholder="留空则不修改">
                    </div>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">分站域名</label>
                    <div class="md:col-span-3">
                        <input type="text" class="w-full px-3 md:px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm" name="domain" value="<?php echo htmlspecialchars($row['domain'] ?? ''); ?>">
                    </div>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">分站域名2</label>
                    <div class="md:col-span-3">
                        <input type="text" class="w-full px-3 md:px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm" name="domain2" value="<?php echo htmlspecialchars($row['domain2'] ?? ''); ?>">
                    </div>
                </div>
                
                <div class="flex flex-col md:flex-row items-stretch md:items-center gap-3 pt-4">
                    <button type="submit" class="w-full md:w-auto px-3 md:px-4 py-1.5 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm">
                        <i class="fas fa-save mr-1"></i>保存修改
                    </button>
                    <a href="user.php" class="w-full md:w-auto px-3 md:px-4 py-1.5 bg-gray-100 hover:bg-gray-200 text-gray-700 font-medium rounded-lg transition text-sm">
                        <i class="fas fa-arrow-left mr-1"></i>返回列表
                    </a>
                </div>
            </form>
        </div>
    </div>

<?php } else { ?>
    <!-- 用户列表 -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div class="p-4 md:p-6 border-b border-gray-200">
            <div class="flex flex-col md:flex-row md:items-center md:justify-between gap-3">
                <h2 class="text-base md:text-lg font-semibold text-gray-800">用户列表</h2>
                <div class="flex flex-col md:flex-row md:flex-nowrap md:items-center gap-2 md:gap-3">
                    <input type="hidden" id="is" value="1">
                    <form onsubmit="searchOrder();return false;" class="flex flex-col sm:flex-row sm:flex-wrap sm:items-center gap-2 md:flex-nowrap">
                        <select name="vip_filter" class="w-full sm:w-auto px-3 py-1.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm whitespace-nowrap">
                            <option value="0">全部等级</option>
                            <?php foreach($vipList as $v): ?>
                            <option value="<?php echo $v['vip']; ?>">VIP<?php echo $v['vip']; ?> - <?php echo htmlspecialchars($v['name']); ?></option>
                            <?php endforeach; ?>
                        </select>
                        <div class="flex gap-2">
                            <input type="text" class="flex-1 sm:w-40 px-3 py-1.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm" name="kw" placeholder="QQ / ID / 用户名">
                            <button type="submit" class="px-3 py-1.5 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm whitespace-nowrap shrink-0" title="搜索">
                                <i class="fas fa-search"></i>
                            </button>
                        </div>
                    </form>
                    <div class="flex gap-2 flex-wrap">
                        <button onclick="add_user()" class="px-3 py-1.5 bg-green-600 hover:bg-green-700 text-white font-medium rounded-lg transition text-sm whitespace-nowrap shrink-0">
                            <i class="fas fa-plus mr-1"></i>添加
                        </button>
                        <button onclick="batch_change_vip()" class="px-3 py-1.5 bg-indigo-600 hover:bg-indigo-700 text-white font-medium rounded-lg transition text-sm whitespace-nowrap shrink-0">
                            <i class="fas fa-exchange-alt mr-1"></i>一键修改等级
                        </button>
                        <button onclick="del_user()" class="px-3 py-1.5 bg-red-600 hover:bg-red-700 text-white font-medium rounded-lg transition text-sm whitespace-nowrap shrink-0">
                            <i class="fas fa-trash mr-1"></i>清理
                        </button>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="overflow-x-auto">
            <table class="w-full min-w-[700px]">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">选择</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">ID</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">用户名</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">QQ</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">余额</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">佣金</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">等级</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">上级</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">域名</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">注册时间</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">状态</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">操作</th>
                    </tr>
                </thead>
                <tbody id="user-list" class="divide-y divide-gray-200">
                </tbody>
            </table>
        </div>
        
        <!-- 分页和数量显示 -->
        <div class="px-4 py-3 border-t border-gray-200 flex flex-col sm:flex-row sm:items-center justify-between gap-3 text-sm">
            <div class="text-gray-500">
                共 <span id="totalCount">0</span> 条记录
            </div>
            <div class="flex items-center gap-2">
                <select class="px-2.5 py-1 border border-gray-300 rounded text-xs" name="pageSize" onchange="refreshTable()">
                    <option value="30">30</option>
                    <option value="50">50</option>
                    <option value="100">100</option>
                    <option value="200">200</option>
                    <option value="500">500</option>
                </select>
                <button onclick="prevPage()" id="prevPage" disabled 
                        class="px-2.5 py-1 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded text-xs transition-colors">
                    <i class="fas fa-chevron-left"></i>
                </button>
                <span class="text-gray-500">第 <span id="currentPage">1</span> / <span id="totalPages">1</span> 页</span>
                <button onclick="nextPage()" id="nextPage" disabled 
                        class="px-2.5 py-1 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded text-xs transition-colors">
                    <i class="fas fa-chevron-right"></i>
                </button>
            </div>
        </div>
    </div>
<?php } ?>

<!-- 清理无效用户弹窗 -->
<div class="modal fade" id="del_user_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">清理无效用户</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="mb-3">
                    <label class="block text-sm font-medium mb-2">删除类型</label>
                    <select id="del_type" class="w-full px-3 py-2 border rounded-lg">
                        <option value="0">保留有余额/佣金的用户</option>
                        <option value="1">删除所有无QQ数据用户</option>
                    </select>
                </div>
                <div class="text-sm text-red-500">
                    <p>1. 单次最多删除5000条数据</p>
                    <p>2. 数量较多请多次执行</p>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="confirm_del_user()">确定</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!-- 添加用户弹窗 -->
<div class="modal fade" id="add_user_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">添加用户</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="space-y-4">
                    <div>
                        <label class="block text-sm font-medium mb-1">登录账号</label>
                        <input type="text" id="user" class="w-full px-3 py-2 border rounded-lg" placeholder="建议使用QQ号">
                    </div>
                    <div>
                        <label class="block text-sm font-medium mb-1">登录密码</label>
                        <input type="text" id="pwd" class="w-full px-3 py-2 border rounded-lg">
                    </div>
                    <div>
                        <label class="block text-sm font-medium mb-1">QQ号码</label>
                        <input type="number" id="qq" class="w-full px-3 py-2 border rounded-lg" placeholder="用于找回">
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="confirm_add_user()">确定</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!-- 一键修改等级弹窗 -->
<div class="modal fade" id="batch_change_vip_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">一键修改等级</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="space-y-4">
                    <div>
                        <label class="block text-sm font-medium mb-2">原等级</label>
                        <select id="change_vip_from" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                            <?php foreach($vipList as $v): ?>
                            <option value="<?php echo $v['vip']; ?>">VIP<?php echo $v['vip']; ?> - <?php echo htmlspecialchars($v['name']); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-medium mb-2">目标等级</label>
                        <select id="change_vip_to" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                            <?php foreach($vipList as $v): ?>
                            <option value="<?php echo $v['vip']; ?>">VIP<?php echo $v['vip']; ?> - <?php echo htmlspecialchars($v['name']); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <p class="text-sm text-gray-500">将原等级的所有用户批量修改为目标等级</p>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="confirm_batch_change_vip()">确定</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!-- 设置用户等级弹窗 -->
<div class="modal fade" id="set_vip_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">设置用户等级</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div>
                    <label class="block text-sm font-medium mb-2">用户等级</label>
                    <input type="number" id="batch_vip" class="w-full px-3 py-2 border rounded-lg" placeholder="请输入等级" min="1" max="<?php echo $vipnum; ?>">
                    <p class="mt-1 text-xs text-gray-500">等级范围：1 - <?php echo $vipnum; ?></p>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="confirm_set_vip()">确定</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!-- 操作余额弹窗 -->
<div class="modal fade" id="recharge_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">操作余额</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="space-y-4">
                    <div class="flex items-center gap-2">
                        <select id="type" class="px-3 py-2 border rounded-lg">
                            <option value="0">充值</option>
                            <option value="1">扣除</option>
                        </select>
                        <input type="number" id="money" class="flex-1 px-3 py-2 border rounded-lg" placeholder="金额">
                        <span>元</span>
                    </div>
                    <div>
                        <input type="text" id="remark" class="w-full px-3 py-2 border rounded-lg" placeholder="备注(可留空)">
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="confirm_recharge()">确定</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!-- 操作佣金弹窗 -->
<div class="modal fade" id="recharge_tc_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">操作佣金</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="space-y-4">
                    <div class="flex items-center gap-2">
                        <select id="type_tc" class="px-3 py-2 border rounded-lg">
                            <option value="0">充值</option>
                            <option value="1">扣除</option>
                        </select>
                        <input type="number" id="money_tc" class="flex-1 px-3 py-2 border rounded-lg" placeholder="金额">
                        <span>元</span>
                    </div>
                    <div>
                        <input type="text" id="remark_tc" class="w-full px-3 py-2 border rounded-lg" placeholder="备注(可留空)">
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="confirm_recharge_tc()">确定</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!-- 设置单个用户等级弹窗 -->
<div class="modal fade" id="set_single_vip_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">设置用户等级</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div>
                    <label class="block text-sm font-medium mb-2">用户等级</label>
                    <input type="number" id="single_vip" class="w-full px-3 py-2 border rounded-lg" placeholder="请输入等级" min="1" max="<?php echo $vipnum; ?>">
                    <p class="mt-1 text-xs text-gray-500">等级范围：1 - <?php echo $vipnum; ?></p>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="confirm_set_single_vip()">确定</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!-- 删除确认弹窗 -->
<div class="modal fade" id="del_confirm_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">删除确认</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p>确定要删除此用户吗?</p>
                <p class="text-red-500">数据将被彻底抹除且无法恢复!</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" onclick="confirm_del_user_single()">确定删除</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!-- 用户状态弹窗 -->
<div class="modal fade" id="user_status_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">用户状态</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <form id="user_status" class="space-y-4">
                    <input type="hidden" name="zid" id="status_zid">
                    <div>
                        <label class="block text-sm font-medium mb-1">状态</label>
                        <select name="status" class="w-full px-3 py-2 border rounded-lg">
                            <option value="0">正常</option>
                            <option value="1">封禁</option>
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-medium mb-1">封禁天数</label>
                        <input type="number" name="ban_day" id="status_ban_day" class="w-full px-3 py-2 border rounded-lg" placeholder="留空为永久">
                    </div>
                    <div>
                        <label class="block text-sm font-medium mb-1">封禁原因</label>
                        <input type="text" name="reason" id="status_reason" class="w-full px-3 py-2 border rounded-lg" placeholder="可留空">
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="confirm_user_status()">确定</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

</div>

<!-- Tailwind 弹窗 -->
<div id="tailwind-modal" class="fixed inset-0 z-50 hidden">
    <div class="fixed inset-0 bg-black/50 backdrop-blur-sm transition-opacity" onclick="closeModal()"></div>
    <div class="fixed inset-0 flex items-center justify-center p-4">
        <div class="bg-white rounded-xl shadow-2xl w-full max-w-md max-h-[85vh] flex flex-col relative transform transition-all">
            <div class="flex items-center justify-between px-5 py-4 border-b border-gray-200">
                <h3 id="modal-title" class="text-lg font-semibold text-gray-800"></h3>
                <button onclick="closeModal()" class="w-8 h-8 flex items-center justify-center rounded-full hover:bg-gray-100 text-gray-400 hover:text-gray-600 transition-colors">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            <div id="modal-body" class="px-5 py-4 overflow-y-auto"></div>
            <div id="modal-footer" class="px-5 py-3 border-t border-gray-200 flex gap-2"></div>
        </div>
    </div>
</div>

<!-- 弹窗内容模板 -->
<div id="modal-templates" class="hidden">
    <!-- 清理无效用户 -->
    <div id="template-del-user">
        <div class="space-y-4">
            <div>
                <label class="block text-sm font-medium mb-1">删除类型</label>
                <select id="del_type" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                    <option value="0">保留有余额/佣金的用户</option>
                    <option value="1">删除所有无QQ数据用户</option>
                </select>
            </div>
            <div class="text-sm text-red-500">
                <p>1. 单次最多删除5000条数据</p>
                <p>2. 数量较多请多次执行</p>
            </div>
        </div>
    </div>
    
    <!-- 添加用户 -->
    <div id="template-add-user">
        <div class="space-y-4">
            <div>
                <label class="block text-sm font-medium mb-1">登录账号</label>
                <input type="text" id="user" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="建议使用QQ号">
            </div>
            <div>
                <label class="block text-sm font-medium mb-1">登录密码</label>
                <input type="text" id="pwd" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
            </div>
            <div>
                <label class="block text-sm font-medium mb-1">QQ号码</label>
                <input type="number" id="qq" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="用于找回">
            </div>
        </div>
    </div>
    
    <!-- 设置用户等级 -->
    <div id="template-set-vip">
        <div>
            <label class="block text-sm font-medium mb-2">用户等级</label>
            <input type="number" id="batch_vip" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="请输入等级">
        </div>
    </div>
    
    <!-- 操作余额 -->
    <div id="template-recharge">
        <div class="space-y-4">
            <div class="flex items-center gap-2">
                <select id="type" class="px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                    <option value="0">充值</option>
                    <option value="1">扣除</option>
                </select>
                <input type="number" id="money" class="flex-1 px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="金额">
                <span>元</span>
            </div>
            <div>
                <input type="text" id="remark" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="备注(可留空)">
            </div>
        </div>
    </div>
    
    <!-- 操作佣金 -->
    <div id="template-recharge-tc">
        <div class="space-y-4">
            <div class="flex items-center gap-2">
                <select id="type_tc" class="px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                    <option value="0">充值</option>
                    <option value="1">扣除</option>
                </select>
                <input type="number" id="money_tc" class="flex-1 px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="金额">
                <span>元</span>
            </div>
            <div>
                <input type="text" id="remark_tc" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="备注(可留空)">
            </div>
        </div>
    </div>
    
    <!-- 设置单个用户等级 -->
    <div id="template-set-single-vip">
        <div>
            <label class="block text-sm font-medium mb-2">用户等级</label>
            <input type="number" id="single_vip" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="请输入等级">
        </div>
    </div>
    
    <!-- 删除确认 -->
    <div id="template-del-confirm">
        <div class="space-y-2">
            <p>确定要删除此用户吗?</p>
            <p class="text-red-500">数据将被彻底抹除且无法恢复!</p>
        </div>
    </div>
    
    <!-- 用户状态 -->
    <div id="template-user-status">
        <form id="user_status" class="space-y-4">
            <input type="hidden" name="zid" id="status_zid">
            <div>
                <label class="block text-sm font-medium mb-1">状态</label>
                <select name="status" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                    <option value="0">正常</option>
                    <option value="1">封禁</option>
                </select>
            </div>
            <div>
                <label class="block text-sm font-medium mb-1">封禁天数</label>
                <input type="number" name="ban_day" id="status_ban_day" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="留空为永久">
            </div>
            <div>
                <label class="block text-sm font-medium mb-1">封禁原因</label>
                <input type="text" name="reason" id="status_reason" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="可留空">
            </div>
        </form>
    </div>
</div>

<!-- 上级代理查询弹窗 -->
<div class="modal fade" id="shopagent_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">上级代理查询</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div id="shopagent_content"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

</div>

<?php include 'foot.php'; ?>

<script>
var checkflag1 = false;
var pagesize = 30;
var page = 1;
var totalCount = 0;
var totalPages = 1;
var vipnum;

function checkAll() {
    var checkboxes = document.querySelectorAll('input[name="checkbox[]"]');
    checkflag1 = !checkflag1;
    checkboxes.forEach(function(cb) {
        cb.checked = checkflag1;
    });
}

function del_user() {
    $('#del_user_modal').modal('show');
}

function confirm_del_user() {
    $('#del_user_modal').modal('hide');
    showLoading();
    $.ajax({
        type: 'POST',
        url: 'ajax.php?act=del_user',
        data: {type: 1, del_type: $('#del_type').val()},
        dataType: 'json',
        success: function(data) {
            hideLoading();
            showMsg(data.msg, data.code == 1 ? 1 : 2);
            if (data.code == 1) {
                get_list(page);
            }
        }
    });
}

function add_user() {
    $('#add_user_modal').modal('show');
}

function confirm_add_user() {
    var user = $('#user').val();
    var pwd = $('#pwd').val();
    var qq = $('#qq').val();
    if (user == '' || pwd == '') {
        showMsg('账号密码不能为空', 0);
        return false;
    }
    $('#add_user_modal').modal('hide');
    showLoading();
    $.ajax({
        type: 'POST',
        url: 'ajax.php?act=add_user',
        data: {user: user, pwd: pwd, qq: qq},
        dataType: 'json',
        success: function(data) {
            hideLoading();
            showMsg(data.msg, data.code == 1 ? 1 : 2);
            if (data.code == 1) {
                get_list(page);
            }
        }
    });
}

function batch_change_vip() {
    $('#batch_change_vip_modal').modal('show');
}

function confirm_batch_change_vip() {
    var fromVip = $('#change_vip_from').val();
    var toVip = $('#change_vip_to').val();
    if (fromVip == toVip) {
        showMsg('原等级与目标等级不能相同', 0);
        return;
    }
    $('#batch_change_vip_modal').modal('hide');
    showLoading();
    $.post('ajax.php?act=batch_change_vip', {from_vip: fromVip, to_vip: toVip}, function(d) {
        hideLoading();
        showMsg(d.msg, d.code == 1 ? 1 : 2);
        if (d.code == 1) {
            get_list(page);
        }
    }, 'json');
}

function operation() {
    let status = $('select[name="status"]').val();
    if (status != 1) {
        showMsg('请选择操作', 0);
        return false;
    }
    $('#set_vip_modal').modal('show');
}

function confirm_set_vip() {
    $('#set_vip_modal').modal('hide');
    var text = $('#batch_vip').val();
    if (!text) {
        showMsg('请输入等级', 0);
        return;
    }
    showLoading();
    $.post('ajax.php?act=batch_set_vip', $('#form1').serialize() + "&text=" + text, function(d) {
        hideLoading();
        showMsg(d.msg, d.code == 1 ? 1 : 2);
        if (d.code == 1) {
            get_list(page);
        }
    }, 'json');
}

$(document).ready(function() {
    if ($('#is').val() == 1) {
        get_list(page);
    }
});

function get_list(p) {
    if ($('#is').val() != 1) return false;
    page = p; // 更新全局页码变量
    var kw = $("input[name='kw']").val();
    var vip = $("select[name='vip_filter']").val();
    showLoading();
    $.ajax({
        type: 'POST',
        data: {pagesize: pagesize, page: page, kw: kw, vip: vip},
        url: 'ajax.php?act=get_user',
        dataType: 'json',
        success: function(data) {
            hideLoading();
            adddata(data.data);
            vipnum = data.vipnum;
            
            // 更新分页信息
            totalCount = data.total || 0;
            totalPages = Math.ceil(totalCount / pagesize) || 1;
            $('#totalCount').text(totalCount);
            $('#currentPage').text(page);
            $('#totalPages').text(totalPages);
            
            // 更新分页按钮状态
            $('#prevPage').prop('disabled', page == 1);
            $('#nextPage').prop('disabled', page == totalPages);
        },
        error: function() {
            hideLoading();
            showMsg('服务器错误', 2);
        }
    });
}

function adddata(data) {
    $("#user-list").empty();
    $.each(data, function(i, v) {
        var statusBtn = v.status == 1 
            ? '<span class="px-2 py-1 text-xs bg-red-100 text-red-700 rounded cursor-pointer" onclick="user_status(' + v.zid + ',' + v.status + ',\'' + (v.reason || '') + '\')">封禁</span>' 
            : '<span class="px-2 py-1 text-xs bg-green-100 text-green-700 rounded cursor-pointer" onclick="user_status(' + v.zid + ',' + v.status + ',\'' + (v.reason || '') + '\')">正常</span>';
        
        var tmp = `<tr class="hover:bg-gray-50">
            <td class="px-4 py-3 text-sm text-gray-500 whitespace-nowrap"><input type="checkbox" name="checkbox[]" value="${v.zid}" class="rounded border-gray-300"></td>
            <td class="px-4 py-3 text-sm font-medium text-gray-800 whitespace-nowrap">${v.zid}</td>
            <td class="px-4 py-3 text-sm whitespace-nowrap">${v.user}</td>
            <td class="px-4 py-3 text-sm text-gray-600 whitespace-nowrap">${v.qq || '-'}</td>
            <td class="px-4 py-3 text-sm whitespace-nowrap"><a href="javascript:recharge(${v.zid})" class="text-blue-600 hover:underline">${v.rmb}</a></td>
            <td class="px-4 py-3 text-sm whitespace-nowrap"><a href="javascript:recharge_tc(${v.zid})" class="text-orange-500 hover:underline">${v.rmb_tc}</a></td>
            <td class="px-4 py-3 whitespace-nowrap"><span class="px-2 py-1 text-xs bg-blue-100 text-blue-700 rounded cursor-pointer" onclick="setvip(${v.zid},${v.vip})">VIP${v.vip}</span></td>
            <td class="px-4 py-3 text-sm whitespace-nowrap"><span class="cursor-pointer hover:text-blue-600" onclick="shopagent(${v.zid})">${v.agent || '-'}</span></td>
            <td class="px-4 py-3 text-sm text-xs whitespace-nowrap">${isdomain(v.domain, v.domain2)}</td>
            <td class="px-4 py-3 text-sm text-gray-600 whitespace-nowrap">${v.addtime}</td>
            <td class="px-4 py-3 whitespace-nowrap">${statusBtn}</td>
            <td class="px-4 py-3 whitespace-nowrap">
                <div class="flex items-center gap-1.5 whitespace-nowrap">
                    <a href="./sso.php?zid=${v.zid}" target="_blank" class="px-2 py-0.5 text-xs bg-green-600 text-white rounded hover:bg-green-700 whitespace-nowrap">登入</a>
                    <a href="?my=edit&zid=${v.zid}" class="px-2 py-0.5 text-xs bg-blue-600 text-white rounded hover:bg-blue-700 whitespace-nowrap">编辑</a>
                    <button onclick="del(${v.zid})" class="px-2 py-0.5 text-xs bg-red-600 text-white rounded hover:bg-red-700 whitespace-nowrap">删除</button>
                </div>
            </td>
        </tr>`;
        $("#user-list").append(tmp);
    });
}

function searchOrder() {
    get_list(1, pagesize);
}

function refreshTable() {
    pagesize = $('select[name="pageSize"]').val();
    get_list(1);
}

function prevPage() {
    if (page > 1) {
        get_list(page - 1);
    }
}

function nextPage() {
    if (page < totalPages) {
        get_list(page + 1);
    }
}

function isdomain(domain, domain2) {
    var tmp = '';
    if (domain) tmp += domain;
    if (domain2) tmp += '<br>' + domain2;
    return tmp || '-';
}

var currentRechargeZid;

function recharge(zid) {
    currentRechargeZid = zid;
    $('#recharge_modal').modal('show');
}

function confirm_recharge() {
    $('#recharge_modal').modal('hide');
    showLoading();
    $.ajax({
        type: 'POST',
        url: 'ajax.php?act=recharge',
        data: {zid: currentRechargeZid, money: $('#money').val(), type: $('#type').val(), remark: $('#remark').val()},
        dataType: 'json',
        success: function(data) {
            hideLoading();
            showMsg(data.msg, data.code == 1 ? 1 : 2);
            if (data.code == 1) {
                get_list(page);
            }
        }
    });
}

var currentRechargeTcZid;

function recharge_tc(zid) {
    currentRechargeTcZid = zid;
    $('#recharge_tc_modal').modal('show');
}

function confirm_recharge_tc() {
    $('#recharge_tc_modal').modal('hide');
    showLoading();
    $.ajax({
        type: 'POST',
        url: 'ajax.php?act=recharge_tc',
        data: {zid: currentRechargeTcZid, money: $('#money_tc').val(), type: $('#type_tc').val(), remark: $('#remark_tc').val()},
        dataType: 'json',
        success: function(data) {
            hideLoading();
            showMsg(data.msg, data.code == 1 ? 1 : 2);
            if (data.code == 1) {
                get_list(page);
            }
        }
    });
}

var currentSetVipZid;

function setvip(zid, vip) {
    currentSetVipZid = zid;
    $('#single_vip').val(vip);
    $('#set_single_vip_modal').modal('show');
}

function confirm_set_single_vip() {
    $('#set_single_vip_modal').modal('hide');
    var text = $('#single_vip').val();
    showLoading();
    $.post('ajax.php?act=set_vip', {zid: currentSetVipZid, vip: text}, function(d) {
        hideLoading();
        showMsg(d.msg, d.code == 1 ? 1 : 2);
        if (d.code == 1) {
            get_list(page);
        }
    }, 'json');
}

var currentDelZid;

function del(zid) {
    currentDelZid = zid;
    $('#del_confirm_modal').modal('show');
}

function confirm_del_user_single() {
    $('#del_confirm_modal').modal('hide');
    showLoading();
    $.ajax({
        type: 'POST',
        url: 'ajax.php?act=del_user',
        data: {zid: currentDelZid},
        dataType: 'json',
        success: function(data) {
            hideLoading();
            showMsg(data.msg, data.code == 1 ? 1 : 2);
            if (data.code == 1) {
                get_list(page);
            }
        }
    });
}

function saveSetting(obj) {
    showLoading();
    $.ajax({
        type: 'POST',
        url: 'ajax.php?act=edit_user',
        data: $(obj).serialize(),
        dataType: 'json',
        success: function(data) {
            hideLoading();
            showMsg(data.msg, data.code == 1 ? 1 : 2);
        },
        error: function() {
            hideLoading();
            showMsg('服务器错误', 2);
        }
    });
}

function shopagent(zid) {
    showLoading();
    $.ajax({
        type: 'POST',
        url: 'ajax.php?act=shopagent',
        data: {zid: zid},
        dataType: 'html',
        success: function(data) {
            hideLoading();
            $('#shopagent_content').html(data);
            $('#shopagent_modal').modal('show');
        }
    });
}

function user_status(zid, zt, reason, ban_day) {
    $('#status_zid').val(zid);
    $('#status_ban_day').val(ban_day || '');
    $('#status_reason').val(reason == 'null' ? '' : reason);
    $('#user_status_modal').modal('show');
}

function confirm_user_status() {
    $('#user_status_modal').modal('hide');
    showLoading();
    $.ajax({
        type: 'POST',
        url: 'ajax.php?act=edit_user',
        data: $('#user_status').serialize(),
        dataType: 'json',
        success: function(data) {
            hideLoading();
            showMsg(data.msg, data.code == 1 ? 1 : 2);
            if (data.code == 1) {
                get_list(page);
            }
        }
    });
}
</script>
