<template>
  <view class="goods">
    <view class="u-skeleton" v-if="isExsitGoods">
      <view class="u-skeleton-rect">
      
		<swiper
		  indicator-dots
		  :autoplay="false"
		  :interval="1000"
		  style="height:190px;"
		>
		  <block v-for="(item, index) in g.pic_url" :key="index">
			<swiper-item>
			   <image @click="previewImage(index)" style="width:100%;height:100%;"  :src="item.pic_url" mode=""></image>
			</swiper-item>
		  </block>
		</swiper>
        <view class="is_share" v-if="g.share > 0" style="top:60px;">
          <image
            src="@/static/images/icon/WechatIMG424.png"
            style="width: 20px; margin-left: 12px; opacity: 1;height: auto;"
            mode="widthFix"
          />
          <view
            style="
              display: flex;
              flex-direction: column;
              margin-left: 8px;
              font-size: 11px;
              color: #fff;
              opacity: 1;
            "
          >
            <Text>即刻分享赚</Text>
            <Text>￥ {{ g.share }}</Text>
          </view>
        </view>
        <view class="is_share" v-if="g.myBuyCommission > 0">
          <image
            src="@/static/images/icon/WechatIMG424.png"
            style="width: 20px; margin-left: 12px; opacity: 1;height: auto;"
            mode="widthFix"
          />
          <view
            style="
		      display: flex;
		      flex-direction: column;
		      margin-left: 8px;
		      font-size: 11px;
		      color: #fff;
		      opacity: 1;
		    "
          >
            <Text>即刻购买省</Text>
            <Text>￥ {{ g.myBuyCommission }}</Text>
          </view>
        </view>
      </view>
      <view class="g-info">
        <view class="g-name u-skeleton-rect">
          <view
            v-if="g.marketingLogo.iswords&&g.marketingLogo.iswords==1 && g.marketingLogo.words!=''"
            class="Logo"
            :style="{color: g.marketingLogo.wordsColor,
								background:g.marketingLogo.wordsBack ,}"
          >{{g.marketingLogo.words}}</view>
          <image
            v-if="g.marketingLogo.iswords&&g.marketingLogo.iswords==2&& g.marketingLogo.ico!=''"
            mode="heightFix"
            :src="g.marketingLogo.ico"
            style=" height: 12px;margin-right: 5px"
          />
          {{ g.name }}
        </view>
        <view class="g-price-box">
          <view class="left">
            <view class="price u-skeleton-rect" :style="{ color: mc }">
              <text class="small">￥</text>
              <text
                class="numfont"
              >{{ g.price_min == g.price_max ? g.price_min : g.price_min + '-' + g.price_max }}</text>
            </view>
     
            
            <view class="sell u-skeleton-rect">
              <text class="oprice" v-if="setting.is_underline_price == 1">{{ g.original_price }}</text>
              <text v-if="setting.is_sales == 1">已售出{{ g.sales }}{{ g.unit }}</text>
            </view>
          </view>
          <view class="right u-skeleton-rect" @click="openShare" v-if="isAllowShare == 1">
            <image src="@/static/images/icon/icon-share.png" class="share-icon" />
            <text>分享</text>
          </view>
        </view>
      </view>
      <view class="sku-box u-skeleton-rect" style="align-items: flex-start;">
        <view class="label" style="margin-top: 5px;">活动</view>
        <view class="content" style="display: flex;flex-direction: column">
          <view
            style="display: flex;flex-direction: row;align-items: center;margin-bottom: 5px;"
            v-if="g.goods_marketing_award.integral.title != ''"
          >
            <u-tag text="送积分" type="error" />
            <text
              style="margin-left: 5px; font-size: 12px;"
            >{{ g.goods_marketing_award.integral.title }}</text>
          </view>
          <view
            style="display: flex;flex-direction: row;align-items: center"
            v-if="g.goods_marketing_award.PresentFXGradeMsg != ''"
            @click="goVIP"
          >
            <u-tag text="送会员" type="error" />
            <text
              style="margin-left: 5px; font-size: 12px;"
            >{{ g.goods_marketing_award.PresentFXGradeMsg }}</text>
          </view>
        </view>
      </view>
      <view class="sku-box u-skeleton-rect" v-if="comments.length == 0 && setting.is_comment == 1">
        <view class="label">暂无评论信息</view>
      </view>
      <view class="comment2" v-if="comments.length > 0">
        <view class="chead">
          <u-section
            :bold="false"
            
            :show-split="false"
            :title="`商品评价(${comments.length})`"
            sub-title="查看更多"
            @click="clickCommentHandler"
            font-size="24"
          ></u-section>
        </view>
        
		<view class="comment u-skeleton-rect">
			<view class="content" v-for="(item, index) in comments" :key='index'>
			  <view class="u-box">
			    <view class="user">
			      <u-avatar :src="item.UserPhotoPath" :size="50"></u-avatar>
			      <text style="margin:0 10px;overflow: hidden; white-space: nowrap;max-width: 200rpx;text-overflow: ellipsis;margin-bottom: 2px;">{{ item.UserName }}</text>
					  <u-rate
					    :current="item.CommentScore"
					    active-color="#FFC86D"
					    inactive-color="#b2b2b2"
					    active-icon="star"
					    inactive-icon="star-o"
					    size="26"
					  	:disabled="true"
					  ></u-rate>
			    </view>
			    <view class="timer">{{ item.CreateDate }}</view>
			  </view>
			  <view class="ucontent" style="line-height: 22px;">{{ item.Content }}</view>
			  <view class="plimg">
					<image v-for="(x, j) in item.CommentImgList" :key="j" class="plimg-item" mode="aspectFill" :src="x"
					   @click="previewImage2(j,item.CommentImgList)" :style="{'width':san,'height':san}" />
				  
			  </view>
			  <view class="reply" v-if="item.Reply!=''">
				  <view style="font-size: 12px;color: #121212;">回复:</view>
				  <view style="font-size: 12px;color: #9A9A9A;margin-top: 3px;">{{item.Reply}}</view>
			  </view>
			</view>
		</view>
		
      </view>
      <u-divider :margin-top="20" :margin-bottom="20" bg-color="transparent">商品详情</u-divider>
      <view class="details">
        <!-- <rich-text :nodes="detailContent"  @click="clickDescription"></rich-text> -->
        <h-parse :content="detailContent" @navigate="clickDescription"></h-parse>
      </view>
     <view class="submit">
     	<view class="submit-l">
     		<text>金额：</text>
     		<text :style="{'color':mc}">￥<text style='font-size: 20px;'>{{g.price}}</text></text>
     	</view>
     	<u-button
     		size="80"
     		:ripple="true"
     		shape="square"
     		@click="gosubscribe()"
     		:custom-style="{
     			backgroundColor: mc,
     			height: '80rpx',
     			color: '#FFF',
     			fontSize: '14px'
     		}"
     	>
     		立即预约
     	</u-button>
     </view>
    </view>
    <u-empty text="没有找到商品，或商品已下架" mode="favor" v-if="!isExsitGoods"></u-empty>
    <share
      :good-id="id"
      v-if="showShare"
      :good-name="g.name"
      :images="g.pic_url"
      :advertising="g.advertising"
      @close="closeShare"
    ></share>
    <auth v-if="showAuth" @changeuserinfo="reloadUserinfo" @gbAuth="gbAuth"></auth>
    <coupon
      v-if="showCoupons"
      :coupon-message="couponMessage"
      @goLook="goLook"
      @closeBtn="closeBtn"
    ></coupon>
  </view>
