<template>
  <view class="marketing-seckill-config">
    <view class="filter-card border-shadow">
      <view class="filter-row">
        <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>

    <view class="action-bar">
      <button class="btn-add" @click="showDrawer = true">添加数据</button>
    </view>

    <view class="table-card border-shadow">
      <view class="table-container">
        <view class="table-head">
          <view class="th cell-id">编号</view>
          <view class="th cell-hour">开启时间(整点)</view>
          <view class="th cell-duration">持续时间(整数小时)</view>
          <view class="th cell-img">幻灯片</view>
          <view class="th cell-sort">排序</view>
          <view class="th cell-status">状态</view>
          <view class="th cell-op">操作</view>
        </view>

        <view class="table-body">
          <view v-for="item in pagedList" :key="item.id" class="table-row">
            <view class="td cell-id">
              <text class="td-txt">{{ item.id }}</text>
            </view>
            <view class="td cell-hour">
              <text class="td-txt">{{ item.start_hour }}</text>
            </view>
            <view class="td cell-duration">
              <text class="td-txt">{{ item.duration }}</text>
            </view>
            <view class="td cell-img">
              <image v-if="item.image" class="thumb" :src="item.image" mode="aspectFill"></image>
              <text v-else class="td-txt">-</text>
            </view>
            <view class="td cell-sort">
              <text class="td-txt">{{ item.sort }}</text>
            </view>
            <view class="td cell-status">
              <StatusSwitch v-model="item.status" />
            </view>
            <view class="td cell-op">
              <view class="op-links">
                <text class="op-link" @click="handleEdit(item)">编辑</text>
                <text class="op-split">|</text>
                <text class="op-link" @click="handleDelete(item)">删除</text>
              </view>
            </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>

    <!-- Drawer Overlay -->
    <view v-if="showDrawer || isAnimating" class="drawer-mask" :class="{ active: showDrawer }" @click="closeDrawer"></view>
    
    <!-- Drawer Panel -->
    <view class="drawer-panel" :class="{ active: showDrawer }">
      <view class="drawer-header">
        <text class="drawer-title">添加数据</text>
        <text class="drawer-close" @click="closeDrawer">×</text>
      </view>
      <view class="drawer-content">
        <view class="form-item">
          <text class="form-label required">开启时间(整点)：</text>
          <input class="form-input" placeholder="请输入开启时间(整点)" />
        </view>
        <view class="form-item">
          <text class="form-label required">持续时间(整数小时)：</text>
          <input class="form-input" placeholder="请输入持续时间(整数小时)" />
        </view>
        <view class="form-item">
          <text class="form-label">幻灯片：</text>
          <view class="upload-mock" @click="handleUpload">
            <image v-if="formData.image" :src="formData.image" mode="aspectFill" class="thumb" />
            <text v-else class="upload-ic">🖼️</text>
          </view>
        </view>
        <view class="form-item">
          <text class="form-label">排序：</text>
          <input class="form-input" type="number" v-model="formData.sort" placeholder="1" />
        </view>
        <view class="form-item">
          <text class="form-label">状态：</text>
          <StatusSwitch v-model="formData.status" activeText="显示" inactiveText="隐藏" />
        </view>
      </view>
      <view class="drawer-footer">
        <button class="btn-cancel" @click="closeDrawer">取消</button>
        <button class="btn-confirm" @click="handleSubmit">确定</button>
      </view>
    </view>
  </view>
</template>

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

const showDrawer = ref(false)
const isAnimating = ref(false)

const formData = ref({
  id: 0,
  start_hour: 0,
  duration: 1,
  image: '',
  sort: 1,
  status: true
})

