<template>
  <div class="complaint-list">
    <el-form :inline="true" :model="searchForm" class="search-form">
      <el-form-item>
        <el-input
          v-model="searchForm.keyword"
          placeholder="搜索关键词"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchComplaints">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="complaintList" style="width: 100%">
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="phone" label="手机号"></el-table-column>
      <el-table-column prop="corporateName" label="公司名称"></el-table-column
      ><el-table-column
        prop="create_time"
        label="创建时间"
        width="180"
      ></el-table-column>
      <el-table-column prop="content" label="内容"></el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click="viewComplaint(scope.row)" type="primary"
            >查看</el-button
          > <el-button @click="handleDelete(scope.row)" type="danger" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog title="投诉详情" :visible.sync="dialogVisible" width="50%">
      <el-descriptions :column="1" border>
        <el-descriptions-item label="姓名">{{
          selectedComplaint.name
        }}</el-descriptions-item>
        <el-descriptions-item label="手机号">{{
          selectedComplaint.phone
        }}</el-descriptions-item>
        <el-descriptions-item label="公司名称">{{
          selectedComplaint.corporateName
        }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{
          selectedComplaint.create_time
        }}</el-descriptions-item>
        <el-descriptions-item label="内容">{{
          selectedComplaint.content
        }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>
  
  <script>
import { getComplaintList,deleteComplaint } from "@/api/applet";

export default {
  data() {
    return {
      complaintList: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      dialogVisible: false,
      selectedComplaint: {},
      searchForm: {
        keyword: "",
      },
    };
  },
  methods: {
    async fetchComplaintList() {
      try {
        const params = {
          page: this.currentPage,
          pageSize: this.pageSize,
          keyword: this.searchForm.keyword,
        };
        const res = await getComplaintList(params);
        if (res.status === 200) {
          this.complaintList = res.data.dataList;
          this.currentPage = Number(res.data.totalPage);
          this.pageSize = Number(res.data.pageSize);
          this.total = Number(res.data.totalRecord);
        } else {
          this.$message.error(res.message || "获取投诉列表失败");
        }
      } catch (error) {
        console.error("获取投诉列表出错:", error);
        this.$message.error("获取投诉列表失败");
      }
    },
    /* 删除 */
    async handleDelete(row) {
      try {
        await this.$confirm('确定要删除这条投诉记录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        });
        
        const res = await deleteComplaint({id:row.id});
        if (res.status === 200) {
          this.$message.success('删除成功');
          this.fetchComplaintList();
        } else {
          this.$message.error(res.message || '删除失败');
        }
      } catch (error) {
        if (error !== 'cancel') {
          console.error("删除投诉记录出错:", error);
          this.$message.error('删除失败');
        }
      }
    },
    searchComplaints() {
      this.currentPage = 1;
      this.fetchComplaintList();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchComplaintList();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchComplaintList();
    },
    viewComplaint(row) {
      this.selectedComplaint = row;
      this.dialogVisible = true;
    },
  },
  mounted() {
    this.fetchComplaintList();
  },
};
</script>
  
  <style scoped>
.complaint-list {
  padding: 20px;
}
.search-form {
  margin-bottom: 20px;
}
.pagination-container {
  display: flex;
  justify-content: flex-end;
}
</style>