<template>
  <view class="admin-kefu-feedback">
    <view class="content-body">
      <!-- 顶部搜索栏 -->
      <view class="filter-card border-shadow">
        <view class="filter-item">
          <text class="label-txt">留言信息:</text>
          <input class="search-input" placeholder="请输入用户昵称/电话/留言内容搜索" v-model="searchQuery" @confirm="handleQuery" />
        </view>
        <view class="filter-item">
          <text class="label-txt">状态:</text>
          <uni-data-select v-model="statusValue" :localdata="statusOptions" style="width: 120px;" @change="handleQuery" />
        </view>
        <view class="btn-query" @click="handleQuery">
          <text class="query-txt">查询</text>
        </view>
      </view>

      <!-- 数据表格 -->
      <view class="table-card border-shadow">
        <view class="table-header">
          <view class="th col-id"><text class="th-txt">序号</text></view>
          <view class="th col-nick"><text class="th-txt">用户信息</text></view>
          <view class="th col-phone"><text class="th-txt">联系电话</text></view>
          <view class="th col-content"><text class="th-txt">留言内容</text></view>
          <view class="th col-status"><text class="th-txt">状态</text></view>
          <view class="th col-time"><text class="th-txt">留言时间</text></view>
          <view class="th col-op"><text class="th-txt">操作</text></view>
        </view>

        <view class="table-body">
          <view v-if="loading" class="table-loading" style="padding: 40px; text-align: center;">
            <text>加载中...</text>
          </view>
          <view v-else-if="feedbackList.length === 0" class="table-empty" style="padding: 40px; text-align: center;">
            <text>暂无留言记录</text>
          </view>
          <view v-else class="table-row" v-for="(item, index) in feedbackList" :key="item.id">
            <view class="td col-id"><text class="td-txt">{{ (page - 1) * pageSize + index + 1 }}</text></view>
            <view class="td col-nick">
              <view class="u-info">
                <text class="td-txt">{{ item.nickname || '匿名' }}</text>
                <text class="u-account" v-if="item.user_account">账号:{{ item.user_account }}</text>
              </view>
            </view>
            <view class="td col-phone"><text class="td-txt">{{ item.phone || '-' }}</text></view>
            <view class="td col-content">
              <text class="td-txt ellipsis">{{ item.content }}</text>
            </view>
            <view class="td col-status">
              <text class="status-tag" :class="item.status == 1 ? 'processed' : 'pending'">
                {{ item.status == 1 ? '已处理' : '未处理' }}
              </text>
            </view>
            <view class="td col-time"><text class="td-txt">{{ item.created_at.substring(0, 16).replace('T', ' ') }}</text></view>
            <view class="td col-op">
              <text class="btn-link" v-if="item.status == 0" @click="handleProcess(item)">处理</text>
              <text class="btn-link gray" v-else>已回复</text>
            </view>
          </view>
        </view>

        <!-- 分页 -->
        <view class="pagination-bar">
          <text class="page-total">共 {{ total }} 条</text>
          <view class="page-nav">
            <view class="nav-btn" :class="{ disabled: page <= 1 }" @click="prevPage"><text class="nav-icon"> < </text></view>
            <view class="nav-item active"><text class="nav-num">{{ page }}</text></view>
            <view class="nav-btn" :class="{ disabled: page >= totalPages }" @click="nextPage"><text class="nav-icon"> > </text></view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="uts">
import { ref, onMounted, computed } from 'vue'
import { fetchFeedbackPage, processFeedback, type KefuFeedback } from '@/services/admin/kefuService.uts'

const searchQuery = ref('')
const statusValue = ref('all')
const feedbackList = ref<KefuFeedback[]>([])
const total = ref(0)
const page = ref(1)
const pageSize = ref(15)
const loading = ref(false)

const statusOptions = [
  { value: 'all', text: '全部状态' },
  { value: '0', text: '未处理' },
  { value: '1', text: '已处理' }
]

const totalPages = computed((): number => {
  if (pageSize.value <= 0) return 1
  return Math.ceil(total.value / pageSize.value)
})

const loadData = async () => {
  loading.value = true
  try {
    const status = statusValue.value == 'all' ? null : parseInt(statusValue.value)
    const res = await fetchFeedbackPage(page.value, pageSize.value, searchQuery.value || null, status)
    feedbackList.value = res.items
    total.value = res.total
  } catch (e) {
    uni.showToast({ title: '加载留言失败', icon: 'none' })
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadData()
})

