<?php
$queryKey = (string)($query_key ?? '');
$result = is_array($result ?? null) ? $result : null;
$devices = is_array($result['devices'] ?? null) ? $result['devices'] : [];
$isActive = (($result['status'] ?? '') === 'active');
$isActivated = !empty($result['activated']);
$deviceCount = (int)($result['devices_count'] ?? 0);
$maxDevices = (int)($result['max_devices'] ?? 0);
$expiredAt = trim((string)($result['expired_at'] ?? ''));
$canUnbind = !empty($result['allow_self_unbind']) && $deviceCount > 0;

$statusText = $isActive ? '正常可用' : '已封禁';
$statusClass = $isActive ? 'ok' : 'no';
$activationText = $isActivated ? '已激活' : '未激活';
$activationClass = $isActivated ? 'ok' : 'warn';
$expiryText = $expiredAt !== '' ? $expiredAt : '未激活 / 未设置';
$deviceUsageText = $maxDevices > 0 ? ($deviceCount . ' / ' . $maxDevices) : (string)$deviceCount;
$deviceRate = $maxDevices > 0 ? (int)min(100, max(0, round(($deviceCount / max(1, $maxDevices)) * 100))) : 0;
?>

<style>
  .query-wrap {
    display: grid;
    gap: 18px;
  }

  .query-intro {
    padding: 22px;
    border: 1px solid #dbe6ff;
    border-radius: 22px;
    background:
      radial-gradient(500px 220px at 100% 0%, rgba(79, 70, 229, 0.12), transparent 65%),
      linear-gradient(180deg, #ffffff, #f8fbff);
  }

  .query-eyebrow {
    margin-bottom: 8px;
    color: #4f46e5;
    font-size: 12px;
    font-weight: 800;
    letter-spacing: .12em;
    text-transform: uppercase;
  }

  .query-heading {
    margin: 0;
    color: #172033;
    font-size: 28px;
    line-height: 1.25;
    font-weight: 800;
  }

  .query-description {
    margin: 10px 0 0;
    color: #64748b;
    font-size: 14px;
    line-height: 1.8;
    max-width: 820px;
  }

  .query-form {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 12px;
  }

  .query-input {
    width: 100%;
    min-height: 52px;
    padding: 0 16px;
    border: 1px solid #cfe0ff;
    border-radius: 14px;
    outline: none;
    font-size: 15px;
    color: #172033;
    background: #fff;
    transition: border-color .2s ease, box-shadow .2s ease, transform .2s ease;
  }

  .query-input:focus {
    border-color: #2563eb;
    box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.10);
    transform: translateY(-1px);
  }

  .query-button {
    min-height: 52px;
    padding: 0 22px;
    border: 0;
    border-radius: 14px;
    background: linear-gradient(135deg, #2563eb, #4f46e5);
    color: #fff;
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
    box-shadow: 0 14px 28px rgba(37, 99, 235, 0.18);
  }

  .query-button.warn {
    width: 100%;
    background: linear-gradient(135deg, #d97706, #ea580c);
    box-shadow: 0 14px 28px rgba(217, 119, 6, 0.18);
  }

  .query-empty {
    padding: 18px;
    border: 1px dashed #cfe0ff;
    border-radius: 18px;
    color: #64748b;
    background: rgba(248, 250, 255, 0.7);
    line-height: 1.8;
  }

  .query-result {
    display: grid;
    gap: 16px;
  }

  .query-result-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    padding: 18px 20px;
    border: 1px solid #e5ecff;
    border-radius: 18px;
    background: linear-gradient(180deg, #ffffff, #f8fbff);
  }

  .query-result-title {
    margin: 0;
    color: #172033;
    font-size: 18px;
    line-height: 1.4;
    font-weight: 800;
    word-break: break-all;
  }

  .query-status-row {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 8px;
  }

  .query-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 7px 12px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 700;
    white-space: nowrap;
  }

  .query-chip.ok {
    color: #166534;
    background: rgba(22, 163, 74, 0.12);
  }

  .query-chip.no {
    color: #991b1b;
    background: rgba(220, 38, 38, 0.12);
  }

  .query-chip.warn {
    color: #92400e;
    background: rgba(245, 158, 11, 0.14);
  }

  .query-summary-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 12px;
  }

  .query-summary-card,
  .query-detail-card,
  .query-device-card,
  .query-action-box {
    padding: 16px 18px;
    border: 1px solid #e5ecff;
    border-radius: 18px;
    background: linear-gradient(180deg, #ffffff, #f8fbff);
  }

  .query-stat-label,
  .query-detail-label,
  .query-device-label {
    margin-bottom: 8px;
    color: #64748b;
    font-size: 12px;
    letter-spacing: .08em;
    text-transform: uppercase;
  }

  .query-stat-value {
    color: #172033;
    font-size: 18px;
    font-weight: 800;
    line-height: 1.4;
    word-break: break-word;
  }

  .query-stat-note,
  .query-device-note,
  .query-action-note {
    margin-top: 8px;
    color: #64748b;
    font-size: 12px;
    line-height: 1.7;
  }

  .query-progress {
    margin-top: 12px;
    width: 100%;
    height: 10px;
    border-radius: 999px;
    background: #e8efff;
    overflow: hidden;
  }

  .query-progress > span {
    display: block;
    height: 100%;
    border-radius: 999px;
    background: linear-gradient(90deg, #2563eb, #4f46e5);
  }

  .query-detail-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
  }

  .query-detail-card .query-value {
    color: #172033;
    font-size: 15px;
    font-weight: 700;
    line-height: 1.65;
    word-break: break-all;
  }

  .query-device-card {
    grid-column: 1 / -1;
  }

  .query-device-list {
    margin: 0;
    padding-left: 18px;
    color: #475569;
    font-size: 13px;
    line-height: 1.8;
  }

  .query-device-list li {
    margin-bottom: 6px;
    word-break: break-all;
  }

  .query-action-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
  }

  .query-action-title {
    margin: 0 0 6px;
    color: #172033;
    font-size: 16px;
    font-weight: 800;
  }

  .query-action-form {
    min-width: 220px;
  }

  @media (max-width: 960px) {
    .query-summary-grid {
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }
  }

  @media (max-width: 720px) {
    .query-result-head,
    .query-action-box {
      flex-direction: column;
      align-items: stretch;
    }

    .query-intro {
      padding: 18px;
    }

    .query-heading {
      font-size: 22px;
    }

    .query-form,
    .query-summary-grid,
    .query-detail-grid {
      grid-template-columns: 1fr;
    }

    .query-button {
      width: 100%;
    }
  }
