<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px" @close="close">
    <el-form ref="form" label-width="100px" :model="form">
      <el-form-item label="审核结果">
        <el-tag :type="form.status === 2 ? 'success' : 'danger'">
          {{ form.status === 2 ? '通过' : '拒绝' }}
        </el-tag>
      </el-form-item>
      <el-form-item v-if="form.status === 3" label="拒绝原因">
        <el-input v-model="form.rejectReason" placeholder="选填，请输入拒绝原因" :rows="3" type="textarea" />
      </el-form-item>
      <el-form-item label="审核备注">
        <el-input v-model="form.auditRemark" placeholder="选填，请输入内部审核备注" :rows="3" type="textarea" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button :loading="saving" type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { reviewTopic } from '@/api/rating/topicOperate'

  export default {
    name: 'ReviewEdit',
    data() {
      return {
        title: '审核确认',
        dialogFormVisible: false,
        saving: false,
        form: {
          topicId: '',
          itemId: '',
          userId: '',
          status: '',
          type: '', // topic 或 topicItem
          rejectReason: '',
          auditRemark: '',
        },
      }
    },
    methods: {
      showEdit(row, status, type) {
        this.title = `审核确认 (${type === 'topic' ? '话题' : '评价项'})`
        this.form = {
          topicId: type === 'topic' ? row.id : row.topicId || '',
          itemId: type === 'topicItem' ? row.id : '',
          userId: this.$store.state.user.userId || '',
          status,
          type,
          rejectReason: '',
          auditRemark: '',
        }
        this.dialogFormVisible = true
      },
      close() {
        this.dialogFormVisible = false
        this.saving = false
      },
      async save() {
        this.saving = true
        try {
          const payload = { ...this.form }
          if (payload.itemId && !payload.itemmId) {
            payload.itemmId = payload.itemId
          }
          const { msg } = await reviewTopic(payload)
          this.$baseMessage(msg || '操作成功', 'success')
          this.$emit('fetch-data')
          this.close()
        } catch (error) {
          console.error(error)
        } finally {
          this.saving = false
        }
      },
    },
  }
</script>
