<template>
  <view class="admin-page data-dictionary">
      <view class="admin-sections">
        <view class="admin-card">
          <!-- 搜索和添加 -->
          <view class="header-tools">
            <view class="search-form">
              <text class="label">字典名称：</text>
              <input class="search-input" v-model="searchQuery" placeholder="请输入字典名称" />
              <button class="btn-search" @click="handleSearch">查询</button>
            </view>
            <button class="btn-primary" @click="handleAdd">添加数据字典</button>
          </view>

          <!-- 数据表格 -->
          <view class="table-container">
            <view class="table-header">
              <view class="table-cell" style="flex: 0 0 60px;">ID</view>
              <view class="table-cell" style="flex: 2;">字典名称</view>
              <view class="table-cell" style="flex: 2;">数据标识</view>
              <view class="table-cell" style="flex: 1;">类型</view>
              <view class="table-cell" style="flex: 2;">添加时间</view>
              <view class="table-cell action-cell" style="flex: 2;">操作</view>
            </view>
            <view class="table-body">
              <view class="table-row" v-for="(item, index) in pagedList" :key="index">
                <view class="table-cell" style="flex: 0 0 60px;">{{ item.id }}</view>
                <view class="table-cell" style="flex: 2;">{{ item.name }}</view>
                <view class="table-cell" style="flex: 2;">{{ item.tag }}</view>
                <view class="table-cell" style="flex: 1;">{{ item.type }}</view>
                <view class="table-cell" style="flex: 2;">{{ item.add_time }}</view>
                <view class="table-cell action-cell" style="flex: 2;">
                  <text class="action-link" @click="handleEdit(item)">编辑</text>
                  <text class="action-link ml-10" @click="handleManage(item)">数据管理</text>
                  <text class="action-link link-danger ml-10" @click="handleDelete(item)">删除</text>
                </view>
              </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'

const searchQuery = ref('')
const dictionaryList = ref([
  { id: 8, name: '21', tag: '25425', type: '一级', add_time: '2024-08-23 17:37:08' },
  { id: 10, name: '相册', tag: 'xc', type: '多级', add_time: '2024-09-09 15:49:58' },
  { id: 11, name: '品类', tag: 'category', type: '多级', add_time: '2024-09-24 17:47:16' },
  { id: 12, name: '123', tag: '1', type: '一级', add_time: '2024-09-27 15:57:03' },
  { id: 13, name: '企业类型', tag: 'company_type', type: '多级', add_time: '2024-09-27 16:16:21' },
  { id: 14, name: '测试', tag: '343322222222234555343434', type: '多级', add_time: '2024-09-27 16:17:23' },
  { id: 16, name: '用户管理模块', tag: '123', type: '多级', add_time: '2024-10-22 09:30:46' },
  { id: 18, name: '类型', tag: '1', type: '多级', add_time: '2024-12-11 13:48:01' },
  { id: 19, name: '店铺', tag: 'shop', type: '多级', add_time: '2024-12-11 22:23:07' },
  { id: 20, name: 'ce', tag: 'zzz', type: '一级', add_time: '2024-12-14 12:19:17' },
  { id: 21, name: '语言', tag: 'language', type: '多级', add_time: '2025-01-05 10:00:00' },
  { id: 22, name: '地区', tag: 'region', type: '多级', add_time: '2025-01-10 11:30:00' },
  { id: 23, name: '货币类型', tag: 'currency', type: '一级', add_time: '2025-01-15 09:00:00' },
  { id: 24, name: '支付方式', tag: 'payment_type', type: '一级', add_time: '2025-01-20 14:00:00' },
  { id: 25, name: '订单状态', tag: 'order_status', type: '一级', add_time: '2025-01-25 10:30:00' },
  { id: 26, name: '物流公司', tag: 'express_company', type: '一级', add_time: '2025-02-01 08:00:00' },
  { id: 27, name: '售后类型', tag: 'after_sale_type', type: '多级', add_time: '2025-02-05 15:00:00' },
  { id: 28, name: '套餐类型', tag: 'package_type', type: '多级', add_time: '2025-02-10 09:30:00' },
  { id: 29, name: '签到规则', tag: 'sign_rule', type: '一级', add_time: '2025-02-15 11:00:00' }
])
// ========== 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(() => dictionaryList.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 dictionaryList.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 handleSearch() {
  uni.showToast({ title: '搜索: ' + searchQuery.value, icon: 'none' })
}

function handleAdd() {
  uni.showToast({ title: '添加数据字典', icon: 'none' })
}

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

function handleManage(item: any) {
  uni.showToast({ title: '数据管理: ' + item.name, icon: 'none' })
}

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

<style scoped>
.admin-page {
  /* 使用 Layout 的背景和内边距 */
  min-height: 100vh;
}

.admin-card {
  background-color: #fff;
  padding: 24px;
  border-radius: 8px;
}

.header-tools {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

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

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

.search-input {
  width: 200px;
  height: 32px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  padding: 0 12px;
  font-size: 14px;
  margin-right: 10px;
}

.btn-search {
  background-color: #1890ff;
  color: #fff;
  font-size: 14px;
  height: 32px;
  line-height: 32px;
  padding: 0 15px;
  border-radius: 4px;
  border: none;
}

.btn-primary {
  background-color: #1890ff;
  color: #fff;
  font-size: 14px;
  height: 32px;
  line-height: 32px;
  padding: 0 20px;
  border-radius: 4px;
  border: none;
}

.table-container {
  border: 1px solid #f0f0f0;
  border-radius: 4px;
}

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

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

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

.table-cell {
  font-size: 14px;
  color: #666;
  padding: 0 10px;
  display: flex;
  flex-direction: row;
}

.action-link {
  color: #1890ff;
  font-size: 13px;
  cursor: pointer;
}

.link-danger {
  color: #ff4d4f;
}

.ml-10 {
  margin-left: 10px;
}
</style>

