<template>
  <view class="admin-page">
    <view class="admin-sections">
      <!-- 搜索栏 -->
      <view class="admin-card filter-card">
        <view class="filter-row">
          <view class="filter-item">
            <text class="label">数据搜索：</text>
            <input class="filter-input input-large" placeholder="请输入ID,KEY,数据组名称,简介" />
          </view>
          <button class="btn primary" @click="onSearch">查询</button>
        </view>
      </view>

        <!-- 内容区 -->
        <view class="admin-card 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-id"><text>ID</text></view>
            <view class="col col-key"><text>KEY</text></view>
            <view class="col col-name"><text>数据组名称</text></view>
            <view class="col col-desc"><text>简介</text></view>
            <view class="col col-ops"><text>操作</text></view>
          </view>

          <view class="table-body">
            <view v-for="item in pagedList" :key="item.id" class="table-row">
              <view class="col col-id"><text>{{ item.id }}</text></view>
              <view class="col col-key"><text>{{ item.key }}</text></view>
              <view class="col col-name"><text>{{ item.name }}</text></view>
              <view class="col col-desc"><text>{{ item.desc }}</text></view>
              <view class="col col-ops">
                <text class="op-link" @click="onDataList(item)">数据列表</text>
                <view class="op-divider">|</view>
                <text class="op-link" @click="onEdit(item)">编辑</text>
                <view class="op-divider">|</view>
                <text class="op-link op-danger" @click="onDelete(item)">删除</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>
  </view>
</template>

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

const dataList = ref([
  { id: 49, key: 'routine_seckill_time', name: '秒杀时间段', desc: '秒杀时间段' },
  { id: 52, key: 'routine_home_bast_banner', name: '首页精品推荐图片', desc: '首页精品推荐图片' },
  { id: 53, key: 'order_details_images', name: '订单详情状态图', desc: '订单详情状态图' },
  { id: 54, key: 'routine_my_menus', name: '个人中心菜单', desc: '个人中心菜单' },
  { id: 55, key: 'sign_day_num', name: '签到天数配置', desc: '签到天数配置' },
  { id: 57, key: 'routine_home_hot_banner', name: '热门榜单推荐图片', desc: '热门榜单推荐图片' },
  { id: 58, key: 'routine_home_new_banner', name: '首发新品推荐图片', desc: '首发新品推荐图片' },
  { id: 59, key: 'routine_home_benefit_banner', name: '促销单品推荐图片', desc: '促销单品推荐图片' },
  { id: 62, key: 'user_recharge_quota', name: '充值金额设置', desc: '设置充值金额额度选择' },
  { id: 65, key: 'admin_login_slide', name: '后台登录页面幻灯片', desc: '后台登录页面幻灯片' },
  { id: 66, key: 'uni_app_link', name: '前端页面链接', desc: '前端页面链接' },
  { id: 67, key: 'combination_banner', name: '拼团列表轮播图', desc: '拼团列表轮播图' },
  { id: 68, key: 'integral_shop_banner', name: '积分商城轮播图', desc: '积分商城轮播图' },
  { id: 70, key: 'live_share_img', name: '直播分享图片', desc: '直播分享图片' },
  { id: 71, key: 'live_home_banner', name: '直播首页轮播图', desc: '直播首页轮播图' },
  { id: 72, key: 'user_welfare', name: '会员权益配置', desc: '会员权益配置' },
  { id: 73, key: 'presale_banner', name: '预售轮播图配置', desc: '预售轮播图配置' },
  { id: 74, key: 'share_image', name: '分享海报图片', desc: '分享海报图片' },
  { id: 75, key: 'coupon_banner', name: '优惠券列表轮播图', desc: '优惠券列表轮播图' },
  { id: 76, key: 'vip_privilege', name: 'VIP特权配置', desc: 'VIP特权配置' }
])
// ========== 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(() => dataList.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 dataList.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' })
}

function onDataList(item: any) {
  uni.showToast({ title: '数据列表: ' + item.name, icon: 'none' })
}

function onEdit(item: any) {
  uni.showToast({ title: '编辑: ' + item.name, icon: 'none' })
}

function onDelete(item: any) {
  uni.showModal({
    title: '提示',
    content: '确定要删除该数据组吗？',
    success: (res) => {
      if (res.confirm) {
        uni.showToast({ title: '已删除', icon: 'none' })
      }
    }
  })
}
</script>

<style scoped lang="scss">
.admin-page {
  /* 使用 Layout 的背景和内边距 */
  padding: 0;
  background-color: transparent;
  min-height: auto;
}

.admin-card {
  background-color: #fff;
  border-radius: 4px;
  padding: var(--admin-card-padding);
  margin-bottom: var(--admin-section-gap);
}

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

.filter-item {
  display: flex;
  flex-direction: row;
  align-items: center;
  margin-right: 24px;
}

.label {
  font-size: 14px;
  color: #333;
}

.filter-input {
  width: 200px;
  height: 32px;
  border: 1px solid #d9d9d9;
  border-radius: 2px;
  padding: 0 10px;
  font-size: 14px;
}

.input-large {
  width: 300px;
}

.btn {
  height: 32px;
  padding: 0 20px;
  border-radius: 2px;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border: 1px solid #d9d9d9;
  background-color: #fff;
}

.btn.primary {
  background-color: #1890ff;
  border-color: #1890ff;
  color: #fff;
}

.btn.small {
  height: 28px;
  padding: 0 10px;
  font-size: 12px;
}

.action-bar {
  margin-bottom: 16px;
}

.table-container {
  width: 100%;
  border: 1px solid #f0f0f0;
}

.table-header {
  display: flex;
  flex-direction: row;
  background-color: #fafafa;
  border-bottom: 1px solid #f0f0f0;
}

.table-row {
  display: flex;
  flex-direction: row;
  border-bottom: 1px solid #f0f0f0;
}

.col {
  padding: 12px 16px;
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #333;
}

.col-id { width: 80px; }
.col-key { flex: 2; }
.col-name { flex: 2; }
.col-desc { flex: 2; }
.col-ops { flex: 2; justify-content: flex-end; flex-direction: row;}

.op-link {
  color: #1890ff;
  cursor: pointer;
  margin: 0 4px;
}

.op-danger {
  color: #ff4d4f;
}

.op-divider {
  color: #f0f0f0;
  margin: 0 4px;
}
</style>


