<template>
  <view class="admin-page ticket-config">
      <view class="admin-sections">
        <view class="admin-card">
          <!-- 搜索和添加 -->
          <view class="header-tools">
            <view class="search-form">
              <view class="form-item">
                <text class="label">打印机名称：</text>
                <input class="search-input" v-model="searchQuery.name" placeholder="请输入打印机名称" />
              </view>
              <view class="form-item ml-20">
                <text class="label">平台选择：</text>
                <picker :range="platforms" @change="onPlatformChange">
                  <view class="picker-view">{{ platforms[selectedPlatform] }}</view>
                </picker>
              </view>
              <button class="btn-search ml-20" @click="handleSearch">查询</button>
            </view>
            <button class="btn-primary" @click="handleAdd">添加打印机</button>
          </view>

          <!-- 数据表格 -->
          <view class="table-container">
            <view class="table-header">
              <view class="table-cell" style="flex: 0 0 60px;">ID</view>
              <view class="table-cell" style="flex: 2;">打印机名称</view>
              <view class="table-cell" style="flex: 1.5;">平台</view>
              <view class="table-cell" style="flex: 1.5;">应用账号</view>
              <view class="table-cell" style="flex: 1;">打印联数</view>
              <view class="table-cell" style="flex: 2;">创建时间</view>
              <view class="table-cell" style="flex: 1;">打印开关</view>
              <view class="table-cell action-cell" style="flex: 2;">操作</view>
            </view>
            <view class="table-body">
              <view class="table-row" v-for="(item, index) in pagedList" :key="index">
                <view class="table-cell" style="flex: 0 0 60px;">{{ item.id }}</view>
                <view class="table-cell" style="flex: 2;">{{ item.name }}</view>
                <view class="table-cell" style="flex: 1.5;">{{ item.platform }}</view>
                <view class="table-cell" style="flex: 1.5;">{{ item.account }}</view>
                <view class="table-cell" style="flex: 1;">{{ item.copies }}</view>
                <view class="table-cell" style="flex: 2;">{{ item.add_time }}</view>
                <view class="table-cell" style="flex: 1;">
                  <switch color="#1890ff" :checked="item.status" style="transform: scale(0.7);" />
                </view>
                <view class="table-cell action-cell" style="flex: 2;">
                  <text class="action-link" @click="handleDesign(item)">设计</text>
                  <text class="action-link ml-10" @click="handleEdit(item)">编辑</text>
                  <text class="action-link link-danger ml-10" @click="handleDelete(item)">删除</text>
                </view>
              </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 searchQuery = ref({ name: '' })
const platforms = ['全部', '易联云', '飞鹅打印机']
const selectedPlatform = ref(0)

