<?php
if (!defined('ROOT_PATH')) {
    define('ROOT_PATH', dirname(__DIR__, 2) . '/');
}

require_once ROOT_PATH . 'core/lib/license.php';
require_once ROOT_PATH . 'core/lib/module_loader.php';

$licenseObj = new \Core\Lib\License();
$premiumFile = ROOT_PATH . 'modules-premium/pro/payment/index.php';

function ytPaymentReadonlyValue($value)
{
    return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
}

function ytRenderPaymentReadonly($db)
{
    $allConfigs = $db->fetchAll("SELECT * FROM {$db->table('config')} WHERE config_group = 'payment' ORDER BY id ASC");
    $configData = [];
    foreach ($allConfigs as $cfg) {
        $configData[$cfg['config_key']] = $cfg['config_value'];
    }

    $alipayEnabled = isset($configData['alipay_enabled']) ? $configData['alipay_enabled'] : '0';
    ?>
    <div class="page-container fade-in">
        <div style="margin-bottom:20px;padding:18px 20px;border:1px solid #fdba74;background:linear-gradient(135deg,#fff7ed 0%,#ffffff 100%);border-radius:14px;display:flex;align-items:center;justify-content:space-between;gap:16px;flex-wrap:wrap;">
            <div>
                <div style="font-size:16px;font-weight:700;color:#9a3412;">支付设置当前为只读模式</div>
                <div style="margin-top:6px;font-size:13px;color:#7c2d12;line-height:1.8;">免费版可查看当前支付参数，启用、修改、保存支付配置需升级专业版授权。</div>
            </div>
            <a href="?act=auth" style="display:inline-flex;align-items:center;justify-content:center;padding:11px 18px;border-radius:10px;background:linear-gradient(135deg,#ea580c 0%,#dc2626 100%);color:#fff;text-decoration:none;font-weight:700;white-space:nowrap;">升级授权</a>
        </div>

        <div class="page-header">
            <div class="page-title">
                <div class="page-icon"><svg style="width:22px;height:22px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="1" y="4" width="22" height="16" rx="2" ry="2"/><line x1="1" y1="10" x2="23" y2="10"/></svg></div>
                <div>
                    <h1>支付设置</h1>
                    <p style="font-size:12px;color:#64748b;margin:0;">当前仅开放查看</p>
                </div>
            </div>
            <div class="page-actions">
                <span style="display:inline-flex;align-items:center;justify-content:center;padding:12px 18px;border-radius:10px;background:#f8fafc;border:1px dashed #cbd5e1;color:#94a3b8;font-weight:700;">保存需专业版</span>
            </div>
        </div>

        <form>
            <div class="form-section">
                <div class="form-section-header"><svg style="width:18px;height:18px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 12V7H5a2 2 0 0 1 0-4h14v4"/><path d="M3 5v14a2 2 0 0 0 2 2h16v-5"/><path d="M18 12a2 2 0 0 0 0 4h4v-4z"/></svg> 支付宝支付</div>
                <div class="form-section-body">
                    <div class="form-group" style="display:flex;align-items:center;gap:12px;">
                        <div style="display:flex;align-items:center;gap:10px;">
                            <div style="width:44px;height:24px;background:<?php echo $alipayEnabled == '1' ? '#10b981' : '#cbd5e1'; ?>;border-radius:12px;position:relative;">
                                <div style="width:18px;height:18px;background:#fff;border-radius:50%;position:absolute;top:3px;left:<?php echo $alipayEnabled == '1' ? '23px' : '3px'; ?>;box-shadow:0 1px 3px rgba(0,0,0,0.2);"></div>
                            </div>
                            <span style="font-weight:500;color:<?php echo $alipayEnabled == '1' ? '#10b981' : '#64748b'; ?>;"><?php echo $alipayEnabled == '1' ? '已启用' : '未启用'; ?></span>
                            <span style="display:inline-flex;align-items:center;padding:4px 10px;border-radius:999px;background:#fff7ed;border:1px solid #fdba74;color:#9a3412;font-size:12px;font-weight:700;">修改需专业版</span>
                        </div>
                    </div>
                    <div class="form-row">
                        <div class="form-group">
                            <label>APPID</label>
                            <input type="text" class="form-control" value="<?php echo ytPaymentReadonlyValue($configData['alipay_appid'] ?? ''); ?>" placeholder="支付宝开放平台APPID" disabled>
                        </div>
                        <div class="form-group">
                            <label>商家私钥</label>
                            <input type="text" class="form-control" value="<?php echo ytPaymentReadonlyValue($configData['alipay_private_key'] ?? ''); ?>" placeholder="RSA2商家私钥" disabled>
                        </div>
                    </div>
                    <div class="form-group">
                        <label>支付宝公钥</label>
                        <textarea class="form-control" rows="4" placeholder="RSA2支付宝公钥" disabled><?php echo ytPaymentReadonlyValue($configData['alipay_public_key'] ?? ''); ?></textarea>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <?php
}

if (!$licenseObj->hasFeature('payment_alipay')) {
    ytRenderPaymentReadonly($db);
    return;
}

if (!is_file($premiumFile) && $licenseObj->getLevel() > \Core\Lib\License::LEVEL_FREE) {
    $loader = new \Core\Lib\ModuleLoader($licenseObj);
    $loader->syncAuthorizedModules();
}

if (is_file($premiumFile)) {
    require $premiumFile;
    return;
}
?>
<div class="page-container fade-in">
    <div class="page-header">
        <div>
            <h2>支付设置</h2>
            <p style="font-size:13px;color:var(--text-light);margin:4px 0 0 0;">当前安装包未包含支付模块文件</p>
        </div>
    </div>
    <div class="content-card" style="padding:28px;">
        <div style="padding:18px;border:1px solid #fdba74;background:#fff7ed;border-radius:12px;color:#9a3412;line-height:1.8;">
            当前站点尚未安装支付设置模块。授权后可自动同步安装。
        </div>
    </div>
</div>
