<template>
  <view class="mall-container">
    <!-- 顶部搜索框 -->
    <view class="search-header">
      <view class="search-box">
        <view class="search-input">
          <image class="search-icon" src="/static/icons/search.png" mode="aspectFit"></image>
          <input 
            class="input" 
            placeholder="搜索农资商品（如：化肥、除草剂）" 
            placeholder-style="color: #999;"
            v-model="searchKeyword"
            @confirm="handleSearch"
          />
        </view>
      </view>
    </view>

    <!-- 分类导航 -->
    <view class="category-nav">
      <scroll-view 
        class="nav-scroll" 
        scroll-x="true" 
        :show-scrollbar="false"
        :enhanced="true"
        :bounces="false"
        :scroll-with-animation="true"
        :scroll-left="scrollLeft"
      >
        <view class="nav-list">
          <view 
            class="nav-item"
            :class="{ active: activeCategory === item.id }"
            v-for="item in categoryList" 
            :key="item.id"
            @click="switchCategory(item.id)"
          >
            {{ item.name }}
          </view>
        </view>
      </scroll-view>
    </view>

    <!-- 商品展示区 -->
    <view class="goods-container">
      <view class="goods-grid">
        <view 
          class="goods-card"
          v-for="item in filteredGoods" 
          :key="item.id"
          @click="navigateToDetail(item)"
        >
          <view class="goods-image">
            <image :src="item.image" mode="aspectFill"></image>
          </view>
          <view class="goods-info">
            <text class="goods-title">{{ item.title }}</text>
            <text class="goods-desc">{{ item.description }}</text>
            <view class="goods-price">
              <text class="price">¥{{ item.price }}</text>
              <text class="unit">/{{ item.unit }}</text>
            </view>
            <view class="action-btn">了解更多</view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      searchKeyword: '',
      activeCategory: 'recommend',
      scrollLeft: 0,
      
      // 分类列表
      categoryList: [
        { id: 'recommend', name: '推荐' },
        { id: 'seeds', name: '种子' },
        { id: 'fertilizer', name: '肥料' },
        { id: 'pesticide', name: '农药' },
        { id: 'film', name: '农膜' },
        { id: 'parts', name: '农机配件' }
      ],
      
      // 商品列表
      goodsList: [
        {
          id: 1,
          title: '高产玉米种子',
          description: '优质杂交种，抗病性强，适应性广',
          price: '68',
          unit: '袋',
          image: '/static/images/products/corn-seeds-new.jpg',
          category: 'seeds'
        },
        {
          id: 2,
          title: '复合肥料NPK',
          description: '15-15-15配比，全营养均衡',
          price: '120',
          unit: '袋',
          image: '/static/images/products/fertilizer.jpg',
          category: 'fertilizer'
        },
        {
          id: 3,
          title: '植物保护剂',
          description: '广谱除草，效果持久',
          price: '45',
          unit: '瓶',
          image: '/static/images/products/plant-protection.jpg',
          category: 'pesticide'
        },
        {
          id: 4,
          title: '优质水稻种子',
          description: '高产稳产，米质优良',
          price: '85',
          unit: '袋',
          image: '/static/images/products/seeds-packets.jpg',
          category: 'seeds'
        },
        {
          id: 5,
          title: '有机肥料',
          description: '纯天然有机，改良土壤',
          price: '95',
          unit: '袋',
          image: '/static/images/products/organic-fertilizer-new.jpg',
          category: 'fertilizer'
        },
        {
          id: 6,
          title: '农用工具套装',
          description: '多功能农具，提高作业效率',
          price: '285',
          unit: '套',
          image: '/static/images/products/agriculture-tools.jpg',
          category: 'pesticide'
        },
        {
          id: 7,
          title: '温室农膜',
          description: '保温保湿，延长生长期',
          price: '125',
          unit: '卷',
          image: '/static/images/products/greenhouse-film.jpg',
          category: 'film'
        },
        {
          id: 8,
          title: '农业机械配件',
          description: '耐用配件，维护设备正常运行',
          price: '350',
          unit: '套',
          image: '/static/images/products/farming-equipment.jpg',
          category: 'parts'
        }
      ]
    }
  },
  
  computed: {
    filteredGoods() {
      let goods = this.goodsList;
      
      // 按分类筛选
      if (this.activeCategory !== 'recommend') {
        goods = goods.filter(item => item.category === this.activeCategory);
      }
      
      // 按搜索关键词筛选
      if (this.searchKeyword.trim()) {
        const keyword = this.searchKeyword.trim().toLowerCase();
        goods = goods.filter(item => 
          item.title.toLowerCase().includes(keyword) || 
          item.description.toLowerCase().includes(keyword)
        );
      }
      
      return goods;
    }
  },
  
  methods: {
    // 切换分类
    switchCategory(categoryId) {
      this.activeCategory = categoryId;
      // 滚动到当前分类位置
      this.$nextTick(() => {
        this.scrollToActiveCategory(categoryId);
      });
    },
    
    // 滚动到激活的分类  
    scrollToActiveCategory(categoryId) {
      const index = this.categoryList.findIndex(item => item.id === categoryId);
      if (index > -1) {
        // 计算滚动位置，让当前分类居中显示
        const itemWidth = 120; // 每个分类项大约120rpx宽度
        const containerWidth = 750; // 屏幕宽度
        const scrollLeft = Math.max(0, index * itemWidth - containerWidth / 2 + itemWidth / 2);
        
        // 设置scroll-view的scrollLeft
        this.scrollLeft = scrollLeft;
      }
    },
    
    // 搜索处理
    handleSearch() {
      if (!this.searchKeyword.trim()) {
        uni.showToast({
          title: '请输入搜索关键词',
          icon: 'none'
        });
        return;
      }
      // 搜索逻辑已在computed中处理
    },
    
    // 导航到商品详情
    navigateToDetail(goods) {
      uni.navigateTo({
        url: `/pages/service/mall-detail?id=${goods.id}&title=${encodeURIComponent(goods.title)}`
      });
    }
  },
  
  onNavigationBarTitleText() {
    return '农资商城';
  }
}
</script>

