<template>
  <!-- 为你推荐 or 商品列表 -->
  <div class="guess">
    <h2 v-if="guessTitle">为你推荐</h2>
    <div class="guessList">
      <div
        class="item"
        v-for="(item, index) in guessList"
        :key="index"
        @click="goDetail(item.goods_id)"
      >
        <div class="pictrue">
          <img v-lazy="imgUrl + item.sku_image" v-if="item.goods_image" />
          <img src="~assets/images/no_goods.jpg" alt v-else />
        </div>

        <div style="padding: 0 15px">
          <div class="name">{{ item.goods_name }}</div>
          <div class="kc">
            <span>库存：{{ item.stock }}</span>
            <span> 起订：{{ item.min_buy }} </span>
          </div>
          <div class="kc">
            <span>产地：{{ item.address }}</span>
          </div>
          <div class="kc">
            <span>生产企业：{{ item.business }} </span>
          </div>
          <div class="kc">
            <span>执行标准：{{ item.execu_content.title }}</span>
          </div>
          <div class="jbq" @click.stop.prevent v-if="userInfo.show_price">
            <el-input-number
              v-model="item.num"
              size="mini"
              :min="0"
              @change="numChange"
            ></el-input-number>
          </div>

          <div class="stock" v-if="userInfo.show_price">
            <p>
              <span>{{ showPrice(item) }} /{{ item.unit }}</span>
            </p>

            <img @click.stop.prevent="addCart(item)" src="~assets/images/card.png" alt />
          </div>
        </div>
      </div>
    </div>
    <Page
      :total="Page.total"
      :pageSize="Page.pageSize"
      :currentPage="Page.currentPage"
      :emptyShow="false"
      @currentChange="currentChange"
    />
  </div>
</template>
<script>
import Setting from "~/setting";
export default {
  name: "Guess",
  props: {
    // 显示标题
    guessTitle: {
      type: Boolean,
      default: true,
    },
    // 渲染商品数组
    guessList: {
      type: Array,
      default: [],
    },
    // 分页
    Page: {
      type: Object,
      default: () => ({
        total: 0,
        pageSize: 4,
        currentPage: 1,
      }),
    },
    // 秒杀
    isSeckill: {
      type: Boolean,
      default: false,
    },
    // 拼团
    isCollage: {
      type: Boolean,
      default: false,
    },
  },

  data() {
    return {
      isSignin: localStorage.getItem("token") ? true : false,
      userInfo: {},
      imgUrl: Setting.uplodBaseURL,
      isSeckillStatus: this.isSeckill,
      isCollageStatus: this.isCollage,
    };
  },
  watch: {
    isSeckill(val) {
      this.isSeckillStatus = val;
    },
    isCollage(val) {
      this.isCollageStatus = val;
    },
  },
  created() {
    // 已登录
    if (this.isSignin) {
      this.userInfo = JSON.parse(localStorage.getItem("userInfo"));
    }
  },
  methods: {
    numChange(e) {
      this.$forceUpdate();
    },
    // 跳转详情
    goDetail(id) {
      this.$router.push({
        path: `/goods_detail/${id}`,
        query: {
          isSeckill: this.isSeckillStatus,
          isCollage: this.isCollageStatus,
        },
      });
    },
    // 添加至购物车
    addCart(row) {
      if (row.num < row.min_buy) {
        this.$message({
          message: "最少不低于起订数量",
          type: "warning",
          offset: 100,
        });
      } else if (row.num == 0) {
        this.$message({
          message: "至少添加一件商品",
          type: "warning",
        });
      } else {
        let { num, sku_id } = { ...row };
        let obj = { num, sku_id };
        this.$axios
          .post(`/cart/add`, obj)
          .then((res) => {
            // console.log(res);
            this.$message({
              message: "成功添加购物车",
              type: "success",
              offset: 100,
            });
          })
          .catch((err) => {
            this.$message.error(err);
          });
      }
    },
    // 分页
    currentChange(e) {
      this.$emit("changePage", e);
    },
    showPrice(data) {
      let price = data.price;
      if (data.discount_price && parseFloat(data.discount_price) < parseFloat(price))
        price = data.discount_price;
      if (data.member_price && parseFloat(data.member_price) < parseFloat(price))
        price = data.member_price;
      return price;
    },
  },
};
</script>
<style lang="scss" scoped>
.guess {
  .guessList {
    display: flex;
    flex-wrap: wrap;
    width: 1200px;
    // justify-content: space-between;
    .item {
      width: 260px;
      min-height: 410px;
      background-color: #fff;
      margin: 10px 20px;
      cursor: pointer;
      margin-bottom: 20px;
      box-shadow: 0px 0px 10px 0px rgba(102, 102, 102, 0.2);
      border-radius: 8px;
      overflow: hidden;
      box-sizing: border-box;
      padding: 10px;
      &:hover {
        box-shadow: 0 3px 20px rgba(0, 0, 0, 0.06);
      }

      .pictrue {
        width: 100%;
        height: 202px;
        position: relative;
        img {
          width: 100%;
          height: 100%;
        }
        .newTag {
          color: white;
          width: 60px;
          height: 30px;
          line-height: 30px;
          text-align: center;
          background: #ff7d02;
          position: absolute;
          top: 0px;
          left: 0px;
          border-bottom-right-radius: 15px;
        }
      }
      .package {
        width: 230px;
        height: 30px;
        background: #d8f6ff;
        border-radius: 3px;
        line-height: 30px;
        span {
          width: 142px;
          height: 17px;
          font-size: 12px;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
          color: #222222;
          padding-left: 10px;
        }
      }
      .name {
        margin-top: 10px;
        font-size: 16px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #222222;
        line-height: 28px;
      }
      .kc {
        margin-top: 10px;
        span {
          font-size: 13px;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
          color: #666666;
          line-height: 20px;
          margin-right: 20px;
        }
      }
      .jbq {
        margin-top: 10px;
        /deep/ .el-input-number {
          width: 100px;
          .el-input-number__decrease {
            width: 26px;
            height: 26px;
            border: none;
            border-radius: 16px;
            background: #d8f6ff;
            .el-icon-minus {
              color: var(--themeColor);
              font-weight: 900;
            }
          }
          .el-input-number__increase {
            width: 26px;
            height: 26px;
            border: none;
            border-radius: 16px;
            background: var(--themeColor);
            .el-icon-plus {
              color: #fff;
              font-weight: 900;
            }
          }
          .el-input {
            .el-input__inner {
              border: none;
            }
          }
        }
      }
      .stock {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-top: 5px;
        img {
          width: 24px;
          height: 24px;
        }

        p {
          font-size: 12px;
          font-family: PingFangSC-Medium, PingFang SC;
          font-weight: 500;
          color: #999;
          span {
            font-size: 18px;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 500;
            color: #ff1d1d;
            line-height: 25px;
          }
        }

        // 秒杀文字
        .seckill_font {
          font-size: 14px;
        }
      }
    }
  }
  h2 {
    text-align: center;
    height: 40px;
    font-size: 28px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #222222;
    line-height: 40px;
    margin-top: 20px;
  }
}
</style>
