const app = getApp();
const api = app.api;
let QRCode = require('../../../utils/weapp.qrcode.min.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    signTime: '',
    dataForm: {
      id: '',
      status: '0',
      statusOther: '',
      realName: '',
      idType: '0',
      idNumber: '',
      country: '0',
      liveCity: ['北京市', '北京市', '东城区'],
      liveCityNotChina: '',
      gender: '0',
      emailAddress: '',
      company: '',
      position: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 查询核验二维码
    const self = this
    self.getGuestQRCode()

    // 设置页面标题
    wx.setNavigationBarTitle({
      title: '入场凭证',
    })
  },

  /**
   * 查询客人核验二维码
   */
  getGuestQRCode() {
    const self = this;
    app.post(api.InvitationGet, {}).then(response => {
      if(response.success) {
        self.setData({
          ['dataForm.id']: response.data.id,
          ['dataForm.status']: response.data.roles + '',
          ['dataForm.statusOther']: response.data.roleOther,
          ['dataForm.realName']: response.data.name,
          ['dataForm.idType']: response.data.types + '',
          ['dataForm.idNumber']: response.data.nums,
          ['dataForm.country']: response.data.country + '',
          ['dataForm.liveCity']: response.data.country === 0 ? [response.data.province, response.data.city, response.data.district] : [],
          ['dataForm.liveCityNotChina']: response.data.country === 1 ? response.data.states : '',
          ['dataForm.gender']: response.data.gender + '',
          ['dataForm.emailAddress']: response.data.mail,
          ['dataForm.company']: response.data.company,
          ['dataForm.position']: response.data.position,
        });

        // 生成核销二维码
        const systemInfo = wx.getSystemInfoSync()
        const width = 107*systemInfo.windowWidth/375
        const height = width

        new QRCode({
          text: self.data.dataForm.id + '',
          width: width,
          height: height,
          canvasId: 'myQrcode'
        });
      }
    }).catch(() => {
      wx.showModal({
        content: '没有查询到您的入场凭证',
        cancelText: '返回',
        confirmText: '获取凭证',
        success: function(response) {
          if(response.confirm) {
            wx.redirectTo({
              url: '../index/index',
            })
          } else {
            wx.navigateBack({
              delta: 0,
            })
          }
        }
      })
    })
  }
})