<!--
  Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.

  https://www.mall4j.com/

  未经允许，不可做商业用途！

  版权所有，侵权必究！
-->

<template>
  <view class="Mall4j module-four-component">
    <template v-if="currentForm">
      <view class="top-image">
        <image src="" />
      </view>
      <view class="bottom-content">
        <template
          v-for="(item, index) in newArr"
          :key="index"
        >
          <view class="bottom-items">
            <image src="" />
            <view class="goods-description">
              商品名称
            </view>
            <view class="bottom-actions">
              <view class="real-price">
                <text>￥</text>
                <text>价格</text>
                <text />
              </view>
              <view class="add-carts">
                <text>+</text>
              </view>
            </view>
          </view>
        </template>
      </view>
    </template>
    <template v-else>
      <view
        class="top-image"
        @tap="handleImg(moduleForm.path)"
      >
        <img-show :src="moduleForm.topImg" />
      </view>
      <view class="bottom-content">
        <template
          v-for="(item, index) in moduleForm.goodsList"
          :key="index"
        >
          <view
            class="bottom-items"
            @tap.stop="handleItems(item)"
          >
            <view class="bottom-items-imgs">
              <img-show
                :src="item.imgs"
                img-mode="aspectFill"
              />
              <view
                v-if="item.status !== 1"
                class="imgs_shelves"
              >
                <image
                  mode="aspectFit"
                  src="@/static/images/been_shelves.png"
                />
              </view>
            </view>
            <view class="goods-description">
              {{ item.name }}
            </view>
            <view class="bottom-actions">
              <view class="real-price">
                <text>￥</text>
                <text>
                  {{ getPrice(item.price, 'left') }}<text class="small">
                    {{ `.${getPrice(item.price, 'right')}` }}
                  </text>
                </text>
              </view>
              <view
                class="add-carts"
                @tap.stop="handleAdd(item)"
              >
                <text>+</text>
              </view>
            </view>
          </view>
        </template>
      </view>
    </template>
  </view>
</template>

<script setup>
import util from '@/utils/util.js'

const props = defineProps({
  config: { // 配置信息
    type: Object,
    default: () => {}
  },
  shopId: {
    type: [Number, String],
    default: 0
  }
})

const newArr = ref(new Array(3)) // 数组占比
const moduleForm = ref({}) // 模块信息
const prodInfo = ref({})

const currentForm = computed(() => {
  return JSON.stringify(moduleForm.value) === '{}'
})

const getPrice = computed(() => {
  return (price, type) => {
    if (!price) return
    price = (price / 100).toFixed(2)
    const point = price.toString().indexOf('.') // 如果为-1则表示没找到
    let leftPrice = ''
    let rightPrice = ''
    if (point === -1) { // 当前是整数
      leftPrice = price
      rightPrice = '00'
    } else {
      leftPrice = price.toString().slice(0, point)
      rightPrice = price.toString().slice(point + 1)
    }
    switch (type) {
      case 'left':
        return leftPrice
      case 'right':
        return rightPrice
      default:
        break
    }
  }
})

watch(() => props.config,
  (newVal) => {
    moduleForm.value = {
      ...newVal
    }
  },
  {
    immediate: true,
    deep: true
  }
)

// 点击图片
const handleImg = (item) => {
  util.toRedirectLink(item.type, item.link, props.shopId)
}
// 点击商品
const handleItems = (item) => {
  if (item.status !== 1) {
    uni.showToast({
      title: '当前商品已下架',
      icon: 'error'
    })
    return
  }
  util.toRedirectLink('1', item.id)
}
// 添加到购物车
const handleAdd = (item) => {
  if (item.status !== 1) {
    uni.showToast({
      title: '当前商品已下架',
      icon: 'error'
    })
    return
  }
  prodInfo.value = item
  util.checkAuthInfo(getProdInfo)
}
const getProdInfo = () => {
  http.request({
    url: '/tmerclub_product/ma/spu/prod_info',
    method: 'GET',
    data: {
      spuId: prodInfo.value.id
    }
  }).then((res) => {
    callChangeItem(res)
  })
}

const callChangeItem = (data) => {
  http.request({
    url: '/tmerclub_product/shop_cart/change_item',
    method: 'POST',
    data: {
      count: 1,
      spuId: data.spuId,
      skuId: data.skus[0].skuId
    }
  }).then(() => {
    util.tapLog(2, { n: 1, sid: props.shopId, bid: data.spuId })
    uni.showToast({
      title: $t('successfullyAddedCart'),
      icon: 'none'
    })
  }).catch((err) => {
    uni.showToast({
      title: err.msg,
      icon: 'none'
    })
  })
}

</script>

<style lang="scss" scoped>
.module-four-component {
  width: 100%;
  height: 100%;
  min-height: 572rpx;
  position: relative;
  overflow: hidden;
  .top-image {
    width: 100%;
    height: 196rpx;
    border-radius: 12rpx 12rpx 0 0;
    overflow: hidden;
    image {
      width: 100%;
      height: 100%;
      background: rgba(235, 237, 240, 0.39);
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
  .bottom-content {
    min-height: 396rpx;
    background-color: #fff;
    display: flex;
    flex-wrap: wrap;
    border-radius: 20rpx;
    padding: 20rpx 16rpx;
    position: relative;
    margin-top: -20rpx;
    :deep(.bottom-items) {
      width: calc((100% - 28rpx) / 3);
      margin-right: 14rpx;
      margin-bottom: 14rpx;
      &:nth-child(3n) {
        margin-right: 0;
      }
      image {
        width: 214rpx;
        height: 200rpx;
        background: rgba(243, 245, 247, 0.39);
        display: flex;
        align-items: center;
        justify-content: center;
      }
      .goods-description {
        font-size: 24rpx;
        height: 68rpx;
        line-height: 34rpx;
        padding: 0 16rpx;
        text-align: center;
        color: #333333;
        font-family: PingFang SC,serif;
        margin: 20rpx 0 8rpx 0;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
        text-overflow: ellipsis;
        word-break: break-all;
      }
      .bottom-actions {
        display: flex;
        justify-content: space-between;
        padding: 0 16rpx;
        align-items: center;
      }
    }
  }
}
.bottom-content {
  .bottom-items {
    .bottom-items-imgs {
      position: relative;
      .imgs_shelves {
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        background: rgba(0, 0, 0, 0.6);
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
    .bottom-actions {
      .real-price {
        color: #E43130;
        width: calc(100% - 44rpx);
        display: flex;
        font-weight: bold;
        align-items: flex-end;
        text {
          font-size: 20rpx;
          font-family: PingFang SC;
          .small {
            font-size: 20rpx;
          }
          &:nth-child(1) {
            padding-bottom: 2rpx;
          }
          &:nth-child(2) {
            font-size: 26rpx;
            max-width: 80%;
            overflow: hidden;
            white-space: nowrap;
            word-break: break-all;
            text-overflow: ellipsis;
            display: block;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 1;
          }
        }
      }
      .add-carts {
        width: 36rpx;
        height: 36rpx;
        line-height: 36rpx;
        border-radius: 50%;
        text-align: center;
        color: #fff;
        font-size: 36rpx;
        background: linear-gradient(180deg, #FF6A37 0%, #FF302A 100%);
        position: relative;
        text {
          transform: translate(-50%, -50%);
        }
      }
    }
  }
}
</style>
