<template>
  <div class="mall" :style="bgStyle" style="background-size: 100% 62.933vw;background-repeat: no-repeat;">
    <div class="top">
      <div class="search_box">
        <img src="/img/search.png" alt="">
        <input v-model="searchParams.name" placeholder="请输入关键词搜索"
          @search="$navigate('Category?name=' + searchParams.name)">
        <button @click="$navigate('Category?name=' + searchParams.name)" :style="btnStyle">搜索</button>
      </div>
      <div class="banner">
        <van-swipe :autoplay="3000" indicator-color="white" @change="onSwipeChange">
          <van-swipe-item v-for="item in data.Carousel">
            <a @click="item.link ? $openUrl(item.link) : ''"><img :src="$file(item.img) || '/img/avatar.png'"
                width="100%"></a>
          </van-swipe-item>
        </van-swipe>
      </div>

    </div>

    <div class="cg_bigbox">
      <div class="top">
        <div class="left_box">
          <img src="/img/safity.png" alt="">
          <span>安心购</span>
        </div>
        <div class="right_box">
          <span>官方商城</span> | <span>正品保障</span> | <span>售后无忧</span>
        </div>
      </div>

      <div class="Category_box">
        <div class="Category_item" v-for="item in data.SpuType?.slice(0, 9)"
          @click="$navigate(`Category?id=${item.id}`)">
          <img :src="$file(item.img)">
          <span>{{ item.name }}</span>
        </div>
        <div class="Category_item" @click="$navigate('Category')">
          <img src="/img/mall_i15.png">
          <span>全部分类</span>
        </div>
      </div>
    </div>

    <!-- Style1 -->
    <!-- <div class="banner_style1" v-if="hotData.length > 0">
       Style1
      <div class="top">
        <div class="left">
          <img src="/img/rexiao.png" alt="">
          <span>热门好物抢不停</span>
        </div>
        <span @click="$navigate('Category?SortType=2')">
          更多
          <van-icon name="arrow" />
        </span>
      </div>

      <div class="banner">
        <van-swipe class="my-swipe" :autoplay="3000" show-indicators indicator-color="#565656">
          <van-swipe-item v-for="item in hotData">
            <div class="item" v-for="list in item" @click="$navigate('GoodsDetail?id=' + list.id)">
              <div class="_img">
                <img :src="$file(list.img)" alt="">
                v-if="list.MemberPoint > 0"
                <div class="_point_">
                  <img src="/img/mall-hot-i.png">
                  <div>赠<b>{{ list.gongxianzhi }}</b>贡献值</div>
                </div>
              </div>
              <span class="title">
                {{ list.name }}
              </span>
              <span class="sales">
                已售 {{ list.salenums }}
              </span>
              <span class="oprice">
                ￥{{ list.originalprice.toFixed(2) }}
              </span>
              <div>
                <span class="price">
                  <span class="money">￥</span>
                  {{ list.saleprice?.toFixed(2) }}
                </span>

                <img class="r" src="/img/cart.png" alt="">
              </div>
            </div>
          </van-swipe-item>
        </van-swipe>
      </div>

    </div> -->

    <!-- Style2 -->
    <div class="banner_style2" v-if="hotData.length > 0">
      <div class="top">
        <img src="/img/hot.png" alt="">
        <b>爆款好物</b>
        <p>超多会员已购买</p>
        <span class="r" @click="$navigate('Category?SortType=2')">
          查看更多
          <van-icon name="arrow" />
        </span>
      </div>

      <div class="banner">
        <div class="item" v-for="list in hotData" @click="$navigate('GoodsDetail?id=' + list.id)">
          <img :src="$file(list.img)" alt="">
          <span class="title">
            {{ list.name }}
          </span>
          <span class="oprice">
            ￥{{ list.originalprice?.toFixed(2) }}
          </span>
          <span class="price">
            ￥{{ list.saleprice?.toFixed(2) }}
          </span>
        </div>
      </div>
    </div>

    <div class="goods">
      <van-tabs v-model:active="activeType" :ellipsis="false" @change="changeTab" line-height="2vw" line-width="10.67vw"
        color="#f48fa7" title-active-color="#000" title-inactive-color="#4e4e4e">
        <van-tab name="" title="新品">
        </van-tab>
        <van-tab v-for="item in typeData" :name="item.id" :title="item.name">
        </van-tab>
      </van-tabs>

      <BaseList ref="BaseList" url="/v1/client/EProsClient" :params="searchParams" :parseData="res => res.data.items"
        class="goods_box">
        <template #default="{ item }">
          <div class="good" :key="item.id" @click="$navigate('GoodsDetail?id=' + item.id)">
            <img :src="$file(item.img)" alt="">
            <div>
              <span class="title">
                {{ item.name }}
              </span>
              <span class="oldprice">
                ￥{{ item.originalprice.toFixed(2) }}
              </span>
              <div class="price">
                <div>
                  <span class="money">￥</span>
                  <span v-html="$toMoney(item.saleprice, 'b')"></span>
                </div>
              </div>
            </div>
          </div>
        </template>
      </BaseList>
      <!-- <div class="goods_box">
        <div class="good" v-for="item in goodsList" @click="$navigate('GoodsDetail?id=' + item.id)">
          <img :src="$file(item.Icon)" alt="">
          <div>
            <span class="title">
              {{ item.SpuName }}
            </span>
             v-if="item.MemberPoint > 0"
            <div class="zeng_box">
              <span>赠</span>
              <div>
                {{ item.MemberPoint }}贡献值
              </div>
            </div>
            <span class="oldprice">
              ￥{{ item.OriginPrice.toFixed(2) }}
            </span>
            <div class="price">
              <div>
                <span class="money">￥</span>
                <span v-html="$toMoney(item.Price, 'b')"></span>
              </div>

              <img style="width:5.6vw;height: 5.07vw;margin-bottom: 0;" class="r" src="/img/cart.png" alt="">
            </div>
          </div>

          <div class="sell-out" v-if="item.StockQty <= 0">
            <div>已售罄</div>
          </div>
        </div>
      </div> -->
    </div>

  </div>
