Component({
  data: {
    reply: '',
    draft: null,
    actionUrl: '',
    missingText: '',
  },
  lifetimes: {
    created() {
      const { NotificationType } = wx.modelContext
      this._viewCtx = wx.modelContext.getViewContext(this)
      wx.modelContext.getContext(this).on(NotificationType.Result, (data) => {
        const result = data && data.result ? data.result : {}
        const structured = result.structuredContent || {}
        const meta = result._meta || {}
        const draft = meta.draft || structured.draft || null
        const actions = meta.actions || structured.actions || []
        this.setData({
          reply: structured.reply || '',
          draft,
          actionUrl: draft && draft.actionUrl ? draft.actionUrl : actions[0] && actions[0].url ? actions[0].url : '',
          missingText: draft && draft.missingFields && draft.missingFields.length ? draft.missingFields.join('、') : '',
        })
      })
    },
  },
  methods: {
    onOpenDraft() {
      if (!this.data.actionUrl) return
      this._viewCtx.openDetailPage({ url: this.data.actionUrl })
    },
  },
})
