import { defineComponent } from 'vue'
import styles from './index.module.less'
import iconPc from './images/icon_pc.png'
import { Button } from 'vant'
import { postMessage } from '@/helpers/native-message'
import request from '@/helpers/request'

export default defineComponent({
  name: 'ScanLogin',
  data() {
    const query = this.$route.query
    return {
      code: query.code
    }
  },
  async mounted() {
    try {
      // 确认是否扫码
      await request.get('/api-auth/qrLogin', {
        params: {
          code: this.code
        }
      })
    } catch {}
  },
  methods: {
    async onSubmit() {
      try {
        // 确定登录
        await request.get('/api-auth/doQrLogin', {
          params: {
            code: this.code
          }
        })

        postMessage({ api: 'back', content: {} })
      } catch {}
    },
    onCancel() {
      postMessage({ api: 'back', content: {} })
    }
  },
  render() {
    return (
      <div class={styles.scanLogin}>
        <img src={iconPc} class={styles.iconPc} />

        <div class={styles.sure}>酷乐秀登录确认</div>

        {/* <p class={styles.tips}>请不要扫描来源不明的二维码</p> */}

        <div class={['btnGroup', styles.btnSection]}>
          <Button type="primary" round block onClick={this.onSubmit}>
            确认登录
          </Button>

          <div class={styles.cancelLogin} onClick={this.onCancel}>
            取消登录
          </div>
        </div>
      </div>
    )
  }
})
