<template>
  <view class="admin-page-container">
    <view class="page-card">
      <!-- 搜索栏 -->
      <view class="search-wrap">
        <view class="search-item">
          <text class="label">状态：</text>
          <uni-data-select v-model="statusValue" :localdata="statusOptions" style="width: 120px;" @change="handleQuery" />
        </view>
        <view class="search-item">
          <text class="label">搜索：</text>
          <input class="input" placeholder="请输入姓名或者账号" v-model="searchKey" @confirm="handleQuery" />
        </view>
        <button class="btn btn-primary" @click="handleQuery">查询</button>
      </view>

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

      <!-- 表格区域 -->
      <view class="table-wrap border-shadow">
        <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-if="loading" class="loading-box">
            <text>加载中...</text>
          </view>
          <view v-else-if="adminList.length === 0" class="no-data">
            <text class="no-data-text">暂无管理员数据</text>
          </view>
          <view v-else v-for="item in adminList" :key="item.id" class="tr">
            <view class="td" style="flex: 2;"><text class="td-txt">{{ item.real_name || '-' }}</text></view>
            <view class="td" style="flex: 2;"><text class="td-txt">{{ item.username }}</text></view>
            <view class="td" style="flex: 2;">
              <view class="role-tags">
                <text v-for="role in (item.roles || [])" :key="role" class="role-tag">{{ role }}</text>
                <text v-if="!item.roles || item.roles.length === 0" class="td-txt">-</text>
              </view>
            </view>
            <view class="td" style="flex: 3;"><text class="td-txt-small">{{ formatTime(item.last_login_at) }}</text></view>
            <view class="td" style="flex: 3;"><text class="td-txt-small">{{ item.last_login_ip || '-' }}</text></view>
            <view class="td" style="flex: 1;">
              <switch :checked="item.is_active" color="#1890ff" scale="0.7" disabled />
            </view>
            <view class="td" style="flex: 2;">
              <text class="action-btn" @click="onEdit(item)">编辑</text>
              <view class="divider"></view>
              <text class="action-btn danger">删除</text>
            </view>
          </view>
        </view>
      </view>

      <!-- 分页栏 -->
      <view class="pagination-footer">
        <text class="total-txt">共 {{ total }} 条</text>
        <view class="page-btns">
          <text :class="['p-btn', page <= 1 ? 'disabled' : '']" @click="prevPage"> < </text>
          <text class="p-btn active">{{ page }}</text>
          <text :class="['p-btn', adminList.length < pageSize ? 'disabled' : '']" @click="nextPage"> > </text>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="uts">
import { ref, onMounted } from 'vue'
import { fetchAdminPage, type AdminUser } from '@/services/admin/authService.uts'

const adminList = ref<AdminUser[]>([])
const loading = ref(false)
const total = ref(0)
const page = ref(1)
const pageSize = 15
const searchKey = ref('')
const statusValue = ref('all')

const statusOptions = [
  { value: 'all', text: '所有' },
  { value: '1', text: '启用' },
  { value: '0', text: '禁用' }
]

onMounted(() => {
  loadData()
})

async function loadData() {
  loading.value = true
  try {
    const status = statusValue.value === 'all' ? null : parseInt(statusValue.value)
    const res = await fetchAdminPage(page.value, pageSize, searchKey.value || null, status)
    adminList.value = res.items
    total.value = res.total
  } catch (e) {
    uni.showToast({ title: '加载失败', icon: 'none' })
  } finally {
    loading.value = false
  }
}

function handleQuery() {
  page.value = 1
  loadData()
}

function onAdd() {
  uni.showToast({ title: '添加管理员功能开发中', icon: 'none' })
}

function onEdit(item : AdminUser) {
  console.log('Edit admin:', item.id)
  uni.showToast({ title: '编辑功能开发中', icon: 'none' })
}

function prevPage() { if (page.value > 1) { page.value--; loadData(); } }
function nextPage() { if (adminList.value.length >= pageSize) { page.value++; loadData(); } }

function formatTime(iso : string | null) : string {
  if (!iso) return '-'
  return iso.substring(0, 16).replace('T', ' ')
}
</script>

<style scoped lang="scss">
.admin-page-container { padding: 24px; background-color: #f5f7f9; min-height: 100vh; }
.page-card { background-color: #fff; border-radius: 4px; padding: 24px; }

.search-wrap { display: flex; flex-direction: row; align-items: center; padding-bottom: 24px; border-bottom: 1px solid #f0f0f0; margin-bottom: 24px; }
.search-item { display: flex; flex-direction: row; align-items: center; margin-right: 24px; }
.label { font-size: 14px; color: #606266; margin-right: 8px; }
.input { width: 200px; height: 32px; border: 1px solid #dcdfe6; border-radius: 4px; padding: 0 12px; font-size: 14px; }

.btn { height: 32px; padding: 0 20px; border-radius: 4px; border: none; cursor: pointer; display: flex; align-items: center; }
.btn-primary { background-color: #1890ff; color: #fff; font-size: 14px; }

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

.table-wrap { border: 1px solid #f0f0f0; border-radius: 4px; }
.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; }
.tr { display: flex; flex-direction: row; border-bottom: 1px solid #f0f0f0; min-height: 54px; align-items: center; }
.td { padding: 10px; display: flex; align-items: center; justify-content: center; }
.td-txt { font-size: 13px; color: #606266; }
.td-txt-small { font-size: 12px; color: #999; }

.role-tags { display: flex; flex-direction: row; flex-wrap: wrap; gap: 4px; justify-content: center; }
.role-tag { background-color: #e6f7ff; color: #1890ff; border: 1px solid #91d5ff; padding: 1px 6px; border-radius: 2px; font-size: 11px; }

.action-btn { color: #1890ff; font-size: 13px; cursor: pointer; }
.danger { color: #ff4d4f; }
.divider { width: 1px; height: 12px; background-color: #e8e8e8; margin: 0 8px; }

.no-data { padding: 60px 0; text-align: center; }
.no-data-text { font-size: 14px; color: #c5c8ce; }
.loading-box { padding: 60px 0; text-align: center; color: #1890ff; }

.pagination-footer { margin-top: 24px; display: flex; flex-direction: row; align-items: center; justify-content: flex-end; gap: 12px; }
.total-txt { font-size: 13px; color: #999; }
.page-btns { display: flex; flex-direction: row; gap: 8px; }
.p-btn { width: 30px; height: 30px; border: 1px solid #dcdee2; display: flex; align-items: center; justify-content: center; border-radius: 4px; cursor: pointer; font-size: 13px; }
.p-btn.active { background-color: #1890ff; color: #fff; border-color: #1890ff; }
.p-btn.disabled { opacity: 0.5; cursor: not-allowed; }
</style>
