<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { accountTransactionApi } from '@/api/finance.js'

const tableData = ref([])
const total = ref(0)
const loading = ref(false)

const searchForm = reactive({ account_id: '', type: '', date_from: '', date_to: '' })
const pagination = reactive({ page: 1, per_page: 20 })

const typeMap = {
  deposit: '充值',
  consume: '消费',
  refund: '退款',
  adjust: '调整'
}
const typeTagType = {
  deposit: 'success',
  consume: 'danger',
  refund: 'warning',
  adjust: 'info'
}

async function fetchList() {
  loading.value = true
  try {
    const res = await accountTransactionApi.getList({ ...searchForm, ...pagination })
    tableData.value = res.data?.list || res.data?.data || []
    total.value = res.data?.total || 0
  } catch {
    ElMessage.error('获取流水列表失败')
  } finally {
    loading.value = false
  }
}

function handleSearch() { pagination.page = 1; fetchList() }
function handleReset() {
  Object.assign(searchForm, { account_id: '', type: '', date_from: '', date_to: '' })
  handleSearch()
}
function handlePageChange(val) { pagination.page = val; fetchList() }
function handleSizeChange(val) { pagination.per_page = val; pagination.page = 1; fetchList() }

function amountClass(row) {
  return ['deposit', 'refund'].includes(row.type) ? 'text-green-600 font-semibold' : 'text-red-500 font-semibold'
}
function amountPrefix(row) {
  return ['deposit', 'refund'].includes(row.type) ? '+' : '-'
}

onMounted(() => fetchList())
</script>

<template>
  <div class="p-6 space-y-4">
    <!-- 搜索栏 -->
    <div class="bg-white rounded-2xl border border-gray-100 shadow-sm p-4 flex items-center flex-wrap gap-3">
      <el-input
        v-model="searchForm.account_id"
        placeholder="账户ID"
        style="width:150px"
        clearable
        @keydown.enter="handleSearch"
      />
      <el-select v-model="searchForm.type" placeholder="流水类型" clearable style="width:130px">
        <el-option v-for="(v, k) in typeMap" :key="k" :label="v" :value="k" />
      </el-select>
      <el-date-picker
        v-model="searchForm.date_from"
        type="date"
        value-format="YYYY-MM-DD"
        placeholder="开始日期"
        style="width:150px"
      />
      <el-date-picker
        v-model="searchForm.date_to"
        type="date"
        value-format="YYYY-MM-DD"
        placeholder="结束日期"
        style="width:150px"
      />
      <el-button type="primary" @click="handleSearch">查询</el-button>
      <el-button @click="handleReset">重置</el-button>
    </div>

    <!-- 列表卡片 -->
    <div class="bg-white rounded-2xl border border-gray-100 shadow-sm">
      <div class="flex items-center gap-3 px-5 py-4 border-b border-gray-50">
        <span class="w-1 h-5 bg-rose-500 rounded-full inline-block"></span>
        <span class="font-medium text-gray-700">账户流水明细</span>
        <el-tag type="info" size="small" class="ml-auto">只读</el-tag>
      </div>
      <div class="px-2">
        <el-table v-loading="loading" :data="tableData" style="width:100%" stripe>
          <el-table-column prop="id" label="流水ID" width="80" align="center" />
          <el-table-column prop="customer_name" label="客户" min-width="110" />
          <el-table-column label="类型" width="90" align="center">
            <template #default="{ row }">
              <el-tag :type="typeTagType[row.type]" size="small">
                {{ typeMap[row.type] || row.type }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="金额" width="120" align="right">
            <template #default="{ row }">
              <span :class="amountClass(row)">
                {{ amountPrefix(row) }}{{ Number(row.amount || 0).toFixed(2) }}
              </span>
            </template>
          </el-table-column>
          <el-table-column label="余额(后)" width="120" align="right">
            <template #default="{ row }">
              <span class="text-gray-700">{{ Number(row.balance_after || 0).toFixed(2) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="operator_name" label="操作人" width="100" align="center" />
          <el-table-column prop="remark" label="备注" min-width="140" show-overflow-tooltip />
          <el-table-column prop="created_at" label="时间" min-width="160" />
        </el-table>
      </div>
      <div class="px-5 py-3 border-t border-gray-50">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.per_page"
          :total="total"
          :page-sizes="[10, 20, 50]"
          layout="total,sizes,prev,pager,next,jumper"
          background
          @current-change="handlePageChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
  </div>
</template>
