<template>
  <view class="admin-page">
    <view class="admin-sections">
      <!-- 操作栏 -->
      <view class="action-bar header-actions">
        <view class="left-btns">
          <button class="btn primary" @click="addProvince">添加省份</button>
          <button class="btn ghost ml-12" @click="clearCache">清除缓存</button>
        </view>
      </view>

      <!-- 表格内容 -->
      <view class="admin-card content-card">
        <view class="table-container list-table tree-table">
          <view class="table-header">
            <view class="col col-id"><text>编号</text></view>
            <view class="col col-name"><text>地区名称</text></view>
            <view class="col col-parent"><text>上级名称</text></view>
            <view class="col col-action"><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-name">
                <text class="expand-icon">˃</text>
                <text>{{ item.name }}</text>
              </view>
              <view class="col col-parent"><text>{{ item.parentName }}</text></view>
              <view class="col col-action">
                <text class="action-btn" @click="addItem(item)">添加</text>
                <text class="action-btn divider">|</text>
                <text class="action-btn" @click="editItem(item)">编辑</text>
                <text class="action-btn divider">|</text>
                <text class="action-btn danger" @click="deleteItem(item)">删除</text>
              </view>
            </view>
          </view>
        </view>
      </view>
      <CommonPagination
        v-if="true"
        :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: 接真实接口时替换此处 dataList 为 fetchCityList() 调用
const dataList = ref([
  { id: 1, name: '北京市', parentName: '中国' },
  { id: 2, name: '天津市', parentName: '中国' },
  { id: 3, name: '河北省', parentName: '中国' },
  { id: 4, name: '山西省', parentName: '中国' },
  { id: 5, name: '内蒙古自治区', parentName: '中国' },
  { id: 6, name: '辽宁省', parentName: '中国' },
  { id: 7, name: '吉林省', parentName: '中国' },
  { id: 8, name: '黑龙江省', parentName: '中国' },
  { id: 9, name: '上海市', parentName: '中国' },
  { id: 10, name: '江苏省', parentName: '中国' },
  { id: 11, name: '浙江省', parentName: '中国' },
  { id: 12, name: '安徽省', parentName: '中国' },
  { id: 13, name: '福建省', parentName: '中国' },
  { id: 14, name: '江西省', parentName: '中国' },
  { id: 15, name: '山东省', parentName: '中国' },
  { id: 16, name: '河南省', parentName: '中国' },
  { id: 17, name: '湖北省', parentName: '中国' },
  { id: 18, name: '湖南省', parentName: '中国' },
  { id: 19, name: '广东省', parentName: '中国' },
  { id: 20, name: '广西壮族自治区', parentName: '中国' },
  { id: 21, name: '海南省', parentName: '中国' },
  { id: 22, name: '重庆市', parentName: '中国' },
  { id: 23, name: '四川省', parentName: '中国' },
  { id: 24, name: '贵州省', parentName: '中国' },
  { id: 25, name: '云南省', parentName: '中国' },
  { id: 26, name: '西藏自治区', parentName: '中国' },
  { id: 27, name: '陕西省', parentName: '中国' },
  { id: 28, name: '甘肃省', parentName: '中国' },
  { id: 29, name: '青海省', parentName: '中国' },
  { id: 30, name: '宁夏回族自治区', parentName: '中国' },
  { id: 31, name: '新疆维吾尔自治区', parentName: '中国' },
  { id: 32, name: '香港特别行政区', parentName: '中国' },
  { id: 33, name: '澳门特别行政区', parentName: '中国' },
  { id: 34, name: '台湾省', parentName: '中国' }
])
// ========== 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 addProvince() {
  uni.showToast({ title: '添加省份', icon: 'none' })
}

function clearCache() {
  uni.showLoading({ title: '清理中...' })
  setTimeout(() => {
    uni.hideLoading()
    uni.showToast({ title: '清理成功' })
  }, 1000)
}

function addItem(item: any) {
  uni.showToast({ title: '添加下级: ' + item.name, icon: 'none' })
}

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

function deleteItem(item: any) {
  uni.showModal({
    title: '提示',
    content: '确定要删除该区域吗？',
    success: (res) => {
      if (res.confirm) {
        uni.showToast({ title: '删除成功' })
      }
    }
  })
}
</script>

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

.header-actions {
  margin-bottom: 20px;
}

.left-btns {
  display: flex;
  flex-direction: row;
}

.ml-12 {
  margin-left: 12px;
}

.admin-card {
  background-color: #fff;
  border-radius: 4px;
  padding: 0; /* 表格一般不带卡片内边距 */
  margin-bottom: 20px;
  overflow: hidden;
}

.btn {
  height: 32px;
  padding: 0 15px;
  border-radius: 4px;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

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

.btn.ghost {
  background-color: #fff;
  border: 1px solid #d9d9d9;
  color: #666;
}

.table-container {
  width: 100%;
}

.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: 100px; }
.col-name { flex: 2; }
.col-parent { flex: 1; }
.col-action { width: 200px; justify-content: flex-end; display: flex; flex-direction: row; }

.expand-icon {
  margin-right: 8px;
  color: #999;
  font-size: 12px;
}

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

.action-btn.danger {
  color: #ff4d4f;
}

.action-btn.divider {
  margin: 0 8px;
  color: #e8e8e8;
  cursor: default;
}
</style>
