<template>
  <view class="page">
    <!-- 导航栏 -->
    <NavBar title="商品管理" :showBack="true" />
    
    <!-- 分类筛选 -->
    <view class="filter-section">
      <scroll-view class="category-scroll" scroll-x>
        <view class="category-list">
          <view 
            class="category-item" 
            :class="{ active: currentCategory === '' }"
            @click="changeCategory('')"
          >
            <text>全部</text>
          </view>
          <view 
            class="category-item" 
            v-for="cat in categories" 
            :key="cat.id"
            :class="{ active: currentCategory === cat.name }"
            @click="changeCategory(cat.name)"
          >
            <text>{{ cat.name }}</text>
          </view>
        </view>
      </scroll-view>
    </view>
    
    <!-- 商品列表 -->
    <scroll-view 
      class="product-scroll" 
      scroll-y 
      @scrolltolower="loadMore"
    >
      <view class="product-list">
        <view 
          class="product-card" 
          v-for="product in productList" 
          :key="product.id"
        >
          <view class="product-image">
            <image v-if="product.image" :src="product.image" mode="aspectFill" />
            <view v-else class="image-placeholder">
              <view class="box-icon"></view>
            </view>
          </view>
          
          <view class="product-content">
            <view class="product-info">
              <text class="product-name">{{ product.name }}</text>
              <text class="product-category">{{ product.category }}</text>
            </view>
            
            <view class="product-footer">
              <text class="product-price">¥{{ formatMoney(product.price) }}</text>
              <view class="status-tag" :class="getStatusClass(product.status)">
                <text>{{ getStatusText(product.status) }}</text>
              </view>
            </view>
            
            <view class="product-stats">
              <view class="stat">
                <text class="stat-label">库存</text>
                <text class="stat-value">{{ product.totalStock }}</text>
              </view>
              <view class="stat">
                <text class="stat-label">今日</text>
                <text class="stat-value">{{ product.todaySales }}</text>
              </view>
              <view class="stat">
                <text class="stat-label">月销</text>
                <text class="stat-value">{{ product.monthSales }}</text>
              </view>
            </view>
          </view>
        </view>
      </view>
      
      <view class="loading-more" v-if="loading">
        <view class="loading-spinner"></view>
        <text>加载中...</text>
      </view>
      
      <view class="no-more" v-if="!hasMore && productList.length > 0">
        <text>— 没有更多了 —</text>
      </view>
      
      <view class="empty-state" v-if="!loading && productList.length === 0">
        <view class="empty-icon">
          <view class="empty-icon-inner"></view>
        </view>
        <text class="empty-text">暂无商品数据</text>
      </view>
    </scroll-view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import NavBar from '@/components/NavBar.vue';
import { getProductList, getProductCategories } from '@/api/product';
import { ProductStatusText, ProductStatusColor } from '@/mock/product';
import { formatMoney as formatMoneyUtil } from '@/utils/common';

const productList = ref<any[]>([]);
const categories = ref<any[]>([]);
const currentCategory = ref('');
const loading = ref(false);
const hasMore = ref(true);
const page = ref(1);
const pageSize = 10;

const formatMoney = formatMoneyUtil;

const getStatusText = (status: number) => ProductStatusText[status] || '未知';

const getStatusClass = (status: number) => {
  if (status === 1) return 'active';
  return 'inactive';
};

const loadCategories = async () => {
  try {
    categories.value = await getProductCategories();
  } catch (error) {
    console.error('加载分类失败', error);
  }
};

const changeCategory = (category: string) => {
  currentCategory.value = category;
  page.value = 1;
  hasMore.value = true;
  loadProducts();
};

const loadProducts = async () => {
  loading.value = true;
  try {
    const res = await getProductList({
      page: page.value,
      pageSize,
      category: currentCategory.value
    });
    
    if (page.value === 1) {
      productList.value = res.list;
    } else {
      productList.value = [...productList.value, ...res.list];
    }
    
    hasMore.value = productList.value.length < res.total;
  } catch (error) {
    console.error('加载商品列表失败', error);
  } finally {
    loading.value = false;
  }
};

const loadMore = () => {
  if (loading.value || !hasMore.value) return;
  page.value++;
  loadProducts();
};

onMounted(() => {
  loadCategories();
  loadProducts();
});
</script>

<style lang="scss" scoped>
.page {
  min-height: 100vh;
  background: #f8fafc;
  display: flex;
  flex-direction: column;
}

