<template>
  <view class="content">
    <!-- 顶部Tab区域 - 只改这里 -->
    <view class="top-tabs-container" v-if="areaSpecialAreaList.length > 1">
      <scroll-view
        class="tabs-scroll"
        scroll-x="true"
        :show-scrollbar="false"
        :scroll-with-animation="true"
      >
        <view class="tabs-wrapper">
          <view
            v-for="(item, index) in areaSpecialAreaList"
            :key="index"
            @click="setSpecial(item)"
            :class="[
              'tab-item',
              specialArea == item.specialArea ? 'tab-active' : '',
            ]"
          >
            <text class="tab-text">{{ item.label }}</text>
          </view>
        </view>
      </scroll-view>
    </view>

    <!-- 保持原来的布局结构 -->
    <view class="index_btm">
      <view class="index_l">
        <view
          v-for="item in oneList"
          :class="['classIfy', oneId == item.pkId ? 'actOne' : '']"
          @click="
            ((oneId = item.pkId),
            getAllGoods(item.pkId),
            getClassIfyTwo(item.pkId))
          "
          :key="item.pkId"
          >{{ item.classifyName }}</view
        >
      </view>
      <view class="index_r">
        <view class="sub-tabs-container">
          <scroll-view
            class="sub-tabs-scroll"
            scroll-x="true"
            :show-scrollbar="false"
            :scroll-with-animation="true"
          >
            <view class="sub-tabs-wrapper">
              <view
                v-for="(item, index) in twoList"
                :key="index"
                @click="((twoId = item.pkId), getAllGoods1(item.pkId))"
                :class="[
                  'sub-tab-item',
                  twoId == item.pkId ? 'sub-tab-active' : '',
                ]"
              >
                <text class="sub-tab-text">{{ item.classifyName }}</text>
              </view>
            </view>
          </scroll-view>
        </view>

        <view class="search">
          <u--input
            placeholder="请输入商品名称"
            placeholderStyle="font-size:14px;"
            prefixIcon="search"
            shape="circle"
            aw
            v-model="waresName"
            border="none"
            @confirm="getAllGoods1()"
            prefixIconStyle="font-size: 22px;color: #909399"
          ></u--input>
        </view>
        <view class="goodList">
          <view
            v-for="item in goodList"
            :key="item.waresCode"
            class="goodList_i"
            @tap="goDetails(item)"
          >
            <view
              class="fly"
              v-show="item.preSaleStatus == 3 || item.isSale == 1"
            ></view>
            <img :src="item.cover1" class="cover" alt="" />
            <view class="goodList_ir">
              <view>
                <span class="qzbq" v-if="item.prefixLabelTarget != undefined">{{
                  item.prefixLabelTarget.label
                }}</span>
                {{ item.waresName }}
              </view>
              <view
                class="pv"
                v-if="
                  specialArea != 18 &&
                  specialArea != 13 &&
                  specialArea != 31 &&
                  specialArea != 10
                "
              >
                业绩:{{ item.waresAchieve | numberToCurrency }}
              </view>
              <view class="pv" v-if="specialArea == 10">
                积分可抵扣:{{ item.deductMoney | numberToCurrency }}
              </view>
              <view
                class="pv"
                v-if="specialArea == 31 && userInfo.isMakerSpace == 0"
              >
                业绩:{{ item.waresAchieve | numberToCurrency }}
              </view>
              <view class="pv" v-if="specialArea == 13">
                BV:{{ item.assAchieve | numberToCurrency }}
              </view>
              <!-- <view class="pv"> 销量:{{ item.sales | seles }} </view> -->
              <view class="goodList_ib">
                <view v-if="specialArea == 31 && userInfo.isMakerSpace == 1">
                  {{ item.vipPrice | numberToCurrency }}
                </view>
                <view
                  v-if="
                    (specialArea == 31 && userInfo.isMakerSpace == 0) ||
                    specialArea != 31
                  "
                >
                  {{ item.waresPrice | numberToCurrency }}
                </view>
                <img
                  @click.stop="addCar(item)"
                  v-show="
                    item.preSaleStatus != 3 &&
                    item.isSale != 1 &&
                    specialArea != 31
                  "
                  src="@/static/images/cart.png"
                  alt=""
                />
                <img
                  @click.stop="addCar(item)"
                  v-show="
                    item.preSaleStatus != 3 &&
                    item.isSale != 1 &&
                    specialArea == 31 &&
                    userInfo.isMakerSpace == 0
                  "
                  src="@/static/images/cart.png"
                  alt=""
                />
              </view>
              <!-- <view class="pv" v-show="specialArea == 31"> 统一零售价:{{ item.retailPrice| numberToCurrency | isLocal}} </view> -->
            </view>
          </view>
          <view class="zhan"></view>
        </view>
      </view>
    </view>

    <selSpaceGoods ref="selSpaceGoods" @getCar="getCatLength"></selSpaceGoods>
    <cartBall
      ref="cart"
      :carLength="shopCarLength"
      :specialArea="specialArea"
    ></cartBall>
  </view>
