<template>
  <view class="finance-capital-flow">
    <!-- 头部筛选区 -->
    <view class="filter-card border-shadow">
      <view class="filter-row">
        <view class="filter-item">
          <text class="filter-label">订单时间:</text>
          <view class="date-picker-wrap">
            <text class="calendar-icon">📅</text>
            <text class="date-placeholder">开始日期 - 结束日期</text>
          </view>
        </view>
        <view class="filter-item">
          <text class="filter-label">交易类型:</text>
          <view class="select-box">
            <text class="select-txt">请选择</text>
            <text class="arrow-down">▼</text>
          </view>
        </view>
        <view class="filter-item search-wrap">
          <text class="filter-label">流水搜索:</text>
          <input class="search-input" placeholder="订单号/昵称/电话/用户ID" />
        </view>
        <view class="btn-query">
          <text class="btn-txt">查询</text>
        </view>
      </view>
    </view>

    <!-- 数据表格 (Flex 模拟) -->
    <view class="table-container border-shadow">
      <view class="table-header">
        <view class="th col-flow"><text class="th-txt">交易单号</text></view>
        <view class="th col-order"><text class="th-txt">关联订单</text></view>
        <view class="th col-time"><text class="th-txt">交易时间</text></view>
        <view class="th col-amount"><text class="th-txt">交易金额</text></view>
        <view class="th col-user"><text class="th-txt">交易用户</text></view>
        <view class="th col-method"><text class="th-txt">支付方式</text></view>
        <view class="th col-remark"><text class="th-txt">备注</text></view>
        <view class="th col-ops"><text class="th-txt">操作</text></view>
      </view>

      <view class="table-body">
        <view class="table-row" v-for="item in pagedList" :key="item.flowNo">
          <view class="td col-flow"><text class="td-txt">{{ item.flowNo }}</text></view>
          <view class="td col-order text-left"><text class="td-txt">{{ item.orderNo }}</text></view>
          <view class="td col-time"><text class="td-txt">{{ item.time }}</text></view>
          <view class="td col-amount"><text class="td-txt red-txt">{{ item.amount }}</text></view>
          <view class="td col-user"><text class="td-txt">{{ item.user }}</text></view>
          <view class="td col-method"><text class="td-txt">{{ item.method }}</text></view>
          <view class="td col-remark"><text class="td-txt">{{ item.remark }}</text></view>
          <view class="td col-ops">
            <text class="op-link">备注</text>
          </view>
        </view>
      </view>
    </view>
    <CommonPagination
      v-if="true"
      :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>
</template>

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

interface FlowRecord {
  flowNo: string
  orderNo: string
  time: string
  amount: string
  user: string
  method: string
  remark: string
}

