<template>
  <view class="animal-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 === 'cow' }" 
          @click="changeCategory('cow')"
        >
          <text>牛类</text>
        </view>
        <view 
          class="category-item" 
          :class="{ active: activeCategory === 'sheep' }" 
          @click="changeCategory('sheep')"
        >
          <text>羊类</text>
        </view>
        <view 
          class="category-item" 
          :class="{ active: activeCategory === 'pig' }" 
          @click="changeCategory('pig')"
        >
          <text>猪类</text>
        </view>
        <view 
          class="category-item" 
          :class="{ active: activeCategory === 'chicken' }" 
          @click="changeCategory('chicken')"
        >
          <text>鸡类</text>
        </view>
        <view 
          class="category-item" 
          :class="{ active: activeCategory === 'other' }" 
          @click="changeCategory('other')"
        >
          <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="animal-list">
      <view class="animal-item" v-for="animal in animalList" :key="animal.id" @click="navigateToDetail(animal.id)">
        <image :src="animal.image" class="animal-image" mode="aspectFill"></image>
        <view class="animal-info">
          <text class="animal-name">{{ animal.name }}</text>
          <text class="animal-species">{{ animal.species }}</text>
          <text class="animal-location">{{ animal.location }}</text>
          <view class="animal-price">
            <text class="price-label">认养价格</text>
            <text class="price-value">¥{{ animal.price }}</text>
          </view>
          <view class="animal-status">
            <text class="status-tag" :class="getStatusClass(animal.status)">{{ getStatusText(animal.status) }}</text>
            <text class="hot-tag" v-if="animal.isHot">热门</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="location-options">
              <view 
                class="location-option"
                v-for="location in locationOptions"
                :key="location.value"
                :class="{ active: filterParams.location === location.value }"
                @click="toggleLocation(location.value)"
              >
                <text>{{ location.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 { animalService } from '../../api/services.js'

export default {
  data() {
    return {
      animalList: [],
      activeCategory: 'all',
      currentSort: 'default',
      sortOptions: {
        default: '默认排序',
        priceAsc: '价格从低到高',
        priceDesc: '价格从高到低',
        popularity: '人气最高',
        newest: '最新上架'
      },
      filterParams: {
        minPrice: '',
        maxPrice: '',
        location: ''
      },
      locationOptions: [
        { label: '西藏', value: 'tibet' },
        { label: '内蒙古', value: 'inner_mongolia' },
        { label: '新疆', value: 'xinjiang' },
        { label: '四川', value: 'sichuan' },
        { label: '云南', value: 'yunnan' }
      ],
      currentPage: 1,
      pageSize: 10,
      hasMore: true,
      loading: false
    }
  },

  onLoad() {
    this.loadAnimalList()
  },

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

  methods: {
    async loadAnimalList() {
      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 animalService.getList(params)
        this.animalList = 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 animalService.getList(params)
        this.animalList = [...this.animalList, ...(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.animalList = []
        this.loadAnimalList()
      }
    },

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

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

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

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

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

    toggleLocation(location) {
      this.filterParams.location = this.filterParams.location === location ? '' : location
    },

    applyFilter() {
      this.closeFilterPopup()
      this.currentPage = 1
      this.animalList = []
      this.loadAnimalList()
    },

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

    getStatusClass(status) {
      const statusMap = {
        available: 'available',
        adopted: 'adopted',
        reserved: 'reserved'
      }
      return statusMap[status] || 'available'
    },

    getStatusText(status) {
      const statusTextMap = {
        available: '可认养',
        adopted: '已认养',
        reserved: '预定中'
      }
      return statusTextMap[status] || '可认养'
    },

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

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

<style scoped>
.animal-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: #007aff;
  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;
}

.animal-list {
  padding: 20rpx;
}

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

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

.animal-info {
  padding: 20rpx;
}

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

.animal-species {
  font-size: 28rpx;
  color: #666;
  display: block;
  margin-bottom: 8rpx;
}

.animal-location {
  font-size: 26rpx;
  color: #999;
  display: block;
  margin-bottom: 15rpx;
}

.animal-price {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15rpx;
}

.price-label {
  font-size: 26rpx;
  color: #999;
}

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

.animal-status {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.status-tag {
  padding: 4rpx 12rpx;
  border-radius: 12rpx;
  font-size: 22rpx;
}

.status-tag.available {
  background-color: #e6f7ff;
  color: #1890ff;
}

.status-tag.adopted {
  background-color: #f6ffed;
  color: #52c41a;
}

.status-tag.reserved {
  background-color: #fff2f0;
  color: #ff4d4f;
}

.hot-tag {
  padding: 4rpx 12rpx;
  border-radius: 12rpx;
  background-color: #fff7e6;
  color: #fa8c16;
  font-size: 22rpx;
}

.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;
}

.location-options {
  display: flex;
  flex-wrap: wrap;
  gap: 15rpx;
}

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

.location-option.active {
  border-color: #007aff;
  background-color: #e6f7ff;
  color: #007aff;
}

.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: #007aff;
  color: #fff;
}
</style>