<template>
  <view class="container">
    <view class="tui-coupon-list">
      <view class="tui-coupon-item tui-top20"  >
         <image src="https://kntobs.jnmyunl.com/shop/images/bg_coupon_3x.png" class="tui-coupon-bg" mode="widthFix"></image>
		<view class="tui-coupon-item-left">
          <view class="tui-coupon-price-box"  >
            <view class="tui-coupon-price-sign">￥</view>
            <view class="tui-coupon-price" :class="{ 'tui-price-small': false }">{{ item.couponPrice }}</view>
          </view>
          <view class="tui-coupon-intro">满{{ item.useMinPrice }}元可用</view>
        </view>
        <view class="tui-coupon-item-right">
          <view class="tui-coupon-content">
            <view class="tui-coupon-title-box">
              <view class="tui-coupon-title">{{ item.couponName }}</view>
            </view>
            <view class="tui-coupon-rule">
              <view class="tui-rule-box tui-padding-btm">
                <view class="tui-coupon-circle"></view>
                <view class="tui-coupon-text">不可叠加使用</view>
              </view>
              <view class="tui-rule-box">
                <view class="tui-coupon-circle"></view>
                <view class="tui-coupon-text">{{ item.limitTime }} 到期</view>
            
              </view>
            </view>
          </view>
        </view>
        <view class="tui-btn-box">
			<view class="btn cancel" v-if='CompanyUserInfo' @click="showpop(item)">发送</view>
			<view class="btn cancel" v-if="item.receiveCount>0">已领取</view>
			<view class="btn cancel" v-else-if="item.receiveCount==item.totalCount" >已领完</view>
			<view class="btn receive" v-else @click="receive(item.id,index)">立即领取</view>
        </view>
      </view>
    </view>
<!-- 	<view class="banner" @click="showImg()">
		<image class="img" src="https://hos-1309931967.cos.ap-chongqing.myqcloud.com/fs/20221116/99f6dedb47394b43adb61a1aa1b2e7e2.jpg"></image>
	</view> -->
	
	<!-- <view class="ad">
		<ad unit-id="adunit-4768dead82e0c4ff" ad-type="video" ad-theme="white"></ad>
	</view> -->
	<view>
		<u-popup :show="popshow" @close="close"  :closeOnClickOverlay='true'>
			<view class="popbox">
				<view class="pop-tit">
					<view class="poptitle">选择用户</view>
					<u-icon name="close" size="24" @click="close"></u-icon>
				</view>
				<view class="popsh">
					<u-search :showAction="true" actionText="搜索" placeholder="输入手机号搜索"
					 :animation="true" v-model="keyword" @search='searchkey(value)' @custom='searchkey(value)'></u-search>
				</view>
				<view class="poplist">
					<view class="list-box" v-for="(item,index) in phoneUser" :key="index">
						<view class="tit">{{item.name}}</view>
						<view class="send" @click="sendpop(item.id)">发送</view>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
  </view>
