<?php
$biz = (isset($_GET['biz']) && $_GET['biz'] === 'dg') ? 'dg' : 'cloud';
$title = $biz === 'dg' ? '代挂QQ列表' : '云端QQ列表';
include __DIR__ . '/head.php';
$kw=isset($_GET['kw'])?daddslashes($_GET['kw']):'';
$type=isset($_GET['type'])?daddslashes($_GET['type']):'';
$class_list = $DB->getAll("SELECT id,name FROM pre_login_class WHERE biz='{$biz}' ORDER BY sort ASC");
$version_name_dg = preg_replace('/版$/u', '', ($conf['version_name_dg'] ?? '代挂'));
?>

<div class="p-4 md:p-6">
    <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">
                <div class="flex items-center gap-3">
                    <h2 class="text-base md:text-lg font-semibold text-gray-800"><?php echo $biz === 'dg' ? htmlspecialchars($version_name_dg).'QQ列表' : '云端QQ列表' ?></h2>
                    <?php if(($conf['dg_open'] ?? '0') == '1'): ?>
                    <select class="px-2.5 py-1.5 border border-gray-300 rounded-lg text-sm" onchange="location.href='qqlist.php?biz='+this.value">
                        <option value="cloud" <?php echo $biz==='cloud'?'selected':''; ?>>云端</option>
                        <option value="dg" <?php echo $biz==='dg'?'selected':''; ?>><?php echo htmlspecialchars($version_name_dg); ?></option>
                    </select>
                    <?php endif; ?>
                </div>
                <div class="flex flex-wrap items-center gap-2">
                    <form onsubmit="searchOrder();return false;" class="flex items-center gap-2 w-full md:w-auto">
                        <input type="text" class="flex-1 min-w-[100px] px-2.5 py-1.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm" name="kw" value="<?php echo $kw?>" placeholder="输入QQ号/接口id">
                        <select class="min-w-[80px] px-2.5 py-1.5 border border-gray-300 rounded-lg text-sm" name="type">
                            <option value="-1"<?php echo $type=='-1'?' selected':''?>>全部</option>
                            <option value="1"<?php echo $type=='1'?' selected':''?>>在线</option>
                            <option value="0"<?php echo $type=='0'?' selected':''?>>离线</option>
                            <option value="3"<?php echo $type=='3'?' selected':''?>>过期</option>
                            <option value="2"<?php echo $type=='2'?' selected':''?>>接口id</option>
                            <option value="4"<?php echo $type=='4'?' selected':''?>>分类id</option>
                        </select>
                        <button type="submit" class="px-2.5 py-1.5 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm" title="搜索">
                            <i class="fas fa-search"></i>
                        </button>
                    </form>
                    <div class="flex gap-2 w-full sm:w-auto">
                        <button type="button" class="flex-1 sm:flex-none px-2.5 py-1.5 bg-green-600 hover:bg-green-700 text-white font-medium rounded-lg transition text-sm whitespace-nowrap" onclick="openModal()">
                            <i class="fas fa-cog mr-1"></i>批量操作
                        </button>
                        <button type="button" class="flex-1 sm:flex-none px-2.5 py-1.5 bg-red-600 hover:bg-red-700 text-white font-medium rounded-lg transition text-sm whitespace-nowrap" onclick="$('#batch_del_confirm_modal').modal('show')">
                            <i class="fas fa-trash mr-1"></i>批量删除
                        </button>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="p-3 md:p-6">
            <input type="hidden" id="biz" value="<?php echo htmlspecialchars($biz) ?>">
            <div class="overflow-x-auto">
                <table class="w-full min-w-[800px]">
                    <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">QQ</th>
                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">所属ZID</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">接口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">到期时间</th>
                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">操作</th>
                        </tr>
                    </thead>
                    <tbody id="qq-list" class="divide-y divide-gray-200">
                    </tbody>
                </table>
            </div>
        </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>
</div>

<!-- 批量删除结果弹窗 -->
<div class="modal fade" id="batch_del_result_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered" style="max-width: 400px;">
        <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-3">
                <div id="batch_del_result_content"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="px-3 py-1.5 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm" data-bs-dismiss="modal">确定</button>
            </div>
        </div>
    </div>
</div>

