<?php
ob_start();
require_once '../../config.php';

$result = null;
$domain = isset($_GET['domain']) ? trim($_GET['domain']) : '';

if (!empty($domain)) {
    // 域名格式验证
    $domain_pattern = '/^[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/';
    if (preg_match($domain_pattern, $domain)) {
        $domain = $db->escape($domain);
        
        // 查询该域名是否有有效的授权码
        $license = $db->fetchOne("
            SELECT l.*, a.name as app_name 
            FROM licenses l 
            JOIN apps a ON l.app_id = a.id 
            WHERE l.domain = '$domain' 
            AND l.status = 1 
            AND (l.expire_date IS NULL OR l.expire_date >= CURDATE())
            LIMIT 1
        ");
        
        if ($license) {
            $result = [
                'authorized' => true,
                'message' => '授权有效'
            ];
        } else {
            $result = [
                'authorized' => false,
                'message' => '未授权'
            ];
        }
    } else {
        $result = [
            'authorized' => false,
            'message' => '域名格式不正确'
        ];
    }
}

$site_name = getSiteConfig('site_name') ?? '麟泓授权系统';
?>
<?php include 'header.php'; ?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>授权查询 - <?php echo htmlspecialchars($site_name); ?></title>
    <link rel="stylesheet" href="/assets/css/style.css">
    <link rel="stylesheet" href="/assets/css/dark.css" id="dark-theme" disabled>
    <script src="/assets/js/main.js" defer></script>
    <style>
        /* 授权查询页面专用样式 */
        .query-hero {
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-hover) 100%);
            padding: 60px 20px;
            text-align: center;
            color: white;
        }
        .query-hero h1 {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 12px;
            font-size: 42px;
            margin-bottom: 15px;
            font-weight: 700;
        }
        .query-hero p {
            font-size: 18px;
            opacity: 0.9;
            max-width: 600px;
            margin: 0 auto;
        }
        .query-card-modern {
            max-width: 700px;
            margin: -40px auto 40px;
            background: var(--bg-primary);
            border-radius: 24px;
            padding: 40px;
            box-shadow: 0 20px 35px -10px rgba(0, 0, 0, 0.1);
            border: 1px solid var(--border-color);
        }
        
        /* 公告区域 */
        .announcement-section {
            margin-bottom: 28px;
        }
        .announcement-header {
            display: flex;
            align-items: center;
            gap: 8px;
            font-size: 14px;
            font-weight: 600;
            color: var(--text-primary);
            margin-bottom: 10px;
        }
        .announce-list {
            background: var(--bg-secondary);
            border-radius: 14px;
            border: 1px solid var(--border-color);
            overflow: hidden;
        }
        .announce-item {
            display: flex;
            align-items: flex-start;
            gap: 12px;
            padding: 14px 16px;
            border-bottom: 1px solid var(--border-color);
        }
        .announce-item:last-child {
            border-bottom: none;
        }
        .announce-icon {
            width: 24px;
            height: 24px;
            border-radius: 8px;
            background: #fef3c7;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-shrink: 0;
            margin-top: 2px;
        }
        .announce-content {
            flex: 1;
        }
        .announce-text {
            font-size: 13px;
            color: var(--text-secondary);
            line-height: 1.6;
        }
        .announce-text strong {
            color: var(--primary-color);
        }
        
        .query-form-modern {
            display: flex;
            gap: 12px;
            margin-bottom: 30px;
        }
        .input-wrapper {
            flex: 1;
            position: relative;
        }
        .input-icon {
            position: absolute;
            left: 18px;
            top: 50%;
            transform: translateY(-50%);
        }
        .query-form-modern input {
            width: 100%;
            padding: 16px 20px 16px 50px;
            border: 2px solid var(--border-color);
            border-radius: 50px;
            font-size: 16px;
            background: var(--bg-primary);
            transition: all 0.3s;
            box-sizing: border-box;
        }
        .query-form-modern input:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }
        .query-form-modern button {
            padding: 16px 32px;
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-hover) 100%);
            color: white;
            border: none;
            border-radius: 50px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .query-form-modern button:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.3);
        }
        .result-card {
            margin-top: 20px;
            padding: 30px 25px;
            border-radius: 20px;
            text-align: center;
            animation: fadeIn 0.5s ease;
        }
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .result-card.valid {
            background: linear-gradient(135deg, #d1fae5 0%, #a7f3d0 100%);
            border: 1px solid #10b981;
        }
        .result-card.invalid {
            background: linear-gradient(135deg, #fee2e2 0%, #fecaca 100%);
            border: 1px solid #ef4444;
        }
        .result-icon-large {
            width: 80px;
            height: 80px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 20px;
        }
        .result-card.valid .result-icon-large {
            background: #10b981;
            box-shadow: 0 5px 15px rgba(16, 185, 129, 0.3);
        }
        .result-card.invalid .result-icon-large {
            background: #ef4444;
            box-shadow: 0 5px 15px rgba(239, 68, 68, 0.3);
        }
        .result-icon-large img {
            width: 44px;
            height: 44px;
            filter: brightness(0) invert(1);
        }
        .result-card h3 {
            font-size: 28px;
            margin-bottom: 10px;
        }
        .result-card.valid h3 {
            color: #065f46;
        }
        .result-card.invalid h3 {
            color: #991b1b;
        }
        .result-card p {
            font-size: 16px;
            margin-bottom: 20px;
        }
        .result-card.valid p {
            color: #065f46;
        }
        .result-card.invalid p {
            color: #991b1b;
        }
        .action-buttons {
            display: flex;
            gap: 15px;
            justify-content: center;
            margin-top: 20px;
        }
        .btn-outline-light {
            padding: 10px 24px;
            border-radius: 50px;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s;
            display: inline-flex;
            align-items: center;
            gap: 6px;
        }
        .result-card.valid .btn-outline-light {
            background: transparent;
            border: 2px solid #065f46;
            color: #065f46;
        }
        .result-card.valid .btn-outline-light:hover {
            background: #065f46;
            color: white;
        }
        .result-card.valid .btn-outline-light:hover img {
            filter: brightness(0) invert(1);
        }
        .result-card.invalid .btn-outline-light {
            background: transparent;
            border: 2px solid #991b1b;
            color: #991b1b;
        }
        .result-card.invalid .btn-outline-light:hover {
            background: #991b1b;
            color: white;
        }
        .result-card.invalid .btn-outline-light:hover img {
            filter: brightness(0) invert(1);
        }
        .features-list {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-top: 50px;
            padding: 0 20px;
        }
        .feature-item {
            text-align: center;
            padding: 20px;
        }
        .feature-icon {
            width: 52px;
            height: 52px;
            margin: 0 auto 12px;
            background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(102, 126, 234, 0.05) 100%);
            border-radius: 14px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .feature-item h4 {
            font-size: 16px;
            margin-bottom: 8px;
            color: var(--text-primary);
        }
        .feature-item p {
            font-size: 13px;
            color: var(--text-secondary);
        }
        .query-note-modern {
            text-align: center;
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid var(--border-color);
            font-size: 13px;
            color: var(--text-muted);
        }
        .query-note-modern p {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 6px;
            margin: 8px 0;
        }
        @media (max-width: 768px) {
            .query-hero h1 {
                font-size: 28px;
            }
            .query-card-modern {
                padding: 24px;
                margin: -20px 20px 40px;
            }
            .query-form-modern {
                flex-direction: column;
            }
            .query-form-modern button {
                width: 100%;
                justify-content: center;
            }
            .result-icon-large {
                width: 60px;
                height: 60px;
            }
            .result-icon-large img {
                width: 32px;
                height: 32px;
            }
            .result-card h3 {
                font-size: 22px;
            }
            .action-buttons {
                flex-direction: column;
            }
            .btn-outline-light {
                justify-content: center;
            }
        }
    </style>
</head>
<body>
    <div class="front-container">
        
        <main class="front-main">
            <!-- Hero 区域 -->
            <div class="query-hero">
                <h1>
                    <img src="https://api.iconify.design/mdi:shield-check.svg?color=white" width="42" height="42" alt="">
                    系统授权查询
                </h1>
                <p>请输入域名查询授权状态，验证软件正版性</p>
            </div>
            
            <!-- 查询卡片 -->
            <div class="query-card-modern">
                <!-- 硬编码公告区域 -->
                <div class="announcement-section">
                    <div class="announcement-header">
                        <img src="https://api.iconify.design/mdi:bullhorn.svg?color=%23f59e0b" width="18" height="18" alt="">
                        重要公告
                    </div>
                    <div class="announce-list">
                        <div class="announce-item">
                            <div class="announce-icon">
                                <img src="https://api.iconify.design/mdi:information.svg?color=%23f59e0b" width="16" height="16" alt="">
                            </div>
                            <div class="announce-content">
                                <div class="announce-text">
                                    <strong>温馨提示：</strong>授权查询仅显示域名是否已授权，不显示任何详细信息。
                                </div>
                            </div>
                        </div>
                        <div class="announce-item">
                            <div class="announce-icon">
                                <img src="https://api.iconify.design/mdi:web.svg?color=%23f59e0b" width="16" height="16" alt="">
                            </div>
                            <div class="announce-content">
                                <div class="announce-text">
                                    <strong>示例域名：</strong>cs.demo.lhwks.cn(可复制本站域名测试)
                                </div>
                            </div>
                        </div>
                        <div class="announce-item">
                            <div class="announce-icon">
                                <img src="https://api.iconify.design/mdi:clock-outline.svg?color=%23f59e0b" width="16" height="16" alt="">
                            </div>
                            <div class="announce-content">
                                <div class="announce-text">
                                    <strong>服务时间：</strong>周一至周五 9:00-18:00，如有疑问请联系客服。
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <form method="GET" class="query-form-modern">
                    <div class="input-wrapper">
                        <span class="input-icon">
                            <img src="https://api.iconify.design/mdi:web.svg?color=%239ca3af" width="20" height="20" alt="">
                        </span>
                        <input type="text" name="domain" placeholder="请输入域名，例如：example.com" value="<?php echo htmlspecialchars($domain); ?>" required>
                    </div>
                    <button type="submit">
                        <img src="https://api.iconify.design/mdi:magnify.svg?color=white" width="20" height="20" alt="">
                        查询
                    </button>
                </form>
                
                <?php if ($result !== null): ?>
                    <div class="result-card <?php echo $result['authorized'] ? 'valid' : 'invalid'; ?>">
                        <div class="result-icon-large">
                            <?php if ($result['authorized']): ?>
                                <img src="https://api.iconify.design/mdi:check.svg?color=white" alt="">
                            <?php else: ?>
                                <img src="https://api.iconify.design/mdi:close.svg?color=white" alt="">
                            <?php endif; ?>
                        </div>
                        <h3><?php echo $result['message']; ?></h3>
                        <p><?php echo $result['authorized'] ? '该域名已获得正版授权' : '该域名未获得授权，请联系开发者购买'; ?></p>
                        <?php if (!$result['authorized']): ?>
                            <div class="action-buttons">
                                <a href="/modules/home/buy.php" class="btn-outline-light">
                                    <img src="https://api.iconify.design/mdi:cart.svg?color=currentColor" width="18" height="18" alt="">
                                    立即购买授权
                                </a>
                                <a href="/modules/home/contact.php" class="btn-outline-light">
                                    <img src="https://api.iconify.design/mdi:headset.svg?color=currentColor" width="18" height="18" alt="">
                                    联系客服
                                </a>
                            </div>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>
                
                <div class="features-list">
                    <div class="feature-item">
                        <div class="feature-icon">
                            <img src="https://api.iconify.design/mdi:shield-check.svg?color=%23667eea" width="28" height="28" alt="">
                        </div>
                        <h4>使用正版软件</h4>
                        <p>保护您的合法权益</p>
                    </div>
                    <div class="feature-item">
                        <div class="feature-icon">
                            <img src="https://api.iconify.design/mdi:message-text.svg?color=%23667eea" width="28" height="28" alt="">
                        </div>
                        <h4>QQ在线客服</h4>
                        <p>官网技术团队支持</p>
                    </div>
                    <div class="feature-item">
                        <div class="feature-icon">
                            <img src="https://api.iconify.design/mdi:gavel.svg?color=%23667eea" width="28" height="28" alt="">
                        </div>
                        <h4>商业运营合法权利</h4>
                        <p>保障您的商业利益</p>
                    </div>
                    <div class="feature-item">
                        <div class="feature-icon">
                            <img src="https://api.iconify.design/mdi:shield-lock.svg?color=%23667eea" width="28" height="28" alt="">
                        </div>
                        <h4>专注系统安全性优化</h4>
                        <p>提供专业技术支持</p>
                    </div>
                    <div class="feature-item">
                        <div class="feature-icon">
                            <img src="https://api.iconify.design/mdi:refresh.svg?color=%23667eea" width="28" height="28" alt="">
                        </div>
                        <h4>享有免费升级服务</h4>
                        <p>持续更新迭代</p>
                    </div>
                    <div class="feature-item">
                        <div class="feature-icon">
                            <img src="https://api.iconify.design/mdi:clock-outline.svg?color=%23667eea" width="28" height="28" alt="">
                        </div>
                        <h4>7×24小时技术支持</h4>
                        <p>随时为您服务</p>
                    </div>
                </div>
            </div>
        </main>
        
        <?php include 'footer.php'; ?>
    </div>
</body>
</html>