<template>
  <view class="rate-config-container">
    <!-- 顶部导航栏 -->
    <view class="header-nav">
      <view class="nav-left">
        <text class="back-icon" @click="goBack">←</text>
      </view>
      <text class="nav-title">价格模板管理</text>
      <view class="nav-right"></view>
    </view>
    
    <!-- 模板列表 -->
    <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>
          <text class="arrow">→</text>
        </view>
      </view>
    </view>
    
    <!-- 空状态 -->
    <view class="empty-state" v-if="rateList.length === 0 && !loading">
      <text class="empty-icon">💰</text>
      <text class="empty-text">暂无价格模板</text>
      <text class="empty-desc">点击右上角添加模板</text>
    </view>
    
    <!-- 悬浮添加按钮 -->
    <view class="fab-add" @click="navigateToAddConfig">
      <text class="plus-icon">+</text>
    </view>
    
    <!-- 加载状态 -->
    <view class="loading-overlay" v-if="loading">
      <text class="loading-spinner">🔄</text>
      <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: #F8F9FA;
  padding-bottom: 180rpx;
  box-sizing: border-box;
}

/* 顶部导航栏 */
.header-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: calc(24rpx + var(--status-bar-height));
  padding-right: 30rpx;
  padding-bottom: 24rpx;
  padding-left: 30rpx;
  background: linear-gradient(135deg, #667EEA 0%, #764BA2 100%);
  box-shadow: 0 4rpx 16rpx rgba(102, 126, 234, 0.2);
}

.nav-left {
  width: 180rpx;
  display: flex;
  align-items: center;
}

.back-icon {
  font-size: 40rpx;
  color: #FFFFFF;
  font-weight: 600;
}

.nav-title {
  font-size: 34rpx;
  color: #FFFFFF;
  font-weight: 600;
  flex: 1;
  text-align: center;
  white-space: nowrap;
}

.nav-right {
  width: 180rpx;
}

/* 悬浮添加按钮 */
.fab-add {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: 60rpx;
  width: 110rpx;
  height: 110rpx;
  background: linear-gradient(135deg, #667EEA 0%, #764BA2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 10rpx 30rpx rgba(102, 126, 234, 0.4);
  z-index: 99;
  transition: all 0.3s;
}

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

.plus-icon {
  font-size: 70rpx;
  color: #FFFFFF;
  font-weight: 300;
  margin-top: -6rpx;
}

/* 列表 */
.config-list {
  margin: 20rpx;
  background-color: #FFFFFF;
  border-radius: 16rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  /* 移除内部 padding */
}

.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: #667EEA;
  background-color: rgba(102, 126, 234, 0.1);
  padding: 4rpx 12rpx;
  border-radius: 12rpx;
  margin-right: 12rpx;
}

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

.arrow {
  font-size: 30rpx;
  color: #CCCCCC;
}

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

.empty-icon {
  font-size: 120rpx;
  margin-bottom: 32rpx;
  opacity: 0.5;
}

.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-spinner {
  font-size: 64rpx;
  animation: spin 1s linear infinite;
  margin-bottom: 32rpx;
  color: #fff;
}

.loading-text {
  font-size: 28rpx;
  color: #fff;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}
</style>