<template>
  <view class="admin-page">
    <view class="filter-card">
      <view class="filter-row">
        <view class="filter-item">
          <text class="label">代理商查询：</text>
          <input class="filter-input" placeholder="请输入姓名、手机号或UID" />
        </view>
        <view class="filter-btns">
          <button class="btn primary" @click="onSearch">查询</button>
        </view>
      </view>
    </view>
    <view class="content-card">
      <view class="action-bar">
        <button class="btn primary small" @click="onAdd">添加代理商</button>
      </view>
      <view class="table-container">
        <view class="table-header">
          <view class="col col-uid"><text>用户UID</text></view>
          <view class="col col-avatar"><text>头像</text></view>
          <view class="col col-name"><text>名称</text></view>
          <view class="col col-ratio"><text>分佣比例</text></view>
          <view class="col col-count"><text>员工数量</text></view>
          <view class="col col-time"><text>过期时间</text></view>
          <view class="col col-status"><text>状态</text></view>
          <view class="col col-ops"><text>操作</text></view>
        </view>
        <view class="table-body">
          <view v-for="item in pagedList" :key="item.uid" class="table-row">
            <view class="col col-uid"><text>{{ item.uid }}</text></view>
            <view class="col col-avatar">
              <image class="avatar-img" src="/static/logo.png" mode="aspectFill" />
            </view>
            <view class="col col-name"><text>{{ item.name }}</text></view>
            <view class="col col-ratio"><text>{{ item.ratio }}%</text></view>
            <view class="col col-count"><text>{{ item.staffCount }}</text></view>
            <view class="col col-time"><text>{{ item.endTime }}</text></view>
            <view class="col col-status">
              <switch :checked="item.status" color="#1890ff" scale="0.8" />
            </view>
            <view class="col col-ops">
              <text class="op-link">编辑</text>
              <text class="op-divider">|</text>
              <text class="op-link">查看</text>
              <text class="op-divider">|</text>
              <text class="op-link">员工</text>
              <text class="op-divider">|</text>
              <text class="op-link">删除</text>
            </view>
          </view>
        </view>
      </view>
      <CommonPagination
        v-if="total > 0"
        :total="total"
        :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.value = val }"
        @jump-page="handleJumpPage"
      />
    </view>
  </view>
</template>
<script setup lang="uts">
import { ref, computed } from 'vue'
import CommonPagination from '@/components/CommonPagination/CommonPagination.uvue'

// ========== MOCK DATA START ==========
// TODO: 接真实接口时替换此处 agentList 为 fetchAgentList() 调用
const agentList = ref([
  { uid: '60569', name: 'cs2020', ratio: 50, staffCount: 1, endTime: '2026-01-01', status: true },
  { uid: '60570', name: '张伟', ratio: 45, staffCount: 3, endTime: '2026-06-30', status: true },
  { uid: '60571', name: '李华', ratio: 40, staffCount: 5, endTime: '2026-12-31', status: true },
  { uid: '60572', name: '王芳', ratio: 38, staffCount: 2, endTime: '2027-03-01', status: false },
  { uid: '60573', name: '赵磊', ratio: 42, staffCount: 7, endTime: '2026-09-30', status: true },
  { uid: '60574', name: '陈浩', ratio: 35, staffCount: 0, endTime: '2026-04-15', status: true },
  { uid: '60575', name: '刘娜', ratio: 48, staffCount: 4, endTime: '2026-08-01', status: true },
  { uid: '60576', name: '黄明', ratio: 33, staffCount: 6, endTime: '2027-01-31', status: false },
  { uid: '60577', name: '周静', ratio: 52, staffCount: 1, endTime: '2026-11-30', status: true },
  { uid: '60578', name: '吴强', ratio: 30, staffCount: 9, endTime: '2026-07-15', status: true },
  { uid: '60579', name: '郑丽', ratio: 55, staffCount: 2, endTime: '2026-03-31', status: true },
  { uid: '60580', name: '孙勇', ratio: 28, staffCount: 11, endTime: '2027-06-30', status: true },
  { uid: '60581', name: '朱婷', ratio: 46, staffCount: 3, endTime: '2026-10-31', status: false },
  { uid: '60582', name: '马林', ratio: 37, staffCount: 0, endTime: '2026-05-01', status: true },
  { uid: '60583', name: '胡倩', ratio: 41, staffCount: 8, endTime: '2026-02-28', status: true },
  { uid: '60584', name: '高峰', ratio: 36, staffCount: 4, endTime: '2027-09-30', status: true },
  { uid: '60585', name: '梁雪', ratio: 49, staffCount: 2, endTime: '2026-06-15', status: false },
  { uid: '60586', name: '邓超', ratio: 32, staffCount: 6, endTime: '2027-02-01', status: true },
  { uid: '60587', name: '彭宇', ratio: 44, staffCount: 5, endTime: '2026-08-30', status: true },
  { uid: '60588', name: '曹芸', ratio: 39, staffCount: 1, endTime: '2026-12-01', status: true },
])
// ========== MOCK DATA END ==========

// ========== PAGINATION STATE ==========
const currentPage = ref(1)
const pageSize = ref(15)
const jumpPageInput = ref('')
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 total = computed(() => agentList.value.length)
const totalPage = computed(() => Math.max(1, Math.ceil(total.value / pageSize.value)))
const pagedList = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value
  return agentList.value.slice(start, start + pageSize.value)
})
const visiblePages = computed((): number[] => {
  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.value)
  if (!isNaN(p) && p >= 1 && p <= totalPage.value) currentPage.value = p
}
// ========== END PAGINATION STATE ==========

function onSearch() { uni.showToast({ title: '查询中...', icon: 'none' }) }
function onAdd() { uni.showToast({ title: '添加中...', icon: 'none' }) }
</script>
<style scoped lang="scss">
.admin-page { padding: 0; }
.filter-card { background: #fff; padding: 24px; margin-bottom: 16px; border-radius: 4px; }
.filter-row { display: flex; flex-direction: row; align-items: center; gap: 24px; }
.label { font-size: 14px; color: #333; }
.filter-input { border: 1px solid #d9d9d9; height: 32px; width: 220px; padding: 0 12px; font-size: 14px; }
.btn { height: 32px; padding: 0 16px; font-size: 14px; border: 1px solid #d9d9d9; background: #fff; display: flex; align-items: center; justify-content: center; cursor: pointer; }
.btn.primary { background: #1890ff; border-color: #1890ff; color: #fff; }
.content-card { background: #fff; border-radius: 4px; }
.action-bar { padding: 16px 24px; }
.table-container { padding: 0 24px 24px; }
.table-header { display: flex; flex-direction: row; background: #f8faff; border-bottom: 1px solid #f0f0f0; padding: 12px 0; }
.table-row { display: flex; flex-direction: row; border-bottom: 1px solid #f0f0f0; padding: 12px 0; align-items: center; &:hover { background: #fafafa; } }
.col { padding: 0 8px; font-size: 14px; color: #333; display: flex; align-items: center; }
.col-uid { width: 80px; } .col-avatar { width: 80px; justify-content: center; } .col-name { width: 120px; } .col-ratio { width: 100px; justify-content: center; } .col-count { width: 100px; justify-content: center; } .col-time { width: 120px; justify-content: center; } .col-status { width: 80px; justify-content: center; } 
.col-ops { flex: 1; justify-content: flex-end; display: flex; flex-direction: row; }
.avatar-img { width: 32px; height: 32px; border-radius: 2px; }
.op-link { color: #1890ff; cursor: pointer; }
.op-divider { color: #e8e8e8; margin: 0 8px; }
</style>