<template>
  <view class="rate-config-container">
    <NavBar title="平台费率配置" @back="goBack" />
    
    <!-- 模板列表 -->
    <view class="config-list">
      <view 
        v-for="item in rateList" 
        :key="item.id" 
        class="config-item"
        @click="navigateToEditConfig(item.id)"
      >
        <view class="config-info">
          <text class="config-name">{{ item.name }}</text>
          <text class="config-desc">平台费率: {{ (item.feeRate * 100).toFixed(2) }}% | 提现手续费: {{ (item.withdrawalFeeRate * 100).toFixed(2) }}%</text>
        </view>
        <view class="config-status">
          <text class="status-text active">ID: {{ item.id }}</text>
          <AppIcon name="chevron-right" size="18" color="#CCCCCC" />
        </view>
      </view>
    </view>
    
    <!-- 空状态 -->
    <view class="empty-state" v-if="rateList.length === 0 && !loading">
      <view class="empty-icon-wrapper">
        <AppIcon name="dollar" size="48" color="#BFBFBF" />
      </view>
      <text class="empty-text">暂无价格模板</text>
      <text class="empty-desc">点击右上角添加模板</text>
    </view>
    
    <!-- 悬浮添加按钮 -->
    <view class="fab-add" @click="navigateToAddConfig">
      <AppIcon name="plus" size="28" color="#FFFFFF" />
    </view>
    
    <!-- 加载状态 -->
    <view class="loading-overlay" v-if="loading">
      <view class="loading-spinner"></view>
      <text class="loading-text">加载中...</text>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getPlatformFeeRateList } from '../../api/rate.js'
import { showToast } from '../../utils/index.js'

const rateList = ref([])
const loading = ref(false)

// 加载费率模板列表
const loadRateConfigs = async () => {
  loading.value = true
  try {
    const res = await getPlatformFeeRateList({
      page: 1,
      pageSize: 100
    })
    
    if (res && res.code === 200) {
      const data = res.data
      rateList.value = data.list || data.records || (Array.isArray(data) ? data : [])
    } else {
      showToast('获取费率模板失败')
    }
  } catch (error) {
    console.error('加载费率模板失败:', error)
    showToast('加载费率模板失败')
  } finally {
    loading.value = false
  }
}

// 导航到添加页面
const navigateToAddConfig = () => {
  uni.navigateTo({
    url: '/pages/setting/rate-config-detail'
  })
}

// 导航到编辑页面
const navigateToEditConfig = (id) => {
  uni.navigateTo({
    url: `/pages/setting/rate-config-detail?id=${id}`
  })
}

// 返回上一页
const goBack = () => {
  uni.navigateBack()
}

onMounted(() => {
  loadRateConfigs()
})
</script>

<style scoped>
.rate-config-container {
  min-height: 100vh;
  background-color: #F5F7FA;
  padding-bottom: 180rpx;
  box-sizing: border-box;
}

/* 悬浮添加按钮 */
.fab-add {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: 60rpx;
  width: 110rpx;
  height: 110rpx;
  background: #C6171E;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 10rpx 30rpx rgba(198, 23, 30, 0.4);
  z-index: 99;
  transition: all 0.3s;
}

.fab-add:active {
  transform: translateX(-50%) scale(0.9);
  box-shadow: 0 4rpx 15rpx rgba(198, 23, 30, 0.3);
}

/* 列表 */
.config-list {
  margin: 20rpx;
  background-color: #FFFFFF;
  border-radius: 16rpx;
  box-shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.06);
}

.config-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 24rpx 30rpx;
  border-bottom: 1rpx solid #F0F0F0;
}

.config-item:last-child {
  border-bottom: none;
}

.config-info {
  flex: 1;
}

.config-name {
  font-size: 30rpx;
  color: #1A1A1A;
  font-weight: 600;
  margin-bottom: 8rpx;
  display: block;
}

.config-desc {
  font-size: 24rpx;
  color: #999999;
  display: block;
}

.config-status {
  display: flex;
  align-items: center;
}

.status-text {
  font-size: 22rpx;
  color: #C6171E;
  background-color: rgba(198, 23, 30, 0.1);
  padding: 4rpx 12rpx;
  border-radius: 12rpx;
  margin-right: 12rpx;
}

.status-text.active {
  color: #52C41A;
  background-color: rgba(82, 196, 26, 0.1);
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 120rpx 0;
  color: #999999;
}

.empty-text {
  font-size: 28rpx;
  margin-bottom: 16rpx;
}

.empty-desc {
  font-size: 24rpx;
}

/* 加载状态 */
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(2px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 999;
}

.loading-text {
  font-size: 28rpx;
  color: #fff;
}
</style>