<?php
$title='接口列表';
include __DIR__ . '/head.php';
include __DIR__ . '/../includes/loginapi.php';
$act=isset($_GET['my'])?daddslashes($_GET['my']):null;
$biz = isset($_GET['biz']) ? daddslashes($_GET['biz']) : 'all';
if(!in_array($biz, ['all','cloud','dg'], true)) $biz = 'all';
$list = array();
if($act=='add' || $act=='edit'){
	$rs=$DB->query("SELECT * FROM `pre_protocol` ORDER BY sort");
	while($res = $rs->fetch(PDO::FETCH_ASSOC)){
		$list[$res['cid']][] = $res;
	}
}
?>
<input type="hidden" id="isalert" value="<?php echo $act?>">
<input type="hidden" id="biz" value="<?php echo htmlspecialchars($biz)?>">
<?php if($act=='add'){?>
<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">
            <h2 class="text-base md:text-lg font-semibold text-gray-800">添加登录接口</h2>
        </div>
        <div class="p-4 md:p-6">
            <form id="add" onsubmit="saveSetting(this);return false;" class="space-y-6">
                <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="citybz" required placeholder="自定义一个名称">
                    </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">
                        <select class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" name="biz" default="<?php echo $biz === 'all' ? 'cloud' : $biz ?>">
                            <option value="cloud">云端</option>
                            <option value="dg">代挂</option>
                        </select>
                    </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">登录API</label>
                    <div class="md:col-span-3">
                        <div class="flex flex-nowrap gap-2">
                            <input type="url" class="flex-1 min-w-0 px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm" name="url" placeholder="http://127.0.0.1:8886" required>
                            <button type="button" class="px-4 py-2 bg-gray-100 hover:bg-gray-200 text-gray-700 font-medium rounded-lg transition whitespace-nowrap text-sm" onclick="check_api()">检测是否正常</button>
                        </div>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center query_api_row">
                    <label class="text-sm font-medium text-gray-700">查询/代挂API</label>
                    <div class="md:col-span-3">
                        <input type="url" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm" name="query_api" placeholder="http://127.0.0.1:8886">
                        <p class="mt-1 text-xs text-gray-500">代挂业务用于 查询/补挂/开关 的节点API根地址</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">
                        <select class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" name="type" onchange="frame_type();">
                        <?php
                            foreach (framelist() as $k => $v) {
                                echo '<option value="'.$k.'" is_pwd="'.$v['ispwd'].'">'.($k=='little'?'little/panda':$k).'</option>';
                            }
                        ?>
                        </select>
                        <p class="mt-1 text-xs text-red-500">注：PC框架就选择PC</p>
                    </div>
                </div>
                <div id="protocol_row" 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">
                        <select class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" name="cid">
                        <option value="-1">请选择登录协议</option>
                        <?php
                            $rs=$DB->query("SELECT cid,count(*) as num FROM pre_protocol GROUP BY cid");
                            while($res = $rs->fetch()){
                                if($res['cid'] == $row['cid'] || $res['cid'] == $row['type']){
                                    echo '<option selected="selected" value="'.$res['cid'].'">'.$res['cid'].'</option>';
                                }else{
                                    echo '<option value="'.$res['cid'].'">'.$res['cid'].'</option>';
                                }
                            }
                        ?>
                        </select>
                        <p class="mt-1 text-xs text-red-500">dulu、xlz 需选择协议，dr 和 myqq 可留空</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">
                        <select class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" name="cids">
                            <?php
                                $rs=$DB->query("SELECT * FROM pre_login_class");
                                while($res = $rs->fetch()){
                                    echo '<option value="'.$res['id'].'">'.$res['name'].'</option>';
                                }
                            ?>
                        </select>
                    </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">
                        <select class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" name="show_online_census">
                            <option value="1">开启</option>
                            <option value="0">关闭</option>
                        </select>
                    </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">
                        <select class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" name="api_error_check">
                            <option value="1">开启</option>
                            <option value="0">关闭</option>
                        </select>
                    </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="max">
                    </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="apipwd" placeholder="填写0是免费、留空是收费">
                        <p class="mt-1 text-xs text-gray-500">如：填写123,用户输入123可免费使用，填写0直接免费</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">
                        <textarea class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" name="msg" rows="3"></textarea>
                    </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">
                        <select class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" name="msg_type">
                            <option value="0">默认_公告栏</option>
                            <option value="1">弹窗提示</option>
                            <option value="5">弹窗提示_5秒后可关闭</option>
                            <option value="10">弹窗提示_10秒后可关闭</option>
                        </select>
                    </div>
                </div>
                <div class="flex items-center gap-4 pt-4">
                    <button type="submit" class="px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition">
                        <i class="fas fa-plus mr-2"></i>添加
                    </button>
                    <button type="button" class="px-6 py-2 bg-gray-200 hover:bg-gray-300 text-gray-700 font-medium rounded-lg transition" onclick="if(document.referrer){history.back()}else{location.href='loginapi.php'}">
                        <i class="fas fa-arrow-left mr-2"></i>返回
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
<?php }else if($act=='edit'){
	$id = intval($_GET['id']);
	$row=$DB->getRow("select * from pre_loginconfig where id='$id' limit 1");
?>
<div class="p-6">
    <div class="bg-white rounded-xl shadow-sm border border-gray-200">
        <div class="p-6 border-b border-gray-200">
            <h2 class="text-lg font-semibold text-gray-800">编辑登录接口</h2>
        </div>
        <div class="p-6">
            <form id="add" onsubmit="saveSetting(this,'edit_loginapi');return false;" class="space-y-6">
                <input type="hidden" name="id" value="<?php echo $id?>">
                <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="citybz" value="<?php echo $row['citybz']?>" required placeholder="自定义一个名称">
                    </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">
                        <select class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" name="biz" default="<?php echo $row['biz'] ?? 'cloud' ?>">
                            <option value="cloud">云端</option>
                            <option value="dg">代挂</option>
                        </select>
                    </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">登录API</label>
                    <div class="md:col-span-3">
                        <div class="flex flex-nowrap gap-2">
                            <input type="url" class="flex-1 min-w-0 px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm" name="url" placeholder="http://127.0.0.1:8886" value="<?php echo $row['url']?>" required>
                            <button type="button" class="px-4 py-2 bg-gray-100 hover:bg-gray-200 text-gray-700 font-medium rounded-lg transition whitespace-nowrap text-sm" onclick="check_api()">检测是否正常</button>
                        </div>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center query_api_row">
                    <label class="text-sm font-medium text-gray-700">查询/代挂API</label>
                    <div class="md:col-span-3">
                        <input type="url" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm" name="query_api" value="<?php echo htmlspecialchars($row['query_api'] ?? '') ?>" placeholder="http://127.0.0.1:8886">
                    </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">
                        <select class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" name="type" onchange="frame_type();">
                        <?php
                            foreach (framelist() as $k => $v) {
                                if($k=='WA')continue;
                                if($row['type'] == $k){
                                    echo '<option selected="selected" value="'.$k.'" is_pwd="'.$v['ispwd'].'">'.($k=='little'?'little/panda':$k).'</option>';
                                }else{
                                    echo '<option value="'.$k.'" is_pwd="'.$v['ispwd'].'">'.($k=='little'?'little/panda':$k).'</option>';
                                }
                            }
                        ?>
                        </select>
                        <p class="mt-1 text-xs text-red-500">注：PC框架就选择PC</p>
                    </div>
                </div>
                <div id="protocol_row" 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">
                        <select class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" name="cid" id="cid" cid_name="<?php echo $row['cid']==''?$row['type']:$row['cid']?>">
                        <option value="-1">请选择登录协议</option>
                        <?php
                            $rs=$DB->query("SELECT cid,count(*) as num FROM pre_protocol GROUP BY cid");
                            while($res = $rs->fetch()){
                                echo '<option value="'.$res['cid'].'">'.$res['cid'].'</option>';
                            }
                        ?>
                        </select>
                        <p class="mt-1 text-xs text-red-500">dulu、xlz 需选择协议，dr 和 myqq 可留空</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">
                        <select class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" name="cids" default="<?php echo $row['cids']?>">
                            <?php
                                $rs=$DB->query("SELECT * FROM pre_login_class");
                                while($res = $rs->fetch()){
                                    echo '<option value="'.$res['id'].'">'.$res['name'].'</option>';
                                }
                            ?>
                        </select>
                    </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">
                        <select class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" name="show_online_census" default="<?php echo $row['show_online_census']?>">
                            <option value="1">开启</option>
                            <option value="0">关闭</option>
                        </select>
                    </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">
                        <select class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" name="api_error_check" default="<?php echo $row['api_error_check']?>">
                            <option value="1">开启</option>
                            <option value="0">关闭</option>
                        </select>
                    </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="max" value="<?php echo $row['max']?>">
                    </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="apipwd" value="<?php echo $row['apipwd']?>" placeholder="填写0是免费、留空是收费">
                        <p class="mt-1 text-xs text-gray-500">如：填写123,用户输入123可免费使用，填写0直接免费</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">
                        <textarea class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" name="msg" rows="3"><?php echo $row['msg']?></textarea>
                    </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">
                        <select class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" name="msg_type" default="<?php echo $row['msg_type']?>">
                            <option value="0">默认_公告栏</option>
                            <option value="1">弹窗提示</option>
                            <option value="5">弹窗提示_5秒后可关闭</option>
                            <option value="10">弹窗提示_10秒后可关闭</option>
                        </select>
                    </div>
                </div>
                <div class="flex items-center gap-4 pt-4">
                    <button type="submit" class="px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition">
                        <i class="fas fa-save mr-2"></i>修改/保存
                    </button>
                    <button type="button" class="px-6 py-2 bg-gray-200 hover:bg-gray-300 text-gray-700 font-medium rounded-lg transition" onclick="if(document.referrer){history.back()}else{location.href='loginapi.php'}">
                        <i class="fas fa-arrow-left mr-2"></i>返回
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
<?php }else{?>

<div class="p-4 md:p-6 space-y-4 md:space-y-6">
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div class="p-3 md:p-4 border-b border-gray-200">
            <button type="button" class="w-full text-left flex items-center justify-between px-3 py-2 md:px-4 md:py-3 bg-gray-50 hover:bg-gray-100 rounded-lg transition" onclick="toggleCollapse('custom_protocol')">
                <span class="text-sm md:text-base font-medium text-gray-800"><i class="fas fa-cog mr-2"></i>点我_自定义协议</span>
                <i class="fas fa-chevron-down transition-transform text-sm md:text-base" id="custom_protocol_icon"></i>
            </button>
        </div>
        <div id="custom_protocol" class="p-3 md:p-6 hidden">
            <div class="bg-red-50 border border-red-200 rounded-lg p-3 md:p-4 mb-4">
                <p class="text-xs md:text-sm text-red-700"><i class="fas fa-info-circle mr-2"></i>示：此区域可以个性化定义接口的登录协议。</p>
            </div>
            <div class="grid grid-cols-1 md:grid-cols-4 gap-3 md:gap-4 items-center mb-4">
                <label class="text-xs md:text-sm font-medium text-gray-700">登录接口</label>
                <div class="md:col-span-3">
                    <select 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" id="login_api_test">
                        <option value="-1">请选择接口</option>
                    <?php
                    $rs = $DB->query("SELECT * FROM pre_loginconfig");
                    while($res = $rs->fetch()){
                        echo '<option value="'.$res['id'].'">'.$res['citybz'].'</option>';
                    }
                    ?>
                    </select>
                </div>
            </div>
            <div class="grid grid-cols-1 md:grid-cols-4 gap-3 md:gap-4 items-center mb-4">
                <label class="text-xs md:text-sm font-medium text-gray-700">协议数量</label>
                <div class="md:col-span-3">
                    <div class="flex flex-nowrap gap-2">
                        <input type="number" class="flex-1 min-w-0 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" id="protocol_num" placeholder="填写框架的协议数量">
                        <button type="button" class="px-2 md:px-3 py-2 bg-blue-50 hover:bg-blue-100 text-blue-600 rounded-lg text-xs md:text-sm whitespace-nowrap" onclick="showAlert('例如【QY框架】的登录协议有20个【协议数量 填写】20即可<hr>可用本工具批量添加测试号来区分协议ID<br><br>测试号格式：123456<font color=\'red\'>0</font>-123456<font color=\'red\'>1</font><br><br>123456为固定值后面的值变量<br>协议ID从0开始，有些框架是从1开始的，以实际的为准');">疑问？</button>
                    </div>
                </div>
            </div>
            <div class="flex flex-wrap items-center gap-2 md:gap-3 mb-4">
                <button type="button" class="px-3 md:px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm" onclick="batch_qq()">
                    <i class="fas fa-plus mr-2"></i>批量添加测试号
                </button>
                <button type="button" class="px-3 md:px-4 py-2 bg-green-600 hover:bg-green-700 text-white font-medium rounded-lg transition text-sm" onclick="showHelpModal()">
                    <i class="fas fa-book mr-2"></i>图文教程
                </button>
            </div>
            <form id="form1" onsubmit="saveSetting(this,'custom_protocol');return false;">
                <div class="overflow-x-auto mb-4">
                    <table class="w-full min-w-[600px] divide-y divide-gray-200 table-responsive">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider whitespace-nowrap">协议ID</th>
                                <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider whitespace-nowrap">协议名称</th>
                                <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider whitespace-nowrap">操作</th>
                            </tr>
                        </thead>
                        <tbody id="tmp_list_protocol" class="bg-white divide-y divide-gray-200">
                            <tr id="row1">
                                <td class="px-4 py-3"><input type="number" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm" name="pid[]" placeholder="填写协议ID"></td>
                                <td class="px-4 py-3"><input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm" name="p_name[]" placeholder="填写协议名称"></td>
                                <td class="px-4 py-3">
                                    <button type="button" class="px-3 py-1 bg-red-500 hover:bg-red-600 text-white text-xs rounded-lg transition" onclick="del_tr('row1')">
                                        <i class="fas fa-trash mr-1"></i>移除
                                    </button>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                <button type="button" class="px-4 py-2 bg-green-600 hover:bg-green-700 text-white font-medium rounded-lg transition mb-4" onclick="add_tr()">
                    <i class="fas fa-plus mr-2"></i>增加一行
                </button>
                <div class="flex flex-nowrap gap-2">
                    <input type="text" class="flex-1 min-w-0 px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" id="protocol_name" name="protocol_name" placeholder="请定义一个协议组名称">
                    <button type="submit" class="px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition whitespace-nowrap">
                        <i class="fas fa-save mr-2"></i>保存
                    </button>
                </div>
            </form>
        </div>
    </div>

    <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div class="p-3 md:p-4 border-b border-gray-200">
            <button type="button" class="w-full text-left flex items-center justify-between px-3 py-2 md:px-4 md:py-3 bg-gray-50 hover:bg-gray-100 rounded-lg transition" onclick="toggleCollapse('protocol')">
                <span class="text-sm md:text-base font-medium text-gray-800"><i class="fas fa-list mr-2"></i>点我_协议列表</span>
                <i class="fas fa-chevron-down transition-transform text-sm md:text-base" id="protocol_icon"></i>
            </button>
        </div>
        <div id="protocol" class="p-3 md:p-6 hidden">
        <?php
            $rs=$DB->query("SELECT cid,count(*) as num FROM pre_protocol GROUP BY cid");
            while($res = $rs->fetch()){
                echo '<div class="mb-4">
                    <button type="button" class="w-full text-left flex items-center justify-between px-3 py-2 md:px-4 md:py-3 bg-gray-50 hover:bg-gray-100 rounded-lg transition" onclick="get_list_protocol(\''.$res['cid'].'\');toggleCollapse(\'protocol_'.$res['cid'].'\')">
                        <span class="text-sm md:text-base font-medium text-gray-800">'.($res['cid']=='little'?'little/panda':$res['cid']).'</span>
                        <i class="fas fa-chevron-down transition-transform text-sm md:text-base" id="protocol_'.$res['cid'].'_icon"></i>
                    </button>
                    <div id="protocol_'.$res['cid'].'" class="hidden mt-4">
                        <form id="form_protocol" onsubmit="saveSetting(this,\'edit_protocol\');return false;">
                            <div class="overflow-x-auto mb-4">
                                <table class="w-full min-w-[900px]">
                                    <thead class="bg-gray-50">
                                        <tr>
                                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap" style="width:60px;">排序</th>
                                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap" style="width:80px;">框架类型</th>
                                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap" style="width:100px;">协议名称</th>
                                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap" style="width:50px;">协议ID</th>
                                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap" style="width:80px;">登录方式</th>
                                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap" style="width:60px;">状态</th>
                                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap" style="width:80px;">操作</th>
                                        </tr>
                                    </thead>
                                    <tbody id="list_protocol'.$res['cid'].'" class="divide-y divide-gray-200">
                                    </tbody>
                                </table>
                            </div>
                            <div class="bg-red-50 border border-red-200 rounded-lg p-3 md:p-4 mb-4">
                                <p class="text-xs md:text-sm text-red-700"><i class="fas fa-exclamation-triangle mr-2"></i>注：不懂什么是【协议ID】千万别乱填写，否则协议会错乱。密码登录：可控制某个协议是否使用密码登录</p>
                            </div>
                            <button type="submit" class="w-full px-4 md:px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm md:text-base">
                                <i class="fas fa-save mr-2"></i>全部保存
                            </button>
                        </form>
                    </div>
                </div>';
            }
        ?>
        </div>
    </div>

    <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div class="p-3 md:p-4 border-b border-gray-200">
            <button type="button" class="w-full text-left flex items-center justify-between px-3 py-2 md:px-4 md:py-3 bg-gray-50 hover:bg-gray-100 rounded-lg transition" onclick="toggleCollapse('flush-collapseOne')">
                <span class="text-sm md:text-base font-medium text-gray-800"><i class="fas fa-cogs mr-2"></i>点我_扩展配置</span>
                <i class="fas fa-chevron-down transition-transform text-sm md:text-base" id="flush-collapseOne_icon"></i>
            </button>
        </div>
        <div id="flush-collapseOne" class="p-3 md:p-6 hidden">
            <form onsubmit="saveSetting(this,'set');return false;" method="post" class="space-y-4 md:space-y-6">
                <div class="grid grid-cols-1 md:grid-cols-4 gap-3 md:gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">登录接口排序</label>
                    <div class="md:col-span-3">
                        <select 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="login_api_sort" default="<?php echo $conf['login_api_sort']?>">
                            <option value="0">默认_按ID排序</option>
                            <option value="1">按在线人数高至低</option>
                            <option value="2">按在线人数低至高</option>
                        </select>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-3 md:gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">接口人数上限后</label>
                    <div class="md:col-span-3">
                        <select 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="login_api_is">
                            <option value="0" <?php echo (string)($conf['login_api_is'] ?? '0') === '0' ? 'selected' : '' ?>>默认_原用户也可以登录</option>
                            <option value="1" <?php echo (string)($conf['login_api_is'] ?? '0') === '1' ? 'selected' : '' ?>>新老用户都不能使用</option>
                        </select>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-3 md:gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">新增QQ绑定默认接口</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" value="<?php echo $conf['defaultApiId']?>" name="defaultApiId" placeholder="留空不绑定，填写接口ID，如:88">
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-3 md:gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">跳转QQ快捷登录</label>
                    <div class="md:col-span-3">
                        <select 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="open_qq_quick" default="<?php echo $conf['open_qq_quick']?>">
                            <option value="0">默认_不显示</option>
                            <option value="1">显示</option>
                        </select>
                        <p class="mt-1 text-xs text-red-500">现在QQ快捷登录失效了，好像TIM还可以跳转</p>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-3 md:gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">登录框提示语</label>
                    <div class="md:col-span-3">
                        <textarea 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="login_tip" rows="3" placeholder="<font color=&quot;#b2262d&quot;>注意:请使用Tim登录QQ后截图扫码方式进行登录<br>或者摄像头扫码方式登录<br>Ps:Tim(办公QQ)各大应用中心都可下载</font>"><?php echo $conf['login_tip']?></textarea>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-3 md:gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">节点异常提示语</label>
                    <div class="md:col-span-3">
                        <textarea 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="api_error_tip" rows="3" placeholder="本节点可能存在异常，请选择其他节点"><?php echo $conf['api_error_tip']?></textarea>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-3 md:gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">“请选择分类/节点”提示开关</label>
                    <div class="md:col-span-3">
                        <select 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="login_select_str">
                            <option value="1" <?php echo (string)($conf['login_select_str'] ?? '1') === '1' ? 'selected' : '' ?>>开启（显示“请选择分类/节点”）</option>
                            <option value="0" <?php echo (string)($conf['login_select_str'] ?? '1') === '0' ? 'selected' : '' ?>>关闭（不显示提示项）</option>
                        </select>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-3 md:gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">WiFi 网络提示弹窗</label>
                    <div class="md:col-span-3">
                        <?php $login_wifi_hint_v = (string)($conf['login_wifi_hint'] ?? '1'); ?>
                        <select 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="login_wifi_hint">
                            <option value="5" <?php echo $login_wifi_hint_v === '5' ? 'selected' : '' ?>>开启（倒计时5秒）</option>
                            <option value="15" <?php echo $login_wifi_hint_v === '15' ? 'selected' : '' ?>>开启（倒计时15秒）</option>
                            <option value="1" <?php echo $login_wifi_hint_v === '1' ? 'selected' : '' ?>>开启</option>
                            <option value="0" <?php echo $login_wifi_hint_v === '0' ? 'selected' : '' ?>>关闭</option>
                        </select>
                        <p class="mt-1 text-xs text-gray-500">依赖浏览器 Network Information（多数安卓 Chrome/WebView 可识别 WiFi；iOS 等可能无法检测则不弹窗）</p>
                    </div>
                </div>

                <div class="flex items-center gap-4 pt-4">
                    <button type="submit" class="px-4 md:px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm md:text-base">
                        <i class="fas fa-save mr-2"></i>保存
                    </button>
                </div>
            </form>
        </div>
    </div>

    <div class="bg-white rounded-xl shadow-sm border border-gray-200">
        <div class="p-6 border-b border-gray-200">
            <h2 class="text-lg font-semibold text-gray-800">登录接口配置</h2>
        </div>
        <div class="p-6">

            <div class="mb-4">
                <a href="?my=add" class="px-6 py-2 bg-green-600 hover:bg-green-700 text-white font-medium rounded-lg transition inline-block">
                    <i class="fas fa-plus mr-2"></i>添加接口
                </a>
            </div>
            <div class="mb-4 flex items-center gap-2">
                <span class="text-xs text-gray-500">业务</span>
                <select class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 focus:border-transparent" onchange="location.href='loginapi.php?biz='+this.value">
                    <option value="all" <?php echo $biz === 'all' ? 'selected' : '' ?>>全部</option>
                    <option value="cloud" <?php echo $biz === 'cloud' ? 'selected' : '' ?>>云端</option>
                    <option value="dg" <?php echo $biz === 'dg' ? 'selected' : '' ?>>代挂</option>
                </select>
            </div>
            <input type="hidden" id="isgetdata" value="1">
            <form name="form_data" id="form_data">
                <div class="overflow-x-auto">
                    <table class="w-full min-w-[1200px]">
                        <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">框架类型</th>
                                <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">登录API</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="list" class="divide-y divide-gray-200">
                        </tbody>
                    </table>
                </div>
            </form>
            <div class="mt-4 flex items-center justify-between">
                <p class="text-sm text-gray-600">共有 <span id="total" class="font-medium">0</span> 条/现第：
                    <select class="px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" id="page">
                        <option value="1">1</option>
                    </select>
                    页
                </p>
                <button type="button" class="px-6 py-2 bg-gray-200 hover:bg-gray-300 text-gray-700 font-medium rounded-lg transition" onclick="if(document.referrer){history.back()}else{location.href='loginapi.php'}">
                    <i class="fas fa-arrow-left mr-2"></i>返回
                </button>
            </div>
        </div>
    </div>
</div>

<div id="protocol_sort" class="fixed inset-0 bg-black/50 hidden items-center justify-center z-50 p-4">
    <div class="bg-white rounded-xl shadow-2xl w-full max-w-lg max-h-[90vh] overflow-y-auto">
        <div class="p-4 border-b border-gray-200 flex items-center justify-between sticky top-0 bg-white">
            <h3 class="text-base md:text-lg font-semibold">操作排序</h3>
            <button type="button" class="text-gray-400 hover:text-gray-600" onclick="document.getElementById('protocol_sort').classList.add('hidden');document.getElementById('protocol_sort').classList.remove('flex');">
                <i class="fas fa-times text-xl"></i>
            </button>
        </div>
        <div class="p-4 md:p-6">
            <form id="protocol_con"></form>
        </div>
        <div class="p-4 border-t border-gray-200 flex justify-end gap-3 sticky bottom-0 bg-white">
            <button type="button" class="px-4 py-2 bg-gray-100 hover:bg-gray-200 text-gray-700 font-medium rounded-lg transition" onclick="document.getElementById('protocol_sort').classList.add('hidden');document.getElementById('protocol_sort').classList.remove('flex');">关闭</button>
            <button type="button" class="px-4 py-2 bg-green-600 hover:bg-green-700 text-white font-medium rounded-lg transition" onclick="edit_protocol_sort()">确定</button>
        </div>
    </div>
</div>

<div id="p_help" class="fixed inset-0 bg-black/50 hidden items-center justify-center z-50 p-4">
    <div class="bg-white rounded-xl shadow-2xl w-full max-w-2xl max-h-[90vh] overflow-y-auto">
        <div class="p-4 border-b border-gray-200 flex items-center justify-between sticky top-0 bg-white">
            <h3 class="text-base md:text-lg font-semibold">帮助</h3>
            <button type="button" class="text-gray-400 hover:text-gray-600" onclick="document.getElementById('p_help').classList.add('hidden');document.getElementById('p_help').classList.remove('flex');">
                <i class="fas fa-times text-xl"></i>
            </button>
        </div>
        <div class="p-4 md:p-6">
            <p class="text-sm text-gray-700 mb-4">用以下方法来区分框架协议ID(从0开始)<br>例如QY框架一共有20个协议。<br>随便往框架里面添加20个QQ如<br>1234560----(选择第一个协议)<br>1234561----(选择第二个协议)<br>1234562----(选择第三个协议)<br>以此类推</p>
            <hr class="my-4">
            <img src="assets/img/p_help.png" class="w-full rounded-lg" alt="帮助图片">
        </div>
        <div class="p-4 border-t border-gray-200 flex justify-end sticky bottom-0 bg-white">
            <button type="button" class="px-4 py-2 bg-gray-100 hover:bg-gray-200 text-gray-700 font-medium rounded-lg transition" onclick="document.getElementById('p_help').classList.add('hidden');document.getElementById('p_help').classList.remove('flex');">关闭</button>
        </div>
    </div>
</div>

<script>
function showHelpModal() {
    document.getElementById('p_help').classList.remove('hidden');
    document.getElementById('p_help').classList.add('flex');
}
function toggleCollapse(id) {
    const element = document.getElementById(id);
    const icon = document.getElementById(id + '_icon');
    if (element) {
        if (element.classList.contains('hidden')) {
            element.classList.remove('hidden');
            if (icon) {
                icon.classList.add('rotate-180');
            }
        } else {
            element.classList.add('hidden');
            if (icon) {
                icon.classList.remove('rotate-180');
            }
        }
    }
}
</script>
<?php }?>
</div>
</main>

