<template>
  <view class="marketing-seckill-product">
    <view class="filter-card border-shadow">
      <view class="filter-row">
        <view class="filter-item">
          <text class="label">商品搜索：</text>
          <input class="input-mock" placeholder="请输入商品名称, ID" />
        </view>
        <view class="filter-item">
          <text class="label">活动搜索：</text>
          <input class="input-mock" placeholder="请输入活动名称" />
        </view>
        <view class="filter-item">
          <text class="label">活动状态：</text>
          <view class="select-mock">
            <text class="select-val">请选择</text>
            <text class="arrow">▼</text>
          </view>
        </view>
      </view>
      <view class="filter-row mt-16">
        <view class="filter-item">
          <text class="label">活动时间：</text>
          <view class="date-picker-mock">
            <text class="calendar-ic">📅</text>
            <text class="date-placeholder">开始日期 - 结束日期</text>
          </view>
        </view>
        <button class="btn-query">查询</button>
      </view>
    </view>

    <view class="action-bar">
      <button class="btn-export">导出</button>
    </view>

    <view class="table-card border-shadow">
      <view class="table-container">
        <view class="table-head">
          <view class="th cell-id">ID</view>
          <view class="th cell-img">商品图片</view>
          <view class="th cell-title">商品标题</view>
          <view class="th cell-intro">商品简介</view>
          <view class="th cell-activity">活动名称</view>
          <view class="th cell-price">售价</view>
          <view class="th cell-price">秒杀价</view>
          <view class="th cell-num">限量</view>
          <view class="th cell-num">限量剩余</view>
          <view class="th cell-status-txt">秒杀状态</view>
          <view class="th cell-time">活动时间</view>
          <view class="th cell-status">状态</view>
          <view class="th cell-op">操作</view>
        </view>

        <view class="table-body">
          <view v-for="item in productList" :key="item.id" class="table-row">
            <view class="td cell-id">
              <text class="td-txt">{{ item.id }}</text>
            </view>
            <view class="td cell-img">
              <image class="thumb" :src="item.image" mode="aspectFill"></image>
            </view>
            <view class="td cell-title">
              <text class="product-title line-clamp-2">{{ item.title }}</text>
            </view>
            <view class="td cell-intro">
               <text class="product-title line-clamp-2">{{ item.intro }}</text>
            </view>
            <view class="td cell-activity">
              <text class="td-txt">{{ item.activity_name }}</text>
            </view>
            <view class="td cell-price">
              <text class="td-txt">{{ item.price.toFixed(2) }}</text>
            </view>
            <view class="td cell-price">
              <text class="td-txt-price">{{ item.seckill_price.toFixed(2) }}</text>
            </view>
            <view class="td cell-num">
              <text class="td-txt">{{ item.limit }}</text>
            </view>
            <view class="td cell-num">
              <text class="td-txt">{{ item.limit_remaining }}</text>
            </view>
            <view class="td cell-status-txt">
              <text class="status-text green">进行中</text>
            </view>
            <view class="td cell-time">
              <text class="td-txt-small">起: {{ item.start_date }}</text>
              <text class="td-txt-small">止: {{ item.end_date }}</text>
            </view>
            <view class="td cell-status">
              <view class="switch-mock" :class="{ active: item.status }" @click="toggleStatus(item)">
                <view class="switch-dot"></view>
                <text class="switch-txt">{{ item.status ? '开启' : '关闭' }}</text>
              </view>
            </view>
            <view class="td cell-op">
              <view class="op-links">
                <text class="op-link" @click="handleDelete(item)">删除</text>
                <text class="op-split">|</text>
                <text class="op-link" @click="handleStats(item)">统计</text>
              </view>
            </view>
          </view>
        </view>
      </view>

      <!-- 分页 -->
      <CommonPagination
        v-if="productList.length > 0"
        :total="productList.length"
        :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 = val }"
        @jump-page="handleJumpPage"
      />
    </view>
  </view>
</template>

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

const productList = ref([
  {
    id: 599,
    image: 'https://img0.baidu.com/it/u=2257917711,1359654032&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
    title: '爱奇艺智能 奇遇LT01 投影仪 家用卧室超...',
    intro: '爱奇艺智能 奇遇LT01 投影仪...',
    activity_name: '秒杀活动',
    price: 44.00,
    seckill_price: 1.00,
    limit: 100,
    limit_remaining: 55,
    status: true,
    start_date: '2025-07-01 00:00:00',
    end_date: '2028-08-22 23:59:59'
  },
  {
    id: 598,
    image: 'https://img1.baidu.com/it/u=3175865615,2002599723&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
    title: '美式复古阔腿牛仔裤女夏季2024新款高...',
    intro: '',
    activity_name: '秒杀活动',
    price: 49.98,
    seckill_price: 20.00,
    limit: 750,
    limit_remaining: 736,
    status: true,
    start_date: '2025-07-01 00:00:00',
    end_date: '2028-08-22 23:59:59'
  },
  {
    id: 596,
    image: 'https://img0.baidu.com/it/u=3023224345,1529124233&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
    title: '真力时 (ZENITH) 瑞士手表DEFY系列C...',
    intro: '真力时 (ZENITH) 瑞士手表...',
    activity_name: '秒杀活动',
    price: 61000.00,
    seckill_price: 20.00,
    limit: 50,
    limit_remaining: 46,
    status: true,
    start_date: '2025-07-01 00:00:00',
    end_date: '2028-08-22 23:59:59'
  }
])