</template>

<script>
// #ifdef APP-NVUE
const dom = weex.requireModule("dom");
// #endif

import auth from "@/components/auth/index";
import coupon from "@/components/coupons/coupons";
import share from "./components/share/share";
import hParse from "@/components/u-parse/parse.vue";
export default {
  data() {
    return {
      id: 0,
      loading: true,
      g: {},
      imgs: [],
      mc: "",
      sku: "",
      skuimage: [],
      comments: [],
      secondary: "",
      detailContent: "",
      recommend: [],
      showSku: false,
      ot: 0,
      currentSku: {},
      isExsitGoods: true,
      pageTitle: "商品详情",
      setting: {},
      prevPage: "",
      //自定义接手传递
      opTionObj: {},
      showShare: false,
      msg: {
        pageIndex: 1,
        pageSize: 20,
        GoodsId: 0,
        CommentGrade: 0,
      },
      showAuth: false,
      showCoupons: false,
      couponMessage: "",
      isAllowShare: 1,
      u: {},
      vipState: {},
	  Up:0,
	  san: '100px',
	  StoreId:0,
    };
  },
  components: {
    auth,
    coupon,
    share,
    hParse,
  },
  onLoad(option) {
    this.opTionObj = option;
    console.log(option, "option");

    if (option && option.id) {
      this.id = option.id ? option.id : 29; //40887 59512 46942
    } else {
      this.id = option.GoodsId ? option.GoodsId : 29; //40887 59512 46942
    }
    if (option && option.custom_params) {
      let custom_params = JSON.parse(decodeURIComponent(option.custom_params));
      console.log(custom_params);
      if (custom_params.user_id) {
        uni.setStorageSync("pid", { pid: custom_params.user_id });
      }
	  if (custom_params.Up) {//分享进入校园id
	    uni.setStorageSync("Up", { Up: custom_params.Up });
	  }
    }
    if (option && option.user_id) {
      uni.setStorageSync("pid", { pid: option.user_id });
    }
	if (option && option.SmallShopId) {
	  uni.setStorageSync("SmallShopId", { SmallShopId: option.SmallShopId });
	}
    if (uni.getStorageSync("AnchorName")) {
      //如果有主播名称了先清除
      uni.removeStorageSync("AnchorName");
    }
	if (option && option.Up) {//分享进入校园id
	  uni.setStorageSync("Up", { Up: option.Up });
	}
	this.Up = uni.getStorageSync("mall_UserInfo")?uni.getStorageSync("mall_UserInfo").UserPageType:0;//用于分享出去的参数
    if (option && option.AnchorName) {
      //当传过来有主播名称的时候 存入缓存
      uni.setStorageSync("AnchorName", { AnchorName: option.AnchorName });
    }
    this.u = uni.getStorageSync("mall_UserInfo");
    this.init();
    this.initPage();
    this.getVipId();

    this.mc = this.$uiConfig.mainColor;
    this.secondary = this.$uiConfig.secondary;

    this.setting = uni.getStorageSync("basedata").mall.setting;
	
    wx.showShareMenu({
      withShareTicket: true,
      menus: ["shareAppMessage", "shareTimeline"],
    });
	let windowWidth = this.$utils.SystemInfo().windowWidth
	this.san=(windowWidth - 30-9) / 3 + 'px';
	this.StoreId = uni.getStorageSync("storeId")?uni.getStorageSync("storeId").storeId:0;
  },
  onShareTimeline() {
    setTimeout(() => {
      console.log("分享调用");
      this.getReceive();
    }, 2500);
    let uid = uni.getStorageSync("mall_UserInfo")
      ? uni.getStorageSync("mall_UserInfo").UserId
      : 0;
	  if(uid==0){
	  	uid = uni.getStorageSync("pid")?uni.getStorageSync("pid").pid:0
	  }
	  let SmallShopId = uni.getStorageSync("mall_UserInfo")
	    ? uni.getStorageSync("mall_UserInfo").SmallShopId
	    : 0;
		if(SmallShopId == 0){//如果微店id为0 去找所属微店id
			SmallShopId = uni.getStorageSync("mall_UserInfo")
			  ? uni.getStorageSync("mall_UserInfo").UserSmallShopId
			  : 0;
		}
    let title =
      this.g.app_share_title != null && this.g.app_share_title != ""
        ? this.g.app_share_title
        : this.g.name;
    let imageUrl =
      this.g.app_share_pic != null && this.g.app_share_pic != ""
        ? this.g.app_share_pic
        : this.g.pic_url[0].pic_url;
    let id = this.id;
    return {
      title: title,
      query: "id=" + this.id + "&user_id=" + uid+ "&SmallShopId=" + SmallShopId+ "&Up=" + this.Up,
      imageUrl: imageUrl,
    };
  },
  onShareAppMessage(res) {
    setTimeout(() => {
      console.log("分享调用");
      this.getReceive();
    }, 2500);
    let u = uni.getStorageSync("mall_UserInfo");
    let uid = u.UserId ? u.UserId : 0;
	if(uid==0){
		uid = uni.getStorageSync("pid")?uni.getStorageSync("pid").pid:0
	}
    let SmallShopId = u.SmallShopId ? u.SmallShopId : 0;
	if(SmallShopId == 0){//如果微店id为0 去找所属微店id
		SmallShopId = u.UserSmallShopId? u.UserSmallShopId:0
	}
    return {
      title:
        this.g.app_share_title != null && this.g.app_share_title != ""
          ? this.g.app_share_title
          : this.g.name,
      path: "/pages/index/index?id=" + this.id + "&user_id=" + uid+ "&SmallShopId=" + SmallShopId+ "&Up=" + this.Up+'&JumpType=9',
      imageUrl:
        this.g.app_share_pic != null && this.g.app_share_pic != ""
          ? this.g.app_share_pic
          : this.g.pic_url[0].pic_url,
    };
  },
  mounted() {
    setTimeout(() => {
      this.initImages();
    }, 3000);
  },
  methods: {
    clickDescription(e) {
      console.log(e);
    },
    initImages() {
     
    },
    reloadUserinfo() {
      this.u = uni.getStorageSync("mall_UserInfo");
      // this.showAuth = false;
      this.init();
      this.initPage();
    },
    //关闭登录窗口
    gbAuth() {
      this.showAuth = false;
    },
    initPage() {
      let currentPages = getCurrentPages();
      let u = "/" + currentPages[currentPages.length - 1].route;
      let pages = uni.getStorageSync("basedata")
        ? uni.getStorageSync("basedata").bar_title
        : [];
      pages.forEach((x) => {
        if (x.value == u) {
          this.pageTitle = x.new_name ? x.new_name : x.name;
        }
      });
      uni.setNavigationBarTitle({
        title: this.pageTitle,
      });
    },
    clickCommentHandler() {
      uni.navigateTo({
      	url: "/pages/reserve/commentList?StoreId="+this.StoreId+'&GoodsId='+this.id
      })
    },
    getVipId() {
      //获取会员ID
      this.request2(
        {
          url: "/api/AppletGoods/GetAppletVipGradeInfo",
          data: {},
        },
        (res) => {
          if (res.data) {
            this.vipState = res.data.VIPModel;
          }
          
        }
      );
    },
    goVIP() {
      //跳转到购买的页面查看
      let GradeId = this.vipState.Id;
      uni.navigateTo({
        url: "/pages/share/buyInterest/index?showLook=1&GradeId=" + GradeId,
      });
    },
    init() {
      this.loading = true;
      this.request2(
        {
          url: "/api/AppletGoods/GetAppletGoodsInfo",
          data: {
            GoodsId: this.id,
          },
        },
        (res) => {
          if (res.resultCode == 1) {
            this.g = res.data.goods;
            this.g.totalStock = 0;
            this.g.attr.forEach((x) => {
              this.g.totalStock += x.stock;
            });
            this.g.marketingLogo = JSON.parse(this.g.marketingLogo);
            this.isAllowShare = res.data.goods.isAllowShare; //1为可以分享 2为不能分享
            if (this.isAllowShare == 2) {
              //隐藏分享的按钮
              wx.hideShareMenu({
                menus: ["shareAppMessage", "shareTimeline"],
              });
            }
            this.g.pic_url.forEach((x) => {
              this.imgs.push(x.pic_url);
            });
            if (this.g.attr_groups.length > 0) {
              this.g.attr_groups.forEach((x, i) => {
                if (i > 0) {
                  this.sku += ",";
                }
                this.sku += x.attr_group_name;
              });
              this.g.attr_groups[0].attr_list.forEach((x) => {
                if (x.pic_url) {
                  this.skuimage.push(x.pic_url);
                }
              });
            }

            var richtext = this.g.detail;
            //          richtext = richtext.replace(
            //   /<img/g,
            //   '< img style="margin-top:-4px;width:100%"'
            // );
            this.detailContent = richtext;
            this.loading = false;

            //登录在调用评论接口
            if (this.u) {
              this.initComments();
              this.setFootMarkInfo(res.data.goods.id);
            }
            
          } else {
            this.isExsitGoods = false;
          }
        }
      );
    },
    previewImage(i) {
      uni.previewImage({
        urls: this.imgs,
        current: i,
        longPressActions: {
          itemList: ["发送给朋友", "保存图片", "收藏"],
          success: function (data) {
            console.log(
              "选中了第" +
                (data.tapIndex + 1) +
                "个按钮,第" +
                (data.index + 1) +
                "张图片"
            );
          },
          fail: function (err) {
            console.log(err.errMsg);
          },
        },
      });
    },
    initComments() {
      this.msg.GoodsId = this.id;
      this.request2({
      		url: '/api/AppletStores/GetAppletGoodsCommentPageList',
      		data: {pageIndex:1,pageSize:5,StoreId:this.StoreId,GoodsId:this.id,ServicePersonalId:0}
      	},
      	res => {
      		
      		if (res.resultCode == 1) {
      			this.comments = res.data.pageData
      		}
      	}
      );
    },
 
    setFootMarkInfo(id) {
      this.request2(
        {
          url: "/api/AppletUser/SetUserFootMarkInfo",
          data: {
            GoodsId: id,
          },
        },
        (res) => {}
      );
    },
 

 
    closeShare() {
      this.showShare = false;
    },
    openShare() {
		if (this.u) {
		  this.showShare = true;
		} else {
		  this.showAuth = true;
		}
      
    },
    getReceive() {
      //分享进入调取领券接口
      // 1-分享，2-购买并付款
	  let storeId = uni.getStorageSync("storeId") && uni.getStorageSync("storeId").storeId ? uni.getStorageSync("storeId")
	  	.storeId : 0;
      this.request2(
        {
          url: "/api/AppletStores/ShareEducationCoupon",
          data: {
            TriggerType: 1,
			StoreId:storeId
          },
        },
        (res) => {
          console.log(res, "res");
          if (res.couponResultCode == 1) {
            this.couponMessage = res.couponMessage;
            this.showCoupons = true;
          }
        }
      );
    },
    goLook() {
      this.showCoupons = true;
      uni.navigateTo({
        url: "/pages/reserve/personal/cardList",
      });
    },
    closeBtn() {
      this.showCoupons = false;
    },
	gosubscribe(){//预约界面
		if (this.u) {
		  uni.navigateTo({
		  	url: "/pages/reserve/subscribe?GoodsId="+this.id,
		  })
		} else {
		  this.showAuth = true;
		}
	},
	//图片预览
	previewImage2(index, images) {
		uni.previewImage({
			urls: images,
			current: index,
		});
	},
 

  },
};
</script>