<script src="../assets/js/jquery.min.js"></script>
<script src="../assets/js/bootstrap.min.js"></script>
<script src="../assets/js/jquery.cookie.min.js"></script>
<script src="./assets/js/loginapi.js?ver=<?php echo VERSION ?>"></script>
<script>
    // 侧边栏切换
    document.getElementById('sidebarToggle')?.addEventListener('click', function() {
        document.getElementById('sidebar').classList.toggle('open');
    });

    // 点击外部关闭侧边栏（移动端）
    document.addEventListener('click', function(e) {
        const sidebar = document.getElementById('sidebar');
        const toggle = document.getElementById('sidebarToggle');
        if (window.innerWidth < 1024 && !sidebar.contains(e.target) && !toggle.contains(e.target)) {
            sidebar.classList.remove('open');
        }
    });

    // 通用提示函数
    function showMsg(msg, icon) {
        if (icon == 2) {
            showToast(msg, 'error');
        } else if (icon == 3) {
            showToast(msg, 'warning');
        } else {
            showToast(msg, 'success');
        }
    }

    // AJAX请求封装
    function ajaxPost(url, data, callback) {
        $.ajax({
            url: url,
            type: 'POST',
            data: data,
            dataType: 'json',
            success: function(res) {
                if (callback) callback(res);
            },
            error: function() {
                showMsg('请求失败', 2);
            }
        });
    }
