<template>
  <view class="page">
    <u-navbar
      v-if="pageStatus != 'platform'"
      title="商品详情"
      @rightClick="rightClick"
      :autoBack="true"
    >
    </u-navbar>
    <u-navbar
      v-if="pageStatus == 'platform'"
      title="true"
      @rightClick="rightClick"
      :autoBack="true"
    >
      <view slot="center" class="u-top">
        <view
          :class="{ 'goods-Detail': goodsOrDetail == 1 }"
          @click="switchTab(1)"
          >商品</view
        >
        <view
          :class="{ 'goods-Detail': goodsOrDetail == 2 }"
          @click="switchTab(2)"
          >详情</view
        >
      </view>
    </u-navbar>
    <!-- v-if="pageStatus != 'platform'" -->
    <view v-if="!downOrGrounding && pageStatus != 'platform'">
      <view class="top-fail" v-if="commodityDetail.final_status != 'unreview'">
        <image
          src="../../static/mine/332.png"
          class="fail-icon"
          mode=""
        ></image>
        <text v-if="commodityDetail.final_status == 'refuse'"
          >涉及敏感文字，您发布的商品审核未通过</text
        >
        <text v-else-if="!downOrGrounding"
          >该商品已下架，如有需要请重新上架</text
        >
      </view>
      <view class="fail" v-if="commodityDetail.final_status == 'unreview'">
        <image
          src="../../static/mine/333.png"
          class="fail-icon"
          mode=""
        ></image>
        <text>商品还在审核中，请耐心等待</text>
      </view>
    </view>
    <image class="goods-img" :src="commodityDetail.image" mode=""></image>
    <view class="title-price" style="">
      <view class="price" v-if="commodityDetail.discount_price">
        <text style="font-size: 20rpx">折扣价</text>
        <text style="font-size: 18rpx">￥</text>
        <text style="font-size: 36rpx; font-weight: 600">{{
          commodityDetail.discount_price.split(".")[0]
        }}</text
        >.
        <text style="font-size: 18rpx">{{
          commodityDetail.discount_price.split(".")[1]
        }}</text>

        <text
          style="
            font-size: 24rpx;
            color: #ccc;
            text-decoration-line: line-through;
          "
        >
          ￥{{ commodityDetail.price }}
        </text>
      </view>
      <view class="goods-title">
        {{ commodityDetail.name_cn }}
      </view>
    </view>

    <view class="goods-detail" id="product-detail"> 商品详情 </view>
    <view class="detail">
      <view class="u-content">
        <u-parse :content="commodityDetail.detail_cn"></u-parse>
      </view>
    </view>

    <view
      class="footer"
      v-if="
        (!downOrGrounding || pageStatus == 'platform') &&
        commodityDetail.final_status != 'unreview' &&
        pageStatus != 'supply'
      "
    >
      <!-- <button class="btn">编辑商品</button> -->
      <button
        class="btn"
        v-if="pageStatus == 'platform'"
        @click="groundingProduct"
      >
        上架商品
      </button>
      <button class="btn" v-else @click="groundingCommodity">上架商品</button>
    </view>
    <view
      class="footer"
      v-if="
        pageStatus == 'supply' &&
        commodityDetail.final_status != 'unreview' &&
        commodityDetail.final_status != 'refuse'
      "
    >
      <button
        class="btn"
        v-if="commodityDetail.status == 'down'"
        @click="supplyChain('normal')"
      >
        上架商品</button
      ><button
        class="btn"
        v-if="commodityDetail.status == 'normal'"
        @click="supplyChain('down')"
      >
        下架商品
      </button></view
    >
  </view>
</template>