<!-- 批量删除确认弹窗 -->
<div class="modal fade" id="batch_del_confirm_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered" style="max-width: 400px;">
        <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-3">
                <p>确定要删除吗？</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="px-3 py-1.5 bg-red-600 hover:bg-red-700 text-white font-medium rounded-lg transition text-sm" onclick="confirm_batch_del()">确定删除</button>
                <button type="button" class="px-3 py-1.5 bg-gray-600 hover:bg-gray-700 text-white font-medium rounded-lg transition text-sm" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!-- 通知弹窗 -->
<div class="modal fade" id="notification_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered" style="max-width: 400px;">
        <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-3">
                <p id="notification_message"></p>
            </div>
            <div class="modal-footer">
                <button type="button" class="px-3 py-1.5 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm" data-bs-dismiss="modal">确定</button>
            </div>
        </div>
    </div>
</div>

<!-- 批量操作弹窗 -->
<div class="modal fade" id="batch_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered" style="max-width: 500px;">
        <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-3">
                <div class="space-y-2">
                    <div>
                        <label class="block text-xs font-medium mb-1">接口分类</label>
                        <select class="w-full px-2.5 py-1.5 border rounded-lg text-sm" id="cid1">
                            <?php
                            foreach ($class_list as $v) {
                                echo '<option value="'.$v['id'].'">'.htmlspecialchars($v['name']).'</option>';
                            }
                            ?>
                            <option value="9999">全部接口</option>
                        </select>
                    </div>
                    <div>
                        <label class="block text-xs font-medium mb-1">增减时长</label>
                        <div class="flex flex-wrap gap-1.5">
                            <select class="px-2.5 py-1.5 border rounded-lg flex-1 min-w-[100px] text-sm" id="renew_type1">
                                <option value="0">增加时长</option>
                                <option value="1">减少时长</option>
                            </select>
                            <input type="number" class="flex-1 px-2.5 py-1.5 border rounded-lg min-w-[100px] text-sm" id="num1" placeholder="请输入数值">
                            <select class="px-2.5 py-1.5 border rounded-lg flex-1 min-w-[70px] text-sm" id="type1">
                                <option value="0">月</option>
                                <option value="1">天</option>
                            </select>
                        </div>
                    </div>
                    <div>
                        <label class="block text-xs font-medium mb-1">备注</label>
                        <input type="text" class="w-full px-2.5 py-1.5 border rounded-lg text-sm" id="remark" placeholder="可留空">
                    </div>
                    <button type="button" class="w-full px-3 py-1.5 bg-blue-600 hover:bg-blue-700 text-white rounded transition text-sm" onclick="all_edit_time()">
                        一键_全体用户_增减时长
                    </button>
                    <button type="button" class="w-full px-3 py-1.5 bg-red-600 hover:bg-red-700 text-white rounded transition text-sm" onclick="all_del_qq()">
                        一键_删除所有过期的分类和过期QQ
                    </button>
                    <button type="button" class="w-full px-3 py-1.5 bg-gray-600 hover:bg-gray-700 text-white rounded transition text-sm" onclick="to_all_time()">
                        一键_把 代挂时间 转成 全部接口
                    </button>
                    <hr class="my-2">
                    <div>
                        <label class="block text-xs font-medium mb-1">接口分类</label>
                        <select class="w-full px-2.5 py-1.5 border rounded-lg text-sm" id="cid_add_qq_time">
                            <?php
                            foreach ($class_list as $v) {
                                echo '<option value="'.$v['id'].'">'.htmlspecialchars($v['name']).'</option>';
                            }
                            ?>
                            <option value="9999">全部接口</option>
                        </select>
                    </div>
                    <div>
                        <label class="block text-xs font-medium mb-1">QQ号</label>
                        <input type="text" class="w-full px-2.5 py-1.5 border rounded-lg text-sm" id="add_qq_time" placeholder="输入QQ号">
                    </div>
                    <div>
                        <label class="block text-xs font-medium mb-1">用户ZID</label>
                        <input type="text" class="w-full px-2.5 py-1.5 border rounded-lg text-sm" id="userzid" placeholder="可留空，填写zid这是新增QQ">
                    </div>
                    <div>
                        <label class="block text-xs font-medium mb-1">增加时长</label>
                        <div class="flex flex-wrap gap-1.5">
                            <select class="px-2.5 py-1.5 border rounded-lg flex-1 min-w-[70px] text-sm" id="add_qq_time_type">
                                <option value="0">月</option>
                                <option value="1">天</option>
                            </select>
                            <input type="number" class="flex-1 px-2.5 py-1.5 border rounded-lg min-w-[100px] text-sm" id="add_qq_time_num" placeholder="请输入数值">
                            <button type="button" class="px-3 py-1.5 bg-green-600 hover:bg-green-700 text-white rounded transition text-sm flex-1 min-w-[70px]" onclick="add_qq_time()">
                                增加
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<!-- 再次确认弹窗 -->
<div class="modal fade" id="to_all_time_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered" style="max-width: 400px;">
        <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-3">
                <div class="text-red-500 text-sm">
                    把现有的代挂时长<br>转成<br>全部接口<br><br>示：仅保留一个最长的时间<hr>请先告知用户在转换<br>此操作毕竟会删除其他分类的代挂时长
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" onclick="confirm_to_all_time()">确定</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!-- 再确认删除弹窗 -->
<div class="modal fade" id="all_del_qq_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered" style="max-width: 400px;">
        <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-3">
                <div class="space-y-2">
                    <div>
                        <label class="block text-xs font-medium mb-1">删除类型</label>
                        <select class="w-full px-2.5 py-1.5 border rounded-lg text-sm" id="del_type">
                            <option value="0">网站端</option>
                            <option value="1">网站端+框架</option>
                        </select>
                    </div>
                    <div class="text-red-500 text-xs">
                        <p>1. 单次最多删除5000条数据</p>
                        <p>2. 数量较多请多次执行</p>
                        <p>3. 删除框架里的QQ可能会数据错乱、闪退(部分框架)手动删除也会有这种问题</p>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" onclick="confirm_all_del_qq()">确定</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!-- 续费QQ弹窗 -->
