<template>
<view>
<!--components/popAd/popAd.wxml-->
<template name="home_pop_up_ads">
  <block v-if="!first&&!iscouponAd&&!storeShow&&isAppLayerAd">
    <view class="layer" v-if="popAdTemplate&&popAdTemplate.state==0&&isLayerAd&&popAdList.pic">
      <image lazy-load="true" :src="popAdList.pic" mode="aspectFit" @tap="getUrl2" :data-type="popAdList.type" :data-content="popAdList.content"></image>
      <image class="close" src="/static/pages/images/ad_close_w.png" @tap="hideAd"></image>
    </view>
  </block>
</template>
</view>
</template>

<script>
	const req = require("../../utils/request");
	const utils = require("../../utils/util");
	const app = getApp();
	export default {
		data() {
			return {
				isAppLayerAd:app.globalData.isLayerAd
			};
		},

		components: {},
		props: {
			first:Boolean,
			popAdTemplate:Object,
			isLayerAd:Boolean,
			popAdList:Object,
			iscouponAd:Boolean,
			storeShow:Boolean
		},
		options: {
			addGlobalClass: true
		},

		// created(){
		//   console.log(this.data.merchant)
		//   req.getRequest('/api/merchant/getMerchantList',{}, res => {
		//     res = res.filter(it=>it.isDefault==1)
		//      this.setData({merchant:res[0]})
		//    })
		// },
		mounted() {
			// console.log(this.iscouponAd,this.first,this.storeShow)
		},

		methods: {
			async getUrl2(e) {
				console.log(e)
				var dt = e.currentTarget.dataset;
				var url = "";
				var id = "";
				var acid = "";
			
				if (dt.content != "") {
					let conunt = dt.content.split("_");
					id = conunt[1];
					acid = conunt[0];
				}
			
				if (dt.type * 1 == 2 && dt.content != "") {
					url = "product/detail/detail?id=" + dt.content;
				} else if (dt.type * 1 == 3) {
					url = dt.content;
					console.log('urlgetUrl2===',url)
					if(!url){
						req.msg('功能待接入')
						return false
					}
					if(url=='pages/index/index'||url=='pages/matchList/index'||url=='pages/interactive/index'||url=='pages/found/found'||url=='pages/user/user'){
						app.globalData.switchTab(url)
						return false
					}else{
						if(url.indexOf('http')!=-1){
							if(url.indexOf('${salesno}')!=-1){
								// 如果打开的分享的销售员微页
								if(req.getStorage('shareId')){
									url = url.replace(/\${salesno}/g,'salesno='+req.getStorage('shareId'))
								}else{
									// 如果自己是销售员
									if(req.getStorage('userInfo').saleNo){
										url = url.replace(/\${salesno}/g,'salesno='+req.getStorage('userInfo').saleNo)
									}else{
										//当前绑定的微页或推荐的微页
										let salesno = await new Promise((r,j)=>{
											req.getLocation(res => {
												const to = {
													latitude: res.latitude,
													longitude: res.longitude
												};
												req.getRequest('/api/visiting/card/recommendCardInfo', to, res => {
													r(res.jobNumber?res.jobNumber:'')
												})
											});
										})
										url = url.replace(/\${salesno}/g,'salesno='+salesno)
									}
								}
							}
							app.globalData.openPage('pages/web/web?url='+encodeURIComponent(url));
							return false
						}
					}
				} else if (dt.type * 1 == 4) {
					url = "product/groupDetail/groupDetail?acid=" + acid + "&id=" + id;
				} else if (dt.type * 1 == 5) {
					url = "product/coupon/coupon";
				} else if (dt.type * 1 == 6) {
					url = "pages/live/live";
				} else if (dt.type * 1 == 7) {
					url = "product/groupList/groupList" + dt.merchant;
				} else if (dt.type * 1 == 9 && dt.content != "") {
					url = "product/list/list?id=" + dt.content + '&title=' + dt.title + this.web;
				} else if (dt.type * 1 == 10) {
					url = "plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=" + dt.content;
					// console.log(
					// url); // url=`plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${dt.content}`
				} else if (dt.type * 1 == 11) {
					url = "activity/group/index/index" + dt.merchant;
				} else if (dt.type * 1 == 12) {
					url = "activity/seckill/seckill" + dt.merchant;
				} else if (dt.type * 1 == 13) {
					url = "activity/bargain/index/index" + dt.merchant;
				} else if (dt.type * 1 == 14) {
					url = "activity/newbornZone/newbornZone" + dt.merchant;
				} else if (dt.type * 1 == 15 && dt.content != "") {
					url = "activity/newbornDetails/newbornDetails?acid=" + acid + "&id=" + id;
				} else if (dt.type * 1 == 16 && dt.content != "") {
					url = "activity/seckillDetails/seckillDetails?acid=" + acid + "&id=" + id;
				} else if (dt.type * 1 == 17 && dt.content != "") {
					url = "activity/bargain/detail/detail?acid=" + acid + "&id=" + id;
				} else if (dt.type * 1 == 18 && dt.content != "") {
					url = "activity/group/detail/detail?acid=" + acid + "&id=" + id;
				} else if (dt.type * 1 == 19 && dt.content != "") {//文章内容
					url = "topic/content/content?id=" + dt.content + "&type=" + 1;//1为普通文章内容
				} else if (dt.type * 1 == 21 && dt.content != "") {//专题类型
					url = "pages/topic/topic?id=" + dt.content;
				} else if (dt.type * 1 == 27 && dt.content != "") {
					url = "match/activityDetail/activityDetail?id=" + dt.content;
				} else{
					url = "";
				}
			
				if (dt.type * 1 == 6) app.globalData.switchTab(url);
				else app.globalData.openPage(url);
				this.hideAd();
			},
			
			hideAd() {
				//关闭广告
				// if(this.data.first==false){
				this.setData({
					isLayerAd: false
				}); // }
			},
		}
	};
</script>
<style>
@import "./popAd.css";
</style>