<template>
  <view class="flower-list-page">
    <!-- 顶部搜索栏 -->
    <view class="search-header">
      <view class="search-input" @click="navigateToSearch">
        <uni-icons type="search" size="16" color="#999"></uni-icons>
        <text class="placeholder">搜索花束名称、用途</text>
      </view>
    </view>

    <!-- 分类筛选 -->
    <view class="category-bar">
      <scroll-view class="category-scroll" scroll-x="true">
        <view 
          class="category-item" 
          :class="{ active: activeCategory === 'all' }" 
          @click="changeCategory('all')"
        >
          <text>全部</text>
        </view>
        <view 
          class="category-item" 
          :class="{ active: activeCategory === 'love' }" 
          @click="changeCategory('love')"
        >
          <text>爱情鲜花</text>
        </view>
        <view 
          class="category-item" 
          :class="{ active: activeCategory === 'birthday' }" 
          @click="changeCategory('birthday')"
        >
          <text>生日祝福</text>
        </view>
        <view 
          class="category-item" 
          :class="{ active: activeCategory === 'friendship' }" 
          @click="changeCategory('friendship')"
        >
          <text>友情鲜花</text>
        </view>
        <view 
          class="category-item" 
          :class="{ active: activeCategory === 'congratulation' }" 
          @click="changeCategory('congratulation')"
        >
          <text>祝贺鲜花</text>
        </view>
        <view 
          class="category-item" 
          :class="{ active: activeCategory === 'apology' }" 
          @click="changeCategory('apology')"
        >
          <text>道歉鲜花</text>
        </view>
      </scroll-view>
    </view>

    <!-- 排序和筛选 -->
    <view class="filter-bar">
      <view class="filter-item" @click="showSortPopup">
        <text>{{ sortOptions[currentSort] }}</text>
        <uni-icons type="arrow-down" size="14" color="#666"></uni-icons>
      </view>
      <view class="filter-item" @click="showFilterPopup">
        <text>筛选</text>
        <uni-icons type="funnel" size="14" color="#666"></uni-icons>
      </view>
    </view>

    <!-- 花束列表 -->
    <view class="flower-list">
      <view class="flower-item" v-for="flower in flowerList" :key="flower.id" @click="navigateToDetail(flower.id)">
        <image :src="flower.image" class="flower-image" mode="aspectFill"></image>
        <view class="flower-info">
          <text class="flower-name">{{ flower.name }}</text>
          <text class="flower-description">{{ flower.description }}</text>
          <view class="flower-price">
            <text class="price-symbol">¥</text>
            <text class="price-value">{{ flower.price }}</text>
          </view>
          <view class="flower-meta">
            <text class="flower-category">{{ getCategoryText(flower.category) }}</text>
            <text class="flower-stock" v-if="flower.stock > 0">库存 {{ flower.stock }} 件</text>
            <text class="flower-soldout" v-else>已售罄</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 加载更多 -->
    <view class="load-more" v-if="hasMore">
      <text>加载中...</text>
    </view>
    <view class="no-more" v-else>
      <text>没有更多数据了</text>
    </view>

    <!-- 排序弹窗 -->
    <uni-popup ref="sortPopup" type="bottom">
      <view class="popup-content">
        <view class="popup-header">
          <text class="popup-title">排序方式</text>
          <uni-icons type="close" size="20" color="#999" @click="closeSortPopup"></uni-icons>
        </view>
        <view class="sort-options">
          <view 
            class="sort-option" 
            v-for="(label, key) in sortOptions" 
            :key="key"
            :class="{ active: currentSort === key }"
            @click="selectSort(key)"
          >
            <text>{{ label }}</text>
            <uni-icons type="checkmark" size="16" color="#007aff" v-if="currentSort === key"></uni-icons>
          </view>
        </view>
      </view>
    </uni-popup>

    <!-- 筛选弹窗 -->
    <uni-popup ref="filterPopup" type="bottom">
      <view class="popup-content">
        <view class="popup-header">
          <text class="popup-title">筛选</text>
          <uni-icons type="close" size="20" color="#999" @click="closeFilterPopup"></uni-icons>
        </view>
        <view class="filter-options">
          <!-- 价格范围 -->
          <view class="filter-section">
            <text class="section-title">价格范围</text>
            <view class="price-range">
              <input 
                type="number" 
                placeholder="最低价" 
                v-model="filterParams.minPrice"
                class="price-input"
              />
              <text class="range-separator">-</text>
              <input 
                type="number" 
                placeholder="最高价" 
                v-model="filterParams.maxPrice"
                class="price-input"
              />
            </view>
          </view>

          <!-- 配送时间 -->
          <view class="filter-section">
            <text class="section-title">配送时间</text>
            <view class="delivery-options">
              <view 
                class="delivery-option"
                v-for="time in deliveryOptions"
                :key="time.value"
                :class="{ active: filterParams.deliveryTime === time.value }"
                @click="toggleDeliveryTime(time.value)"
              >
                <text>{{ time.label }}</text>
              </view>
            </view>
          </view>

          <!-- 花店筛选 -->
          <view class="filter-section">
            <text class="section-title">花店选择</text>
            <view class="shop-options">
              <view 
                class="shop-option"
                v-for="shop in shopOptions"
                :key="shop.value"
                :class="{ active: filterParams.shop === shop.value }"
                @click="toggleShop(shop.value)"
              >
                <text>{{ shop.label }}</text>
              </view>
            </view>
          </view>

          <!-- 操作按钮 -->
          <view class="filter-actions">
            <view class="reset-btn" @click="resetFilter">重置</view>
            <view class="confirm-btn" @click="applyFilter">确认</view>
          </view>
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<script>
import { flowerService } from '../../api/services.js'

