<template>
  <view class="admin-kefu-list">
    <!-- 顶部操作栏 -->
    <view class="page-top-bar">
      <view class="btn-primary" @click="handleAdd">
        <text class="btn-txt">添加客服</text>
      </view>
    </view>
    
    <!-- 数据表格 -->
    <view class="table-container border-shadow">
      <view class="table-header">
        <view class="th col-id"><text class="th-txt">ID</text></view>
        <view class="th col-avatar"><text class="th-txt">客服头像</text></view>
        <view class="th col-name"><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 class="table-row" v-for="item in kefuList" :key="item.id">
          <view class="td col-id"><text class="td-txt">{{ item.id }}</text></view>
          <view class="td col-avatar">
            <image class="kefu-avatar" :src="item.avatar" mode="aspectFill"></image>
          </view>
          <view class="td col-name"><text class="td-txt">{{ item.name }}</text></view>
          <view class="td col-status">
            <StatusSwitch v-model="item.status" />
          </view>
          <view class="td col-time"><text class="td-txt">{{ item.time }}</text></view>
          <view class="td col-op">
            <text class="btn-action">编辑</text>
            <view class="divider"></view>
            <text class="btn-action">删除</text>
            <view class="divider"></view>
            <text class="btn-action">进入工作台</text>
          </view>
        </view>
      </view>

      <!-- 分页栏 -->
      <CommonPagination
        v-if="kefuList.length > 0"
        :total="kefuList.length"
        :loading="false"
        :currentPage="currentPage"
        :pageSize="pageSize"
        :pageSizeOptionLabels="pageSizeOptionLabels"
        :pageSizeIndex="pageSizeIndex"
        :visiblePages="visiblePages"
        :totalPage="totalPage"
        :jumpPageInput="jumpPageInput"
        @page-size-change="handlePageSizeChange"
        @page-change="handlePageChange"
        @update:jumpPageInput="(val : string) => { jumpPageInput = val }"
        @jump-page="handleJumpPage"
      />
    </view>
  </view>
</template>

<script setup lang="uts">
import { ref, computed } from 'vue'
import StatusSwitch from '@/components/StatusSwitch.uvue'
import CommonPagination from '@/components/CommonPagination/CommonPagination.uvue'

interface KefuItem {
  id: string
  avatar: string
  name: string
  status: boolean
  time: string
}

const kefuList = ref<KefuItem[]>([
  { id: '167', avatar: '/static/logo.png', name: '客服', status: true, time: '2025-12-31 11:24:35' },
  { id: '166', avatar: '/static/logo.png', name: '测试', status: true, time: '2025-11-18 17:33:26' },
  { id: '165', avatar: '/static/logo.png', name: 'zhicheng', status: true, time: '2025-09-10 10:13:00' },
  { id: '164', avatar: '/static/logo.png', name: 'kefu', status: true, time: '2025-07-29 11:50:20' },
  { id: '162', avatar: '/static/logo.png', name: '呐呐', status: true, time: '2024-12-09 10:51:11' },
  { id: '153', avatar: '/static/logo.png', name: '小天', status: true, time: '2024-09-19 16:04:59' },
  { id: '152', avatar: '/static/logo.png', name: 'kk', status: true, time: '2024-09-16 00:05:22' }
])

const handleAdd = () => {
  console.log('Add Kefu')
}

// 分页适配状态
const currentPage = ref(1)
const pageSize = ref(15)
let jumpPageInput = ''
const pageSizeOptions = [10, 15, 20, 30, 50]
const pageSizeOptionLabels = computed(() => pageSizeOptions.map((n: number) => `${n}条/页`))
const pageSizeIndex = computed(() => {
  const idx = pageSizeOptions.indexOf(pageSize.value)
  return idx >= 0 ? idx : 0
})
const totalPage = computed(() => Math.max(1, Math.ceil(kefuList.value.length / pageSize.value)))
const visiblePages = computed(() => {
  const t = totalPage.value
  const cur = currentPage.value
  if (t <= 7) return Array.from({ length: t }, (_: any, i: number) => i + 1)
  if (cur <= 4) return [1, 2, 3, 4, 5, -1, t]
  if (cur >= t - 3) return [1, -1, t - 4, t - 3, t - 2, t - 1, t]
  return [1, -1, cur - 1, cur, cur + 1, -1, t]
})
const handlePageChange = (p: number) => { currentPage.value = p }
const handlePageSizeChange = (e: any) => {
  const idx = Number(e.detail.value)
  pageSize.value = pageSizeOptions[idx] ?? pageSizeOptions[0]
  currentPage.value = 1
}
const handleJumpPage = () => {
  const p = parseInt(jumpPageInput)
  if (!isNaN(p) && p >= 1 && p <= totalPage.value) currentPage.value = p
}
</script>

<style scoped lang="scss">
.admin-kefu-list {
  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);
}

.page-top-bar {
  margin-bottom: 20px;
}

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

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

/* 表格样式 */
.table-container {
  display: flex;
  flex-direction: column;
}

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

.th {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 10px;
}

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

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

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

.td {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 10px;
}

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

/* 列宽百分比对齐截图 */
.col-id { width: 80px; }
.col-avatar { width: 120px; }
.col-name { flex: 1; justify-content: flex-start; }
.col-status { width: 150px; }
.col-time { width: 220px; }
.col-op { 
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  width: 250px; 
}

.kefu-avatar {
  width: 36px;
  height: 36px;
  border-radius: 4px;
  background-color: #f0f0f0;
}

/* 操作按钮 */
.btn-action {
  font-size: 13px;
  color: #2d8cf0;
  cursor: pointer;
}

.divider {
  width: 1px;
  height: 12px;
  background-color: #e8eaec;
  margin: 0 12px;
}

/* 分页区域已迁至 CommonPagination 组件 */
</style>
