<template>
  <el-dialog
    title="评价详情"
    :visible.sync="visible"
    width="800px"
    :before-close="handleClose"
    append-to-body
  >
    <div class="dialog-content">
      <div class="dialog-header">
        <div class="order-info">
          <span>订单号: {{ orderInfo ? orderInfo.orderId : '' }}</span>
          <span style="margin-left: 20px">客户: {{ orderInfo ? orderInfo.name : '' }}</span>
        </div>
        <el-button type="primary" size="mini" icon="el-icon-refresh" @click="refresh">刷新</el-button>
      </div>

      <el-table
        :data="commentList"
        style="width: 100%"
        v-loading="loading"
        border
        stripe
      >
        <el-table-column label="评价时间" align="center" width="180">
          <template slot-scope="scope">
            {{ parseTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="评分" align="center" width="150">
          <template slot-scope="scope">
            <el-rate
              v-model="scope.row.score"
              disabled
              show-score
              text-color="#ff9900"
            ></el-rate>
          </template>
        </el-table-column>
        <el-table-column label="评价内容" align="left">
          <template slot-scope="scope">
            <div class="comment-content">{{ scope.row.content }}</div>
          </template>
        </el-table-column>
        <el-table-column label="评价图片" align="center" width="100">
          <template slot-scope="scope">
            <el-popover
              placement="right"
              trigger="hover"
              popper-class="image-preview-popover"
              v-if="scope.row.images"
            >
              <div class="image-preview-container">
                <el-image
                  v-for="(img, index) in scope.row.images.split(',')"
                  :key="index"
                  :src="img"
                  fit="cover"
                  style="width: 120px; margin-bottom: 10px; object-fit: cover;"
                  :preview-src-list="scope.row.images.split(',')"
                ></el-image>
              </div>
              <el-button slot="reference" type="primary" size="mini" icon="el-icon-picture" circle></el-button>
            </el-popover>
            <el-button v-else type="info" size="mini" icon="el-icon-picture" circle disabled></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div v-if="!commentList || commentList.length === 0" class="empty-comment">
        <i class="el-icon-chat-dot-square"></i>
        <p>暂无评价信息</p>
      </div>
    </div>
  </el-dialog>
</template>


<script>
import { getCommentRecords } from "@/api/system/Order"
export default {
  name: "CommentRecord",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    orderInfo: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      commentList: [],
      loading: false
    }
  },
  watch: {
    visible(val) {
      if (val && this.orderInfo) {
        this.getList()
      }
    }
  },
  mounted(){
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      getCommentRecords(this.orderInfo.orderId).then(response => {
        this.recordList = response.data
        this.loading = false
      }).catch(() => {
        this.loading = false
        this.$message.error('获取记录失败')
      })
    },


   /* getList() {
      this.loading = true
      // 这里应该调用实际的评价API，目前使用模拟数据
      setTimeout(() => {
        // 模拟评价数据 - 实际使用时替换为API调用
        this.commentList = [
          {
            id: 1,
            orderId: this.orderInfo.orderId,
            content: "服务非常好，师傅很专业，态度很好。",
            score: 5,
            createTime: new Date(),
            images: "https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg,https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg"
          },
          {
            id: 2,
            orderId: this.orderInfo.orderId,
            content: "整体还不错，就是时间稍微有点晚。",
            score: 4,
            createTime: new Date(Date.now() - 86400000), // 一天前
            images: ""
          }
        ]
        this.loading = false
      }, 500)
    },*/
    refresh() {
      this.getList()
    },
    handleClose() {
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-content {
  min-height: 300px;
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.order-info {
  font-weight: bold;
  font-size: 14px;
}

.comment-content {
  line-height: 1.6;
  text-align: left;
  padding: 5px 0;
}

.image-preview-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 5px;
  max-height: 300px;
  overflow-y: auto;
}

.image-preview-popover {
  max-width: 150px;
  max-height: 300px;
}

.empty-comment {
  padding: 40px 0;
  text-align: center;
  color: #909399;
}

.empty-comment i {
  font-size: 40px;
  margin-bottom: 15px;
}
</style>
