<template>
  <div class="evaluation-card">
    <div v-if="!action">
      <p>您对这次搜索结果满意吗？</p>
      <a-button-group>
        <a-space>
          <a-button type="primary" icon="like" @click="submitEvaluation(0)">满意</a-button>
          <a-button type="danger" icon="dislike" @click="submitEvaluation(1)">不满意</a-button>
          <a-button type="default" icon="frown" @click="submitEvaluation(3)">问题不明确</a-button>
        </a-space>
      </a-button-group>
    </div>
  </div>
</template>

<script>
import { post } from '@vue2-client/services/api'
import { indexedDB } from '@vue2-client/utils/indexedDB'

export default {
  data () {
    return {
      action: false
    }
  },
  props: {
    uuid: {
      type: String,
      required: true
    }
  },
  methods: {
    submitEvaluation (result) {
      const key = 'question-' + this.uuid
      let flag = true
      indexedDB.get(key, (item) => {
        flag = false
        // 收藏状态不存储到库里
        delete item.isFavorite
        const content = Object.assign(item, {
          evaluation: result
        })
        // 保存到数据库中
        post('/api/af-system/logic/openapi/updateCommonData', {
          type: 'ai-question',
          content: this.uuid,
          newContent: content
        }).then(res => {
          this.action = true
          this.$message.success('评论成功')
          indexedDB.delete(key)
          indexedDB.add(key, content)
        })
      })
      // 如果 indexedDB 没有获取到说明是对话库的数据
      if (flag) {
        this.$emit('submitEvaluationLib', result, this.uuid)
      }
    }
  }
}
</script>
<style lang="less" scoped>
.evaluation-card {
  width: 50%;
  text-align: center;
  margin: 20px auto 0 auto;
}
</style>