</template>

<script>
import backIcon from '@/components/backIcon.vue'
import cartBall from '@/components/cartBall.vue'
import { mapGetters, mapActions } from 'vuex'
import * as api from '@/config/goods'
import clTabbar from '@/components/cl-tabbar.vue'
import selSpaceGoods from '@/components/selSpaceGoods.vue'

export default {
  name: 'AreaWrapper',
  props: {
    areaTitle: {
      type: String,
      default: '会员专区',
    },
    // {specialArea: 1, label: '会员专区', children: []}[]
    areaSpecialAreaList: {
      type: Array,
      default: () => [],
    },
    pkCountry: {
      type: Number,
      default: 1,
    },
  },
  components: {
    'cl-tabbar': clTabbar,
    backIcon,
    cartBall,
    selSpaceGoods,
  },
  computed: {},
  filters: {
    seles(value) {
      if (value > 999) {
        return 999 + '+'
      } else {
        return value
      }
    },
  },
  data() {
    return {
      specialArea: 1,
      oneList: [],
      oneId: '',
      twoList: [],
      twoId: '',
      goodList: [],
      shopCarLength: false,
      userInfo: {},
      waresName: '',
    }
  },
  created() {
    this.userInfo = uni.getStorageSync('User')
    this.setSpecial(this.areaSpecialAreaList[0])
    this.$nextTick(() => {
      this.$refs.cart.getCar()
    })
  },
  beforeDestroy() {
    this.$refs.cart.close()
    this.setSmallCarLength(0)
  },
  methods: {
    ...mapActions(['setSmallCarLength', 'setShopCarLength']),
    goShare() {
      uni.navigateTo({
        url: '/pages/specialArea/share?specialArea=7',
      })
    },
    setSpecial(item) {
      this.specialArea =
        item.specialArea || this.areaSpecialAreaList[0]?.specialArea
      this.getClassIfy()
    },
    addCar(item) {
      let carList = {
        pkCountry: this.pkCountry,
        specialArea: item.specialArea,
        number: 1,
        waresCode: item.waresCode,
        productGroup: item.productGroup,
      }
      if (
        item.isMakerGift == 2 &&
        (item.specialArea == 1 || item.specialArea == 3)
      ) {
        this.$refs.selSpaceGoods.getData(carList)
      } else {
        api.addShopping(carList).then(res => {
          if (res.code == 200) {
            uni.showToast({
              title: '购物车添加成功',
              icon: 'success',
              mask: true,
            })
            setTimeout(() => {
              this.$store
                .dispatch('getCarLength', this.specialArea)
                .then(res => {
                  this.shopCarLength = res.data.smallCount
                })
            }, 200)
          }
        })
      }
    },
    getCatLength() {
      this.$store.dispatch('getCarLength', this.specialArea).then(res => {
        this.shopCarLength = res.data.smallCount
      })
    },
    getClassIfy() {
      api
        .classifyList({
          pkCountry: this.pkCountry,
          specialArea: this.specialArea,
          hierarchy: 0,
        })
        .then(res => {
          res.data.unshift({
            classifyName: '全部',
            pkId: '',
          })
          this.oneList = res.data
          this.oneId = this.oneList[0] ? this.oneList[0].pkId : ''
          this.getClassIfyTwo(this.oneList[0].pkId)
          this.getAllGoods(this.oneList[0].pkId)
        })
    },
    // 获取分类二级
    getClassIfyTwo(pkId) {
      api
        .classifyList({
          specialArea: this.specialArea,
          pkParent: pkId,
          hierarchy: 1,
          pkCountry: this.pkCountry,
        })
        .then(res => {
          res.data.unshift({
            classifyName: '全部',
            pkId: pkId,
          })
          this.twoList = res.data
          this.twoId = pkId
        })
    },
    getAllGoods(id) {
      uni.showLoading({
        title: '加载中...',
      })
      api
        .getAllGoods({
          pkCountry: this.pkCountry,
          specialArea: Number(this.specialArea),
          pkAreaClassify: id ? id : '',
        })
        .then(res => {
          this.goodList = res.data
          this.goodList.forEach(item => {
            if (item.waresName.length > 11) {
              item.waresName = item.waresName.substring(0, 11) + '...'
            }
          })
        })
        .finally(() => {
          uni.hideLoading()
        })
    },
    getAllGoods1(id) {
      uni.showLoading({
        title: '加载中...',
      })
      api
        .getAllGoods({
          pkCountry: this.pkCountry,
          specialArea: this.specialArea,
          waresName: this.waresName,
          pkAreaClassify: id ? id : this.oneId,
        })
        .then(res => {
          this.goodList = res.data
          this.goodList.forEach(item => {
            if (item.waresName.length > 11) {
              item.waresName = item.waresName.substring(0, 11) + '...'
            }
          })
        })
        .finally(() => {
          uni.hideLoading()
        })
    },
    goDetails(item) {
      if (item.preSaleStatus != 3 && item.isSale != 1) {
        uni.navigateTo({
          url:
            '/pages/specialArea/details?waresCode=' +
            item.waresCode +
            '&specialArea=' +
            item.specialArea,
        })
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.content {
  background: #f9f9f9;
  height: 93vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* 性冷淡风格Tab设计 */
.top-tabs-container {
  background: #fff;
  border-bottom: 1px solid #f0f0f0;
  position: sticky;
  top: 0;
  z-index: 100;
  padding: 8rpx 0;
}

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

.tabs-wrapper {
  display: inline-flex;
  align-items: center;
  padding: 0 32rpx;
  gap: 0;
}

.tab-item {
  position: relative;
  padding: 6rpx 24rpx;
  background: transparent;
  border: none;
  white-space: nowrap;
  transition: all 0.2s ease;
  margin-right: 32rpx;
}

.tab-item:last-child {
  margin-right: 0;
}

.tab-item::after {
  content: '';
  position: absolute;
  bottom: -16rpx;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 2rpx;
  background: #333;
  transition: width 0.3s ease;
}

.tab-active::after {
  width: 100%;
}

.tab-text {
  font-size: 28rpx;
  font-weight: 400;
  color: #999;
  transition: color 0.2s ease;
}

.tab-active .tab-text {
  color: #005bac;
  font-weight: 500;
}

/* 性冷淡风格左侧菜单美化 - 移动端优化版 */
.index_l {
  width: 198rpx;
  padding: 12rpx 0;
  font-size: 11px;
  font-family:
    PingFang SC-Regular,
    PingFang SC;
  font-weight: 400;
  color: #000000;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  position: absolute;
  height: 86vh;
  background: linear-gradient(180deg, #fafafa 0%, #f5f5f5 100%);
  border-right: 1px solid #f0f0f0;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.02);
}

/* 隐藏滚动条但保持功能 */
.index_l::-webkit-scrollbar {
  width: 0;
  background: transparent;
}

.index_l::-webkit-scrollbar-track {
  background: transparent;
}

.classIfy {
  padding: 24rpx 20rpx;
  margin: 2rpx 8rpx;
  font-size: 24rpx;
  font-family: PingFang SC;
  font-weight: 400;
  color: #666;
  text-align: center;
  border-radius: 12rpx;
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  line-height: 1.2;

  /* 文字截断处理 */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;

  /* 微妙的边框 */
  border: 1px solid transparent;
}

/* 移动端点击反馈 - 替代hover */
.classIfy:active {
  background: rgba(0, 91, 172, 0.04);
  transform: scale(0.98);
}

/* 左侧指示器 */
.classIfy::before {
  content: '';
  position: absolute;
  left: -8rpx;
  top: 50%;
  transform: translateY(-50%);
  width: 0;
  height: 0;
  background: #005bac;
  border-radius: 2rpx;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* 右侧小点缀 */
.classIfy::after {
  content: '';
  position: absolute;
  right: 16rpx;
  top: 50%;
  transform: translateY(-50%) scale(0);
  width: 6rpx;
  height: 6rpx;
  background: #005bac;
  border-radius: 50%;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* 激活状态 - 确保颜色正确 */
.actOne {
  color: #005bac !important;
  background: rgba(0, 91, 172, 0.08) !important;
  font-weight: 500;
  border-color: rgba(0, 91, 172, 0.15);
  box-shadow: 0 2px 8px rgba(0, 91, 172, 0.1);
  transform: translateX(4rpx);
}

.actOne::before {
  width: 4rpx;
  height: 32rpx;
  left: -8rpx;
}

.actOne::after {
  transform: translateY(-50%) scale(1);
}

/* 添加一个顶部标题区域 */
// .index_l::before {
//   content: '商品分类';
//   display: block;
//   padding: 20rpx 0 16rpx 0;
//   font-size: 22rpx;
//   color: #999;
//   text-align: center;
//   font-weight: 500;
//   border-bottom: 1px solid #f0f0f0;
//   margin-bottom: 8rpx;
//   background: rgba(255, 255, 255, 0.8);
//   backdrop-filter: blur(10px);
//   position: sticky;
//   top: 0;
//   z-index: 10;
// }

.index_r {
  flex: 1;
  background: #fff;
  margin-left: 198rpx;
}

.sub-tabs-container {
  background: #fff;
  border-bottom: 1px solid #f0f0f0;
  padding: 16rpx 0;
}

.sub-tabs-scroll {
  white-space: nowrap;
}

.sub-tabs-wrapper {
  display: inline-flex;
  align-items: center;
  padding: 0 32rpx;
  gap: 12rpx;
}

.sub-tab-item {
  padding: 8rpx 20rpx;
  font-size: 26rpx;
  font-weight: 400;
  color: #999;
  background: transparent;
  border: 1px solid #f0f0f0;
  border-radius: 20rpx;
  white-space: nowrap;
  transition: all 0.2s ease;
  position: relative;
}

.sub-tab-item:hover {
  background: #f8f8f8;
  border-color: #e0e0e0;
}

.sub-tab-active {
  color: #005bac;
  background: #f8f9fa;
  border-color: #005bac;
  font-weight: 500;
}

.sub-tab-text {
  font-size: 26rpx;
  font-weight: inherit;
}

.zhan {
  height: 200px;
}

.goodList {
  padding: 0 20rpx;
  height: 95vh;
  overflow-y: auto;
}

.goodList_i {
  display: flex;
  border-bottom: 1px solid #eee;
  padding: 20rpx 0;
  position: relative;
}

.goodList_ir {
  margin-left: 20rpx;
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  font-size: 24rpx;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: #333333;
}

.goodList_ib {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 28rpx;
  font-family: Source Han Sans CN;
  font-weight: 400;
  color: #f82c1a;
}

.goodList_ib img {
  width: 56rpx;
  height: 56rpx;
}

.fly {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  background: #eee;
  opacity: 0.5;
}

.cover {
  width: 152rpx;
  height: 152rpx;
  background: #ffffff;
  border: 1px solid #eeeeee;
  border-radius: 10rpx;
}

.pv {
  font-size: 22rpx;
  font-family: Source Han Sans CN;
  font-weight: 400;
  color: #999999;
}

.qzbq {
  background: #d61820;
  border-radius: 2px 2px 2px 2px;
  font-size: 10px;
  color: #ffffff;
  padding: 0px 5px;
  margin-right: 5px;
}

/* 性冷淡风格搜索框优化 - 小尺寸版 */
.search {
  margin: 16rpx 24rpx 12rpx 24rpx;
  padding: 0;
  background: transparent;
  border-radius: 0;
  font-size: 14px;
  position: relative;
}

/* 搜索框容器美化 */
.search ::v-deep .u-input {
  background: #fff !important;
  border: 1px solid #f0f0f0 !important;
  border-radius: 18rpx !important;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.03) !important;
  transition: all 0.25s ease !important;
  overflow: hidden !important;
}

/* 搜索框聚焦状态 */
.search ::v-deep .u-input--focus {
  border-color: #005bac !important;
  box-shadow: 0 3px 15px rgba(0, 91, 172, 0.08) !important;
  transform: translateY(-1rpx) !important;
}

/* 输入框内部样式 */
.search ::v-deep .u-input__content {
  padding: 12rpx 16rpx !important;
  background: transparent !important;
}

/* placeholder样式优化 */
.search ::v-deep .u-input__content input {
  font-size: 26rpx !important;
  color: #333 !important;
  font-weight: 400 !important;
}

.search ::v-deep .u-input__content input::placeholder {
  color: #999 !important;
  font-size: 24rpx !important;
}

/* 搜索图标样式优化 */
.search ::v-deep .u-input__prefix {
  margin-right: 10rpx !important;
}

.search ::v-deep .u-input__prefix .u-icon {
  color: #666 !important;
  font-size: 28rpx !important;
  transition: color 0.25s ease !important;
}

/* 聚焦时图标颜色变化 */
.search ::v-deep .u-input--focus .u-input__prefix .u-icon {
  color: #005bac !important;
}

/* 添加搜索框前的小标签 */
.search::before {
  content: '搜索';
  position: absolute;
  top: -28rpx;
  left: 8rpx;
  font-size: 20rpx;
  color: #999;
  font-weight: 500;
  background: #fff;
  padding: 0 6rpx;
  z-index: 10;
}

/* 保持原有的其他样式 */
.index_btm {
  flex: 1;
  display: flex;
}
</style>
