<?php
@header('Content-Type: text/html; charset=UTF-8');
include_once dirname(__DIR__) . '/includes/common.php';
if ($islogin2 != 1) {
    exit("<script>window.location.href='./login.php';</script>");
}

$QZONE_ENABLED = (($conf['qzone_open'] ?? ($conf['qzone_enabled'] ?? '0')) == '1');
$CLOUD_ENABLED = (($conf['cloud_enabled'] ?? '1') == '1');
$DG_ENABLED = (($conf['dg_open'] ?? '0') == '1');

if (!$CLOUD_ENABLED && !$DG_ENABLED) {
    exit("<script>alert('系统未开启');window.location.href='./index.php';</script>");
}

$reqBiz = (isset($_GET['biz']) && $_GET['biz'] === 'dg') ? 'dg' : 'cloud';
if ($reqBiz === 'dg' && !$DG_ENABLED) $reqBiz = $CLOUD_ENABLED ? 'cloud' : 'dg';
if ($reqBiz === 'cloud' && !$CLOUD_ENABLED) $reqBiz = $DG_ENABLED ? 'dg' : 'cloud';

$biz = $reqBiz;
$title = $biz === 'dg' ? '添加代挂QQ' : '添加QQ';
$prefill_qq = isset($_GET['qq']) ? preg_replace('/\D/', '', (string)$_GET['qq']) : '';
include __DIR__ . '/head.php';
?>
<style>
.content { display: none; }
.content.active { display: block; }
</style>

<canvas id="qrcanvas" style="display:none;"></canvas>

<?php
$loginmsg = html_entity_decode($conf['loginmsg'] ?? '');
$hasLoginMsg = !empty(trim($loginmsg));
if($hasLoginMsg && intval($conf['loginmsg_place']) === 0):
?>
<div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden mb-4">
    <div class="p-4">
        <div class="prose prose-sm max-w-none text-gray-700"><?php echo $loginmsg; ?></div>
    </div>
</div>
<?php endif; ?>

<div id="apimsg" class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden mb-4 hidden">
    <div class="p-4">
        <div class="prose prose-sm max-w-none text-gray-700"></div>
    </div>
</div>

<div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden mb-4">
    <div class="px-4 py-3 bg-gray-50 border-b border-gray-100">
        <h3 class="font-medium text-gray-800 flex items-center">
            <i class="fas fa-sign-in-alt text-indigo-500 mr-1.5"></i><?php echo $biz === 'dg' ? '登录代挂QQ' : '登录QQ' ?>
        </h3>
    </div>
    <div class="p-4">
        <form id="form" class="space-y-4">
            <?php
            $version_name_cloud = $conf['version_name_cloud'] ?? '云端版';
            $version_name_dg = $conf['version_name_dg'] ?? '代挂版';
            ?>
            <?php if($CLOUD_ENABLED && $DG_ENABLED): ?>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">版本</label>
                <select class="w-full px-3 py-2 bg-gray-50 border border-gray-200 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 text-sm" name="biz" id="biz" onchange="location.href='add.php?biz='+this.value">
                    <option value="cloud" <?php echo $biz === 'cloud' ? 'selected' : '' ?>><?php echo htmlspecialchars($version_name_cloud); ?></option>
                    <option value="dg" <?php echo $biz === 'dg' ? 'selected' : '' ?>><?php echo htmlspecialchars($version_name_dg); ?></option>
                </select>
            </div>
            <?php else: ?>
            <input type="hidden" name="biz" id="biz" value="<?php echo $biz; ?>">
            <?php endif; ?>
            <div id="cid-container">
                <label class="block text-sm font-medium text-gray-700 mb-1">分类节点</label>
                <select class="w-full px-3 py-2 bg-gray-50 border border-gray-200 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 text-sm" id="cid" name="cid"></select>
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">登录节点</label>
                <select class="w-full px-3 py-2 bg-gray-50 border border-gray-200 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 text-sm" name="apiid" id="apiid"></select>
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">QQ账号</label>
                <input type="number" class="w-full px-3 py-2 bg-gray-50 border border-gray-200 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 text-sm" name="qq" value="<?php echo htmlspecialchars($prefill_qq); ?>" placeholder="输入QQ号">
            </div>
            <div id="_pwd">
                <label class="block text-sm font-medium text-gray-700 mb-1">QQ密码</label>
                <div class="flex">
                    <input type="text" class="flex-1 px-3 py-2 bg-gray-50 border border-gray-200 rounded-l-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 text-sm" name="pwd" placeholder="扫码登录可留空，不填写">
                    <?php if(isset($_GET['qq'])){echo '<button type="button" class="px-3 py-2 bg-indigo-500 hover:bg-indigo-600 text-white text-sm font-medium rounded-r-lg transition-colors whitespace-nowrap" id="btn_loading_pwd" onclick="loading_pwd()">加载密码</button>';}else{echo '<span class="hidden"></span>';}?>
                </div>
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">登录协议</label>
                <select class="w-full px-3 py-2 bg-gray-50 border border-gray-200 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 text-sm" name="protocol"></select>
                <p class="text-sm text-amber-600 mt-1.5 font-medium flex items-center gap-1" id="deviceTip"></p>
            </div>
            <div id="_apipwd" style="display: none;">
                <label class="block text-sm font-medium text-gray-700 mb-1">接口密码</label>
                <input type="text" class="w-full px-3 py-2 bg-gray-50 border border-gray-200 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 text-sm" name="apipwd" placeholder="请输入接口密码,可免费">
            </div>
            <div class="flex gap-3">
                <button type="button" class="flex-1 py-2.5 bg-indigo-600 hover:bg-indigo-700 text-white rounded-lg font-medium transition-colors text-sm" id="qr" onclick="subbot('qr')">
                    <i class="fas fa-qrcode mr-1"></i>扫码登录
                </button>
                <button type="button" class="flex-1 py-2.5 bg-green-600 hover:bg-green-700 text-white rounded-lg font-medium transition-colors text-sm" id="pwd" onclick="subbot('pwd')">
                    <i class="fas fa-key mr-1"></i>密码登录
                </button>
            </div>
            <p class="text-red-500 text-sm" id="free"></p>
        </form>
    </div>
