<template>
  <view class="finance-commission">
    <!-- 筛选卡片 -->
    <view class="filter-card border-shadow">
      <view class="filter-row">
        <view class="filter-item">
          <text class="filter-label">昵称/手机号/分销商ID:</text>
          <input class="search-input" placeholder="请输入昵称搜索" />
        </view>
        <view class="btn-query">
          <text class="btn-txt">查询</text>
        </view>
      </view>
    </view>

    <!-- 列表表格 -->
    <view class="table-card border-shadow">
      <view class="action-bar">
        <view class="btn-export">
          <text class="export-txt">导出</text>
        </view>
      </view>

      <view class="table-container">
        <view class="table-header">
          <view class="th col-user"><text class="th-txt">用户信息</text></view>
          <view class="th col-total"><text class="th-txt">总佣金金额</text></view>
          <view class="th col-account"><text class="th-txt">账户佣金</text></view>
          <view class="th col-withdraw"><text class="th-txt">提现佣金</text></view>
        </view>

        <view class="table-body">
          <view class="table-row" v-for="item in pagedList" :key="item.uid">
            <view class="td col-user">
              <text class="td-txt">{{ item.userInfo }}</text>
            </view>
            <view class="td col-total">
              <text class="td-txt">{{ item.totalAmount }}</text>
            </view>
            <view class="td col-account">
              <text class="td-txt">{{ item.accountAmount }}</text>
            </view>
            <view class="td col-withdraw">
              <text class="td-txt">{{ item.withdrawAmount }}</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>
</template>

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

interface CommissionSummary {
  uid: string
  userInfo: string
  totalAmount: string
  accountAmount: string
  withdrawAmount: string
}

// ========== MOCK DATA START ==========
// TODO: 接真实接口时替换此处 tableData 为 fetchCommissionList() 调用
const tableData = ref<CommissionSummary[]>([
  { uid: '77418', userInfo: '张迪 | 155****5525 | 77418', totalAmount: '11.00', accountAmount: '11.00', withdrawAmount: '0' },
  { uid: '69696', userInfo: "I'm yours - | 69696", totalAmount: '0.40', accountAmount: '0.40', withdrawAmount: '0' },
  { uid: '68582', userInfo: 'guan | 68582', totalAmount: '140.80', accountAmount: '140.80', withdrawAmount: '0' },
  { uid: '65258', userInfo: '纯爱战神别名王富贵儿 | 158****4881 | 65258', totalAmount: '11.00', accountAmount: '11.00', withdrawAmount: '0' },
  { uid: '66265', userInfo: '王伟兴 | 66265', totalAmount: '11.99', accountAmount: '11.99', withdrawAmount: '0' },
  { uid: '65270', userInfo: '199****1781 | 199****1781 | 65270', totalAmount: '31.60', accountAmount: '31.60', withdrawAmount: '0' },
  { uid: '64572', userInfo: '洒笾菂艸 | 188****2434 | 64572', totalAmount: '0.20', accountAmount: '0.20', withdrawAmount: '0' },
  { uid: '47952', userInfo: '那小子 | 134****3573 | 47952', totalAmount: '19.80', accountAmount: '19.80', withdrawAmount: '0' },
  { uid: '43210', userInfo: '小花猫 | 132****0011 | 43210', totalAmount: '56.00', accountAmount: '30.00', withdrawAmount: '26.00' },
  { uid: '39876', userInfo: '蓝色天空 | 136****5566 | 39876', totalAmount: '88.50', accountAmount: '50.00', withdrawAmount: '38.50' },
  { uid: '35432', userInfo: '星星之火 | 159****7788 | 35432', totalAmount: '200.00', accountAmount: '100.00', withdrawAmount: '100.00' },
  { uid: '31098', userInfo: '夜行者 | 177****9900 | 31098', totalAmount: '15.60', accountAmount: '15.60', withdrawAmount: '0' },
  { uid: '26754', userInfo: '晨曦 | 138****1122 | 26754', totalAmount: '320.00', accountAmount: '200.00', withdrawAmount: '120.00' },
  { uid: '22410', userInfo: '落叶归根 | 152****3344 | 22410', totalAmount: '45.80', accountAmount: '45.80', withdrawAmount: '0' },
  { uid: '18066', userInfo: '风中漫步 | 189****5566 | 18066', totalAmount: '800.00', accountAmount: '500.00', withdrawAmount: '300.00' },
  { uid: '13722', userInfo: '快乐小屋 | 133****7788 | 13722', totalAmount: '12.00', accountAmount: '12.00', withdrawAmount: '0' },
  { uid: '9378',  userInfo: '云端漫步 | 176****9900 | 9378',  totalAmount: '650.00', accountAmount: '400.00', withdrawAmount: '250.00' },
  { uid: '5034',  userInfo: '绿草如茵 | 156****0011 | 5034',  totalAmount: '28.40', accountAmount: '28.40', withdrawAmount: '0' },
  { uid: '88901', userInfo: '阳光路上 | 139****2233 | 88901', totalAmount: '1200.00', accountAmount: '800.00', withdrawAmount: '400.00' },
  { uid: '84557', userInfo: '繁花似锦 | 173****4455 | 84557', totalAmount: '76.60', accountAmount: '76.60', withdrawAmount: '0' },
])
// ========== 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(() => tableData.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 tableData.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 ==========
</script>

<style scoped lang="scss">
.finance-commission {
  padding: 0;
  background-color: transparent;
  min-height: auto;
}

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

/* 筛选 */
.filter-card {
  padding: 24px;
  margin-bottom: 20px;
}

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

.filter-label {
  font-size: 14px;
  color: #333;
  margin-right: 12px;
}

.search-input {
  width: 240px;
  height: 36px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  padding: 0 12px;
  font-size: 14px;
}

.btn-query {
  margin-left: 12px;
  background-color: #1890ff;
  border-radius: 4px;
  height: 36px;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-txt {
  color: #fff;
  font-size: 14px;
}

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

.action-bar {
  padding: 15px 20px;
}

.btn-export {
  width: 60px;
  height: 32px;
  background-color: #e6f7ff;
  border: 1px solid #91d5ff;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.export-txt {
  color: #1890ff;
  font-size: 14px;
}

.table-container {
  display: flex;
  flex-direction: column;
}

.table-header {
  background-color: #e6f0ff; /* 淡蓝色表头 */
  display: flex;
  flex-direction: row;
}

.th {
  padding: 14px 20px;
  display: flex;
  align-items: center;
}

.th-txt {
  font-size: 14px;
  font-weight: 500;
  color: #606266;
}

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

.table-row:hover {
  background-color: #fafafa;
}

.td {
  padding: 16px 20px;
  display: flex;
  align-items: center;
}

.td-txt {
  font-size: 14px;
  color: #606266;
}

/* 列宽分配 */
.col-user { flex: 2; min-width: 300px; justify-content: flex-start; }
.col-total { flex: 1; justify-content: flex-start; }
.col-account { flex: 1; justify-content: flex-start; }
.col-withdraw { flex: 1; justify-content: flex-start; }
</style>