const toggleStatus = (item: any) => {
  item.status = !item.status
  uni.showToast({ title: '修改成功', icon: 'success' })
}

const handleDelete = (item: any) => {
  uni.showModal({
    title: '提示',
    content: '确定要删除该秒杀商品吗？',
    success: (res) => {
      if (res.confirm) {
        productList.value = productList.value.filter(i => i.id !== item.id)
        uni.showToast({ title: '删除成功' })
      }
    }
  })
}

const handleStats = (item: any) => {
  uni.showToast({ title: '统计查看中', icon: 'none' })
}

// 分页适配状态
const currentPage = ref(1)
const pageSize = ref(15)
let jumpPageInput = ''
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 totalPage = computed(() => Math.max(1, Math.ceil(productList.value.length / pageSize.value)))
const visiblePages = computed(() => {
  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)
  if (!isNaN(p) && p >= 1 && p <= totalPage.value) currentPage.value = p
}
</script>

<style scoped lang="scss">
.marketing-seckill-product {
  min-height: auto;
  background: transparent;
  padding: 0;
}

.border-shadow {
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.mt-16 { margin-top: 16px; }

/* 过滤栏 */
.filter-card {
  padding: 24px;
  margin-bottom: 16px;
}

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

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

.label {
  font-size: 14px;
  color: #606266;
  white-space: nowrap;
}

.input-mock, .date-picker-mock, .select-mock {
  width: 220px;
  height: 32px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  display: flex;
  flex-direction: row;
  align-items: center;
  padding: 0 12px;
  font-size: 13px;
}
.select-mock { width: 160px; justify-content: space-between; }
.select-mock.mini { width: 100px; height: 28px; }

.calendar-ic { font-size: 14px; color: #c0c4cc; margin-right: 8px; }
.date-placeholder { font-size: 13px; color: #c0c4cc; }
.select-val { font-size: 13px; color: #606266; }
.arrow { font-size: 10px; color: #c0c4cc; }

.btn-query {
  width: 64px;
  height: 32px;
  background-color: #1890ff;
  color: #fff;
  font-size: 14px;
  border: none;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

/* 操作栏 */
.action-bar {
  margin-bottom: 16px;
}
.btn-export {
  width: auto;
  padding: 0 16px;
  height: 32px;
  background-color: #fff;
  color: #606266;
  border: 1px solid #dcdfe6;
  font-size: 14px;
  border-radius: 4px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

/* 表格区域 */
.table-card {
  padding: 24px;
}

.table-head {
  display: flex;
  flex-direction: row;
  background-color: #f8f8f9;
  border-bottom: 1px solid #e8eaec;
}

.th {
  padding: 12px 8px;
  font-size: 13px;
  color: #515a6e;
  font-weight: bold;
}

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

.td {
  padding: 16px 8px;
}

.td-txt { font-size: 13px; color: #515a6e; }
.td-txt-price { font-size: 13px; color: #ff4d4f; font-weight: bold; }
.td-txt-small { font-size: 12px; color: #808695; display: block; }

/* 各列宽度 */
.cell-id { width: 60px; }
.cell-img { width: 80px; }
.cell-title { width: 200px; }
.cell-intro { width: 180px; }
.cell-activity { width: 120px; }
.cell-price { width: 80px; text-align: center; }
.cell-num { width: 80px; text-align: center; }
.cell-status-txt { width: 100px; text-align: center; }
.cell-time { width: 220px; }
.cell-status { width: 100px; text-align: center; }
.cell-op { width: 120px; text-align: right; }

.thumb {
  width: 40px;
  height: 40px;
  border-radius: 4px;
}

.product-title {
  font-size: 13px;
  color: #515a6e;
  line-height: 1.6;
}
.line-clamp-2 {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.status-text { font-size: 12px; }
.status-text.green { color: #52c41a; }

.switch-mock {
  width: 50px;
  height: 24px;
  background-color: #bfbfbf;
  border-radius: 12px;
  display: flex;
  flex-direction: row;
  align-items: center;
  padding: 0 4px;
  position: relative;
  transition: background-color 0.3s;
}
.switch-mock.active { background-color: #1890ff; }
.switch-dot {
  width: 16px;
  height: 16px;
  background-color: #fff;
  border-radius: 50%;
  position: absolute;
  left: 4px;
  transition: left 0.3s;
}
.switch-mock.active .switch-dot { left: 30px; }
.switch-txt { font-size: 11px; color: #fff; margin-left: 20px; }
.switch-mock.active .switch-txt { margin-left: 4px; }

.op-links {
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
  align-items: center;
}
.op-link { color: #1890ff; font-size: 13px; cursor: pointer; }
.op-split { color: #e8eaec; margin: 0 8px; }

/* 分页区域已迁至 CommonPagination 组件 */

</style>