export default {
  data() {
    return {
      flowerList: [],
      activeCategory: 'all',
      currentSort: 'default',
      sortOptions: {
        default: '默认排序',
        priceAsc: '价格从低到高',
        priceDesc: '价格从高到低',
        popularity: '销量最高',
        newest: '最新上架'
      },
      filterParams: {
        minPrice: '',
        maxPrice: '',
        deliveryTime: '',
        shop: ''
      },
      deliveryOptions: [
        { label: '立即配送', value: 'immediate' },
        { label: '2小时内', value: '2hours' },
        { label: '4小时内', value: '4hours' },
        { label: '指定时间', value: 'specific' }
      ],
      shopOptions: [
        { label: '浪漫花坊', value: 'romantic' },
        { label: '阳光花店', value: 'sunshine' },
        { label: '幸福花艺', value: 'happiness' },
        { label: '爱心花苑', value: 'lovegarden' }
      ],
      currentPage: 1,
      pageSize: 10,
      hasMore: true,
      loading: false
    }
  },

  onLoad() {
    this.loadFlowerList()
  },

  onReachBottom() {
    if (this.hasMore && !this.loading) {
      this.loadMore()
    }
  },

  methods: {
    async loadFlowerList() {
      this.loading = true
      try {
        const params = {
          page: this.currentPage,
          pageSize: this.pageSize,
          category: this.activeCategory === 'all' ? undefined : this.activeCategory,
          sort: this.currentSort === 'default' ? undefined : this.currentSort,
          ...this.filterParams
        }

        const response = await flowerService.getList(params)
        this.flowerList = response.list || []
        this.hasMore = response.total > this.currentPage * this.pageSize
      } catch (error) {
        console.error('加载花束列表失败:', error)
        uni.showToast({
          title: '加载失败',
          icon: 'none'
        })
      } finally {
        this.loading = false
      }
    },

    async loadMore() {
      this.currentPage++
      this.loading = true
      try {
        const params = {
          page: this.currentPage,
          pageSize: this.pageSize,
          category: this.activeCategory === 'all' ? undefined : this.activeCategory,
          sort: this.currentSort === 'default' ? undefined : this.currentSort,
          ...this.filterParams
        }

        const response = await flowerService.getList(params)
        this.flowerList = [...this.flowerList, ...(response.list || [])]
        this.hasMore = response.total > this.currentPage * this.pageSize
      } catch (error) {
        console.error('加载更多失败:', error)
        this.currentPage--
      } finally {
        this.loading = false
      }
    },

    changeCategory(category) {
      if (this.activeCategory !== category) {
        this.activeCategory = category
        this.currentPage = 1
        this.flowerList = []
        this.loadFlowerList()
      }
    },

    getCategoryText(category) {
      const categoryMap = {
        love: '爱情鲜花',
        birthday: '生日祝福',
        friendship: '友情鲜花',
        congratulation: '祝贺鲜花',
        apology: '道歉鲜花'
      }
      return categoryMap[category] || category
    },

    showSortPopup() {
      this.$refs.sortPopup.open()
    },

    closeSortPopup() {
      this.$refs.sortPopup.close()
    },

    selectSort(sort) {
      this.currentSort = sort
      this.closeSortPopup()
      this.currentPage = 1
      this.flowerList = []
      this.loadFlowerList()
    },

    showFilterPopup() {
      this.$refs.filterPopup.open()
    },

    closeFilterPopup() {
      this.$refs.filterPopup.close()
    },

    toggleDeliveryTime(time) {
      this.filterParams.deliveryTime = this.filterParams.deliveryTime === time ? '' : time
    },

    toggleShop(shop) {
      this.filterParams.shop = this.filterParams.shop === shop ? '' : shop
    },

    applyFilter() {
      this.closeFilterPopup()
      this.currentPage = 1
      this.flowerList = []
      this.loadFlowerList()
    },

    resetFilter() {
      this.filterParams = {
        minPrice: '',
        maxPrice: '',
        deliveryTime: '',
        shop: ''
      }
    },

    navigateToDetail(id) {
      uni.navigateTo({
        url: `/pages/flower/detail?id=${id}`
      })
    },

    navigateToSearch() {
      uni.navigateTo({
        url: '/pages/search/index?type=flower'
      })
    }
  }
}
</script>

