<script setup lang="ts">
import { ref, onMounted, nextTick } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '@/utils/request'
import { userApi } from '@/api/user'
import { Refresh, ArrowRight } from '@element-plus/icons-vue'
import { imgUrl } from '@/utils/request'

// 定义微信转账记录类型
interface TransferRecord {
  id: number
  user_id: number
  mch_id: string
  out_bill_no: string
  transfer_bill_no: string
  appid: string
  state: 'APPLYING' | 'ACCEPTED' | 'PROCESSING' | 'WAIT_USER_CONFIRM' | 'TRANSFERING' | 'SUCCESS' | 'FAIL' | 'CANCELING' | 'CANCELLED'
  state_text: string
  transfer_amount: number
  transfer_remark: string
  fail_reason: string | null
  openid: string | null
  user_name: string | null
  create_time: string
  update_time: string
  fee: number
  user?: {
    nickname?: string
    username?: string
    mobile?: string
    avatar?: string
  }
}

// 定义余额记录类型
interface BalanceRecord {
  id: number
  user_id: number
  type: number
  type_text: string
  amount: number
  balance: number
  description: string
  create_time: string
}

// 定义统计数据类型
interface StatisticsData {
  state: string
  count: number
  amount: number
}

// 响应式数据
const transferList = ref<TransferRecord[]>([])
const statisticsData = ref<StatisticsData[]>([])
const loading = ref(false)
const pagination = ref({
  page: 1,
  limit: 10,
  total: 0
})
const tableRef = ref()
const showScrollHint = ref(false)
const balanceRecordDialogVisible = ref(false)
const balanceRecordLoading = ref(false)
const balanceRecords = ref<BalanceRecord[]>([])
const balanceRecordPagination = ref({
  page: 1,
  limit: 10,
  total: 0
})
const currentUserId = ref<number|null>(null)

// 搜索表单
const searchForm = ref({
  state: '',
  out_bill_no: ''
})

// 组件挂载时获取数据
onMounted(() => {
  fetchTransfers()
  nextTick(() => {
    checkTableScroll()
  })
})

// 检查表格是否需要水平滚动
const checkTableScroll = () => {
  nextTick(() => {
    const tableElement = tableRef.value?.$el
    if (tableElement) {
      const tableBodyWrapper = tableElement.querySelector('.el-table__body-wrapper')
      if (tableBodyWrapper) {
        showScrollHint.value = tableBodyWrapper.scrollWidth > tableBodyWrapper.clientWidth
      }
    }
  })
}

// 处理表格滚动事件
const handleTableScroll = (event: Event) => {
  const target = event.target as HTMLElement
  // 可以在这里添加滚动相关的处理逻辑
}

// 获取转账记录列表
const fetchTransfers = async () => {
  loading.value = true
  try {
    const response = await request({
      url: '/wechatpay/transfer_bill',
      method: 'get',
      params: {
        page: pagination.value.page,
        limit: pagination.value.limit,
        state: searchForm.value.state || undefined,
        out_bill_no: searchForm.value.out_bill_no || undefined
      }
    })
    
    if (response.code === 200) {
      transferList.value = response.page.data || []
      pagination.value.total = response.page.total || 0
      // 更新统计数据
      statisticsData.value = response.data || []
      
      // 检查是否需要显示滚动提示
      nextTick(() => {
        checkTableScroll()
      })
    } else {
      ElMessage.error(response.msg || '获取微信转账记录失败')
    }
  } catch (error) {
    ElMessage.error('获取微信转账记录失败')
    console.error('获取微信转账记录失败:', error)
  } finally {
    loading.value = false
  }
}

// 重置搜索
const resetSearch = () => {
  searchForm.value.state = ''
  searchForm.value.out_bill_no = ''
  pagination.value.page = 1
  fetchTransfers()
}

// 搜索
const handleSearch = () => {
  pagination.value.page = 1
  fetchTransfers()
}

// 格式化金额（从分转换为元）
const formatAmount = (amount: number) => {
  return (amount / 100).toFixed(2)
}

// 格式化余额金额（直接保留两位小数）
const formatBalanceAmount = (amount: number) => {
  return parseFloat(amount.toString()).toFixed(2)
}

// 状态标签类型
const getStateType = (state: string) => {
  switch (state) {
    case 'SUCCESS':
      return 'success'
    case 'FAIL':
      return 'danger'
    case 'PROCESSING':
    case 'TRANSFERING':
      return 'warning'
    case 'APPLYING':
    case 'ACCEPTED':
    case 'WAIT_USER_CONFIRM':
      return 'info'
    default:
      return 'info'
  }
}

