<template>
  <view class="admin-page">
      <view class="admin-sections">
        <!-- 顶部通知 -->
        <view class="admin-card alert-card">
          <view class="alert-content">
            <text class="alert-title">启动定时任务两种方式：</text>
            <text class="alert-desc">1、使用命令行启动：php think timer start --d; 如果更改了执行周期、编辑是否开启、删除定时任务需要重新启动下定时任务确保生效;</text>
            <text class="alert-desc">2、使用接口触发定时任务，建议每分钟调用一次，接口地址 https://v5.crmeb.net/api/crontab/run</text>
          </view>
        </view>

        <!-- 操作卡片 -->
        <view class="admin-card content-card">
          <view class="tabs-row">
            <view class="tab-item active"><text>系统任务</text></view>
            <view class="tab-item"><text>自定义任务</text></view>
          </view>

          <!-- 表格 -->
        <view class="table-container list-table mt-16">
          <view class="table-header">
            <view class="col col-title"><text>标题</text></view>
            <view class="col col-desc"><text>任务说明</text></view>
            <view class="col col-cycle"><text>执行周期</text></view>
            <view class="col col-status"><text>是否开启</text></view>
            <view class="col col-ops"><text>操作</text></view>
          </view>

          <view class="table-body">
            <view v-for="item in pagedList" :key="item.id" class="table-row">
              <view class="col col-title"><text>{{ item.title }}</text></view>
              <view class="col col-desc"><text>{{ item.desc }}</text></view>
              <view class="col col-cycle"><text>{{ item.cycle }}</text></view>
              <view class="col col-status">
                <switch :checked="item.status" color="#1890ff" scale="0.8" />
              </view>
              <view class="col col-ops">
                <text class="op-link" @click="onEdit(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 dataList = ref([
  { id: 1, title: '自动开具/冲红电子发票', desc: '每隔10分钟执行自动开具/冲红电子发票', cycle: '每隔10分钟执行一次', status: true },
  { id: 2, title: '清除昨日海报', desc: '每天0时30分0秒执行一次清除昨日海报', cycle: '每天0时30分0秒执行一次', status: true },
  { id: 3, title: '订单商品自动好评', desc: '每隔5分钟执行订单到期商品好评', cycle: '每隔5分钟执行一次', status: true },
  { id: 4, title: '预售商品到期自动下架', desc: '每隔5分钟执行预售商品到期下架', cycle: '每隔5分钟执行一次', status: true },
  { id: 5, title: '订单自动收货', desc: '每隔5分钟执行订单到期自动收货', cycle: '每隔5分钟执行一次', status: true },
  { id: 6, title: '自动更新直播间状态', desc: '每隔3分钟执行更新直播间状态', cycle: '每隔3分钟执行一次', status: true },
  { id: 7, title: '自动更新直播商品状态', desc: '每隔3分钟执行更新直播商品状态', cycle: '每隔3分钟执行一次', status: true },
  { id: 8, title: '到期自动解绑上级', desc: '每隔1分钟执行到期的绑定关系的解除', cycle: '每隔1分钟执行一次', status: true },
  { id: 9, title: '拼团到期订单处理', desc: '每隔1分钟拼团到期后的操作', cycle: '每隔1分钟执行一次', status: true },
  { id: 10, title: '未支付自动取消订单', desc: '每隆30秒执行自动取消到期未支付的订单', cycle: '每隆30秒执行一次', status: true },
  { id: 11, title: '积分过期处理', desc: '每天凅晑59分0秒执行清除已过期积分', cycle: '每天凅晑59分执行一次', status: true },
  { id: 12, title: '余额到期处理', desc: '每天中午1时执行一次余额到期检查', cycle: '每天中午1时执行一次', status: false },
  { id: 13, title: '优惠券过期处理', desc: '每隔1小时检查并更新过期优惠券状态', cycle: '每隔1小时执行一次', status: true },
  { id: 14, title: '会员等级更新', desc: '每天凅昹3时执行会员等级自动更新', cycle: '每天凅昹3时执行一次', status: true },
  { id: 15, title: '统计数据更新', desc: '每天凅昹4时执行一次经营数据汇总', cycle: '每天凅昹4时执行一次', status: true },
  { id: 16, title: '清理缓存数据', desc: '每天凅晑30分执行一次过期缓存清除', cycle: '每天凅晑30分执行一次', status: true },
  { id: 17, title: '商品库存预警', desc: '每隆15分钟检查库存不足预警商品', cycle: '每隆15分钟执行一次', status: false },
  { id: 18, title: '短信队列发送', desc: '每隔1分钟处理短信队列', cycle: '每隔1分钟执行一次', status: true },
  { id: 19, title: '邮件队列发送', desc: '每隔2分钟处理邮件发送队列', cycle: '每隔2分钟执行一次', status: true },
  { id: 20, title: '每日对账任务', desc: '每天凅晑22时执行一次财务对账', cycle: '每天凅晑22时执行一次', 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(() => 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 onEdit(item: any) {
  uni.showToast({ title: '编辑: ' + item.title, icon: 'none' })
}
</script>

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

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

.alert-card {
  background-color: #fffbe6;
  border: 1px solid #ffe58f;
}

.alert-content {
  padding: 8px;
}

.alert-title {
  font-size: 14px;
  color: #faad14;
  margin-bottom: 8px;
  font-weight: bold;
}

.alert-desc {
  font-size: 13px;
  color: #faad14;
  line-height: 20px;
  margin-bottom: 4px;
}

.tabs-row {
  display: flex;
  flex-direction: row;
  border-bottom: 1px solid #f0f0f0;
  margin-bottom: 24px;
}

.tab-item {
  padding: 12px 20px;
  font-size: 14px;
  color: #333;
  cursor: pointer;
  position: relative;
}

.tab-item.active {
  color: #1890ff;
}

.tab-item.active::after {
  content: '';
  position: absolute;
  bottom: -1px;
  left: 0;
  right: 0;
  height: 2px;
  background-color: #1890ff;
}

.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-title { flex: 2; }
.col-desc { flex: 3; }
.col-cycle { flex: 2; }
.col-status { width: 100px; justify-content: center; }
.col-ops { width: 80px; justify-content: center; }

.op-link {
  color: #1890ff;
  cursor: pointer;
}

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

.page-info {
  font-size: 14px;
  color: #999;
}

.mt-16 { margin-top: 16px; }
</style>


