<template>
  <!-- 购物车 -->
  <el-popover
    popper-class="header-cart-popover"
    trigger="hover"
    placement="bottom"
    width="330"
    :disabled="cartCount === 0"
  >
    <div
      slot="reference"
      class="header-cart-popover__refrence flex flex-middle"
      @click="onJumpCart"
    >
      <img src="~/assets/img/layout/icon-shop.png" />
      <span>购物车</span>
      <div v-if="cartCount > 0" class="wrap-right-cart__tip">
        {{ cartCount }}
      </div>
    </div>
    <div class="header-cart-products scrollbar-self">
      <div
        v-for="item in products"
        :key="item.id"
        @click="onJumpGoodsDetail(item.product.id)"
        class="header-cart-products__wrap flex flex-middle flex-between"
      >
        <div class="cart-products-wrap__left flex felx-middle">
          <div class="products-wrap-left__cover">
            <img :src="item.productMainPicture" />
          </div>
          <div class="products-wrap-left__info">
            <p class="wrap-left-info__title">{{ item.productName }}</p>
            <div class="wrap-left-info__detail flex">
              <span class="left-info-detail__skuname">{{
                item.productSku.name
              }}</span>
              <span class="left-info-detail__count">{{ item.number }}</span>
            </div>
          </div>
        </div>
        <UiMoney
          class="cart-products-wrap__right"
          :float="true"
          :money="item.product.startingPrice"
        />
      </div>
      <!-- 失效商品 -->
      <template v-if="failureProducts.length > 0">
        <div class="header-cart-products__bar">以下商品已失效</div>
        <div
          v-for="item in failureProducts"
          :key="item.id"
          @click="onJumpGoodsDetail(item.product.id)"
          class="header-cart-products__wrap flex flex-middle flex-between"
        >
          <div class="cart-products-wrap__left flex felx-middle">
            <div class="products-wrap-left__cover">
              <img :src="item.productMainPicture" />
            </div>
            <div class="products-wrap-left__info">
              <p
                class="wrap-left-info__title header-cart-products--failure-color"
              >
                {{ item.productName }}
              </p>
              <div class="wrap-left-info__detail flex">
                <span class="left-info-detail__skuname">{{
                  item.productSku && item.productSku.name
                }}</span>
                <span class="left-info-detail__count">{{ item.number }}</span>
              </div>
            </div>
          </div>
          <UiMoney
            class="header-cart-products--failure-color"
            :float="true"
            :money="item.product.startingPrice"
          />
        </div>
      </template>
    </div>
    <div class="header-cart-bottom flex flex-middle flex-between">
      <p>共{{ cartCount }}件商品</p>
      <UiButton type="red_panel" :radius="true" @click="onJumCartPage"
        >去购物车</UiButton
      >
    </div>
  </el-popover>
</template>
<script>
import { mapState } from "vuex";
import UiButton from "@/components/UiButton.vue";

export default {
  name: "HeaderCart",
  components: { UiButton },
  data() {
    return {
      products: [],
      failureProducts: [], // 失效商品
    };
  },
  computed: {
    ...mapState(["cartProducts", "token"]),
    cartCount() {
      return this.cartProducts.length;
    },
  },
  watch: {
    token: {
      immediate: true,
      handler(val) {
        if (val) {
          this.$store.dispatch("getCartProducts");
          return;
        }
        this.$store.commit("setCartProducts", []);
      },
    },
    cartProducts: {
      immediate: true,
      deep: true,
      handler(val) {
        this.products = [];
        this.failureProducts = [];
        val.forEach((item) => {
          if (item.product.isEnable) {
            if (item.productSku && item.productSku.stock > 0) {
              // 商品未失效且有库存
              this.products.push(item);
              return;
            }
            this.failureProducts.push(item);
            return;
          }
          this.failureProducts.push(item);
        });
      },
    },
  },
  methods: {
    onJumpCart() {
      if (!this.$isLoginValidate()) {
        return;
      }
      this.$router.push("/cart");
    },
    onJumpGoodsDetail(id) {
      this.$router.push(`/goods/detail/${id}`);
    },
    onJumCartPage() {
      this.$router.push("/cart");
    },
  },
};
</script>
<style lang="scss">
.header-cart-popover {
  padding: 16px 20px;
}
</style>
<style lang="scss" scoped>
.header-cart-popover__refrence {
  padding: 0 18px;
  height: 42px;
  line-height: 42px;
  color: #999999;
  border-radius: 8px 8px 8px 8px;
  border: 1px solid #eeeeee;
  cursor: pointer;
  .wrap-right-cart__tip {
    min-width: 14px;
    height: 14px;
    padding: 0 3px;
    font-size: 10px;
    line-height: 14px;
    text-align: center;
    background: #ff512b;
    border-radius: 50%;
    color: #ffffff;
  }
  img {
    width: 16px;
    height: 16px;
    margin: 0 4px 0 10px;
  }
}
.header-cart-popover {
  padding: 20px 16px;
  .header-cart-products {
    padding: 0 10px 50px 0;
    max-height: 360px;
    overflow: auto;
    &--failure-color {
      color: #999999 !important;
    }
    .header-cart-products__wrap {
      margin-bottom: 20px;
      cursor: pointer;
      .cart-products-wrap__left {
        .products-wrap-left__cover {
          width: 54px;
          height: 54px;
          padding: 3px;
          border: 1px solid #eeeeee;
          border-radius: 4px;
          margin-right: 11px;
          img {
            width: 100%;
            height: 100%;
          }
        }
        .products-wrap-left__info {
          font-size: 12px;
          color: #999999;
          .wrap-left-info__title {
            display: block;
            width: 120px;
            @include ellipsis;
            font-size: 14px;
            color: #333333;
            margin-bottom: 10px;
          }
          .wrap-left-info__detail {
            .left-info-detail__skuname {
              display: block;
              width: 70px;
              @include ellipsis;
            }
            .left-info-detail__count {
              &::before {
                content: "X";
                font-size: 8px;
              }
            }
          }
        }
      }
      .cart-products-wrap__right {
        color: #ff512b;
      }
    }
    .header-cart-products__bar {
      font-size: 14px;
      width: 298px;
      height: 40px;
      line-height: 40px;
      padding: 0 11px;
      background: #f8f8f8;
      color: #999999;
      margin-bottom: 20px;
    }
  }
  .header-cart-bottom {
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 50px;
    background: #eeeeee;
    padding: 0 16px;
    font-size: 14px;
    color: #666666;
    /deep/.ui-button {
      width: 84px;
      height: 30px;
      font-size: 14px;
      font-weight: normal;
      padding: 0;
    }
  }
}
</style>