<div class="modal fade" id="renew_qq_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered" style="max-width: 400px;">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">续费QQ</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body p-3">
                <div>
                    <label class="block text-xs font-medium mb-1">续费时长</label>
                    <input type="number" id="renew_time" class="w-full px-2.5 py-1.5 border rounded-lg text-sm" placeholder="请输入续费时长">
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="confirm_renew_qq()">确定</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!-- 删除QQ确认弹窗 -->
<div class="modal fade" id="delete_qq_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered" style="max-width: 400px;">
        <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-3">
                <p>确定要删除这个QQ吗？</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" onclick="confirm_delete_qq()">确定删除</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!-- 到期时间选择弹窗 -->
<div class="modal fade" id="endtime_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable" style="max-width: 500px; max-height: 90vh;">
        <div class="modal-content max-h-[90vh] flex flex-col">
            <div class="modal-header flex-shrink-0">
                <h5 class="modal-title">到期时间列表</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body p-3 overflow-y-auto overflow-x-auto min-h-0" style="max-height: 60vh;">
                <!-- 动态内容将通过 JavaScript 填充 -->
            </div>
            <div class="modal-footer flex-shrink-0">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<!-- 修改ZID弹窗 -->
<div class="modal fade" id="setzid_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered" style="max-width: 400px;">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">该QQ号转移至其他用户</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body p-3">
                <!-- 动态内容将通过 JavaScript 填充 -->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="confirm_setzid()">确定</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!-- 切换接口弹窗 -->
<div class="modal fade" id="setapiid_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered" style="max-width: 500px;">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">给QQ号更换指定登录接口</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body p-3">
                <!-- 动态内容将通过 JavaScript 填充 -->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="confirm_setapiid()">确定</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!-- 编辑到期时间弹窗 -->
<div class="modal fade" id="edit_endtime_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered" style="max-width: 400px;">
        <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-3">
                <!-- 动态内容将通过 JavaScript 填充 -->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="confirm_edit_endtime()">确定</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!-- 删除到期时间确认弹窗 -->
<div class="modal fade" id="del_time_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered" style="max-width: 400px;">
        <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-3">
                <p>确定要删除这个到期时间吗？</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" onclick="confirm_del_time()">确定删除</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

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

<script src="./assets/js/qqlist.js?ver=<?php echo VERSION ?>"></script>