function handleQuery() {
  page.value = 1
  loadData()
}

async function handleProcess(item: KefuFeedback) {
  uni.showModal({
    title: '留言处理',
    editable: true,
    placeholderText: '请输入回复内容',
    success: async (res) => {
      if (res.confirm && res.content) {
        const ok = await processFeedback(item.id, res.content)
        if (ok) {
          uni.showToast({ title: '处理成功' })
          loadData()
        }
      }
    }
  })
}

function prevPage() {
  if (page.value > 1) {
    page.value--
    loadData()
  }
}

function nextPage() {
  if (page.value < totalPages.value) {
    page.value++
    loadData()
  }
}
</script>

<style scoped lang="scss">
.admin-kefu-feedback {
  padding: 0;
  background-color: transparent;
  min-height: auto;
}

.border-shadow {
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.filter-card {
  padding: 20px;
  margin-bottom: 20px;
  display: flex;
  flex-direction: row;
  align-items: center;
  flex-wrap: wrap;
  gap: 20px;
}

.filter-item {
  display: flex;
  flex-direction: row;
  align-items: center;
}

.label-txt {
  font-size: 14px;
  color: #606266;
  margin-right: 12px;
}

.search-input {
  height: 36px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  padding: 0 12px;
  font-size: 13px;
}

.btn-query {
  width: 76px;
  height: 34px;
  background-color: #2d8cf0;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.query-txt {
  color: #fff;
  font-size: 14px;
}

/* 表格卡片 */
.table-card {
  display: flex;
  flex-direction: column;
}

.table-header {
  height: 50px;
  background-color: #eaf2ff;
  display: flex;
  flex-direction: row;
  align-items: center;
  border-bottom: 1px solid #f0f0f0;
}

.th {
  display: flex;
  align-items: center;
  padding: 0 15px;
}

.th-txt {
  font-size: 13px;
  color: #606266;
  font-weight: bold;
}

.table-body {
  display: flex;
  flex-direction: column;
}

.table-row {
  height: 60px;
  display: flex;
  flex-direction: row;
  align-items: center;
  border-bottom: 1px solid #f0f0f0;
}

.table-row:hover {
  background-color: #f9f9f9;
}

.td {
  display: flex;
  align-items: center;
  padding: 0 15px;
}

.td-txt {
  font-size: 13px;
  color: #606266;
}

.u-info {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  .u-account { font-size: 11px; color: #999; margin-top: 2px; }
}

.ellipsis {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* 列宽定义 */
.col-id { width: 60px; justify-content: center; }
.col-nick { width: 160px; }
.col-phone { width: 140px; }
.col-content { flex: 1; justify-content: flex-start; }
.col-status { width: 100px; justify-content: center; }
.col-time { width: 160px; justify-content: center; }
.col-op { width: 100px; justify-content: center; }

.status-tag {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 2px;
  &.processed { color: #52c41a; background: #f6ffed; border: 1px solid #b7eb8f; }
  &.pending { color: #fa8c16; background: #fff7e6; border: 1px solid #ffd591; }
}

.btn-link {
  font-size: 13px;
  color: #2d8cf0;
  cursor: pointer;
}
.btn-link.gray { color: #999; cursor: default; }

/* 分页栏 */
.pagination-bar {
  padding: 20px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-end;
}

.page-total {
  font-size: 13px;
  color: #606266;
  margin-right: 15px;
}

.page-nav {
  display: flex;
  flex-direction: row;
  align-items: center;
}

.nav-btn {
  width: 30px;
  height: 30px;
  border: 1px solid #dcdee2;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  margin: 0 4px;
  cursor: pointer;
}

.nav-item {
  width: 30px;
  height: 30px;
  border: 1px solid #dcdee2;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  margin: 0 4px;
}

.nav-item.active {
  background-color: #2d8cf0;
  border-color: #2d8cf0;
}

.nav-item.active .nav-num {
  color: #fff;
}

.nav-num, .nav-icon {
  font-size: 13px;
  color: #606266;
}

.disabled { cursor: not-allowed; opacity: 0.5; }
</style>