<style lang="scss">
.mall-container {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.search-header {
  background-color: #fff;
  padding: 20rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.search-box {
  position: relative;
}

.search-input {
  display: flex;
  align-items: center;
  background-color: #f8f8f8;
  border-radius: 32rpx;
  padding: 16rpx 24rpx;
  
  .search-icon {
    width: 32rpx;
    height: 32rpx;
    margin-right: 16rpx;
  }
  
  .input {
    flex: 1;
    font-size: 28rpx;
    line-height: 1.5;
  }
}

.category-nav {
  background-color: #fff;
  border-bottom: 1rpx solid #f0f0f0;
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav-scroll {
  white-space: nowrap;
  height: 88rpx;
  width: 100%;
  overflow: hidden;
  -webkit-overflow-scrolling: touch; /* iOS滑动优化 */
  scroll-behavior: smooth; /* 平滑滚动 */
}

.nav-list {
  display: inline-flex;
  padding: 0 20rpx;
  height: 100%;
  align-items: center;
  min-width: 100%;
}

.nav-item {
  flex-shrink: 0;
  padding: 24rpx 32rpx;
  margin-right: 8rpx;
  font-size: 28rpx;
  color: #666;
  font-weight: 500;
  position: relative;
  white-space: nowrap;
  transition: all 0.2s ease;
  border-radius: 8rpx;
  
  &:active {
    background-color: #f0f0f0;
    transform: scale(0.95);
  }
  
  &.active {
    color: #4CAF50;
    font-weight: bold;
    background-color: #f8fdf8;
  }
  
  &.active::after {
    content: '';
    position: absolute;
    bottom: 8rpx;
    left: 50%;
    transform: translateX(-50%);
    width: 40rpx;
    height: 4rpx;
    background-color: #4CAF50;
    border-radius: 2rpx;
  }
}

.goods-container {
  padding: 20rpx;
}

.goods-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20rpx;
}

.goods-card {
  background-color: #fff;
  border-radius: 16rpx;
  overflow: hidden;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
  transition: transform 0.2s ease;
  
  &:active {
    transform: scale(0.98);
  }
}

.goods-image {
  width: 100%;
  height: 240rpx;
  
  image {
    width: 100%;
    height: 100%;
  }
}

.goods-info {
  padding: 24rpx;
}

.goods-title {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
  line-height: 1.4;
  display: block;
  margin-bottom: 8rpx;
}

.goods-desc {
  font-size: 24rpx;
  color: #666;
  line-height: 1.4;
  display: block;
  margin-bottom: 16rpx;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.goods-price {
  display: flex;
  align-items: baseline;
  margin-bottom: 20rpx;
  
  .price {
    font-size: 32rpx;
    font-weight: bold;
    color: #4CAF50;
  }
  
  .unit {
    font-size: 24rpx;
    color: #999;
    margin-left: 4rpx;
  }
}

.action-btn {
  background-color: #4CAF50;
  color: #fff;
  text-align: center;
  padding: 16rpx 0;
  border-radius: 8rpx;
  font-size: 28rpx;
  font-weight: 500;
}
</style> 