<template>
  <div>
    <div v-if="type === 'shoppingCar'" class="goods-bottom goods-car">
      <div v-if="noStock" class="no-stock-tip">
        抱歉，当前商品无库存，可查看相似商品
      </div>
      <div class="goods-car-icon" @click="goShopCart">
        <img class="goods-car-icon-img" src="https://img.lkbang.net/cart.b2b405fb.png" alt="car" />
        <span class="goods-car-icon-name">购物车</span>
        <span v-if="cartCount" class="badge">{{ cartCount }}</span>
      </div>
      <div class="goods-car-bottom-group">
        <button
          v-if="!noStock"
          :class="['goods-bottom-car', disabled && 'disabled']"
          @click="addToCart"
        >
          加入购物车
        </button>
        <button v-if="!noStock" :class="['goods-bottom-buy', disabled && 'disabled']" @click="buy">
          {{ shopBtnName }}
        </button>
        <button v-if="noStock" class="similar" @click="goSimilar">
          查看相似
        </button>
      </div>
    </div>
    <div v-if="type === 'submitOrder'" class="goods-bottom">
      <div class="goods-bottom-text">
        <div class="Gb-text-top">
          <span>合计：</span>
          <span>¥</span>
          <span>{{ info.totalPayFee || '0.00' }}</span>
        </div>
      </div>
      <cr-button
        class="goods-bottom-buy confirm-order-btn"
        :disabled="goodsList.length === 0"
        type="primary"
        @click="buy"
        >提交订单</cr-button
      >
    </div>
  </div>
</template>
<script>
import { mapGetters as ShopCartMapGetters } from '@/views/shopCart/shopCartModules.js';
export default {
  name: 'BottomNav',
  props: {
    noStock: {
      type: Boolean,
      default: false
    },

    type: {
      type: String,
      default: 'shopCar' // shoppingCar 购物车；settlement 结算;submitOrder 确认订单
    },
    shopBtnName: {
      type: String,
      default: '立即购买' // shoppingCar 购物车；settlement 结算;submitOrder 确认订单
    },
    info: {
      type: Object,
      default() {
        return {};
      }
    },
    disabled: {
      type: Boolean,
      default: false
    },
    goodsId: {
      type: String,
      default: ''
    },
    goodsList: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  computed: {
    ...ShopCartMapGetters(['cartCount'])
  },
  methods: {
    buy() {
      //  如果操作为提交订单
      if (this.type === 'submitOrder') {
        //  如果购物车有商品
        if (this.goodsList.length > 0) {
          //  触发打开商品弹窗事件
          this.$emit('buy', 'openGoodsPopup');
        }
      } else {
        //  如果操作为购买商品
        //  触发打开商品弹窗事件
        this.$emit('buy', 'openGoodsPopup');
      }
    },
    goSimilar() {
      this.$emit('goSimilar');
    },
    addToCart() {
      this.$emit('addToCart', 'openGoodsPopup');
    },
    goShopCart() {
      setTimeout(() => {
        this.$router.push({ name: 'Cart' });
      }, 500);
    }
  }
};
</script>
<style lang="less" scoped>
.goods-bottom {
  position: fixed;
  left: 0;
  bottom: 0;
  display: flex;
  justify-content: space-evenly;
  align-items: center;
  width: 100%;
  // height: 33px;
  padding-top: 8px;
  padding-bottom: calc(8px + constant(safe-area-inset-bottom));
  padding-bottom: calc(8px + env(safe-area-inset-bottom));
  background: @white;
  border-top: 2px solid #f7f7f7;
  // box-sizing: border-box;
  &-radio {
    display: flex;
    align-items: center;
    justify-content: space-around;
    padding-top: 8px;
    width: 140px;
    radio {
      transform: scale(0.7);
    }
    span {
      font-size: 24px;
      color: #999999;
    }
  }
  &-text {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    flex: 1;
    margin: 0 20px;
    .Gb-text-top {
      display: flex;
      align-items: center;
      span {
        &:nth-child(1) {
          padding-top: 4px;
          .text-size(12);
          color: #333333;
        }
        &:nth-child(2) {
          padding-top: 4px;
          .text-size(12);
          color: #ec3333;
        }
        &:nth-child(3) {
          .text-size(18);
          font-weight: bold;
          color: #ec3333;
        }
      }
    }
    .Gb-text-bottom {
      .text-size(12);
      color: #999999;
    }
  }

  &-car {
    position: relative;
    // border: @border-sm solid @border-color-search;
    // .mx_order_one(@border-color-search, solid, 40px);
    border: 1.5px solid @border-color-search;
    background: @white;
    color: @font-color-search;
  }
  &-buy {
    color: @white;
    background-image: @background-primary;
  }
  button {
    margin: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    // width: 305px;
    height: 40px;
    .text-size(14);
    font-weight: bold;
    border-radius: 20px;
    &.disabled {
      opacity: 0.7;
    }
  }
}
.confirm-order-btn {
  width: 130px !important;
  margin-right: 10px !important;
}
.goods-car {
  width: 100%;
  display: flex;
  .no-stock-tip {
    position: absolute;
    background: #ffece6;
    color: #ec1500;
    font-size: 14px;
    line-height: 25px;
    padding-left: 10px;
    width: 100%;
    box-sizing: border-box;
    top: -25px;
  }
  &-icon {
    width: 70px;
    position: relative;
    height: 100%;
    text-align: center;
    &-img {
      max-width: 24px;
      width: 24px;
      height: 24px;
      display: block;
      margin: 0 auto;
      object-fit: contain;
      position: relative;
      left: -2px;
    }
    &-name {
      .text-size(11);
      color: #999999;
      display: block;
    }
    .badge {
      position: absolute;
      right: 10px;
      top: -8px;
      min-width: 16px;
      border-radius: 16px;
      padding: 2px 3px;
      font-size: 12px;
      color: #fff;
      font-weight: 500;
      text-align: center;
      box-sizing: border-box;
      margin-top: 2px;
      background-color: #ec1500;
    }
  }
  &-bottom-group {
    width: 305px;
    display: flex;
    box-sizing: border-box;
    padding-right: 10px;
    justify-content: space-between;

    .similar {
      width: 100%;
      color: @white;
      background-image: @background-primary;
    }

    .goods-bottom-car,
    .goods-bottom-buy {
      width: 48.5%;
    }
  }
}
</style>