// ========== MOCK DATA START ==========
// TODO: 接真实接口时替换此处 printerList 为 fetchPrinterList() 调用
const printerList = ref([
  { id: 20, name: '总部底层打印机', platform: '易联云', account: 'hq_admin', copies: 2, add_time: '2025-02-01 10:00:00', status: true },
  { id: 19, name: '广州仓库打印机', platform: '飞鹅打印机', account: 'gz_wh01', copies: 1, add_time: '2025-02-03 11:00:00', status: true },
  { id: 18, name: '上海分拨打印机', platform: '易联云', account: 'sh_fp01', copies: 1, add_time: '2025-02-05 09:30:00', status: false },
  { id: 17, name: '北京主打印机', platform: '易联云', account: 'bj_main', copies: 2, add_time: '2025-02-08 14:00:00', status: true },
  { id: 16, name: '北京备打印机', platform: '飞鹅打印机', account: 'bj_bak01', copies: 1, add_time: '2025-02-10 08:30:00', status: true },
  { id: 15, name: '化妙打印机', platform: '易联云', account: 'hm_pr01', copies: 3, add_time: '2025-02-12 10:20:00', status: true },
  { id: 14, name: '客服部打印机', platform: '飞鹅打印机', account: 'cs_pr01', copies: 1, add_time: '2025-02-15 15:00:00', status: true },
  { id: 13, name: '采购部打印机', platform: '易联云', account: 'cg_pr01', copies: 1, add_time: '2025-02-18 12:00:00', status: false },
  { id: 12, name: '财务部打印机', platform: '飞鹅打印机', account: 'cw_pr01', copies: 2, add_time: '2025-02-20 09:00:00', status: true },
  { id: 11, name: '运营部打印机', platform: '易联云', account: 'yy_pr01', copies: 1, add_time: '2025-02-22 11:30:00', status: true },
  { id: 10, name: '成都网点打印机', platform: '飞鹅打印机', account: 'cd_np01', copies: 1, add_time: '2025-02-25 10:00:00', status: true },
  { id: 9, name: '武汉网点打印机', platform: '易联云', account: 'wh_np01', copies: 2, add_time: '2025-03-01 14:00:00', status: false },
  { id: 8, name: '深圳网点打印机', platform: '飞鹅打印机', account: 'sz_np01', copies: 1, add_time: '2025-03-05 09:00:00', status: true },
  { id: 7, name: '杭州网点打印机', platform: '易联云', account: 'hz_np01', copies: 1, add_time: '2025-03-08 11:00:00', status: true },
  { id: 6, name: '南京网点打印机', platform: '飞鹅打印机', account: 'nj_np01', copies: 2, add_time: '2025-03-10 12:00:00', status: true },
  { id: 5, name: '苏州网点打印机', platform: '易联云', account: 'sz2_np01', copies: 1, add_time: '2025-03-12 15:00:00', status: false },
  { id: 4, name: '小类目打印机', platform: '飞鹅打印机', account: 'xl_pr01', copies: 1, add_time: '2025-03-15 10:30:00', status: true },
  { id: 3, name: '大档打印机', platform: '易联云', account: 'dd_pr01', copies: 3, add_time: '2025-03-18 09:00:00', status: true },
  { id: 2, name: '二号打印', platform: '飞鹅打印机', account: '333', copies: 1, add_time: '2025-01-01 12:00:00', status: false },
  { id: 1, name: '一号打印', platform: '易联云', account: '222', copies: 1, add_time: '2024-12-11 15:12:58', 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(() => printerList.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 printerList.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 onPlatformChange(e: any) {
  selectedPlatform.value = e.detail.value
}

function handleSearch() {
  uni.showToast({ title: '查询中...', icon: 'none' })
}

function handleAdd() {
  uni.showToast({ title: '添加打印机', icon: 'none' })
}

function handleDesign(item: any) {
  uni.showToast({ title: '打印设计: ' + item.name, icon: 'none' })
}

function handleEdit(item: any) {
  uni.showToast({ title: '编辑打印机: ' + item.name, icon: 'none' })
}

function handleDelete(item: any) {
  uni.showModal({
    title: '提示',
    content: '确定删除该打印机吗？',
    success: (res) => {
      if (res.confirm) {
        uni.showToast({ title: '已删除', icon: 'success' })
      }
    }
  })
}
</script>

<style scoped>
.ticket-config {
  padding: 0;
  background-color: transparent;
}

.admin-card {
  background-color: #fff;
  padding: 24px;
  border-radius: 8px;
}

.header-tools {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.search-form {
  display: flex;
  flex-direction: row;
  align-items: center;
}

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

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

.search-input {
  width: 180px;
  height: 32px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  padding: 0 12px;
  font-size: 14px;
}

.picker-view {
  width: 120px;
  height: 32px;
  line-height: 32px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  padding: 0 12px;
  font-size: 14px;
  background-color: #fff;
}

.btn-search {
  background-color: #1890ff;
  color: #fff;
  height: 32px;
  line-height: 32px;
  padding: 0 15px;
  font-size: 14px;
  border: none;
  border-radius: 4px;
}

.btn-primary {
  background-color: #1890ff;
  color: #fff;
  height: 32px;
  line-height: 32px;
  padding: 0 15px;
  font-size: 14px;
  border: none;
  border-radius: 4px;
}

.table-container {
  border: 1px solid #f0f0f0;
  border-radius: 4px;
}

.table-header {
  display: flex;
  flex-direction: row;
  background-color: #f8f8f9;
  border-bottom: 1px solid #f0f0f0;
  padding: 12px 0;
}

.table-row {
  display: flex;
  flex-direction: row;
  border-bottom: 1px solid #f0f0f0;
  padding: 12px 0;
  align-items: center;
}

.table-cell {
  display:flex;
  flex-direction: row;
  font-size: 13px;
  color: #666;
  padding: 0 10px;
  text-align: center;
}

.action-link {
  color: #1890ff;
  cursor: pointer;
  font-size: 13px;
}

.link-danger {
  color: #ff4d4f;
}

.pagination {
  margin-top: 20px;
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
  align-items: center;
  gap: 15px;
}

.total {
  font-size: 13px;
  color: #666;
}

.page-size {
  font-size: 13px;
  color: #666;
  border: 1px solid #d9d9d9;
  padding: 2px 10px;
  border-radius: 4px;
}

.ml-20 {
  margin-left: 20px;
}

.ml-10 {
  margin-left: 10px;
}
</style>

