<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式信息" prop="contactInfo">
        <el-input
          v-model="queryParams.contactInfo"
          placeholder="请输入联系方式信息"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedAt">
        <el-date-picker clearable
          v-model="queryParams.updatedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="用户IP地址" prop="ipAddress">
        <el-input
          v-model="queryParams.ipAddress"
          placeholder="请输入用户IP地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择处理状态" clearable>
          <el-option
            v-for="dict in statusOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:feedback:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:feedback:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="feedbackList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="唯一标识符" align="center" prop="id" />
      <el-table-column label="反馈内容" align="center" prop="feedbackContent" />
      <el-table-column label="用户姓名" align="center" prop="userName" />
      <el-table-column label="联系方式类型" align="center" prop="contactType" />
      <el-table-column label="联系方式信息" align="center" prop="contactInfo" />
      <el-table-column label="处理状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="getStatusType(scope.row.status)">{{ getStatusLabel(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户IP地址" align="center" prop="ipAddress" />
      <el-table-column label="用户浏览器信息" align="center" prop="userAgent" />
      <el-table-column label="附件路径" align="center" prop="attachmentPath" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-dropdown @command="(command) => handleStatusChange(command, scope.row)" trigger="click" size="mini">
            <el-button type="text" size="mini" class="status-button">
              <i class="el-icon-s-tools"></i> 状态管理<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-if="scope.row.status !== 'pending'" command="pending">
                <i class="el-icon-warning-outline" style="color: #E6A23C;"></i> 设为待处理
              </el-dropdown-item>
              <el-dropdown-item v-if="scope.row.status !== 'processing'" command="processing">
                <i class="el-icon-loading" style="color: #409EFF;"></i> 设为处理中
              </el-dropdown-item>
              <el-dropdown-item v-if="scope.row.status !== 'resolved'" command="resolved">
                <i class="el-icon-success" style="color: #67C23A;"></i> 设为已解决
              </el-dropdown-item>
              <el-dropdown-item v-if="scope.row.status !== 'closed'" command="closed">
                <i class="el-icon-finished" style="color: #909399;"></i> 设为已关闭
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <el-divider direction="vertical"></el-divider>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:feedback:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listFeedback, getFeedback, delFeedback, updateFeedback } from "@/api/feedback/feedback"

export default {
  name: "Feedback",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户反馈表格数据
      feedbackList: [],
      // 状态数据字典
      statusOptions: [
        { value: "pending", label: "待处理" },
        { value: "processing", label: "处理中" },
        { value: "resolved", label: "已解决" },
        { value: "closed", label: "已关闭" }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        feedbackContent: null,
        userName: null,
        contactType: null,
        contactInfo: null,
        status: null,
        createdAt: null,
        updatedAt: null,
        ipAddress: null,
        userAgent: null,
        attachmentPath: null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询用户反馈列表 */
    getList() {
      this.loading = true
      listFeedback(this.queryParams).then(response => {
        this.feedbackList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除用户反馈编号为"' + ids + '"的数据项？').then(function() {
        return delFeedback(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/feedback/export', {
        ...this.queryParams
      }, `feedback_${new Date().getTime()}.xlsx`)
    },
    /** 获取状态标签 */
    getStatusLabel(status) {
      const statusMap = {
        "pending": "待处理",
        "processing": "处理中",
        "resolved": "已解决",
        "closed": "已关闭",
        "0": "待处理" // 兼容旧数据
      };
      return statusMap[status] || status;
    },
    /** 获取状态类型（用于设置标签颜色） */
    getStatusType(status) {
      const statusTypeMap = {
        "pending": "warning",
        "processing": "primary",
        "resolved": "success",
        "closed": "info",
        "0": "warning" // 兼容旧数据
      };
      return statusTypeMap[status] || "";
    },
    /** 处理状态变更 */
    handleStatusChange(status, row) {
      const statusLabels = {
        "pending": "待处理",
        "processing": "处理中",
        "resolved": "已解决",
        "closed": "已关闭"
      };
      
      this.$modal.confirm('确认将反馈状态修改为"' + statusLabels[status] + '"吗？').then(() => {
        // 克隆对象，避免直接修改表格数据
        const feedbackData = JSON.parse(JSON.stringify(row));
        feedbackData.status = status;
        // 更新时间为当前时间
        feedbackData.updatedAt = new Date();
        
        return updateFeedback(feedbackData);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("状态修改成功");
      }).catch(() => {});
    }
  }
}
</script>

<style scoped>
.status-button {
  color: #409EFF;
  padding: 5px 10px;
}
.status-button:hover {
  color: #66b1ff;
}
.el-dropdown-menu__item i {
  margin-right: 5px;
}
</style>