<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" placeholder="请输入语言Code" />
          </view>
          <button class="btn primary" @click="onSearch">搜索</button>
        </view>
      </view>

      <!-- 操作栏 -->
      <view class="action-bar header-actions">
        <button class="btn primary" @click="addRegion">添加语言地区</button>
      </view>

      <!-- 表格内容 -->
      <view class="admin-card content-card">
        <view class="table-container list-table">
          <view class="table-header">
            <view class="col col-id"><text>编号</text></view>
            <view class="col col-code"><text>浏览器语言识别码</text></view>
            <view class="col col-desc"><text>语言说明</text></view>
            <view class="col col-lang"><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-code"><text>{{ item.code }}</text></view>
              <view class="col col-desc"><text>{{ item.desc }}</text></view>
              <view class="col col-lang"><text>{{ item.lang || '暂无' }}</text></view>
              <view class="col col-action">
                <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>
        <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>
  </view>
</template>

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

// ========== MOCK DATA START ==========
// TODO: 接真实接口时替换此处 dataList 为 fetchRegionList() 调用
const dataList = ref([
  { id: 346, code: 'zh-HK', desc: '中文(繁体, 香港特别行政区)', lang: '' },
  { id: 344, code: 'zh-MO', desc: '中文(繁体, 澳门特别行政区)', lang: '' },
  { id: 343, code: 'zh-Hant', desc: '中文(繁体)', lang: '繁体中文(zh-Hant)' },
  { id: 342, code: 'zh', desc: '中文', lang: '' },
  { id: 340, code: 'vi', desc: '越南语', lang: '' },
  { id: 339, code: 'yo-NG', desc: '约鲁巴语(尼日利亚)', lang: '' },
  { id: 337, code: 'en-GB', desc: '英语(英国)', lang: '' },
  { id: 336, code: 'en-IN', desc: '英语(印度)', lang: '' },
  { id: 335, code: 'en-JM', desc: '英语(牙买加)', lang: '' },
  { id: 334, code: 'en-NZ', desc: '英语(新西兰)', lang: '' },
  { id: 333, code: 'en-AU', desc: '英语(澳大利亚)', lang: '' },
  { id: 332, code: 'en-CA', desc: '英语(加拿大)', lang: '' },
  { id: 331, code: 'fr-FR', desc: '法语(法国)', lang: '' },
  { id: 330, code: 'fr-CA', desc: '法语(加拿大)', lang: '' },
  { id: 329, code: 'de-DE', desc: '德语(德国)', lang: '' },
  { id: 328, code: 'de-AT', desc: '德语(奥地利)', lang: '' },
  { id: 327, code: 'ja-JP', desc: '日语(日本)', lang: '日本语(ja-JP)' },
  { id: 326, code: 'ko-KR', desc: '韩语(韩国)', lang: '' },
  { id: 325, code: 'es-ES', desc: '西班牙语(西班牙)', lang: '' }
])
// ========== 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 addRegion() {
  uni.showToast({ title: '添加语言地区', icon: 'none' })
}

function editItem(item: any) {
  uni.showToast({ title: '编辑: ' + item.code, 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;
}

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

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

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

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

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

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

.admin-card {
  background-color: #fff;
  border-radius: 4px;
  padding: 0;
  overflow: hidden;
}

.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: 16px;
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #333;
}

.col-id { width: 100px; }
.col-code { flex: 1; }
.col-desc { flex: 2; }
.col-lang { flex: 1; }
.col-action { width: 150px; justify-content: flex-end; display: flex; flex-direction: row; }

.action-btn {
  color: #1890ff;
  cursor: pointer;
}

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

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