Component({
  data: {
    reply: '',
    cards: [],
    risk: null,
  },
  lifetimes: {
    created() {
      const { NotificationType } = wx.modelContext
      this._modelCtx = wx.modelContext.getContext(this)
      this._viewCtx = wx.modelContext.getViewContext(this)
      this._modelCtx.on(NotificationType.Result, (data) => {
        const result = data && data.result ? data.result : {}
        const structured = result.structuredContent || {}
        const meta = result._meta || {}
        this.setData({
          reply: structured.reply || '',
          cards: meta.cards || structured.cards || [],
          risk: meta.risk || structured.risk || null,
        })
      })
    },
  },
  methods: {
    onTapDetail(event) {
      const item = event.currentTarget.dataset.item
      if (!item) return
      this._modelCtx.sendFollowUpMessage({
        content: [
          { type: 'text', text: `查看${item.title}详情` },
          { type: 'api/call', data: { name: 'getServiceDetail', arguments: { sourceType: item.sourceType, id: item.id } } },
        ],
      })
    },
    onOpenPage(event) {
      const url = event.currentTarget.dataset.url
      if (!url) return
      this._viewCtx.openDetailPage({ url })
    },
  },
})