/* 分类筛选 */
.filter-section {
  padding: 12rpx 0;
  background: #ffffff;
  border-bottom: 1rpx solid #e2e8f0;
}

.category-scroll {
  white-space: nowrap;
}

.category-list {
  display: inline-flex;
  padding: 0 24rpx;
  gap: 10rpx;
  
  .category-item {
    padding: 12rpx 24rpx;
    background: #f8fafc;
    border: 2rpx solid #e2e8f0;
    border-radius: 20rpx;
    font-size: 26rpx;
    color: #64748b;
    transition: all 0.15s;
    
    &.active {
      background: #ecfdf5;
      border-color: #10b981;
      color: #10b981;
    }
  }
}

/* 商品列表 */
.product-scroll {
  flex: 1;
  height: 0;
}

.product-list {
  padding: 16rpx 24rpx;
}

.product-card {
  display: flex;
  background: #ffffff;
  border: 1rpx solid #e2e8f0;
  border-radius: 16rpx;
  margin-bottom: 12rpx;
  overflow: hidden;
  transition: transform 0.15s;
  
  &:active {
    transform: scale(0.98);
  }
  
  .product-image {
    width: 160rpx;
    height: 160rpx;
    flex-shrink: 0;
    
    image {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
    
    .image-placeholder {
      width: 100%;
      height: 100%;
      background: #f8fafc;
      display: flex;
      align-items: center;
      justify-content: center;
      
      .box-icon {
        width: 32rpx;
        height: 32rpx;
        background: #e2e8f0;
        border-radius: 6rpx;
        position: relative;
        
        &::before {
          content: '';
          position: absolute;
          top: -8rpx;
          left: 50%;
          transform: translateX(-50%);
          width: 0;
          height: 0;
          border-left: 10rpx solid transparent;
          border-right: 10rpx solid transparent;
          border-bottom: 10rpx solid #e2e8f0;
        }
      }
    }
  }
  
  .product-content {
    flex: 1;
    padding: 16rpx 20rpx;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }
}

.product-info {
  .product-name {
    display: block;
    font-size: 28rpx;
    font-weight: 600;
    color: #1e293b;
    margin-bottom: 4rpx;
  }
  
  .product-category {
    font-size: 22rpx;
    color: #94a3b8;
  }
}

.product-footer {
  display: flex;
  align-items: center;
  gap: 10rpx;
  margin: 10rpx 0;
  
  .product-price {
    font-size: 28rpx;
    font-weight: 700;
    color: #f97316;
  }
  
  .status-tag {
    padding: 4rpx 12rpx;
    border-radius: 6rpx;
    font-size: 20rpx;
    
    &.active {
      background: #ecfdf5;
      color: #10b981;
    }
    
    &.inactive {
      background: #fff7ed;
      color: #f97316;
    }
  }
}

.product-stats {
  display: flex;
  gap: 20rpx;
  
  .stat {
    .stat-label {
      font-size: 20rpx;
      color: #94a3b8;
      margin-right: 4rpx;
    }
    
    .stat-value {
      font-size: 22rpx;
      color: #475569;
      font-weight: 500;
    }
  }
}

/* 加载状态 */
.loading-more {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12rpx;
  padding: 32rpx;
  color: #94a3b8;
  font-size: 24rpx;
  
  .loading-spinner {
    width: 32rpx;
    height: 32rpx;
    border: 3rpx solid #e2e8f0;
    border-top-color: #10b981;
    border-radius: 50%;
    animation: spin 1s linear infinite;
  }
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.no-more {
  text-align: center;
  padding: 32rpx;
  font-size: 24rpx;
  color: #cbd5e1;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 100rpx 0;
  
  .empty-icon {
    width: 120rpx;
    height: 120rpx;
    background: #f1f5f9;
    border-radius: 24rpx;
    margin-bottom: 20rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    
    .empty-icon-inner {
      width: 40rpx;
      height: 28rpx;
      background: #cbd5e1;
      border-radius: 6rpx;
      position: relative;
      
      &::before {
        content: '';
        position: absolute;
        top: -10rpx;
        left: 50%;
        transform: translateX(-50%);
        width: 0;
        height: 0;
        border-left: 12rpx solid transparent;
        border-right: 12rpx solid transparent;
        border-bottom: 10rpx solid #cbd5e1;
      }
    }
  }
  
  .empty-text {
    font-size: 28rpx;
    color: #94a3b8;
  }
}
</style>