</template>
<script>
import { getCouponIssueById, receive,sendCoupon,searchUser} from '@/api/coupon'
export default {
  name: 'getCoupon',
 
  props: {},
  data: function() {
    return {
		id:null,
		item:null,
		CompanyUserInfo:{},
		user:{},
		popshow:false,
		keyword:'',
		phoneUser:{},
    }
  },
  onLoad(options) {
	console.log(options.id)
	this.id=JSON.parse(options.id);
	this.CompanyUserInfo=uni.getStorageSync('CompanyUserInfo');
	if(this.CompanyUserInfo&&JSON.stringify(this.CompanyUserInfo)!="{}"){
		console.log(JSON.stringify(this.CompanyUserInfo))
		uni.showShareMenu()
	}else{
		uni.hideShareMenu()
		if(!this.utils.isLogin()) return
	}
	this.getCouponIssueById();
  },
  //发送给朋友
  onShareAppMessage(res) {
  	return {
  		title: this.item.couponName,
  		path: `/pages_company/couponDetails?id=${this.item.id}`
  	}
  },
  mounted: function() {
  },
  onShow() {
	  this.CompanyUserInfo=uni.getStorageSync('CompanyUserInfo');
	  this.user=uni.getStorageSync('userInfo');
  },
  methods: {
	  sendpop(id){
	  	this.sendCoupon(id)
	  },
	  sendCoupon(id){
	  	const data={
	  		id:this.item.id,
	  		companyUserId:this.CompanyUserInfo.userId,
	  		companyId:this.CompanyUserInfo.companyId,
	  		setSendUserId:this.user.userId,
	  		userId:id,
	  		companyUserToken:uni.getStorageSync('CompanyUserToken')
	  	}
	  	sendCoupon(data).then(res=>{
	  		if(res.code==200){
	  			uni.showToast({
	  				icon:'none',
	  				title: '发送成功！',
	  			});
	  		}else{
	  			uni.showToast({
	  				icon:'none',
	  				title: res.msg,
	  			});
	  		}
	  	})
	  },
	  searchkey(value){
	  		  console.log(this.keyword);
	  		  searchUser({value:this.keyword}).then(res=>{
	  			  if(res.code==200){
	  				  this.phoneUser=res.rows
	  				  console.log(res)
	  			  }else{
	  				  uni.showToast({
	  				  	icon:'none',
	  				  	title: res.msg,
	  				  });
	  			  }
	  		  })
	  },
	  close(){
	  		  this.popshow=false
	  },
	  showpop(item){
	  	this.popshow=true
	  	this.detailsid=item
	  	console.log(item)
	  },
	showImg() {
		var url=['https://hos-1309931967.cos.ap-chongqing.myqcloud.com/fs/20221116/99f6dedb47394b43adb61a1aa1b2e7e2.jpg']
	  	uni.previewImage({
	  	 	urls: url,
	  	 	current: url[0]
	  	});
	},
	getCouponIssueById: function() {
	  	let that = this
	  	var data={id:this.id}
	  	getCouponIssueById(data)
	  	  .then(function(res) {
				that.item=res.data;
				
	  	  })
	  	  .catch(function(err) {
	  	    uni.showToast({
	  	      title: err.msg ,
	  	      icon: 'none',
	  	      duration: 2000,
	  	    })
	  	  })
	    
	},
    receive: function() {
		if(this.utils.isLogin()){
			let that = this
			var data={id:this.item.id}
			receive(data)
			  .then(function(res) {
				  if(res.code==200){
					  uni.showToast({
					    title: '领取成功',
					    icon: 'success',
					    duration: 2000,
					  })
				  }
				  else{
					  uni.showToast({
					    title: res.msg,
					    duration: 2000,
					  })
				  }
			    
					   
			  })
			  .catch(function(err) {
			    uni.showToast({
			      title: err.msg ,
			      icon: 'none',
			      duration: 2000,
			    })
			  })
		}
      
    },
  },
}
</script>

<style lang="less" scoped>
	.popbox{
		padding: 20rpx;
		height: 400rpx;
		.poplist{
			margin-top: 20rpx;
			.list-box{
				display: flex;
				justify-content: space-between;
				align-items: center;
				.tit{
					font-size:24rpx;
					color: #666;
				}
				.send{
					width: 122rpx;
					height: 50rpx;
					line-height: 50rpx;
					font-size: 26upx;
					font-family: PingFang SC;
					font-weight: 500;
					text-align: center;
					border-radius: 32upx;
					margin-left: 15upx;
					border: 1px solid #2BC7B9;
					color: #2BC7B9;
				}
			}
		}
	}
	.pop-tit{
		display: flex;
		padding: 20rpx;
		.poptitle{
			flex: 1;
			text-align: center;
		}
	}
page {
  background-color: #f5f5f5;
}

.container {
	height: 100%;
	display: flex;
	flex-direction: column;
}

.tui-coupon-list {
  width: 100%;
  padding: 0 25rpx;
  box-sizing: border-box;
}

