<template>
  <AdminLayout currentPage="points-record">
    <view class="marketing-points-record">
      <!-- 1. 搜索过滤栏 -->
      <view class="filter-card border-shadow">
        <view class="filter-row">
          <view class="filter-item">
            <text class="label">搜索记录：</text>
            <input class="input-mock" placeholder="用户名/业务标题" v-model="searchQuery" @confirm="handleSearch" />
          </view>
          <view class="filter-btns">
            <button class="btn-query" @click="handleSearch">查询</button>
            <button class="btn-reset" @click="handleReset">重置</button>
          </view>
        </view>
      </view>

      <!-- 2. 数据表格 -->
      <view class="table-card border-shadow">
        <view class="table-container">
          <view v-if="isLoading" class="loading-mask">
            <text class="loading-text">加载中...</text>
          </view>
          
          <view class="table-header">
            <view class="th cell-id">ID</view>
            <view class="th cell-user">用户信息</view>
            <view class="th cell-title">业务标题</view>
            <view class="th cell-points">积分变动</view>
            <view class="th cell-balance">变动后余额</view>
            <view class="th cell-time">记录时间</view>
          </view>

          <view class="table-body">
            <view v-if="recordList.length === 0 && !isLoading" class="empty-row">
              <text>暂无积分变动记录</text>
            </view>
            <view v-for="item in recordList" :key="item.id" class="table-row">
              <view class="td cell-id">
                <text class="td-txt-small">{{ item.id }}</text>
              </view>
              <view class="td cell-user">
                <view class="u-info">
                  <image class="u-avatar" :src="item.avatar || '/static/logo.png'" mode="aspectFill" />
                  <text class="u-nick">{{ item.nickname || '未知用户' }}</text>
                </view>
              </view>
              <view class="td cell-title">
                <text class="td-txt">{{ item.title }}</text>
              </view>
              <view class="td cell-points">
                <text class="td-txt-bold" :class="item.pm === 1 ? 'color-blue' : 'color-red'">
                  {{ item.pm === 1 ? '+' : '-' }}{{ item.number }}
                </text>
              </view>
              <view class="td cell-balance">
                <text class="td-txt">{{ item.balance }}</text>
              </view>
              <view class="td cell-time">
                <text class="td-txt-small">{{ formatDateTime(item.created_at) }}</text>
              </view>
            </view>
          </view>
        </view>

        <!-- 3. 分页控制 -->
        <view class="pagination-footer">
          <view class="page-total">
            <text class="total-txt">共 {{ total }} 条</text>
          </view>
          <view class="page-btns">
            <text class="p-btn" :class="{ disabled: page <= 1 }" @click="onPrevPage">‹</text>
            <text class="p-btn active">{{ page }}</text>
            <text class="p-btn" :class="{ disabled: recordList.length < pageSize }" @click="onNextPage">›</text>
          </view>
        </view>
      </view>
    </view>
  </AdminLayout>
</template>

<script setup lang="uts">
import { ref, onMounted } from 'vue'
import AdminLayout from '@/layouts/admin/AdminLayout.uvue'
import { fetchPointsRecords } from '@/services/admin/marketingService.uts'

const currentPage = ref('points-record')
const recordList = ref<any[]>([])
const isLoading = ref(false)
const searchQuery = ref('')
const page = ref(1)
const pageSize = 15
const total = ref(0)

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

async function loadData() {
  isLoading.value = true
  try {
    const res = await fetchPointsRecords({
      search: searchQuery.value,
      page: page.value,
      pageSize: pageSize
    })
    recordList.value = res.items
    total.value = res.total
  } catch (e) {
    uni.showToast({ title: '加载失败', icon: 'none' })
  } finally {
    isLoading.value = false
  }
}

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

function handleReset() {
  searchQuery.value = ''
  page.value = 1
  loadData()
}

function onPrevPage() {
  if (page.value > 1) {
    page.value--
    loadData()
  }
}

function onNextPage() {
  if (recordList.value.length >= pageSize) {
    page.value++
    loadData()
  }
}

function formatDateTime(iso : string) : string {
  return iso.substring(0, 16).replace('T', ' ')
}
</script>

<style scoped lang="scss">
.marketing-points-record {
  min-height: 100vh;
  background: #f0f2f5;
  padding: 16px;
}

.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; gap: 24px; }
.filter-item { display: flex; flex-direction: row; align-items: center; }
.label { font-size: 14px; color: #606266; }
.input-mock {
  width: 240px; height: 32px; border: 1px solid #dcdfe6; border-radius: 4px;
  padding: 0 12px; font-size: 13px;
}
.filter-btns { display: flex; flex-direction: row; gap: 12px; }
.btn-query { background: #1890ff; color: #fff; height: 32px; padding: 0 16px; border-radius: 4px; font-size: 14px; border: none; cursor: pointer; }
.btn-reset { background: #fff; color: #666; height: 32px; padding: 0 16px; border-radius: 4px; font-size: 14px; border: 1px solid #dcdfe6; cursor: pointer; }

.table-card { padding: 24px; position: relative; }
.table-container { min-height: 400px; position: relative; }
.loading-mask {
  position: absolute; top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(255,255,255,0.7); display: flex; align-items: center; justify-content: center; z-index: 10;
}
.loading-text { color: #1890ff; font-size: 14px; }

.table-header {
  display: flex; flex-direction: row; background-color: #f8faff;
  border-bottom: 1px solid #e8eaec;
}
.th { padding: 12px 8px; font-size: 13px; color: #515a6e; font-weight: bold; text-align: center; }

.table-row {
  display: flex; flex-direction: row; border-bottom: 1px solid #e8eaec; align-items: center;
  &:hover { background-color: #fafafa; }
}
.td { padding: 12px 8px; text-align: center; display: flex; align-items: center; justify-content: center; }
.td-txt { font-size: 13px; color: #515a6e; }
.td-txt-bold { font-size: 14px; color: #333; font-weight: bold; }
.td-txt-small { font-size: 12px; color: #999; }
.color-blue { color: #1890ff; }
.color-red { color: #f5222d; }

.cell-id { width: 60px; }
.cell-user { width: 180px; justify-content: flex-start; }
.cell-title { flex: 1; min-width: 150px; }
.cell-points { width: 100px; }
.cell-balance { width: 120px; }
.cell-time { width: 150px; }

.u-info { display: flex; flex-direction: row; align-items: center; gap: 8px; }
.u-avatar { width: 32px; height: 32px; border-radius: 16px; background: #f5f5f5; }
.u-nick { font-size: 13px; color: #333; }

.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: 28px; height: 28px; border: 1px solid #dcdfe6; border-radius: 4px;
  display: flex; align-items: center; justify-content: center; font-size: 14px;
  &.active { background: #1890ff; color: #fff; border-color: #1890ff; }
  &.disabled { opacity: 0.5; cursor: not-allowed; }
}
.empty-row { padding: 60px 0; text-align: center; color: #999; }
</style>