<template>
  <view class="wrap">
    <text class="t1">当前卡号：{{ loggedId }}</text>
    <text class="t2">VIP 到期：{{ vip }}</text>

    <view class="grid">
      <button class="cell" :disabled="busy" @click="run('刷新到期', true, () => cli.getDateIC())">
        刷新到期
      </button>
      <button class="cell" :disabled="busy" @click="run('登录状态', false, () => cli.getLoginInfo())">
        登录状态
      </button>
      <button class="cell" :disabled="busy" @click="run('心跳', false, () => cli.heartbeat())">
        心跳
      </button>
      <button class="cell" :disabled="busy" @click="run('公告', false, () => cli.getNotice())">
        公告
      </button>
      <button class="cell" :disabled="busy" @click="run('服务器时间', false, () => cli.getServerDate())">
        服务器时间
      </button>
      <button class="cell" :disabled="busy" @click="run('版本', false, () => cli.getVersion())">
        版本
      </button>
      <button class="cell" :disabled="busy" @click="run('软件描述', false, () => cli.getSoftInfo())">
        软件描述
      </button>
      <button class="cell" :disabled="busy" @click="run('预设URL', false, () => cli.getPresetURL())">
        预设URL
      </button>
      <button class="cell" :disabled="busy" @click="run('Web地址', false, () => cli.getWebURL())">
        Web地址
      </button>
      <button class="cell" :disabled="busy" @click="run('软件配置', false, () => cli.getAppCustom('myapp'))">
        软件配置
      </button>
      <button class="cell" :disabled="busy" @click="run('VIP配置', false, () => cli.getAppCustom('myvip'))">
        VIP配置
      </button>
      <button class="cell" :disabled="busy" @click="run('登录配置', false, () => cli.getAppCustom('mylogin'))">
        登录配置
      </button>
      <button class="cell" :disabled="busy" @click="run('全局配置', false, () => cli.getGlobalInfo())">
        全局配置
      </button>
      <button class="cell" :disabled="busy" @click="run('逻辑A', false, () => cli.getLogicA())">
        逻辑A
      </button>
      <button class="cell" :disabled="busy" @click="run('逻辑B', false, () => cli.getLogicB())">
        逻辑B
      </button>
      <button class="cell" :disabled="busy" @click="run('激活查询', false, () => cli.queryCard(loggedId))">
        激活查询
      </button>
    </view>

    <input v-model="auxPwd" class="inp" password placeholder="辅助密码（卡信息/绑定/解绑）" />
    <button class="rowb" :disabled="busy" @click="run('卡信息示例', false, () => cli.getCardInfo(loggedId, auxPwd, 'UserName'))">
      卡信息示例
    </button>
    <button class="rowb" :disabled="busy" @click="run('绑定本机', false, () => cli.bindCard(cli.machineCode(), loggedId, auxPwd))">
      绑定本机
    </button>
    <button class="rowb" :disabled="busy" @click="run('解除绑定', false, () => cli.unbindCard(loggedId, auxPwd))">
      解除绑定
    </button>

    <button class="sale" :disabled="busy" @click="openRenew">在线续费</button>
    <button class="sale" :disabled="busy" @click="openGen">购买生成卡</button>
    <button class="sale" :disabled="busy" @click="openStock">购买库存卡</button>

    <button class="out" @click="logout">注销登录</button>
    <text class="detail">{{ detail }}</text>
  </view>
</template>

<script>
import { getCardClient } from '@/utils/cardSingleton.js'
import { CARD_CFG, renewUrlForUser } from '@/utils/cardConfig.js'
import { cardResult } from '@/utils/bsphp.js'
import { openExternal } from '@/utils/openUrl.js'

export default {
  data() {
    return {
      loggedId: '',
      vip: '-',
      auxPwd: '',
      detail: '',
      busy: false,
      cli: null
    }
  },
  onLoad(q) {
    this.cli = getCardClient()
    this.loggedId = decodeURIComponent(q.cardId || '')
    this.vip = decodeURIComponent(q.vip || '-') || '-'
  },
  methods: {
    openRenew() {
      openExternal(renewUrlForUser(CARD_CFG.renewUrlBase, this.loggedId))
    },
    openGen() {
      openExternal(CARD_CFG.genUrl)
    },
    openStock() {
      openExternal(CARD_CFG.stockUrl)
    },
    async run(title, refreshVip, fn) {
      if (!this.loggedId || this.busy) return
      this.busy = true
      try {
        const r = await fn()
        let body = cardResult(r, '').message
        if (!body) body = '（无 data 文本）'
        const detail = '[' + title + '] code=' + r.code + '\n' + body
        this.detail = detail
        if (refreshVip && r && r.data != null) {
          const m = cardResult(r, '').message
          if (m) this.vip = m
        }
      } catch (e) {
        this.detail = String(e)
      }
      this.busy = false
    },
    logout() {
      this.cli.logout().finally(() => {
        uni.reLaunch({ url: '/pages/index/index' })
      })
    }
  }
}
</script>

<style scoped>
.wrap {
  padding: 24rpx;
  padding-bottom: 60rpx;
}
.t1,
.t2 {
  display: block;
  font-size: 30rpx;
  margin-bottom: 12rpx;
  font-weight: 600;
}
.grid {
  display: flex;
  flex-wrap: wrap;
  gap: 12rpx;
  margin: 20rpx 0;
}
.cell {
  width: calc(50% - 6rpx);
  font-size: 24rpx;
  background: #fff;
  border: 1px solid #e0e0e0;
}
.inp {
  border: 1px solid #e0e0e0;
  border-radius: 10rpx;
  padding: 16rpx;
  margin: 16rpx 0;
  font-size: 28rpx;
}
.rowb {
  margin-bottom: 12rpx;
  font-size: 26rpx;
  background: var(--theme-primary);
  color: #fff;
}
.sale {
  margin-top: 10rpx;
  font-size: 26rpx;
  background: #fff;
  color: var(--theme-primary);
  border: 1px solid var(--theme-primary);
}
.out {
  margin-top: 28rpx;
  background: #c62828;
  color: #fff;
}
.detail {
  display: block;
  margin-top: 24rpx;
  padding: 20rpx;
  background: #f5f5f5;
  border-radius: 12rpx;
  font-size: 24rpx;
  white-space: pre-wrap;
}
</style>
