<template>
  <view class="car-statistics" @click="handleClick">
    <view class="statistics-nums">
      <text class="car-icon iconfont">&#xe600;</text>
      <text class="car-num">{{ totalNum }}</text>
    </view>
    <view class="car-price">
      <text class="price-tag">￥</text>
      <text class="price-num">{{ totalPrice }}</text>
      <!-- 共减
      <text class="price-discounts">9.9</text>
      元 -->
    </view>
    <view class="car-btn" @click.stop="handleConfirmOrder"> 去结算 </view>
  </view>
</template>

<script lang="ts" setup>
import { ref, watch, computed } from "vue";
const props = defineProps({
  cartList: {
    type: Array,
    default: () => [],
  },
});
const cartList = ref<any[]>(props.cartList || []);



watch(
  () => props.cartList,
  (newVal) => {
    cartList.value = newVal || [];
  }
);

const totalNum = computed(() => {
  return cartList.value.reduce((acc, cur) => acc + cur.num, 0);
});

const totalPrice = computed(() => {
  return cartList.value
    .reduce((acc, cur) => acc + cur.num * cur.salePrice, 0)
    .toFixed(2);
  //   return cartList.value.reduce((acc, cur) => acc + cur.num, 0);
});

const handleClick = () => {
  uni.switchTab({
    url: "/pages/tabtar/shoppingCart",
  });
};

const handleConfirmOrder = () => {
  let parendArr = [];
  // let childArr = [[], [], [], [], [], [], [], [], []];
  let dataJson = {};
  cartList.value.map((res, i) => {
    parendArr.push({
      userShoppingCardId: res.id,
      skuHashCode: res.skuHashCode,
      productNum: res.num,
      productId: res.productId,
    });
  });
  dataJson = {
    shippingMethod: 0, //0物流，10自提
    createOrderDetailBos: parendArr,
    marketingType: 0, //0无活动，1秒杀，2拼团
    orderType: 0,
    channelType: 5,
    exchange: false,
    userUsePoint: true,
    businessId: cartList.value[0].businessId,
  };
  // console.log(dataJson);
  // return;
  uni.setStorageSync("dataJson", dataJson);
  uni.navigateTo({
    url: "/pages/surePay/surePay",
  });
};
</script>

<style lang="scss" scoped>
.car-statistics {
  position: fixed;
  left: 0;
  bottom: 0;
  /* #ifdef H5 */
  bottom: 55px;
  /* #endif */
  height: 130rpx;
  width: 100%;
  background-color: #fff;
  padding: 23rpx 23rpx 23rpx 25rpx;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .statistics-nums {
    flex-shrink: 0;
    width: 84rpx;
    height: 84rpx;
    background-color: #eb3532;
    border-radius: 50%;
    // text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    .car-icon {
      font-size: 48rpx;
      background: linear-gradient(180deg, #ffffff, #f5bdbc);
      -webkit-background-clip: text;
      /* 对于webkit内核的浏览器 */
      background-clip: text;
      -webkit-text-fill-color: transparent;
      /* 使文本颜色透明，显示背景渐变 */
      color: transparent;
      /* 兼容非webkit浏览器 */
    }
    .car-num {
      position: absolute;
      right: 0;
      top: 0;
      transform: translate(20%, -20%);
      width: 32rpx;
      height: 32rpx;
      background: #ffffff;
      border: 2rpx solid #eb5153;
      border-radius: 50%;
      font-size: 24rpx;
      font-family: PingFang SC, PingFang SC-Bold;
      font-weight: 700;
      color: #d73636;
      display: flex;
      justify-content: center;
      align-items: center;
      line-height: 1;
    }
  }

  .car-price {
    flex: 1;
    overflow: hidden;
    padding: 0 20rpx 0 40rpx;
    font-size: 28rpx;
    font-family: PingFang SC, PingFang SC-Regular;
    font-weight: 400;
    color: #666666;

    .price-tag {
      font-size: 24rpx;
      font-family: PingFang SC, PingFang SC-Bold;
      color: #da4f4f;
    }

    .price-num {
      font-size: 41rpx;
      // font-family: 汉真广标, 汉真广标-regular;
      font-weight: 700;
      color: #da4f4f;
      padding-right: 15rpx;
    }

    .price-discounts {
      color: #da4f4f;
    }

    // <text class="price-tag">￥</text>
    // <text class="price-num ">9.9</text>
    // 共减
    // <text class="price-discounts">9.9</text>
    // 元
  }

  .car-btn {
    flex-shrink: 0;
    width: 192rpx;
    height: 76rpx;
    background: #e93534;
    border-radius: 10rpx;
    line-height: 76rpx;
    text-align: center;
    font-size: 28rpx;
    font-family: PingFang SC, PingFang SC-Bold;
    font-weight: 700;
    color: #ffffff;
  }
}
</style>
