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

  https://www.mall4j.com/

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

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

<template>
  <view class="module-three-component">
    <!-- 当为空的时候显示 start -->
    <template v-if="currentForm">
      <view class="goods-three-title">
        主标题
      </view>
      <view class="goods-three-items">
        <image src="" />
        <view class="right-content">
          <view class="description">
            商品名称
          </view>
          <view class="right-actions">
            <view class="left-price">
              <view class="real-price">
                <text>￥</text>
                <text>价格</text>
                <text>价格</text>
              </view>
              <view class="del-price">
                原价
              </view>
            </view>
            <view class="right-go">
              <view>去抢购</view>
            </view>
          </view>
        </view>
      </view>
    </template>
    <!-- 当为空的时候显示 end -->
    <template v-else>
      <view class="goods-three-title">
        {{ moduleForm.mainTitle }}
      </view>
      <template
        v-for="(item, index) in moduleForm.goodsList"
        :key="index"
      >
        <view class="goods-three-items">
          <view class="goods-three-items-imgs">
            <img-show :src="item.imgs" />
            <view
              v-if="item.status !== 1"
              class="imgs_shelves"
            >
              <image
                mode="aspectFit"
                src="@/static/images/been_shelves.png"
              />
            </view>
          </view>
          <view class="right-content">
            <view class="description">
              {{ item.name }}
            </view>
            <view class="right-actions">
              <view class="left-price">
                <view class="real-price">
                  <text>{{ '￥' }}</text>
                  <text>
                    {{ getPrice(item.price, 'left') }}<text class="small">
                      {{ `.${getPrice(item.price, 'right')}` }}
                    </text>
                  </text>
                </view>
                <view
                  v-if="item.orignPrice"
                  class="del-price"
                >
                  ￥{{ getPrice(item.orignPrice, 'left') }}.{{ getPrice(item.orignPrice, 'right') }}
                </view>
              </view>
              <view
                class="right-go"
                @tap="handleTap(item)"
              >
                <view>去抢购</view>
              </view>
            </view>
          </view>
        </view>
      </template>
    </template>
  </view>
</template>

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

const props = defineProps({
  config: { // 配置信息
    type: Object,
    default: () => {}
  }
})

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).trim()
      rightPrice = price.toString().slice(point + 1).trim()
    }
    switch (type) {
      case 'left':
        return leftPrice
      case 'right':
        return rightPrice
      default:
        break
    }
  }
})

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

// 点击事件
const handleTap = (item) => {
  if (item.status !== 1) {
    uni.showToast({
      title: '当前商品已下架',
      icon: 'error'
    })
    return
  }
  util.toRedirectLink('1', item.id)
}
</script>

<style lang="scss" scoped>
.module-three-component {
  height: 100%;
  padding: 0 24rpx;
  border-radius: 12rpx;
  overflow: hidden;
  .goods-three-title {
    font-size: 30rpx;
    font-family: PingFang SC,serif;
    font-weight: bold;
    color: #333333;
    margin: 16rpx 0;
  }
  :deep(.goods-three-items) {
    display: flex;
    margin-bottom: 24rpx;
    image {
      width: 204rpx;
      height: 204rpx;
      background: rgba(243, 245, 247, 0.39);
      border-radius: 12rpx;
      overflow: hidden;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .right-content {
      width: calc(100% - 204rpx);
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      padding-left: 20rpx;
      box-sizing: border-box;
      .description {
        width: 90%;
        font-size: 26rpx;
        color: #333;
        font-family: PingFang SC,serif;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
        text-overflow: ellipsis;
        word-break: break-all;
      }
    }
  }
}
.goods-three-items {
  .goods-three-items-imgs {
    position: relative;
  }
  .right-content {
    .right-actions {
      display: flex;
      justify-content: space-between;
      .left-price {
        .real-price {
          text {
            font-size: 22rpx;
            font-weight: bold;
            color: #E13E2D;
            font-family: PingFang SC,serif;
            .small {
              font-size: 22rpx;
            }
            &:nth-child(2) {
              font-size: 26rpx;
            }
          }
        }
        .del-price {
          font-size: 24rpx;
          font-family: PingFang SC,serif;
          font-weight: bold;
          color: #AAAAAA;
          text-decoration: line-through;
          margin-bottom: 6rpx;
          margin-top: 4rpx;
        }
      }
      .right-go {
        display: flex;
        align-items: center;
        view {
          width: 140rpx;
          height: 56rpx;
          background: linear-gradient(270deg, #FF6A37 0%, #FF302A 100%);
          opacity: 1;
          border-radius: 8rpx;
          display: flex;
          align-items: center;
          justify-content: center;
          color: #fff;
          font-family: PingFang SC,serif;
          font-size: 24rpx;
        }
      }
    }
  }
}
.imgs_shelves {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