<style>
@font-face {
  font-family: "oswald";
  src: url("https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/smallapp/Oswald-Regular.ttf");
}
.goods {
  height: 100%;
  background: #f5f5f5;
  padding-bottom: 55px;
}
.goods .g-info {
  background: #fff;
  padding: 10px;
}
.goods .g-info .g-name {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  font-size: 16px;
  height: 45px;
}
.goods .g-info .g-price-box {
  display: flex;
  align-items: flex-end;
}
.goods .g-info .g-price-box .left {
  flex: 1;
  width: 1px;
}
.goods .g-info .g-price-box .left .price {
  margin: 15px 0;
  font-size: 24px;
  font-weight: bold;
  height: 31px;
}
.goods .g-info .g-price-box .left .price .small {
  font-size: 16px;
}
.goods .numfont {
  font-family: "oswald";
  font-weight: unset;
}
.goods .g-info .g-price-box .left .sell {
  font-size: 12px;
  color: gray;
}
.goods .g-info .g-price-box .left .sell .oprice {
  text-decoration: line-through;
  margin-right: 12px;
}
.goods .g-info .g-price-box .right {
  width: 30px;
  margin-left: 20px;
  height: 100%;
  font-size: 12px;
  color: #333;
}
.goods .g-info .g-price-box .right .share-icon {
  width: 22px;
  height: 22px;
  margin-bottom: 12rpx;
}
.goods .sku-box {
  margin: 10px 0;
  background: #fff;
  display: flex;
  padding: 15px 10px;
  align-items: center;
}
.goods .sku-box .label {
  font-size: 12px;
  color: gray;
  margin-right: 15px;
}
.goods .details {
  padding: 10px;
}
.goods .sku-box .content {
  width: 1px;
  flex: 1;
  color: #000;
  font-size: 12px;
}