// 处理分页变化
const handlePageChange = (page: number) => {
  pagination.value.page = page
  fetchTransfers()
}

// 处理页面大小变化
const handleSizeChange = (size: number) => {
  pagination.value.limit = size
  pagination.value.page = 1
  fetchTransfers()
}

// 同意转账申请
const approveTransfer = async (id: number) => {
  try {
    await ElMessageBox.confirm('确定要同意此转账申请吗？', '确认操作', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    const response = await request({
      url: `/wechatpay/transfer_bill/${id}/approve`,
      method: 'post'
    })
    
    if (response.code === 200) {
      ElMessage.success('操作成功')
      fetchTransfers()
    } else {
      ElMessage.error(response.msg || '操作失败')
    }
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('操作失败')
      console.error('同意转账申请失败:', error)
    }
  }
}

// 驳回转账申请
const rejectTransfer = async (id: number) => {
  try {
    await ElMessageBox.prompt('请输入驳回原因', '驳回操作', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      inputPlaceholder: '请输入驳回原因',
      inputPattern: /\S/,
      inputErrorMessage: '驳回原因不能为空'
    }).then(async ({ value }) => {
      const response = await request({
        url: `/wechatpay/transfer_bill/${id}/reject`,
        method: 'post',
        data: {
          fail_reason: value
        }
      })
      
      if (response.code === 200) {
        ElMessage.success('操作成功')
        fetchTransfers()
      } else {
        ElMessage.error(response.msg || '操作失败')
      }
    }).catch(() => {
      // 用户取消操作
    })
  } catch (error) {
    ElMessage.error('操作失败')
    console.error('驳回转账申请失败:', error)
  }
}

// 刷新转账状态
const refreshTransferStatus = async (id: number) => {
  try {
    const response = await request({
      url: `/wechatpay/transfer_bill/${id}/refresh`,
      method: 'get'
    })
    
    if (response.code === 200) {
      ElMessage.success('刷新成功')
      // 重新获取列表数据以更新状态
      fetchTransfers()
    } else {
      ElMessage.error(response.msg || '刷新失败')
    }
  } catch (error) {
    ElMessage.error('刷新失败')
    console.error('刷新转账状态失败:', error)
  }
}

// 显示余额记录对话框
const showBalanceRecordDialog = (userId: number) => {
  currentUserId.value = userId
  balanceRecordDialogVisible.value = true
  getBalanceRecords()
}

// 关闭余额记录对话框
const closeBalanceRecordDialog = () => {
  balanceRecordDialogVisible.value = false
  balanceRecords.value = []
  balanceRecordPagination.value.page = 1
  balanceRecordPagination.value.total = 0
  currentUserId.value = null
}

// 获取余额记录
const getBalanceRecords = async () => {
  try {
    balanceRecordLoading.value = true
    
    const params = {
      user_id: currentUserId.value,
      page: balanceRecordPagination.value.page,
      limit: balanceRecordPagination.value.limit
    }
    
    const response = await userApi.getUserBalanceList(params)
    if (response.code === 200) {
      balanceRecords.value = response.page?.data || []
      balanceRecordPagination.value.total = response.page?.total || 0
    } else {
      ElMessage.error(response.msg || '获取余额记录失败')
    }
  } catch (error) {
    console.error('获取余额记录失败:', error)
    ElMessage.error('获取余额记录失败')
  } finally {
    balanceRecordLoading.value = false
  }
}

// 余额记录分页处理
const handleBalanceRecordSizeChange = (size: number) => {
  balanceRecordPagination.value.limit = size
  balanceRecordPagination.value.page = 1
  getBalanceRecords()
}

const handleBalanceRecordCurrentChange = (page: number) => {
  balanceRecordPagination.value.page = page
  getBalanceRecords()
}

</script>

