// pages/invitation/verify/index.js
const app = getApp();
const api = app.api;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusOptions: [
      {id: '0', value: '藏家 Collector'},
      {id: '1', value: '艺术行业 Art Professional'},
      {id: '2', value: '院校师生 Academic'},
      {id: '3', value: '媒体 Media'},
      {id: '4', value: '合作方 Partner'},
      {id: '5', value: '其他 Other '}
    ],
    idTypeOptions: [
      {id: '0', value: '身份证 ID Card'},
      {id: '1', value: '护照 Passport'},
      {id: '2', value: '回乡证 Home Return Certificate'},
      {id: '3', value: '台胞证 MTP'}
    ],
    countryOptions: [
      {id: '0', value: '中国 China'},
      {id: '1', value: '外籍 Foreigners'}
    ],
    genderOptions: [
      {id: '0', value: '男 Male'},
      {id: '1', value: '女 Female'},
      {id: '2', value: '其他 Other'}
    ],
    dataForm: {
      id: '',
      status: '0',
      statusOther: '',
      realName: '',
      idType: '0',
      idNumber: '',
      country: '0',
      liveCity: ['', '', ''],
      liveCityNotChina: '',
      gender: '0',
      emailAddress: '',
      company: '',
      position: ''
    }
  },

  /**
   * 查询客人信息
   */
  getGuest(id) {
    const self = this;
    app.post(api.InvitationGetId, {id: id}).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,
          ['dataForm.signTime']: response.data.signTime
        })
    }
    }).catch(() => {
      wx.showToast({
        title: '未查询到登记信息',
        complete: function() {
          setTimeout(() => {
            wx.navigateBack({
              delta: 0,
            })
          }, 1500)
        }
      })
    })
  },

  /**
   * 核销
   */
  guestSign: function() {
    const self = this
    app.post(api.InvitationVerification, {id: self.data.dataForm.id}).then(response => {
      if (response.success) {
        wx.showToast({
          title: '核验成功',
          mask: true,
          complete: function() {
            setTimeout(() => {
              wx.navigateBack({
                delta: 0,
              })
            }, 1500)
          }
        })
      }
    }).catch(() => {
      wx.showModal({
        title: '核验失败',
        showCancel: false,
        content: '未查询到登记信息',
        success: function(res) {
          if(res.confirm) {
            wx.navigateBack({
              delta: 0,
            })
          }
        }
      });
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const id = options.id;
    const self = this
    self.getGuest(id)
    wx.setNavigationBarTitle({
      title: '邀请码核验',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
})