.goods .comment2 {
  margin: 10px 0;
  background: #fff;

  padding: 10px;
}
.goods .comment2 .chead {
  padding-bottom: 10px;
  border-bottom: 1px solid #f1f1f1;
}

.goods .detals {
  padding: 10px;
  background: #fff;
  margin-top: 10px;
}
.goods .detals * {
  max-width: 100%;
}
.goods .is_share {
  width: 120px;
  height: 45px;
  border-top-left-radius: 22.5px;
  border-bottom-left-radius: 22.5px;
  background: #111;
  opacity: 0.8;
  display: flex;
  flex-direction: row;
  align-items: center;
  position: absolute;
  top: 120px;
  right: 0;
}
.goods .Logo {
  padding: 1px 5px;
  display: inline-block;
  font-size: 22rpx;
  margin-right: 5rpx;
  border-radius: 4px;
}
.goods .screen-swiper {
		min-height: 375upx;
	}
.goods .screen-swiper image,
.goods .screen-swiper video{
	width: 100%;
	display: block;
	height: 100%;
	margin: 0;
	pointer-events: none;
}
.goods .submit{
		width: 100%;
		height: 60px;
		background: #FFF;
		position: fixed;
		left: 0;
		bottom: 0;
		padding: 0 15px;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		z-index: 99;
		
	}
	.goods .submit-l{
		display: flex;
		flex-direction: row;
		align-items: flex-end;
		font-size: 13px;
	}
	.goods .comment {
	  background: #fff;
	  font-family: aa;
	    /* padding: 15px; */
	}
	.goods .comment .content {
	  display: flex;
	  flex-direction: column;
	  
	  margin-bottom: 15px;
	}
	.goods .comment .content .u-box {
	  display: flex;
	  margin-bottom: 10px;
	  align-items: center;
	}
	.goods .comment .content .u-box .user {
	  flex: 1;
	  font-size: 13px;
	  color: #111111;
	  font-weight: bold;
	  align-items: center;
	  display: flex;
	}
	.goods .comment .content .u-box .timer {
	  font-size: 12px;
	  color: #999999;
	  text-align: right;
	  
	}
	.goods .comment .content .u-box .ucontent {
	  font-size: 11px;
	  color: #000;
	  margin-top: 10px;
	}
	.goods .comment .content .u-box .plimg{
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
		margin-top: 10px;
	}
	.goods  .plimg-item{
		margin-right: 3px;
		/* margin-bottom: 3px; */
	}
	.goods .reply{
		width: 100%;
		padding: 10px;
		background: #F5F6F7;
		border-radius: 3px;
		margin-top: 10px;
		
	}
</style>
