<template>
  <div class="wrap">
    <h2>仅开发：写入会话</h2>
    <p class="hint">
      将已从后端拿到的 JWT 与 sudo token 写入 localStorage，再跳转首页。生产构建不包含本页。
    </p>
    <van-field v-model="auth" label="Authorization" type="textarea" rows="3" placeholder="login2_with_unionid 返回的 d" />
    <van-field v-model="sudo" label="HtySudoerToken" type="textarea" rows="2" placeholder="sudo2 返回的 d" />
    <van-button type="primary" block class="btn" @click="apply">写入并去首页</van-button>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import { useRouter } from 'vue-router'
import { Button, Field, showFailToast, showSuccessToast } from 'vant'
import { HtyAuthToken, HtySudoToken } from '~/utils'

export default defineComponent({
  name: 'SessionSeed',
  components: {
    [Button.name!]: Button,
    [Field.name!]: Field,
  },
  setup() {
    const router = useRouter()
    const auth = ref('')
    const sudo = ref('')
    const apply = () => {
      const a = auth.value.trim()
      const s = sudo.value.trim()
      if (!a || !s) {
        showFailToast('两项都要填')
        return
      }
      window.localStorage.setItem(HtyAuthToken, a)
      window.localStorage.setItem(HtySudoToken, s)
      showSuccessToast('已写入')
      void router.replace('/')
    }
    return { auth, sudo, apply }
  },
})
</script>

<style scoped>
.wrap {
  padding: 1rem;
  max-width: 36rem;
  margin: 0 auto;
}
.hint {
  color: #666;
  font-size: 14px;
  margin-bottom: 1rem;
}
.btn {
  margin-top: 1rem;
}
</style>