const tableData = ref<FlowRecord[]>([
  {
    flowNo: 'ZJ202602031258428744',
    orderNo: 'hy541422245264752640',
    time: '2026-02-03 00:58:42',
    amount: '+0.00',
    user: 'J.',
    method: '微信',
    remark: ''
  },
  {
    flowNo: 'ZJ202602011056592117',
    orderNo: 'hy541029224517992448',
    time: '2026-02-01 22:56:59',
    amount: '+0.00',
    user: 'dev 王鑫',
    method: '微信',
    remark: ''
  },
  {
    flowNo: 'ZJ202602010513443817',
    orderNo: 'hy540942844546777088',
    time: '2026-02-01 17:13:44',
    amount: '+0.00',
    user: 'Sunshine',
    method: '微信',
    remark: ''
  },
  {
    flowNo: 'ZJ202602010127248683',
    orderNo: 'hy540885887420989440',
    time: '2026-02-01 13:27:24',
    amount: '+0.00',
    user: '132****8769',
    method: '微信',
    remark: ''
  },
  {
    flowNo: 'ZJ202602011215407366',
    orderNo: 'hy540686639874179072',
    time: '2026-02-01 00:15:40',
    amount: '+0.00',
    user: '177****9187',
    method: '微信',
    remark: ''
  },
  {
    flowNo: 'ZJ202601301026512881',
    orderNo: 'hy540296867267739648',
    time: '2026-01-30 22:26:51',
    amount: '+0.00',
    user: '暂未成功人士',
    method: '微信',
    remark: ''
  },
  {
    flowNo: 'ZJ202601300534267557',
    orderNo: 'hy540223279126806528',
    time: '2026-01-30 17:34:26',
    amount: '+0.00',
    user: 'b342865d2077',
    method: '微信',
    remark: ''
  },
  { flowNo: 'ZJ202601290823456789', orderNo: 'hy539987654321234567', time: '2026-01-29 08:23:45', amount: '+88.00', user: '张小明', method: '支付宝', remark: '' },
  { flowNo: 'ZJ202601281534267890', orderNo: 'hy539765432109876543', time: '2026-01-28 15:34:26', amount: '+200.00', user: '王芳芳', method: '微信', remark: '' },
  { flowNo: 'ZJ202601270945123450', orderNo: 'hy539543210987654321', time: '2026-01-27 09:45:12', amount: '-50.00', user: '李建国', method: '余额', remark: '退款' },
  { flowNo: 'ZJ202601261200345678', orderNo: 'hy539321098765432109', time: '2026-01-26 12:00:34', amount: '+150.00', user: 'Tom Wang', method: '支付宝', remark: '' },
  { flowNo: 'ZJ202601251756890123', orderNo: 'hy539098765432109876', time: '2026-01-25 17:56:08', amount: '+30.00', user: '赵小燕', method: '微信', remark: '' },
  { flowNo: 'ZJ202601240314567890', orderNo: 'hy538876543210987654', time: '2026-01-24 03:14:56', amount: '+500.00', user: '陈大伟', method: '微信', remark: '' },
  { flowNo: 'ZJ202601231023456789', orderNo: 'hy538654321098765432', time: '2026-01-23 10:23:45', amount: '+1000.00', user: '周总', method: '支付宝', remark: '' },
  { flowNo: 'ZJ202601221445678901', orderNo: 'hy538432109876543210', time: '2026-01-22 14:45:06', amount: '-20.00', user: '刘小丽', method: '余额', remark: '佣金提现' },
  { flowNo: 'ZJ202601210856789012', orderNo: 'hy538209876543210987', time: '2026-01-21 08:56:07', amount: '+75.00', user: '黄志强', method: '微信', remark: '' },
  { flowNo: 'ZJ202601201234567890', orderNo: 'hy537987654321098765', time: '2026-01-20 12:34:56', amount: '+320.00', user: '吴晓东', method: '支付宝', remark: '' },
  { flowNo: 'ZJ202601191634234567', orderNo: 'hy537765432109876543', time: '2026-01-19 16:34:23', amount: '+65.00', user: '郑美华', method: '微信', remark: '' },
  { flowNo: 'ZJ202601181034567890', orderNo: 'hy537543210987654321', time: '2026-01-18 10:34:56', amount: '+128.00', user: '孙小峰', method: '微信', remark: '' }
])

// ========== 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-capital-flow {
  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: 20px 24px;
  margin-bottom: 20px;
}

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

.filter-item {
  display: flex;
  flex-direction: row;
  align-items: center;
  margin-right: 28px;
}

.filter-label {
  font-size: 14px;
  color: #333;
  margin-right: 10px;
  white-space: nowrap;
}

.date-picker-wrap {
  width: 220px;
  height: 32px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  display: flex;
  flex-direction: row;
  align-items: center;
  padding: 0 12px;
}

.calendar-icon {
  font-size: 12px;
  margin-right: 8px;
}

.date-placeholder {
  font-size: 13px;
  color: #c0c4cc;
}

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

.select-txt {
  font-size: 13px;
  color: #606266;
}

.arrow-down {
  font-size: 8px;
  color: #c0c4cc;
}

.search-wrap {
  flex: 1;
}

.search-input {
  flex: 1;
  height: 32px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  padding: 0 12px;
  font-size: 13px;
}

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

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

/* 表格 */
.table-container {
  display: flex;
  flex-direction: column;
}

.table-header {
  background-color: #f8f9fb;
  display: flex;
  flex-direction: row;
  border-bottom: 1px solid #ebeef5;
}

.th {
  padding: 15px 10px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.th-txt {
  font-size: 14px;
  font-weight: 600;
  color: #909399;
}

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

.td {
  padding: 15px 10px;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

.col-flow { width: 200px; }
.col-order { width: 220px; }
.col-time { width: 180px; }
.col-amount { width: 120px; }
.col-user { width: 150px; }
.col-method { width: 120px; }
.col-remark { flex: 1; min-width: 100px; }
.col-ops { width: 100px; }

.text-left {
  justify-content: flex-start;
}

.red-txt {
  color: #f56c6c;
}

.op-link {
  font-size: 13px;
  color: #1890ff;
}
</style>