</div>

<?php if($hasLoginMsg && intval($conf['loginmsg_place']) === 1): ?>
<div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden mb-4">
    <div class="p-4">
        <div class="prose prose-sm max-w-none text-gray-700"><?php echo $loginmsg; ?></div>
    </div>
</div>
<?php endif; ?>

</div>
</main>

<div id="showQr" class="fixed inset-0 z-50 hidden">
    <div class="fixed inset-0 bg-black/50 backdrop-blur-sm" onclick="closeQrModal()"></div>
    <div class="fixed inset-0 flex items-center justify-center p-4">
        <div class="bg-white rounded-2xl shadow-2xl w-full max-w-sm">
            <div class="flex items-center justify-between px-5 py-4 border-b border-gray-100 bg-gray-50/50 rounded-t-2xl">
                <h3 class="text-lg font-semibold text-gray-800">请扫描二维码</h3>
                <button onclick="closeQrModal()" class="w-8 h-8 flex items-center justify-center rounded-full hover:bg-gray-200 text-gray-400 hover:text-gray-600 transition-colors"><i class="fas fa-times"></i></button>
            </div>
            <div class="p-5 text-center">
                <p class="text-sm text-gray-500 mb-3">扫描二维码，期间请不要关闭本页面！</p>
                <div class="bg-indigo-50 text-indigo-700 rounded-lg px-4 py-2 text-sm font-semibold mb-3">
                    <span id="login_tip"></span><span id="qrloginmsg"></span>
                </div>
                <div id="content" class="flex justify-center mb-3"></div>
                <button type="button" onclick="mloginurlnew()" class="w-full py-2.5 bg-teal-500 hover:bg-teal-600 text-white rounded-lg font-medium transition-colors hidden" id="open_qq_quick">
                    <i class="fas fa-external-link-alt mr-1"></i>跳转QQ快捷登录
                </button>
            </div>
            <div class="px-5 py-3 border-t border-gray-100">
                <button onclick="closeQrModal()" class="w-full py-2 bg-gray-100 hover:bg-gray-200 text-gray-600 rounded-lg text-sm transition-colors">关闭</button>
            </div>
        </div>
    </div>
</div>

<div id="showPwd" class="fixed inset-0 z-50 hidden">
    <div class="fixed inset-0 bg-black/50 backdrop-blur-sm" onclick="closePwdModal()"></div>
    <div class="fixed inset-0 flex items-center justify-center p-4">
        <div class="bg-white rounded-2xl shadow-2xl w-full max-w-md max-h-[85vh] flex flex-col">
            <div class="flex items-center justify-between px-5 py-4 border-b border-gray-100 bg-gray-50/50 rounded-t-2xl">
                <h3 id="showPwdTitle" class="text-lg font-semibold text-gray-800">密码登录</h3>
                <button onclick="closePwdModal()" class="w-8 h-8 flex items-center justify-center rounded-full hover:bg-gray-200 text-gray-400 hover:text-gray-600 transition-colors"><i class="fas fa-times"></i></button>
            </div>
            <div id="content_pwd" class="p-5 overflow-y-auto text-center"></div>
            <div class="px-5 py-3 border-t border-gray-100">
                <button onclick="closePwdModal()" class="w-full py-2 bg-gray-100 hover:bg-gray-200 text-gray-600 rounded-lg text-sm transition-colors">关闭</button>
            </div>
        </div>
    </div>
</div>

<div id="appModal" class="fixed inset-0 z-50 hidden">
    <div class="fixed inset-0 bg-black/50 backdrop-blur-sm" onclick="closeModal()"></div>
    <div class="fixed inset-0 flex items-center justify-center p-4">
        <div class="bg-white rounded-2xl shadow-2xl w-full max-w-md max-h-[85vh] flex flex-col">
            <div class="flex items-center justify-between px-5 py-4 border-b border-gray-100 bg-gray-50/50 rounded-t-2xl">
                <h3 id="modalTitle" 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-200 text-gray-400 hover:text-gray-600 transition-colors"><i class="fas fa-times"></i></button>
            </div>
            <div id="modalBody" class="p-5 overflow-y-auto text-center"></div>
            <div id="modalFooter" class="px-5 py-3 border-t border-gray-100 hidden"></div>
        </div>
    </div>
</div>

<script>
window.ADD_PAGE_PARAMS = <?php echo json_encode([
	'qq' => $_GET['qq'] ?? '',
	'apiid' => $_GET['apiid'] ?? '',
	'protocol' => $_GET['protocol'] ?? '',
	'biz' => $biz,
	'loginWifiHint' => (string)($conf['login_wifi_hint'] ?? '1'),
], JSON_UNESCAPED_UNICODE); ?>;
</script>
<script src="../assets/js/bootstrap.min.js"></script>
<script src="../assets/js/jsQR.js"></script>
<script src="./assets/js/add.js?ver=<?php echo VERSION ?>"></script>
</body>
</html>
