Component({
  data: {
    loaded: false,
    activityId: '',
    canRegister: false,
    confirmToken: '',
    confirmText: '',
    activity: {}
  },
  lifetimes: {
    created() {
      this._modelCtx = wx.modelContext.getContext(this)
      const { NotificationType } = wx.modelContext
      this._modelCtx.on(NotificationType.Result, (data) => {
        const result = data && data.result ? data.result : {}
        const sc = result.structuredContent || {}
        const meta = result._meta || {}
        this.setData({
          loaded: true,
          activityId: sc.activityId,
          canRegister: !!sc.canRegister,
          confirmToken: sc.confirmToken || meta.confirmToken,
          confirmText: sc.confirmText || '请确认是否报名该活动',
          activity: sc.activity || meta.activity || {}
        })
      })
    }
  },
  methods: {
    onConfirm() {
      if (!this.data.canRegister || !this.data.activityId || !this.data.confirmToken) return
      this._modelCtx.sendFollowUpMessage({
        content: [
          { type: 'text', text: `确认报名：${this.data.activity.title || this.data.activityId}` },
          {
            type: 'api/call',
            data: {
              name: 'confirmActivityRegistration',
              arguments: {
                activityId: this.data.activityId,
                confirmToken: this.data.confirmToken
              }
            }
          }
        ]
      })
    },
    onBack() {
      if (!this.data.activityId) return
      this._modelCtx.sendFollowUpMessage({
        content: [
          { type: 'text', text: '查看活动详情' },
          { type: 'api/call', data: { name: 'getActivityDetail', arguments: { activityId: this.data.activityId } } }
        ]
      })
    }
  }
})