</template>

<script>
export default {
  name: 'Mall',

  data() {
    return {
      activeType: '',
      searchParams: {
        mallstate: 3,
        pid: '',
        name: '',
      },
      data: {
        Carousel: [],
        SpuType: [],
      },
      hotData: [],
      typeData: [],
      bgcolor: '',
      _fromDetail: false,
    }
  },

  deactivated() {
    sessionStorage.setItem('mall_scroll', window.scrollY)
  },
  mounted() {
    this.init()
  },
  activated() {
    this.$refs.BaseList?.refresh()
  },
  methods: {
    init() {
      Promise.all([
        this.$get('/v1/client/EProcatesClient').then(res => {
          this.data.SpuType = res.data.filter(i => i.homeshowed)
        }),
        this.$get('/v1/client/CSlidesClient?pid=2').then(res => {
          this.data.Carousel = res.data
          this.bgcolor = res.data?.[0]?.bgcolor || ''
        }),
        this.$get('/v1/client/EProsClient?recommend=true').then(res => {
          this.hotData = res.data.items || []
        }),
        this.$get('/v1/client/EProcatesClient/1').then(res => {
          const filtered = res.data.filter(i => i.homeshowed)
          this.typeData = filtered
          if (filtered.length > 0) {
            this.activeType = filtered[0].id
            this.searchParams.pid = filtered[0].id
          }
        }),
      ]).catch(err => {
        this.$showFailToast(err.message || '加载失败')
      })
    },

    changeTab(type) {
      this.searchParams.pid = type
    },

    onSwipeChange(index) {
      this.bgcolor = this.data.Carousel[index]?.bgcolor || ''
    },

    onSearch() {
      this.$refs.BaseList?.refresh()
    },

    onClear() {
      this.searchParams.name = ''
      this.$refs.BaseList?.refresh()
    },
  },

  computed: {
    bgStyle() {
      return this.bgcolor ? { background: `linear-gradient(${this.bgcolor}, #f5f5f5)` } : {}
    },
    btnStyle() {
      return this.bgcolor ? { background: this.bgcolor } : {}
    },
  },
}
</script>


<style>
.index-bg {
  z-index: -1;
  background-color: #f5f5f5;
  background-size: 100% 53.333vw;
  background-repeat: no-repeat;
  height: 100vh;
  width: 100%;
  position: absolute;
}

.finished-text {
  text-align: center;
  padding: 20px 0;
  color: #999;
  width: 100%;
}
</style>
