<template>
  <view class="admin-page">
    <view class="admin-sections">
      <!-- 操作栏 -->
      <view class="action-bar header-actions">
        <button class="btn primary" @click="addLanguage">添加语言</button>
      </view>

      <!-- 表格内容 -->
      <view class="admin-card content-card">
        <view class="table-container list-table">
          <view class="table-header">
            <view class="col col-id"><text>ID</text></view>
            <view class="col col-name"><text>语言名称</text></view>
            <view class="col col-code"><text>浏览器语言识别码</text></view>
            <view class="col col-status"><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>{{ item.name }}</text>
                <text v-if="item.isDefault" class="default-badge">默认</text>
              </view>
              <view class="col col-code"><text>{{ item.code }}</text></view>
              <view class="col col-status">
                <view :class="['status-tag', item.status ? 'active' : 'inactive']" @click="toggleStatus(item)">
                  <text class="tag-text">{{ item.status ? '开启' : '关闭' }}</text>
                  <view class="tag-dot"></view>
                </view>
              </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 为 fetchLanguageList() 调用
const dataList = ref([
  { id: 1, name: '中文', code: 'zh-CN', status: true, isDefault: true },
  { id: 2, name: 'English', code: 'en-US', status: true, isDefault: false },
  { id: 3, name: '繁體中文', code: 'zh-Hant', status: true, isDefault: false },
  { id: 4, name: 'Français', code: 'fr-FR', status: true, isDefault: false },
  { id: 5, name: 'Italiano', code: 'it-IT', status: true, isDefault: false },
  { id: 6, name: '日本語', code: 'ja-JP', status: true, isDefault: false },
  { id: 7, name: '한국어', code: 'ko-KR', status: true, isDefault: false },
  { id: 8, name: 'Монгол', code: 'mn-MN', status: true, isDefault: false },
  { id: 9, name: 'ภาษาไทย', code: 'th-TH', status: true, isDefault: false },
  { id: 10, name: 'ViệtName', code: 'vi-VN', status: true, isDefault: false },
  { id: 11, name: '한국어', code: 'ko-KR', status: true, isDefault: false },
  { id: 12, name: 'Deutsch', code: 'de-DE', status: false, isDefault: false },
  { id: 13, name: 'Español', code: 'es-ES', status: false, isDefault: false },
  { id: 14, name: 'Português', code: 'pt-PT', status: false, isDefault: false },
  { id: 15, name: 'русский', code: 'ru-RU', status: false, isDefault: false },
  { id: 16, name: 'Italiano', code: 'it-IT', status: true, isDefault: false },
  { id: 17, name: 'Bahasa Indonesia', code: 'id-ID', status: false, isDefault: false },
  { id: 18, name: 'Bahasa Melayu', code: 'ms-MY', status: false, isDefault: false },
  { id: 19, name: 'Türkçe', code: 'tr-TR', status: false, isDefault: false },
  { id: 20, name: 'Nederlands', code: 'nl-NL', status: false, isDefault: false }
])
// ========== 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 addLanguage() {
  uni.showToast({ title: '添加语言', icon: 'none' })
}

function toggleStatus(item: any) {
  item.status = !item.status
}

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;
}

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

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

.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;
}

.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: 80px; }
.col-name { flex: 1; position: relative; }
.col-code { flex: 1; }
.col-status { width: 120px; justify-content: center; }
.col-action { width: 150px; justify-content: flex-end;display: flex; flex-direction: row; }

.default-badge {
  background-color: #e6f7ff;
  color: #1890ff;
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 4px;
  margin-left: 8px;
  border: 1px solid #91d5ff;
}

.status-tag {
  width: 60px;
  height: 24px;
  border-radius: 12px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  padding: 0 8px;
  cursor: pointer;
}

.status-tag.active {
  background-color: #1890ff;
  color: #fff;
}

.status-tag.inactive {
  background-color: #d9d9d9;
  color: #fff;
}

.tag-text {
  font-size: 12px;
}

.tag-dot {
  width: 8px;
  height: 8px;
  background-color: #fff;
  border-radius: 50%;
}

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

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

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

.pagination {
  padding: 16px 24px;
  display: flex;
  justify-content: flex-end;
}

.page-info {
  font-size: 14px;
  color: #999;
}
</style>
