<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>
            <view class="filter-select">
              <text class="select-text">全部</text>
              <text class="arrow">▼</text>
            </view>
          </view>
          <view class="filter-item">
            <text class="label">搜索：</text>
            <input class="filter-input input-lg" placeholder="请输入物流公司名称或者编码" />
          </view>
          <button class="btn primary" @click="onSearch">查询</button>
        </view>
      </view>

      <!-- 操作栏 -->
      <view class="action-bar">
        <button class="btn primary" @click="syncLogistics">同步物流公司</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-sort"><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></view>
              <view class="col col-code"><text>{{ item.code }}</text></view>
              <view class="col col-sort"><text>{{ item.sort }}</text></view>
              <view class="col col-status">
                <switch :checked="item.show" color="#1890ff" @change="onStatusChange(item)" />
              </view>
              <view class="col col-action">
                <text class="action-btn" @click="editItem(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 为 fetchLogisticsList() 调用
const dataList = ref([
  { id: 2, name: '顺丰速运', code: 'shunfeng', sort: 0, show: true },
  { id: 3, name: '圆通速递', code: 'yuantong', sort: 0, show: true },
  { id: 4, name: '中通快递', code: 'zhongtong', sort: 0, show: true },
  { id: 5, name: '申通快递', code: 'shentong', sort: 0, show: true },
  { id: 6, name: '百世快递', code: 'huitongkuaidi', sort: 0, show: true },
  { id: 7, name: '京东物流', code: 'jd', sort: 0, show: true },
  { id: 8, name: '极兔速递', code: 'jtexpress', sort: 0, show: true },
  { id: 9, name: '邮政快递包裹', code: 'youzhengguonei', sort: 0, show: true },
  { id: 10, name: '天天快递', code: 'tiantian', sort: 0, show: true },
  { id: 11, name: 'EMS', code: 'ems', sort: 0, show: true },
  { id: 12, name: '德邦物流', code: 'debangwuliu', sort: 0, show: true },
  { id: 13, name: '德邦快递', code: 'debangkuaidi', sort: 0, show: true },
  { id: 14, name: '众邮快递', code: 'zhongyouex', sort: 0, show: true },
  { id: 15, name: '安能快运', code: 'annengkuaiyun', sort: 0, show: true },
  { id: 16, name: '壹米滴答', code: 'yimidida', sort: 0, show: false },
  { id: 17, name: '宅急送', code: 'zhaijisong', sort: 0, show: true },
  { id: 18, name: '苏宁物流', code: 'suning', sort: 0, show: true },
  { id: 19, name: '中通快运', code: 'zhongtongkuaiyun', sort: 0, show: true },
  { id: 20, name: '芝麻开门', code: 'zhimakaimen', sort: 0, show: false },
  { id: 21, name: '优速快递', code: 'youshuwuliu', sort: 0, show: true }
])
// ========== 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 syncLogistics() {
  uni.showLoading({ title: '同步中...' })
  setTimeout(() => {
    uni.hideLoading()
    uni.showToast({ title: '同步成功' })
  }, 1000)
}

function onStatusChange(item: any) {
  item.show = !item.show
  uni.showToast({ title: '状态已更新', icon: 'none' })
}

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

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

.admin-card {
  background-color: #fff;
  border-radius: 4px;
  padding: 20px;
  margin-bottom: 20px;
}

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

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

.label {
  font-size: 14px;
  color: #333;
  margin-right: 8px;
  white-space: nowrap;
}

.filter-select {
  width: 200px;
  height: 32px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  padding: 0 10px;
}

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

.input-lg {
  width: 300px;
}

.btn {
  height: 32px;
  padding: 0 15px;
  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: 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-name { flex: 2; }
.col-code { flex: 2; }
.col-sort { width: 100px; }
.col-status { width: 120px; justify-content: center; }
.col-action { width: 100px; justify-content: center; }

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

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
