<template>
  <view class="admin-page-container">
    <view class="page-card">
      <!-- 搜索栏 -->
      <view class="search-wrap">
        <view class="search-item">
          <text class="label">状态：</text>
          <picker mode="selector" :range="statusRange" @change="onStatusChange">
            <view class="picker-input">{{ statusText }}</view>
          </picker>
        </view>
        <view class="search-item">
          <text class="label">搜索：</text>
          <input class="input" placeholder="请输入姓名或者账号" v-model="searchKey" />
        </view>
        <button class="btn btn-primary" @click="onSearch">查询</button>
      </view>

      <view class="action-wrap">
        <button class="btn btn-primary" @click="onAdd">添加管理员</button>
      </view>

      <!-- 表格区域 -->
      <view class="table-wrap">
        <view class="table-header">
          <view class="th" style="flex: 2;">姓名</view>
          <view class="th" style="flex: 2;">账号</view>
          <view class="th" style="flex: 2;">身份</view>
          <view class="th" style="flex: 3;">最后一次登录时间</view>
          <view class="th" style="flex: 3;">最后一次登录ip</view>
          <view class="th" style="flex: 1;">开启</view>
          <view class="th" style="flex: 2;">操作</view>
        </view>
        <view class="table-body">
          <view v-for="item in pagedList" :key="item.id" class="tr">
            <view class="td" style="flex: 2;">{{ item.name }}</view>
            <view class="td" style="flex: 2;">{{ item.account }}</view>
            <view class="td" style="flex: 2;">{{ item.role }}</view>
            <view class="td" style="flex: 3;">{{ item.lastLoginTime }}</view>
            <view class="td" style="flex: 3;">{{ item.lastLoginIp }}</view>
            <view class="td" style="flex: 1;">
              <switch :checked="item.status" color="#1890ff" style="transform: scale(0.7);" />
            </view>
            <view class="td" style="flex: 2;">
              <text class="action-btn" @click="onEdit(item)">编辑</text>
              <text class="action-btn-del" @click="onDelete(item)">删除</text>
            </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>
  </view>
</template>

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

const searchKey = ref('')
const statusRange = ['所有', '启用', '禁用']
const statusIndex = ref(0)
const statusText = ref('请选择')

type AdminItem = {
  id: number
  name: string
  account: string
  role: string
  lastLoginTime: string
  lastLoginIp: string
  status: boolean
}