<template>
  <div class="wechatpay-transfer">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>微信支付转账记录</span>
          <div class="header-actions">
            <el-button type="primary" :icon="Refresh" @click="fetchTransfers" circle />
          </div>
        </div>
      </template>
      
      <!-- 搜索表单 -->
      <div class="search-bar">
        <el-form :model="searchForm" inline>
          <el-form-item label="商户单号">
            <el-input 
              v-model="searchForm.out_bill_no" 
              placeholder="请输入商户单号" 
              clearable 
              style="width: 200px;"
            />
          </el-form-item>
          <el-form-item label="状态">
            <el-select 
              v-model="searchForm.state" 
              placeholder="请选择状态" 
              clearable 
              style="width: 150px;"
            >
              <el-option label="申请中" value="APPLYING" />
              <el-option label="转账已受理" value="ACCEPTED" />
              <el-option label="转账锁定资金中" value="PROCESSING" />
              <el-option label="待收款用户确认" value="WAIT_USER_CONFIRM" />
              <el-option label="转账中" value="TRANSFERING" />
              <el-option label="转账成功" value="SUCCESS" />
              <el-option label="转账失败" value="FAIL" />
              <el-option label="转账撤销中" value="CANCELING" />
              <el-option label="转账撤销完成" value="CANCELLED" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch" :loading="loading">搜索</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      
      <!-- 统计数据展示 -->
      <div class="stats-cards">
        <el-row :gutter="20" style="margin-bottom: 20px;">
          <el-col :span="6">
            <el-card class="stats-card">
              <div class="stats-item">
                <div class="stats-label">转账成功</div>
                <div class="stats-value success">
                  <div class="count">{{ statisticsData.find(item => item.state === 'SUCCESS')?.count || 0 }} 笔</div>
                  <div class="amount">¥{{ formatAmount(statisticsData.find(item => item.state === 'SUCCESS')?.amount || 0) }}</div>
                </div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card class="stats-card">
              <div class="stats-item">
                <div class="stats-label">转账失败</div>
                <div class="stats-value failed">
                  <div class="count">{{ statisticsData.find(item => item.state === 'FAIL')?.count || 0 }} 笔</div>
                  <div class="amount">¥{{ formatAmount(statisticsData.find(item => item.state === 'FAIL')?.amount || 0) }}</div>
                </div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card class="stats-card">
              <div class="stats-item">
                <div class="stats-label">处理中</div>
                <div class="stats-value processing">
                  <div class="count">{{ (statisticsData.find(item => item.state === 'PROCESSING')?.count || 0) + (statisticsData.find(item => item.state === 'TRANSFERING')?.count || 0) }} 笔</div>
                  <div class="amount">¥{{ formatAmount((statisticsData.find(item => item.state === 'PROCESSING')?.amount || 0) + (statisticsData.find(item => item.state === 'TRANSFERING')?.amount || 0)) }}</div>
                </div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card class="stats-card">
              <div class="stats-item">
                <div class="stats-label">待处理</div>
                <div class="stats-value pending">
                  <div class="count">{{ (statisticsData.find(item => item.state === 'APPLYING')?.count || 0) + (statisticsData.find(item => item.state === 'ACCEPTED')?.count || 0) + (statisticsData.find(item => item.state === 'WAIT_USER_CONFIRM')?.count || 0) }} 笔</div>
                  <div class="amount">¥{{ formatAmount((statisticsData.find(item => item.state === 'APPLYING')?.amount || 0) + (statisticsData.find(item => item.state === 'ACCEPTED')?.amount || 0) + (statisticsData.find(item => item.state === 'WAIT_USER_CONFIRM')?.amount || 0)) }}</div>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
      
      <div class="table-wrapper">
        <div class="table-container">
          <div class="scroll-hint" v-if="showScrollHint">
            <el-icon><ArrowRight /></el-icon>
            <span>左右滑动查看更多</span>
          </div>
          <el-table
            ref="tableRef"
            v-loading="loading"
            :data="transferList"
            style="width: 100%"
            border
            @scroll="handleTableScroll"
          >
            <el-table-column prop="id" label="ID" width="80" />
            <el-table-column label="用户信息" width="240">
              <template #default="scope">
                <div class="user-info-cell">
                  <div class="avatar-container">
                    <el-avatar 
                      v-if="scope.row.user && scope.row.user.avatar" 
                      :src="scope.row.user.avatar.startsWith('http') ? scope.row.user.avatar : `${imgUrl}${scope.row.user.avatar}`" 
                      :size="30" 
                    />
                    <el-avatar 
                      v-else 
                      :size="30"
                    >
                      {{ scope.row.user?.nickname?.charAt(0) || scope.row.user?.username?.charAt(0) || 'U' }}
                    </el-avatar>
                  </div>
                  <div class="user-details">
                    <div class="nickname">{{ scope.row.user?.nickname || '-' }}</div>
                    <div class="username"><span class="label">用户名:</span> {{ scope.row.user?.username || '-' }}</div>
                    <div class="mobile"><span class="label">手机:</span> {{ scope.row.user?.mobile || '-' }}</div>
                    <div class="user-name"><span class="label">姓名:</span> {{ scope.row.user_name || '-' }}</div>
                    <div class="openid"><span class="label">OpenID:</span> {{ scope.row.openid || '-' }}</div>
                  </div>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="转账金额" width="240">
              <template #default="scope">
                <div class="amount-group">
                  <div class="total-amount">
                    <span class="label">总金额：</span>
                    <span class="amount-text">¥{{ formatAmount(scope.row.transfer_amount + scope.row.fee) }}</span>
                  </div>
                  <div class="amount-details">
                    <div class="detail-item">
                      <span class="label">实际到账：</span>
                      <span class="value">¥{{ formatAmount(scope.row.transfer_amount) }}</span>
                    </div>
                    <div class="detail-item">
                      <span class="label">手续费：</span>
                      <span class="value">¥{{ formatAmount(scope.row.fee) }}</span>
                    </div>
                    <div class="detail-item">
                      <el-button 
                        type="primary" 
                        size="small" 
                        link 
                        style="padding: 0"
                        @click="showBalanceRecordDialog(scope.row.user_id)"
                      >
                        资金明细
                      </el-button>
                    </div>
                  </div>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="out_bill_no" label="商户单号" min-width="180" />
            <el-table-column prop="transfer_bill_no" label="微信转账订单号" min-width="180" />
            <el-table-column prop="state_text" label="状态" width="150">
              <template #default="scope">
                <div style="display: flex; align-items: center;">
                  <el-tag :type="getStateType(scope.row.state)">
                    {{ scope.row.state_text }}
                  </el-tag>
                  <!-- 当状态为处理中时显示刷新图标 -->
                  <el-button
                    v-if="['APPLYING', 'ACCEPTED', 'PROCESSING', 'TRANSFERING', 'WAIT_USER_CONFIRM'].includes(scope.row.state)"
                    type="text"
                    :icon="Refresh"
                    size="small"
                    @click.stop="refreshTransferStatus(scope.row.id)"
                    title="刷新状态"
                    style="margin-left: 4px; color: #606266;"
                  />
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="transfer_remark" label="转账备注" min-width="120" />
            <el-table-column prop="fail_reason" label="失败原因" min-width="120" />
            <el-table-column prop="create_time" label="创建时间" width="180" />
            <el-table-column prop="update_time" label="更新时间" width="180" />
            <el-table-column label="操作" width="150" fixed="right">
              <template #default="scope">
                <div v-if="scope.row.state === 'APPLYING'" style="display: flex; gap: 8px;">
                  <el-button size="small" type="primary" @click.stop="approveTransfer(scope.row.id)">同意</el-button>
                  <el-button size="small" type="danger" @click.stop="rejectTransfer(scope.row.id)">驳回</el-button>
                </div>
                <div v-else>-</div>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      
      <!-- 余额记录对话框 -->
      <el-dialog
        v-model="balanceRecordDialogVisible"
        title="资金明细"
        width="800px"
        :before-close="closeBalanceRecordDialog"
      >
        <el-table
          v-loading="balanceRecordLoading"
          :data="balanceRecords"
          style="width: 100%"
          border
          stripe
        >
          <el-table-column prop="id" label="ID" width="80" />
          <el-table-column prop="type_text" label="类型" width="120">
            <template #default="scope">
              <el-tag v-if="scope.row.type === 1" type="success">收入</el-tag>
              <el-tag v-else type="danger">支出</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="amount" label="金额" width="120">
            <template #default="scope">
              <span :class="scope.row.type === 1 ? 'income-amount' : 'expense-amount'">
                {{ scope.row.type === 1 ? '+' : '-' }}¥{{ formatBalanceAmount(Math.abs(scope.row.amount)) }}
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="balance" label="余额" width="120">
            <template #default="scope">
              ¥{{ formatBalanceAmount(scope.row.balance) }}
            </template>
          </el-table-column>
          <el-table-column prop="description" label="备注" min-width="150" />
          <el-table-column prop="create_time" label="创建时间" width="180" />
        </el-table>
        
        <div class="pagination-container" style="margin-top: 20px; display: flex; justify-content: flex-end;">
          <el-pagination
            v-model:current-page="balanceRecordPagination.page"
            v-model:page-size="balanceRecordPagination.limit"
            :page-sizes="[10, 20, 50, 100]"
            :total="balanceRecordPagination.total"
            layout="total, sizes, prev, pager, next, jumper"
            @current-change="handleBalanceRecordCurrentChange"
            @size-change="handleBalanceRecordSizeChange"
          />
        </div>
        
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="closeBalanceRecordDialog">关闭</el-button>
          </div>
        </template>
      </el-dialog>
      
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.limit"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handlePageChange"
          @size-change="handleSizeChange"
        />
      </div>
    </el-card>
  </div>
