<?php
$gameCatalog = is_array($game_catalog ?? null) ? $game_catalog : [];
$isAdmin = (($user['role'] ?? '') === 'admin');
?>

<div class="row">
  <div class="col-md-7">
    <div class="card">
      <div class="card-header"><h4><?= e(app_zh('编辑卡密')) ?></h4></div>
      <div class="card-body">
        <form method="post" action="<?= e_url(base_url('keys/edit/' . $key['id'])) ?>">
          <?= csrf_field() ?>

          <div class="form-group">
            <label><?= e(app_zh('状态')) ?></label>
            <select class="form-control" name="status">
              <option value="active" <?= ($key['status'] ?? '') === 'active' ? 'selected' : '' ?>><?= e(app_zh('正常')) ?></option>
              <option value="inactive" <?= ($key['status'] ?? '') === 'inactive' ? 'selected' : '' ?>><?= e(app_zh('禁用')) ?></option>
            </select>
          </div>

          <?php if ($isAdmin): ?>
            <div class="form-group">
              <label><?= e(app_zh('游戏')) ?></label>
              <select class="form-control" name="game">
                <?php foreach ($gameCatalog as $code => $game): ?>
                  <option value="<?= e((string)$code) ?>" <?= (($key['game'] ?? '') === (string)$code) ? 'selected' : '' ?>>
                    <?= e((string)($game['name'] ?? $code)) ?> (<?= e((string)$code) ?>)
                  </option>
                <?php endforeach; ?>
              </select>
            </div>

            <div class="form-group">
              <label><?= e(app_zh('卡密')) ?></label>
              <input class="form-control" name="user_key" value="<?= e((string)$key['user_key']) ?>">
            </div>

            <div class="row">
              <div class="col-md-6">
                <div class="form-group">
                  <label><?= e(app_zh('时长（小时）')) ?></label>
                  <input class="form-control" name="duration_hours" value="<?= e((string)$key['duration_hours']) ?>">
                </div>
              </div>
              <div class="col-md-6">
                <div class="form-group">
                  <label><?= e(app_zh('设备上限')) ?></label>
                  <input class="form-control" name="max_devices" value="<?= e((string)$key['max_devices']) ?>">
                </div>
              </div>
            </div>

            <div class="row">
              <div class="col-md-6">
                <div class="form-group">
                  <label><?= e(app_zh('批次号')) ?></label>
                  <input class="form-control" name="batch_code" value="<?= e((string)($key['batch_code'] ?? '')) ?>" placeholder="<?= e(app_zh('留空则取消批次')) ?>">
                </div>
              </div>
              <div class="col-md-6">
                <div class="form-group">
                  <label><?= e(app_zh('批次名称')) ?></label>
                  <input class="form-control" name="batch_name" value="<?= e((string)($key['batch_name'] ?? '')) ?>">
                </div>
              </div>
            </div>

            <div class="form-group">
              <label><?= e(app_zh('到期时间（YYYY-MM-DD HH:MM:SS）')) ?></label>
              <input class="form-control" name="expired_at" value="<?= e((string)($key['expired_at'] ?? '')) ?>">
            </div>
          <?php endif; ?>

          <button class="btn btn-primary" type="submit"><?= e(app_zh('保存')) ?></button>
        </form>
      </div>
    </div>
  </div>
</div>