</style>

<div class="query-wrap">
  <section class="query-intro">
    <div class="query-eyebrow">卡密查询</div>
    <h2 class="query-heading">查询卡密状态与设备信息</h2>
    <p class="query-description">输入卡密后可查看当前状态、激活情况、到期时间和绑定设备信息。</p>
  </section>

  <form method="post" action="<?= e_url(base_url('query-key')) ?>" class="query-form">
    <?= csrf_field() ?>
    <input
      class="query-input"
      name="user_key"
      required
      value="<?= e($queryKey) ?>">
    <button class="query-button" type="submit">立即查询</button>
  </form>

  <?php if ($result === null): ?>
    <div class="query-empty">
      暂无查询结果，输入卡密后这里会显示状态、到期时间和设备信息。
    </div>
  <?php else: ?>
    <section class="query-result">
      <div class="query-result-head">
        <div>
          <div class="query-eyebrow">查询结果</div>
          <h3 class="query-result-title"><?= e((string)($result['user_key'] ?? '')) ?></h3>
        </div>
        <div class="query-status-row">
          <span class="query-chip <?= $statusClass ?>"><?= e($statusText) ?></span>
          <span class="query-chip <?= $activationClass ?>"><?= e($activationText) ?></span>
        </div>
      </div>

      <div class="query-summary-grid">
        <div class="query-summary-card">
          <div class="query-stat-label">卡密状态</div>
          <div class="query-stat-value"><?= e($statusText) ?></div>
          <div class="query-stat-note">用于判断当前卡密是否可以继续使用。</div>
        </div>

        <div class="query-summary-card">
          <div class="query-stat-label">激活状态</div>
          <div class="query-stat-value"><?= e($activationText) ?></div>
          <div class="query-stat-note">显示当前卡密是否已经进入激活使用阶段。</div>
        </div>

        <div class="query-summary-card">
          <div class="query-stat-label">到期时间</div>
          <div class="query-stat-value"><?= e($expiryText) ?></div>
          <div class="query-stat-note">未激活或未设置时会显示为空状态提示。</div>
        </div>

        <div class="query-summary-card">
          <div class="query-stat-label">设备占用</div>
          <div class="query-stat-value"><?= e($deviceUsageText) ?></div>
          <div class="query-stat-note">
            <?= $maxDevices > 0 ? '已绑定设备与最大设备数的占用情况。' : '当前卡密未设置设备上限。' ?>
          </div>
        </div>
      </div>

      <div class="query-detail-grid">
        <div class="query-detail-card">
          <div class="query-detail-label">查询卡密</div>
          <div class="query-value"><?= e((string)($result['user_key'] ?? '')) ?></div>
        </div>

        <div class="query-detail-card">
          <div class="query-detail-label">所属项目</div>
          <div class="query-value"><?= e((string)($result['game'] ?? '')) ?></div>
        </div>

        <div class="query-detail-card">
          <div class="query-detail-label">当前状态说明</div>
          <div class="query-value">
            <?= $isActive ? '卡密当前可正常使用。' : '卡密当前已被封禁，无法继续使用。' ?>
          </div>
        </div>

        <div class="query-detail-card">
          <div class="query-detail-label">激活说明</div>
          <div class="query-value">
            <?= $isActivated ? '已经进入激活状态。' : '尚未激活或当前状态未激活。' ?>
          </div>
        </div>

        <div class="query-detail-card">
          <div class="query-detail-label">设备使用进度</div>
          <div class="query-value"><?= e((string)$deviceCount) ?> / <?= e((string)$maxDevices) ?></div>
          <?php if ($maxDevices > 0): ?>
            <div class="query-progress" aria-hidden="true">
              <span style="width: <?= e((string)$deviceRate) ?>%;"></span>
            </div>
          <?php endif; ?>
        </div>

        <div class="query-detail-card">
          <div class="query-detail-label">自助解绑</div>
          <div class="query-value">
            <?= $canUnbind ? '当前站点支持自助解绑。' : '当前站点未开启自助解绑或暂无可解绑设备。' ?>
          </div>
        </div>

        <div class="query-device-card">
          <div class="query-device-label">当前绑定设备</div>
          <?php if ($devices): ?>
            <div class="query-device-note">已绑定 <?= e((string)count($devices)) ?> 台设备，下面是当前绑定记录。</div>
            <ul class="query-device-list">
              <?php foreach ($devices as $index => $device): ?>
                <li>#<?= e((string)($index + 1)) ?> <?= e((string)$device) ?></li>
              <?php endforeach; ?>
            </ul>
          <?php else: ?>
            <div class="query-device-note">当前卡密暂无设备绑定记录。</div>
          <?php endif; ?>
        </div>
      </div>

      <div class="query-action-box">
        <div>
          <h4 class="query-action-title">设备处理</h4>
          <div class="query-action-note">
            只有在站点开启自助解绑时，才可以清空当前卡密绑定的设备。这个操作不会影响卡密本身的状态。
          </div>
        </div>
        <?php if ($canUnbind): ?>
          <form class="query-action-form" method="post" action="<?= e_url(base_url('query-key/unbind')) ?>" onsubmit="return confirm('确认清空这张卡密当前绑定的设备吗？');">
            <?= csrf_field() ?>
            <input type="hidden" name="user_key" value="<?= e((string)($result['user_key'] ?? '')) ?>">
            <button class="query-button warn" type="submit">自助解绑当前设备</button>
          </form>
        <?php else: ?>
          <div class="query-action-form">
            <button class="query-button warn" type="button" disabled>当前不可解绑</button>
          </div>
        <?php endif; ?>
      </div>
    </section>
  <?php endif; ?>
</div>
