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

  https://www.mall4j.com/

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

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

<template>
  <view class="Mall4j module-five-component">
    <template v-if="currentForm">
      <view class="goods-top">
        <image src="" />
        <image src="" />
      </view>
      <view class="goods-bottom">
        <template
          v-for="(item, index) in newArr"
          :key="index"
        >
          <view class="bottom-items">
            <image src="" />
            <view class="name">
              商品名称
            </view>
            <view class="real-price">
              <text>￥</text>
              <text>价格</text>
              <text />
            </view>
          </view>
        </template>
      </view>
    </template>
    <template v-else>
      <view class="goods-top">
        <img-show
          :src="moduleForm.leftMessage.img"
          :img-mode="'aspectFill'"
          style="margin-right: 18rpx;"
          @handle-tap="()=>handleTap1(moduleForm.leftMessage.path)"
        />
        <img-show
          :src="moduleForm.rightMessage.img"
          :img-mode="'aspectFill'"
          @handle-tap="()=>handleTap1(moduleForm.rightMessage.path)"
        />
      </view>
      <view class="goods-bottom">
        <template
          v-for="(item, index) in moduleForm.goodsList"
          :key="index"
        >
          <view
            class="bottom-items"
            @tap="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="name">
              {{ item.name }}
            </view>
            <view class="real-price">
              <text>￥</text>
              <text>
                {{ getPrice(item.price, 'left') }}<text class="small">
                  {{ `.${getPrice(item.price, 'right')}` }}
                </text>
              </text>
            </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(4))
const moduleForm = 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 handleTap1 = (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)
}
</script>

<style lang="scss" scoped>
.module-five-component {
  width: 100%;
  :deep(.goods-top) {
    display: flex;
    image {
      width: 100%;
      height: 196rpx;
      display: flex;
      align-items: center;
      background: rgba(235, 237, 240, 0.39);
      justify-content: center;
      border-radius: 8rpx;
      overflow: hidden;
      &:last-child {
        margin-right: 0;
      }
    }
  }
  .goods-bottom {
    width: 100%;
    margin-top: 20rpx;
    display: flex;
    flex-wrap: wrap;
    :deep(.bottom-items) {
      width: calc((100% - 54rpx) / 4);
      margin-right: 18rpx;
      height: 240rpx;
      background: #fff;
      border-radius: 8rpx;
      padding: 18rpx 16rpx;
      margin-bottom: 18rpx;
      box-sizing: border-box;
      &:nth-child(4n) {
        margin-right: 0;
      }
      image {
        width: 100%;
        height: 122rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        background: rgba(243, 245, 247, 0.39);
        border-radius: 4rpx;
        overflow: hidden;
      }
      .name {
        width: 100%;
        text-align: center;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        font-size: 24rpx;
        color: #333;
        font-family: PingFang SC,serif;
        margin: 14rpx 0 8rpx 0;
      }
    }
  }
}
.goods-bottom {
  .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;
      }
    }
    .real-price {
      color: #E43130;
      font-weight: bold;
      display: flex;
      justify-content: center;
      align-items: center;
      text {
        font-size: 24rpx;
        .small {
          font-size: 24rpx;
        }
        &: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;
        }
      }
    }
  }
}
</style>