.tui-coupon-banner {
  width: 100%;
}

.tui-coupon-item {
  width: 100%;
  height: 210rpx;
  position: relative;
  display: flex;
  align-items: center;
  padding-right: 30rpx;
  box-sizing: border-box;
  overflow: hidden;
 
}

.tui-coupon-bg {
  width: 100%;
  height: 210rpx;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 1;
}

.tui-coupon-sign {
  height: 110rpx;
  width: 110rpx;
  position: absolute;
  z-index: 9;
  top: -30rpx;
  right: 40rpx;
}

.tui-coupon-item-left {
  width: 218rpx;
  height: 210rpx;
  position: relative;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  flex-shrink: 0;
}

.tui-coupon-price-box {
  display: flex;
  color: #e41f19;
  align-items: flex-end;
}

.tui-coupon-price-sign {
  font-size: 30rpx;
}

.tui-coupon-price {
  font-size: 70rpx;
  line-height: 68rpx;
  font-weight: bold;
}

.tui-price-small {
  font-size: 58rpx !important;
  line-height: 56rpx !important;
}

.tui-coupon-intro {
  background: #f7f7f7;
  padding: 8rpx 10rpx;
  font-size: 26rpx;
  line-height: 26rpx;
  font-weight: 400;
  color: #666;
  margin-top: 18rpx;
}

.tui-coupon-item-right {
  flex: 1;
  height: 210rpx;
  position: relative;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-left: 24rpx;
  box-sizing: border-box;
  overflow: hidden;
}

.tui-coupon-content {
  width: 82%;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.tui-coupon-title-box {
  display: flex;
  align-items: center;
}

.tui-coupon-btn {
  padding: 6rpx;
  background: #ffebeb;
  color: #e41f19;
  font-size: 25rpx;
  line-height: 25rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  transform: scale(0.9);
  transform-origin: 0 center;
  border-radius: 4rpx;
  flex-shrink: 0;
}

.tui-color-grey {
  color: #888 !important;
}

.tui-bg-grey {
  background: #f0f0f0 !important;
  color: #888 !important;
}

.tui-coupon-title {
  width: 100%;
  font-size: 26rpx;
  color: #333;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tui-coupon-rule {
  padding-top: 52rpx;
}

.tui-rule-box {
  display: flex;
  align-items: center;
  transform: scale(0.8);
  transform-origin: 0 100%;
}

.tui-padding-btm {
  padding-bottom: 6rpx;
}

.tui-coupon-circle {
  width: 8rpx;
  height: 8rpx;
  background: rgb(160, 160, 160);
  border-radius: 50%;
}

.tui-coupon-text {
  font-size: 28rpx;
  line-height: 28rpx;
  font-weight: 400;
  color: #666;
  padding-left: 8rpx;
  white-space: nowrap;
}

.tui-top20 {
  margin-top: 20rpx;
}

.tui-coupon-title {
  font-size: 28rpx;
  line-height: 28rpx;
}

.tui-coupon-radio {
  transform: scale(0.7);
  transform-origin: 100% center;
}

.tui-btn-box {
  position: absolute;
  right: 20rpx;
  bottom: 40rpx;
  z-index: 10;
  .btn{
  	width: 155upx;
  	height: 64upx;
  	line-height: 64upx;
  	font-size: 26upx;
  	font-family: PingFang SC;
  	font-weight: 500;
  	text-align: center;
  	border-radius: 32upx;
  	margin-left: 15upx;

  	&.cancel{
  		border: 1px solid #2BC7B9;
  		color: #2BC7B9;
		margin-bottom: 8rpx;
  	}
  	&.receive{
  		background: #2BC7B9;
  		color: #FFFFFF;
  	}
  }
}
.banner{
	margin: 25rpx;
	.img{
		height: 700rpx;
		width: 100%;
	}
}
.ad{
	margin: 0rpx 0rpx 60rpx;
}
</style>
