<template>
  <div class="page-card">
    <h3>反馈处理</h3>
    <el-table :data="items" stripe>
      <el-table-column prop="id" label="编号" width="80" />
      <el-table-column label="长者">
        <template slot-scope="scope">
          {{ elderName(scope.row.elderId) }}
        </template>
      </el-table-column>
      <el-table-column label="类型" width="120">
        <template slot-scope="scope">
          <el-tag :type="typeTag(scope.row.type)">
            {{ typeLabel(scope.row.type) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="content" label="内容" />
      <el-table-column label="状态" width="120">
        <template slot-scope="scope">
          <el-tag :type="statusTag(scope.row.status)">
            {{ statusLabel(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="openReply(scope.row)">回复</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="回复处理" :visible.sync="showReply">
      <el-form :model="replyForm" label-width="120px">
        <el-form-item label="状态">
          <el-select v-model="replyForm.status">
            <el-option label="新建" value="NEW" />
            <el-option label="处理中" value="PROCESSING" />
            <el-option label="已关闭" value="CLOSED" />
          </el-select>
        </el-form-item>
        <el-form-item label="回复">
          <el-input v-model="replyForm.reply" type="textarea" />
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="showReply = false">取消</el-button>
        <el-button type="primary" @click="saveReply">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { feedbackList, feedbackUpdate, eldersList } from "../../api";

export default {
  data() {
    return {
      items: [],
      elders: [],
      elderMap: {},
      showReply: false,
      replyForm: { id: null, status: "PROCESSING", reply: "" }
    };
  },
  created() {
    this.load();
    this.loadElders();
  },
  methods: {
    async loadElders() {
      try {
        const res = await eldersList();
        this.elders = res.data.data;
        const map = {};
        this.elders.forEach((elder) => {
          map[elder.id] = elder.name;
        });
        this.elderMap = map;
      } catch (e) {
        this.$message.error(e.message || "加载长者失败");
      }
    },
    elderName(id) {
      return this.elderMap[id] || `长者${id}`;
    },
    typeLabel(type) {
      if (type === "SUGGESTION") return "建议";
      if (type === "COMPLAINT") return "投诉";
      if (type === "PRAISE") return "表扬";
      return type || "-";
    },
    typeTag(type) {
      if (type === "SUGGESTION") return "info";
      if (type === "COMPLAINT") return "danger";
      if (type === "PRAISE") return "success";
      return "info";
    },
    statusLabel(status) {
      if (status === "NEW") return "新建";
      if (status === "PROCESSING") return "处理中";
      if (status === "CLOSED") return "已关闭";
      return status || "-";
    },
    statusTag(status) {
      if (status === "NEW") return "warning";
      if (status === "PROCESSING") return "";
      if (status === "CLOSED") return "success";
      return "info";
    },
    async load() {
      try {
        const res = await feedbackList();
        this.items = res.data.data;
      } catch (e) {
        this.$message.error(e.message || "加载失败");
      }
    },
    openReply(row) {
      this.replyForm = { id: row.id, status: row.status || "PROCESSING", reply: row.reply || "" };
      this.showReply = true;
    },
    async saveReply() {
      try {
        await feedbackUpdate(this.replyForm);
        this.showReply = false;
        this.load();
      } catch (e) {
        this.$message.error(e.message || "更新失败");
      }
    }
  }
};
</script>

