<template>
  <div class="feedback-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-select v-model="queryForm.type" clearable placeholder="请选择反馈类型" @change="queryData">
              <el-option label="其他" :value="4" />
              <el-option label="功能建议" :value="1" />
              <el-option label="问题反馈" :value="2" />
              <el-option label="投诉" :value="3" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText">
      <el-table-column align="center" label="类型" prop="type" width="100">
        <template #default="{ row }">
          <el-tag v-if="row.type === 1" type="success">功能建议</el-tag>
          <el-tag v-else-if="row.type === 2" type="warning">问题反馈</el-tag>
          <el-tag v-else-if="row.type === 3" type="danger">投诉</el-tag>
          <el-tag v-else type="info">其他</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="反馈内容" prop="content" show-overflow-tooltip />

      <el-table-column align="center" label="图片" width="200">
        <template #default="{ row }">
          <div v-if="row.images && row.images.length" class="image-grid">
            <el-image
              v-for="(img, index) in row.images"
              :key="index"
              class="grid-image"
              :preview-src-list="row.images"
              :src="getThumbUrl(img)"
            />
          </div>
          <span v-else>--</span>
        </template>
      </el-table-column>

      <el-table-column align="left" label="用户信息" show-overflow-tooltip>
        <template #default="{ row }">
          <div v-if="row.fromUser && row.fromUser.id" class="author-cell">
            <img alt="avatar" class="author-avatar" :src="getThumbUrl(row.fromUser.avatar)" />
            <div class="author-meta">
              <div>
                <strong>id：</strong>
                {{ row.fromUser.id || '--' }}
              </div>
              <div>
                <strong>昵称：</strong>
                {{ row.fromUser.nickname || '--' }}
              </div>
            </div>
          </div>
          <div v-else>
            <div>User ID: {{ row.userId }}</div>
            <div>App ID: {{ row.appId }}</div>
          </div>
        </template>
      </el-table-column>

      <el-table-column align="center" label="设备信息" show-overflow-tooltip>
        <template #default="{ row }">
          <div>IP: {{ row.ip }}</div>
          <div>地址: {{ row.address }}</div>
          <div>
            Device ID:
            <el-link v-if="row.deviceId" type="primary" :underline="false" @click="goToDevice(row.deviceId)">
              {{ row.deviceId }}
            </el-link>
            <span v-else>--</span>
          </div>
        </template>
      </el-table-column>

      <el-table-column align="center" label="创建时间" show-overflow-tooltip>
        <template #default="{ row }">
          {{ formatTime(row.createdAt) }}
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
  import { getUserFeedbackList } from '@/api/spring/user'
  import { formatTime } from '@/utils'
  import { getThumbUrl } from '@/utils/blessing'

  export default {
    name: 'UserFeedback',
    data() {
      return {
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          type: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      formatTime,
      getThumbUrl,
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      goToDevice(deviceId) {
        this.$router.push({ path: '/spring/user/userChat', query: { deviceId } })
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getUserFeedbackList(this.queryForm)
        this.list = data.list
        this.total = data.totalCount
        this.listLoading = false
      },
    },
  }
</script>

<style scoped>
  .image-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 5px;
  }
  .grid-image {
    width: 50px;
    height: 50px;
    object-fit: cover;
    border-radius: 4px;
  }
  .author-cell {
    display: flex;
    align-items: center;
    gap: 12px;
  }
  .author-avatar {
    width: 50px;
    height: 50px;
    object-fit: cover;
    border-radius: 50%;
  }
  .author-meta {
    display: flex;
    flex-direction: column;
  }
</style>