// ========== MOCK DATA START ==========
// TODO: 接真实接口时替换此处 adminList 为 fetchAdminList() 调用
const adminList = ref<AdminItem[]>([
  { id: 20, name: '张三', account: 'zhangsan', role: '超级管理员', lastLoginTime: '2025-06-28 10:30:00', lastLoginIp: '192.168.1.1', status: true },
  { id: 19, name: '李四', account: 'lisi', role: '运营管理员', lastLoginTime: '2025-06-27 09:15:00', lastLoginIp: '192.168.1.2', status: true },
  { id: 18, name: '王五', account: 'wangwu', role: '财务管理员', lastLoginTime: '2025-06-26 14:20:00', lastLoginIp: '10.0.0.5', status: true },
  { id: 17, name: '赵六', account: 'zhaoliu', role: '客服专员', lastLoginTime: '2025-06-25 11:00:00', lastLoginIp: '10.0.0.6', status: false },
  { id: 16, name: '陈七', account: 'chenqi', role: '商品管理员', lastLoginTime: '2025-06-24 16:45:00', lastLoginIp: '172.16.0.1', status: true },
  { id: 15, name: '周八', account: 'zhouba', role: '订单管理员', lastLoginTime: '2025-06-23 08:30:00', lastLoginIp: '172.16.0.2', status: true },
  { id: 14, name: '吴九', account: 'wujiu', role: '营销专员', lastLoginTime: '2025-06-22 13:00:00', lastLoginIp: '192.168.2.1', status: true },
  { id: 13, name: '郑十', account: 'zhengshi', role: '内容管理员', lastLoginTime: '2025-06-21 10:00:00', lastLoginIp: '192.168.2.2', status: false },
  { id: 12, name: '马一一', account: 'mayiyi', role: '数据分析师', lastLoginTime: '2025-06-20 15:20:00', lastLoginIp: '10.0.1.1', status: true },
  { id: 11, name: '刘二二', account: 'liuerer', role: '技术支持', lastLoginTime: '2025-06-19 09:40:00', lastLoginIp: '10.0.1.2', status: true },
  { id: 10, name: '张三三', account: 'zhangsansan', role: '分销管理员', lastLoginTime: '2025-06-18 11:30:00', lastLoginIp: '172.16.1.1', status: true },
  { id: 9, name: '李四四', account: 'lisisi', role: '仓库管理员', lastLoginTime: '2025-06-17 14:00:00', lastLoginIp: '172.16.1.2', status: false },
  { id: 8, name: '王五五', account: 'wangwuwu', role: '客服小组长', lastLoginTime: '2025-06-16 10:10:00', lastLoginIp: '192.168.3.1', status: true },
  { id: 7, name: '赵六六', account: 'zhaoliuliu', role: '运营小组长', lastLoginTime: '2025-06-15 09:00:00', lastLoginIp: '192.168.3.2', status: true },
  { id: 6, name: '陈七七', account: 'chenqiqi', role: '商品小组长', lastLoginTime: '2025-06-14 16:00:00', lastLoginIp: '10.0.2.1', status: true },
  { id: 5, name: '周八八', account: 'zhoubaba', role: '订单小组长', lastLoginTime: '2025-06-13 15:00:00', lastLoginIp: '10.0.2.2', status: false },
  { id: 4, name: '吴九九', account: 'wujiujiu', role: '营销小组长', lastLoginTime: '2025-06-12 11:00:00', lastLoginIp: '172.16.2.1', status: true },
  { id: 3, name: '马小明', account: 'maxiaoming', role: '普通管理员', lastLoginTime: '2025-06-11 10:20:00', lastLoginIp: '172.16.2.2', status: true },
  { id: 2, name: '李小花', account: 'lixiaohua', role: '普通管理员', lastLoginTime: '2025-06-10 08:50:00', lastLoginIp: '192.168.4.1', status: true },
  { id: 1, name: 'admin', account: 'admin', role: '超级管理员', lastLoginTime: '2025-06-29 22:00:00', lastLoginIp: '127.0.0.1', status: 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(() => adminList.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 adminList.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 onStatusChange(e: any) {
  statusIndex.value = parseInt(e.detail.value.toString())
  statusText.value = statusRange[statusIndex.value]
}

function onSearch() {
  console.log('Search:', searchKey.value, statusText.value)
}

function onAdd() {
  console.log('Add admin')
}

function onEdit(item: AdminItem) {
  console.log('Edit admin:', item.account)
}

function onDelete(item: AdminItem) {
  uni.showModal({
    title: '提示',
    content: '确定删除该管理员吗？',
    success: (res) => {
      if (res.confirm) {
        uni.showToast({ title: '已删除', icon: 'success' })
      }
    }
  })
}
</script>

<style scoped>
.admin-page-container {
  /* 使用 Layout 的背景和内边距 */
  padding: 0;
  background-color: transparent;
  min-height: auto;
}

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

.search-wrap {
  display: flex;
  flex-direction: row;
  align-items: center;
  flex-wrap: wrap;
  padding-bottom: 20px;
  border-bottom: 1px solid #f0f0f0;
  margin-bottom: 20px;
}

.search-item {
  display: flex;
  flex-direction: row;
  align-items: center;
  margin-right: 20px;
  margin-bottom: 10px;
}

.label {
  font-size: 14px;
  color: #606266;
  margin-right: 8px;
}

.picker-input {
  width: 150px;
  height: 32px;
  line-height: 32px;
  padding: 0 10px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  font-size: 14px;
  color: #606266;
  background-color: #fff;
}

.input {
  width: 220px;
  height: 32px;
  padding: 0 10px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  font-size: 14px;
  color: #606266;
}

.btn {
  height: 32px;
  line-height: 30px;
  padding: 0 15px;
  font-size: 14px;
  border-radius: 4px;
  border: none;
  margin-bottom: 10px;
}

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

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

.table-wrap {
  width: 100%;
  border: 1px solid #f0f0f0;
}

.table-header {
  display: flex;
  flex-direction: row;
  background-color: #f8f8f9;
}

.th {
  padding: 12px 10px;
  font-size: 14px;
  font-weight: bold;
  color: #515a6e;
  border-bottom: 1px solid #f0f0f0;
  text-align: center;
}

.table-body {
  min-height: 100px;
}

.tr {
  display: flex;
  flex-direction: row;
  align-items: center;
  border-bottom: 1px solid #f0f0f0;
  min-height: 54px;
}

.td {
  display: flex;
  flex-direction: row;
  padding: 10px;
  font-size: 13px;
  color: #515a6e;
  text-align: center;
}

.action-btn {
  font-size: 13px;
  color: #1890ff;
  margin-right: 8px;
  cursor: pointer;
}

.action-btn-del {
  font-size: 13px;
  color: #ed4014;
  cursor: pointer;
}
</style>