</template>

<style scoped lang="scss">
.wechatpay-transfer {
  padding: 20px;
  
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: 600;
    font-size: 16px;
  }
  
  .header-actions {
    display: flex;
    gap: 10px;
  }
  
  .search-bar {
    background: #f5f7fa;
    padding: 15px;
    border-radius: 4px;
    margin-bottom: 20px;
  }
  
  .table-container {
    position: relative;
    
    .scroll-hint {
      position: absolute;
      top: 50%;
      right: 10px;
      transform: translateY(-50%);
      background: rgba(64, 158, 255, 0.8);
      color: white;
      padding: 5px 10px;
      border-radius: 4px;
      z-index: 10;
      display: flex;
      align-items: center;
      gap: 5px;
      font-size: 12px;
      pointer-events: none;
      animation: fadeInOut 2s infinite;
    }
    
    @keyframes fadeInOut {
      0%, 100% { opacity: 0.7; }
      50% { opacity: 1; }
    }
  }
  
  .amount-group {
    .total-amount {
      display: flex;
      justify-content: space-between;
      font-weight: 600;
      margin-bottom: 4px;
      
      .label {
        color: #606266;
      }
      
      .amount-text {
        font-size: 16px;
        color: #e74c3c;
      }
    }
    
    .amount-details {
      background-color: #f5f7fa;
      border-radius: 4px;
      padding: 6px 8px;
      
      .detail-item {
        display: flex;
        justify-content: space-between;
        font-size: 12px;
        margin-bottom: 2px;
        
        &:last-child {
          margin-bottom: 0;
        }
        
        .label {
          color: #909399;
        }
        
        .value {
          color: #606266;
        }
      }
    }
  }
  
  .stats-cards {
    .stats-card {
      text-align: center;
      
      .stats-item {
        .stats-label {
          color: #909399;
          font-size: 14px;
          margin-bottom: 8px;
        }
        
        .stats-value {
          font-weight: bold;
          
          .count {
            font-size: 20px;
            margin-bottom: 5px;
          }
          
          .amount {
            font-size: 16px;
          }
          
          &.success {
            .count, .amount {
              color: #67c23a;
            }
          }
          
          &.failed {
            .count, .amount {
              color: #f56c6c;
            }
          }
          
          &.processing {
            .count, .amount {
              color: #e6a23c;
            }
          }
          
          &.pending {
            .count, .amount {
              color: #409eff;
            }
          }
        }
      }
    }
  }
  
  .amount-text {
    font-size: 16px;
    font-weight: 600;
    color: #e74c3c;
  }
  
  .income-amount {
    color: #67c23a;
    font-weight: 600;
  }
  
  .expense-amount {
    color: #f56c6c;
    font-weight: 600;
  }
  
  :deep(.el-form--inline .el-form-item) {
    margin-right: 15px;
    margin-bottom: 10px;
  }
  
  :deep(.el-table) {
    .el-table__cell {
      padding: 8px 0;
    }
    
    // 添加更好的滚动条样式
    &::-webkit-scrollbar {
      height: 8px;
      width: 8px;
    }
    
    &::-webkit-scrollbar-thumb {
      background-color: #c1c1c1;
      border-radius: 4px;
      
      &:hover {
        background-color: #a8a8a8;
      }
    }
    
    &::-webkit-scrollbar-track {
      background-color: #f1f1f1;
      border-radius: 4px;
    }
  }
  
  // 添加阴影效果提示还有更多内容可滚动
  .table-wrapper {
    position: relative;
    
    &::before,
    &::after {
      content: '';
      position: absolute;
      top: 0;
      bottom: 0;
      width: 30px;
      pointer-events: none;
      transition: box-shadow 0.3s;
      z-index: 1;
    }
    
    &::before {
      left: 0;
      box-shadow: inset 10px 0 8px -8px rgba(0, 0, 0, 0.15);
    }
    
    &::after {
      right: 0;
      box-shadow: inset -10px 0 8px -8px rgba(0, 0, 0, 0.15);
    }
  }
  
  .pagination-container {
    display: flex;
    justify-content: flex-end;
    margin-top: 20px;
  }
  
  .user-info-cell {
    display: flex;
    align-items: flex-start;
    
    .avatar-container {
      margin-right: 8px;
      margin-top: 2px;
    }
    
    .user-details {
      flex: 1;
      overflow: hidden;
      
      .nickname {
        font-weight: 500;
        color: #303133;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        margin-bottom: 4px;
      }
      
      .username, .mobile, .openid, .user-name {
        font-size: 12px;
        color: #909399;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        margin-bottom: 2px;
        
        .label {
          color: #606266;
          font-weight: 500;
          margin-right: 4px;
        }
      }
      
      .openid, .user-name {
        font-size: 11px;
        color: #c0c4cc;
      }
    }
  }
}
</style>