</script>

<!-- 提示弹窗 -->
<div class="modal fade" id="alert_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" id="alert_content">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-bs-dismiss="modal">知道了</button>
            </div>
        </div>
    </div>
</div>

<!-- 删除框架QQ确认弹窗 -->
<div class="modal fade" id="del_frame_qq_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">删除框架QQ</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="mb-4 p-4 bg-red-50 border border-red-200 rounded-lg">
                    <p class="text-sm text-red-600 font-medium mb-2"><i class="fas fa-exclamation-triangle mr-2"></i>警告</p>
                    <p class="text-sm text-gray-700">删除框架与接口不匹配的QQ</p>
                    <p class="text-sm text-gray-700 mt-2">数据无价，慎重操作</p>
                </div>
                <div class="mb-3">
                    <label class="block text-sm font-medium text-gray-700 mb-2">请输入"确认删除"以继续</label>
                    <input type="text" id="del_frame_qq_confirm" 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 class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-danger" onclick="confirm_del_frame_qq()">确定删除</button>
            </div>
        </div>
    </div>
</div>

<!-- 删除接口确认弹窗 -->
<div class="modal fade" id="del_loginapi_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-4 p-4 bg-red-50 border border-red-200 rounded-lg">
                    <p class="text-sm text-red-600 font-medium mb-2"><i class="fas fa-exclamation-triangle mr-2"></i>警告</p>
                    <p class="text-sm text-gray-700">确定要删除该接口吗？</p>
                    <p class="text-sm text-gray-700 mt-2">删除后将无法恢复</p>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-danger" onclick="confirm_del_loginapi()">确定删除</button>
            </div>
        </div>
    </div>
</div>

<script>
	var protocol_list = $.parseJSON('<?php echo json_encode($list)?>');
	
	function showAlert(content) {
		$('#alert_content').html(content);
		$('#alert_modal').modal('show');
	}

	function updateQueryApiRow() {
		var bizVal = String($('form#add select[name="biz"]').val() || 'cloud');
		if (bizVal === 'dg') {
			$('.query_api_row').show();
		} else {
			$('.query_api_row').hide();
		}
	}

	$(document).on('change', 'form#add select[name="biz"]', updateQueryApiRow);
	$(updateQueryApiRow);
</script>
</body>
</html>
