<template>
  <div class="shop-container">
    <div class="shop-header">
      <el-input placeholder="请输入商品名称或种类" v-model="searchKeyword" clearable class="product-searcher">
        <template #prefix>
           <el-icon><Search /></el-icon>
        </template>
      </el-input>
      <button class="search-button" @click="searchProducts">搜索</button>
    </div>

    <div class="shop-bar">
      <div class="shop-bar-item" v-for="item in shopBarItems" :key="item.path" @click="$router.push(item.path)">
        <img :src="item.img" :alt="item.name" class="shop-bar-item-img">
        <span class="shop-bar-item-name">{{ item.name }}</span>
      </div>
    </div>

    <div class="shop-tips" @click="$router.push('/coupon')">
      <img src="/imgs/shop/tips.png" alt="提示" class="tips-imgs">
    </div>

    <div class="shop-content">
      <div class="product-content" v-for="item in products" :key="item.id" @click="$router.push(`/productsummary/${item.id}`)">
        <img :src="item.image" :alt="item.name" class="product-content-img">
        <span class="product-content-name">{{ item.name }}</span>
        <div class="price-info">
          <el-icon><Coin /></el-icon>
          <div class="product-content-price">{{ item.points_price }}</div>
        </div>
        <div class="price-info">
          <img src="/imgs/profile/rongdou.png" alt="融豆" class="img-bean">
          <div class="product-content-price">{{ item.rongdou_price }}</div>
        </div>
      </div>
    </div>

    <div class="block">
    </div>

  </div>
</template>

<script setup>
import { Search, Coin } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getImageUrl } from '@/config'
import { ref, onMounted, watch } from 'vue'
import api from '@/utils/api'

const searchKeyword = ref('')
const sortBy = ref('')



const products = ref([])
const loading = ref(false)
const loadingMore = ref(false)
const page = ref(1)
const hasMore = ref(true)
const selectedCategory = ref('all')


const shopBarItems = [
  {name: '商品分类',img: '/imgs/shop/1.png',path: '/productCategory'},
  // {name: '观看直播',img: '/imgs/shop/2.png',path: ''},
  {name: '卡包管理',img: '/imgs/shop/3.png',path: '/couponmanage'},
  // {name: '百亿补贴',img: '/imgs/shop/4.png',path: ''},
  // {name: '现时秒杀',img: '/imgs/shop/5.png',path: ''},
  // {name: '会员专区',img: '/imgs/shop/6.png',path: ''},
  // {name: '商品收藏',img: '/imgs/shop/7.png',path: ''},
  // {name: '充值缴费',img: '/imgs/shop/8.png',path: ''},
  // {name: '资质认证',img: '/imgs/shop/9.png',path: ''},
  {name: '联系客服',img: '/imgs/shop/10.png',path: '/customerservice'},
]

const getProducts = async (isLoadMore = false) => {
  try {
    if (!isLoadMore) {
      loading.value = true
      page.value = 1
    } else {
      loadingMore.value = true
    }
    
    const {data} = await api.get('/products', {
      params: {
        page: page.value,
        limit: 20,
        category: selectedCategory.value === 'all' ? '' : selectedCategory.value,
        search: searchKeyword.value,
        sort: sortBy.value
      }
    })
    
    if (isLoadMore) {
      const newProducts = data.data.products
      // 处理新加载商品图片路径
      newProducts.forEach(product => {
        if (product.image) {
          product.image = getImageUrl(product.image)
        }
      })
      products.value.push(...newProducts)
    } else {
      products.value = data.data.products
      // 处理商品图片路径
      products.value.forEach(product => {
        if (product.image) {
          product.image = getImageUrl(product.image)
        }
      })
    }
    
    hasMore.value = data.data.hasMore
    page.value++
  } catch (error) {
    ElMessage.error('获取商品列表失败')
    console.error('获取商品列表失败:', error)
  } finally {
    loading.value = false
    loadingMore.value = false
  }
}

const searchProducts = () => {
  getProducts(false)
}

watch(searchKeyword, (newVal, oldVal) => {
  if (newVal !== oldVal) {
    getProducts(false)
  }
})

onMounted(() => {
  getProducts()
})

</script>

<style scoped>
.shop-container {
  padding: 20px;
  display: flex;
  flex-direction: column;
  background: #79afff;
  margin-bottom: -100px;
  min-height: 100vh;
}

.shop-header {
  display: flex;
  align-items: center;
  justify-content: center;
}

.product-searcher {
  width: 265px;
  height: 32px;
}

.product-searcher :deep(.el-input__wrapper) {
  border-radius: 400px;
}

.search-button {
  width: 64px;
  height: 28px;
  border-radius: 400px;
  margin-left: 8px;
  color: #fff;
  background: linear-gradient(to right, #7b99ff, #002cc2);
  border: none;
}

.shop-bar {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  column-gap: 21px;
  row-gap: 12px;
  margin-top: 20px;
  background: #FFFFFF80;
  border-radius: 10px;
  width: 92%;
  align-self: center;
  padding: 12px 10px;
}

.shop-bar-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.shop-bar-item-img {
  width: 48px;
  height: 48px;
  margin-bottom: 8px;
}

.shop-bar-item-name {
  font-size: 10px;
  text-align: center;
}

.shop-tips {
  align-self: center;
  margin-top: 15px;
  border-radius: 5px;
  height: 30px;
  width: 92%;
}

.tips-imgs {
  margin-left: -1px;
  width: auto;
  height: auto;
}

.shop-content {
  display: grid;
  grid-template-columns: repeat(2, 153px);
  justify-content: center;
  justify-items: center;
  column-gap: 26px;
  row-gap: 14px;
  align-self: center;
  margin-top: 14px;
  width: 100%;
}

.product-content {
  width: 153px;
  height: 240px;
  border-radius: 5px;
  background-color: #fff;
  display: flex;
  flex-direction: column;
  /* justify-self: center; */
}

.product-content-img {
  width: 100%;
  height: 153px;
  border-radius: 5px 5px 0 0;
  flex-shrink: 0;
}

.product-bottom {
  flex: 1;
  padding: 8px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.product-content-name {
  font-size: 12px;
  text-align: left;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.4;
  min-height: 2.8em;
  padding: 3px 10px;
}

.price-info {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: auto;
  padding: 0 10px;
  padding-bottom: 5px;
}

.product-content-price {
  font-size: 12px;
  color: #ff6b00;
}

.img-bean {
  width: 16px;
  height: 16px;
}

.block {
  height: 100px;
}

</style>