<style scoped>
.flower-list-page {
  background-color: #f8f9fa;
  min-height: 100vh;
  padding-bottom: 20rpx;
}

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

.search-input {
  display: flex;
  align-items: center;
  height: 60rpx;
  background-color: #f0f0f0;
  border-radius: 30rpx;
  padding: 0 20rpx;
}

.placeholder {
  font-size: 28rpx;
  color: #999;
  margin-left: 10rpx;
}

.category-bar {
  background-color: #fff;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #eee;
}

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

.category-item {
  display: inline-block;
  padding: 10rpx 25rpx;
  margin: 0 10rpx;
  border-radius: 25rpx;
  background-color: #f8f9fa;
  font-size: 26rpx;
  color: #666;
}

.category-item.active {
  background-color: #ff6b8b;
  color: #fff;
}

.filter-bar {
  display: flex;
  background-color: #fff;
  padding: 20rpx 30rpx;
  border-bottom: 1rpx solid #eee;
}

.filter-item {
  display: flex;
  align-items: center;
  padding: 10rpx 20rpx;
  margin-right: 20rpx;
  border-radius: 20rpx;
  background-color: #f8f9fa;
  font-size: 26rpx;
  color: #666;
}

.flower-list {
  padding: 20rpx;
}

.flower-item {
  background-color: #fff;
  border-radius: 16rpx;
  margin-bottom: 20rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.flower-image {
  width: 100%;
  height: 300rpx;
}

.flower-info {
  padding: 20rpx;
}

.flower-name {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  display: block;
  margin-bottom: 8rpx;
}

.flower-description {
  font-size: 26rpx;
  color: #666;
  display: block;
  margin-bottom: 15rpx;
  line-height: 1.4;
}

.flower-price {
  margin-bottom: 15rpx;
}

.price-symbol {
  font-size: 24rpx;
  color: #ff6b35;
  font-weight: bold;
}

.price-value {
  font-size: 32rpx;
  color: #ff6b35;
  font-weight: bold;
}

.flower-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.flower-category {
  font-size: 24rpx;
  color: #999;
  background-color: #f0f0f0;
  padding: 4rpx 12rpx;
  border-radius: 12rpx;
}

.flower-stock {
  font-size: 24rpx;
  color: #52c41a;
}

.flower-soldout {
  font-size: 24rpx;
  color: #ff4d4f;
}

.load-more,
.no-more {
  text-align: center;
  padding: 30rpx;
  font-size: 26rpx;
  color: #999;
}

/* 弹窗样式 */
.popup-content {
  background-color: #fff;
  border-radius: 24rpx 24rpx 0 0;
  padding: 40rpx;
}

.popup-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30rpx;
}

.popup-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.sort-options {
  margin-bottom: 30rpx;
}

.sort-option {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 25rpx 0;
  border-bottom: 1rpx solid #f0f0f0;
}

.sort-option.active {
  color: #007aff;
}

.filter-section {
  margin-bottom: 30rpx;
}

.section-title {
  display: block;
  font-size: 28rpx;
  color: #333;
  margin-bottom: 20rpx;
  font-weight: bold;
}

.price-range {
  display: flex;
  align-items: center;
  gap: 20rpx;
}

.price-input {
  flex: 1;
  height: 70rpx;
  border: 1rpx solid #ddd;
  border-radius: 8rpx;
  padding: 0 20rpx;
  font-size: 28rpx;
}

.range-separator {
  color: #999;
  font-size: 28rpx;
}

.delivery-options,
.shop-options {
  display: flex;
  flex-wrap: wrap;
  gap: 15rpx;
}

.delivery-option,
.shop-option {
  padding: 15rpx 25rpx;
  border: 1rpx solid #ddd;
  border-radius: 8rpx;
  font-size: 26rpx;
  color: #666;
}

.delivery-option.active,
.shop-option.active {
  border-color: #ff6b8b;
  background-color: #fff0f3;
  color: #ff6b8b;
}

.filter-actions {
  display: flex;
  gap: 20rpx;
  margin-top: 40rpx;
}

.reset-btn,
.confirm-btn {
  flex: 1;
  height: 80rpx;
  border-radius: 40rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28rpx;
  font-weight: bold;
}

.reset-btn {
  border: 1rpx solid #ddd;
  color: #666;
}

.confirm-btn {
  background-color: #ff6b8b;
  color: #fff;
}
</style>