// ========== MOCK DATA START ==========
// TODO: 接真实接口时替换此处 configList 为 fetchConfigList() 调用
const configList = ref([
  { id: 2268, start_hour: 0, duration: 2, image: '', sort: 1, status: true },
  { id: 2269, start_hour: 2, duration: 2, image: '', sort: 2, status: true },
  { id: 2270, start_hour: 4, duration: 2, image: '', sort: 3, status: true },
  { id: 2271, start_hour: 6, duration: 3, image: '', sort: 4, status: true },
  { id: 2272, start_hour: 9, duration: 3, image: '', sort: 5, status: true },
  { id: 2273, start_hour: 12, duration: 3, image: '', sort: 6, status: false },
  { id: 2274, start_hour: 15, duration: 3, image: '', sort: 7, status: true },
  { id: 2275, start_hour: 18, duration: 2, image: '', sort: 8, status: true },
  { id: 2276, start_hour: 20, duration: 2, image: '', sort: 9, status: true },
  { id: 2277, start_hour: 22, duration: 2, image: '', sort: 10, status: false },
  { id: 2278, start_hour: 8, duration: 4, image: '', sort: 11, status: true },
  { id: 2279, start_hour: 10, duration: 2, image: '', sort: 12, status: true },
  { id: 2280, start_hour: 13, duration: 2, image: '', sort: 13, status: true },
  { id: 2281, start_hour: 16, duration: 3, image: '', sort: 14, status: false },
  { id: 2282, start_hour: 19, duration: 1, image: '', sort: 15, status: true },
  { id: 2283, start_hour: 21, duration: 2, image: '', sort: 16, status: true },
  { id: 2284, start_hour: 7, duration: 3, image: '', sort: 17, status: true },
  { id: 2285, start_hour: 11, duration: 2, image: '', sort: 18, status: false },
  { id: 2286, start_hour: 14, duration: 4, image: '', sort: 19, status: true },
  { id: 2287, start_hour: 17, duration: 2, image: '', sort: 20, 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(() => configList.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 configList.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 ==========

const handleEdit = (item: any) => {
  formData.value = { ...item }
  showDrawer.value = true
}

const handleDelete = (item: any) => {
  uni.showModal({
    title: '提示',
    content: '确定要删除此项配置吗？',
    success: (res) => {
      if (res.confirm) {
        configList.value = configList.value.filter(i => i.id !== item.id)
        uni.showToast({ title: '删除成功' })
      }
    }
  })
}

const handleUpload = () => {
  uni.chooseImage({
    count: 1,
    success: (res) => {
      formData.value.image = res.tempFilePaths[0]
    }
  })
}

const handleSubmit = () => {
  uni.showToast({ title: '提交成功', icon: 'success' })
  closeDrawer()
}

const closeDrawer = () => {
  showDrawer.value = false
  isAnimating.value = true
  setTimeout(() => {
    isAnimating.value = false
  }, 300)
}
</script>

<style scoped lang="scss">
.marketing-seckill-config {
  min-height: auto;
  background: transparent;
  padding: 0;
  position: relative;
  overflow: hidden;
}

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

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

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

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

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

.select-mock {
  width: 200px;
  height: 32px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  display: flex;
  flex-direction: row;
  align-items: center;
  padding: 0 12px;
  justify-content: space-between;
}
.select-mock.mini { width: 100px; height: 28px; }

.select-val { font-size: 13px; color: #606266; }
.arrow { font-size: 10px; color: #c0c4cc; }

/* 操作栏 */
.action-bar {
  margin-bottom: 16px;
}
.btn-add {
  width: auto;
  padding: 0 16px;
  height: 32px;
  background-color: #1890ff;
  color: #fff;
  font-size: 14px;
  border: none;
  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; }

/* 各列宽度 */
.cell-id { width: 80px; }
.cell-hour { width: 150px; text-align: center; }
.cell-duration { width: 150px; text-align: center; }
.cell-img { flex: 1; min-width: 150px; }
.cell-sort { width: 100px; text-align: center; }
.cell-status { width: 100px; text-align: center; }
.cell-op { width: 120px; text-align: right; }

.thumb {
  width: 40px;
  height: 40px;
  border-radius: 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 组件 */

/* Drawer Styles */
.drawer-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.45);
  z-index: 1000;
  opacity: 0;
  transition: opacity 0.3s;
  pointer-events: none;
}
.drawer-mask.active {
  opacity: 1;
  pointer-events: auto;
}

.drawer-panel {
  position: fixed;
  top: 0;
  right: -50%;
  width: 50%;
  height: 100%;
  background-color: #fff;
  z-index: 1001;
  box-shadow: -2px 0 8px rgba(0, 0, 0, 0.15);
  display: flex;
  flex-direction: column;
  transition: right 0.3s ease-out;
}
.drawer-panel.active {
  right: 0;
}

.drawer-header {
  padding: 16px 24px;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}
.drawer-title {
  font-size: 16px;
  font-weight: 600;
  color: #262626;
}
.drawer-close {
  font-size: 24px;
  color: #bfbfbf;
  cursor: pointer;
}

.drawer-content {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
}

.form-item {
  margin-bottom: 24px;
}
.form-label {
  display: block;
  font-size: 14px;
  color: #262626;
  margin-bottom: 8px;
}
.required::before {
  content: '*';
  color: #ff4d4f;
  margin-right: 4px;
}
.form-input {
  width: 100%;
  height: 32px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  padding: 0 12px;
  font-size: 14px;
}

.upload-mock {
  width: 80px;
  height: 80px;
  border: 1px dashed #d9d9d9;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fafafa;
  cursor: pointer;
}
.upload-ic { font-size: 24px; color: #bfbfbf; }

.drawer-footer {
  padding: 10px 16px;
  border-top: 1px solid #f0f0f0;
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
  gap: 8px;
}
.btn-cancel, .btn-confirm {
  width: auto;
  padding: 0 15px;
  height: 32px;
  font-size: 14px;
  border-radius: 4px;
  cursor: pointer;
}
.btn-cancel {
  background-color: #fff;
  border: 1px solid #d9d9d9;
  color: #595959;
}
.btn-confirm {
  background-color: #1890ff;
  border: 1px solid #1890ff;
  color: #fff;
}

</style>


