<?php
/**
 * cabinet.php — личный кабинет crmclick.
 *
 * Источник доступа — СВОЯ crmclick.db (таблица activations), не премиум бота.
 * Модель prepaid: показываем статус подписки, срок действия, код для повторного
 * входа и кнопку продления. Тексты — из lib/i18n.php (RU/EN, один источник).
 */
require __DIR__ . '/lib/config.php';
require __DIR__ . '/lib/i18n.php';
crm_lang_apply();
crm_session_start();

if (isset($_GET['logout'])) { crm_logout(); header('Location: /login'); exit; }

$code = crm_current_code();
$act  = $code !== '' ? crm_activation($code) : null;
if (!$act) { header('Location: /login'); exit; }

$plan      = crm_plan($act['plan']);
$planTitle = $plan ? crm_plan_label($plan, 'title') : strtoupper((string)$act['plan']);
$active    = crm_is_active($act);
$expires   = (int)($act['expires_at'] ?? 0);

if ($act['status'] === 'paid') {
    $status = $active ? t('cab_st_active') : t('cab_st_expired');
} elseif ($act['status'] === 'canceled') {
    $status = t('cab_st_canceled');
} else {
    $status = t('cab_st_pending');
}
$expiresStr = $expires > 0 ? date('d.m.Y', $expires) : '—';
?>
<!DOCTYPE html>
<html lang="<?= crm_lang() ?>">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= t('cab_title') ?> — <?= t('brand') ?></title>
    <style>
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; background-color: #08080f; color: #fff; min-height: 100vh; display: flex; flex-direction: column; }
        header { padding: 16px 20px; border-bottom: 1px solid #1e1e2d; display: flex; justify-content: space-between; align-items: center; gap: 12px; }
        header .logo { font-size: 20px; font-weight: 700; color: #fff; text-decoration: none; }
        header .right { display: flex; align-items: center; gap: 14px; }
        header .right a.out { color: #94a3b8; text-decoration: none; font-size: 14px; }
        .lang-switch{display:inline-flex;align-items:center;gap:2px;border:1px solid rgba(255,255,255,.12);border-radius:8px;padding:2px;background:rgba(255,255,255,.03)}
        .lang-switch a,.lang-switch .lang-cur{font-size:12px;font-weight:700;letter-spacing:.4px;padding:5px 9px;border-radius:6px;text-decoration:none;line-height:1}
        .lang-switch a{color:#7c8598}
        .lang-switch .lang-cur{background:linear-gradient(135deg,#7c3aed,#4f46e5);color:#fff}
        .container { max-width: 600px; margin: 40px auto; padding: 20px; width: 100%; }
        h1 { font-size: 24px; margin-bottom: 24px; }
        .card { background: #13131f; border: 1px solid #1e1e2d; border-radius: 16px; padding: 24px; margin-bottom: 24px; }
        .card h2 { font-size: 18px; margin-bottom: 16px; color: #f8fafc; }
        .info-row { display: flex; justify-content: space-between; gap: 12px; margin-bottom: 12px; padding-bottom: 12px; border-bottom: 1px solid #1e1e2d; }
        .info-row:last-child { border-bottom: none; margin-bottom: 0; padding-bottom: 0; }
        .info-label { color: #64748b; font-size: 15px; }
        .info-value { font-weight: 500; font-size: 15px; text-align: right; word-break: break-all; }
        .status-active { color: #10b981; }
        .status-inactive { color: #ef4444; }
        .code-box { background: #1e1e2d; border: 1px dashed #334155; border-radius: 10px; padding: 12px 14px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; color: #cbd5e1; word-break: break-all; margin-top: 8px; }
        .hint { color: #64748b; font-size: 13px; margin-top: 8px; line-height: 1.5; }
        .btn { display: inline-block; background: linear-gradient(135deg, #7c3aed, #4f46e5); color: #fff; padding: 13px 22px; border-radius: 10px; font-size: 15px; font-weight: 600; text-decoration: none; }
    </style>
</head>
<body>
<header>
    <a href="/" class="logo"><?= t('brand') ?></a>
    <div class="right">
        <?= crm_lang_switcher() ?>
        <a class="out" href="/cabinet?logout=1"><?= t('cab_logout') ?></a>
    </div>
</header>

<div class="container">
    <h1><?= t('cab_title') ?></h1>

    <div class="card">
        <h2><?= t('cab_sub_h') ?></h2>
        <div class="info-row">
            <span class="info-label"><?= t('cab_plan') ?></span>
            <span class="info-value"><?php echo htmlspecialchars($planTitle); ?></span>
        </div>
        <div class="info-row">
            <span class="info-label"><?= t('cab_status') ?></span>
            <span class="info-value <?php echo $active ? 'status-active' : 'status-inactive'; ?>"><?php echo htmlspecialchars($status); ?></span>
        </div>
        <div class="info-row">
            <span class="info-label"><?= t('cab_until') ?></span>
            <span class="info-value"><?php echo htmlspecialchars($expiresStr); ?></span>
        </div>
    </div>

    <div class="card">
        <h2><?= t('cab_code_h') ?></h2>
        <div class="code-box"><?php echo htmlspecialchars($code); ?></div>
        <p class="hint"><?= t('cab_code_hint') ?></p>
    </div>

    <?php if (!$active): ?>
    <div class="card">
        <h2><?= t('cab_renew_h') ?></h2>
        <p class="hint" style="margin-top:0;margin-bottom:16px"><?= t('cab_renew_p') ?></p>
        <a class="btn" href="/"><?= t('cab_renew_btn') ?></a>
    </div>
    <?php endif; ?>
</div>
</body>
</html>