<script>
export default {
  data() {
    return {
      goodsId: 0,
      pageStatus: "", //判断商品是什么商品，平台库或其他
      commodityDetail: {}, //商品详情
      goodsOrDetail: 1,
      scrollTop: 0,
    };
  },
  onLoad(options) {
    this.goodsId = options.goodsId;
    this.pageStatus = options.pageStatus;
    if (options.pageStatus == "platform" || options.pageStatus == "supply") {
      this.getGoodsDetail();
    } else {
      this.getGoodsManageDetail();
    }
  },
  onPageScroll(e) {
    this.scrollTop = e.scrollTop;
  },
  computed: {
    downOrGrounding() {
      //判断供应链商品是否存在审核失败
      if (
        this.pageStatus == "supply" &&
        this.commodityDetail.review_status == "2"
      ) {
        return false;
      }
      if (this.commodityDetail.status == "down") {
        if (
          this.pageStatus == "supply" &&
          this.commodityDetail.review_status == "2"
        ) {
          return false;
        }
        return false;
      } else {
        if (this.commodityDetail.merchant_goods == "down") {
          return false;
        } else {
          return true;
        }
      }
    },
  },
  methods: {
    rightClick() {},
    // 上架 or 下架 供应链商品
    supplyChain(status) {
      uni.$u.http
        .post(`/api/goods/supply_status`, {
          goods_id: this.commodityDetail.id,
          status,
        })
        .then((res) => {
          this.getGoodsDetail();
          uni.showToast({
            title: status == "down" ? "下架成功" : "上架成功",
            icon: "none",
          });
        });
    },
    //获取平台库商品详情
    getGoodsDetail() {
      uni.$u.http
        .get(`/api/goods/platform_goods_details/${this.goodsId}`)
        .then((res) => {
          this.commodityDetail = res;
        });
    },
    switchTab(num) {
      this.goodsOrDetail = num;
      if (num == 1) {
        //返回顶部
        uni.pageScrollTo({
          scrollTop: 0, // 滚动到页面的目标位置  这个是滚动到顶部, 0
          duration: 300, // 滚动动画的时长
        });
      } else {
        uni
          .createSelectorQuery()
          .select("#product-detail")
          .boundingClientRect((data) => {
            //目标位置的节点：类class或者id
            uni
              .createSelectorQuery()
              .select(".page")
              .boundingClientRect((res) => {
                //最外层盒子的节点：类class或者id
                uni.pageScrollTo({
                  duration: 100, //过渡时间
                  scrollTop: data.top - res.top, //到达距离顶部的top值
                  //scrollTop:data.top - res.top,//如果置顶
                });
              })
              .exec();
          })
          .exec();
      }
    },
    //获取商品管理商品详情
    getGoodsManageDetail() {
      uni.$u.http.get(`/api/goods/detail/${this.goodsId}`).then((res) => {
        this.commodityDetail = res;
      });
    },

    //上架平台库商品
    groundingProduct() {
      uni.$u.http
        .post(`/api/goods/elencazione`, {
          goods_id: this.commodityDetail.id,
        })
        .then((res) => {
          this.getGoodsDetail();
          uni.showToast({
            title: "已上架",
            icon: "none",
          });
        });
    },
    //上架商品
    groundingCommodity() {
      if (this.commodityDetail.status == "down") {
        uni.showToast({
          title: "商品已下架，无法上架",
          icon: "none",
        });
        return;
      }
      uni.$u.http
        .post(`/api/goods/elencazione`, {
          goods_id: this.commodityDetail.merchant_goods_id,
        })
        .then((res) => {
          if (this.pageStatus == "platform") {
            this.getGoodsDetail();
          } else {
            this.getGoodsManageDetail();
          }
          uni.showToast({
            title: "已上架",
            icon: "none",
          });
        });
    },
  },
  mounted() {
    // uni.setNavigationBarTitle({
    //   title: "商品详情",
    // });
  },
};
</script>

<style lang="scss" scoped>
.page {
  //background-color: #fff;
  padding-bottom: 150rpx;
  margin-top: 174rpx;
  .u-top {
    display: flex;
    width: 200rpx;
    justify-content: space-between;
    .goods-Detail {
      color: #f83224;
    }
  }
  .top-fail {
    height: 60rpx;
    background-color: #fff1ee;
    padding-left: 24rpx;
    display: flex;
    align-items: center;
    font-size: 24rpx;
    color: #333;

    .fail-icon {
      width: 32rpx;
      height: 32rpx;
      margin-right: 10rpx;
    }
  }

  .fail {
    height: 60rpx;
    background-color: #fff1d7;
    padding-left: 24rpx;
    display: flex;
    align-items: center;
    font-size: 24rpx;
    color: #333;

    .fail-icon {
      width: 32rpx;
      height: 32rpx;
      margin-right: 10rpx;
    }
  }

  .goods-img {
    width: 100%;
    height: 750rpx;
  }
  .title-price {
    background-color: #fff;
    width: 96%;
    margin: 0 auto;
    border-radius: 16rpx;
    padding-bottom: 36rpx;
    margin-top: 20rpx;
    padding-top: 10rpx;
  }

  .price {
    color: #f83224;
    margin-top: 6rpx;
    padding-left: 40rpx;
    font-weight: 600;
  }

  .goods-title {
    padding: 0 40rpx;
    font-size: 32rpx;
    margin-top: 20rpx;
  }

  .goods-detail {
    font-size: 28rpx;
    padding-left: 40rpx;
    background-color: #fff;
    width: 90%;
    margin: 0 auto;
    margin-top: 40rpx;
    border-radius: 16rpx 16rpx 0 0;
    padding-top: 30rpx;
  }

  .detail {
    padding: 24rpx 20rpx;
    background-color: #fff;
    width: 90%;
    margin: 0 auto;
    border-radius: 0 0 16rpx 16rpx;

    .detail-img {
      width: 100%;
    }
  }

  .footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding-bottom: 84rpx;
    padding-top: 20rpx;
    background-color: #fff;

    .btn {
      border-radius: 52rpx;
      background-color: #f83224;
      color: #fff;
      border: none;
      padding: 0;
      margin: 0;
      width: 94%;
      font-size: 30rpx;
      margin: 0 auto;
    }
  }
}
</style>
