<template>
<view class="container">
	<block v-if="isload">
		<view class="coupon-list">
			<view class="coupon-item flex al-item-center" :style="item.stock<=0?'color: #AAAAAA':'color:#0a5d3b'" v-for="(item, index) in datalist" :key="item.id" @tap.stop="goto" :data-url="'coupondetail?id=' + item.id">
				<image class="coupon-item__bg" :src="item.stock<=0 ? event_rul + '/static/img/static/icon/couponed-bg.svg': (event_rul + '/static/img/static/icon/coupon-bg.svg')" mode=""></image>
				<view class="coupon-item__hd flex al-item-center j-center">
					<view class="coupon-item__hd-price" v-if="item.type==1"><text class="symbol">￥</text><text class="txt">{{item.money}}</text></view>
					<view class="coupon-item__hd-txt" v-if="item.type==2">礼品券</view>
					<view class="coupon-item__hd-price" v-if="item.type==3"><text class="txt">{{item.limit_count}}</text><text class="unit">次</text></view>
					<view class="coupon-item__hd-txt" v-if="item.type==4">抵运费</view>
					<view class="coupon-item__hd-price" v-if="item.type==5"><text class="symbol">￥</text><text class="txt">{{item.money}}</text></view>
					<view class="coupon-item__hd-tips" v-if="item.type==1 || item.type==4 || item.type==5">
						<text v-if="item.minprice>0">满{{item.minprice}}元可用</text>
						<text v-else>无门槛</text>
					</view>
				</view>
				<view class="coupon-item__bd flex j-between">
					<view class="coupon-item__bd-title flex al-item-center" :style="item.stock<=0?'color: #AAAAAA':'color:#333333'">
						<view class="coupon-item__bd-tag" :style="item.stock<=0?'color: #AAAAAA;border-color: #AAAAAA':'color:' + t('color1')">{{item.type==1?'代金券':(item.type==2?'礼品券':(item.type==3?'计次券':(item.type==4?'运费抵扣券':(item.type==5?'餐饮券':''))))}}</view>
						{{item.name}}
					</view>							
					<view class="coupon-item__bd-txt" :style="item.stock<=0?'color: #AAAAAA':'color:#999999'" v-if="item.bid>0">适用商家：{{item.bname}}</view>
					<view class="coupon-item__bd-time">有效期至 {{item.yxqdate}}</view>
				</view>
				<view class="coupon-item__ft" v-if="item.haveget>=item.perlimit" @tap.stop="goto" :data-url="'/pages/shop/prolist?cpid=' + item.id" >
					<view class="coupon-item__ft-txt">立即使用</view>
				</view>
				<view class="coupon-item__ft" v-else-if="item.stock<=0">
					<view class="coupon-item__ft-txt">已抢光了</view>
				</view>
				<view class="coupon-item__ft" v-else @tap.stop="getcoupon" :data-id="item.id" :data-price="item.price" :data-score="item.score" :data-key="index">
					<view  class="coupon-item__ft-txt">{{item.price > 0 ? '立即购买' : (item.score>0?'立即兑换':'立即领取')}}</view>
				</view>
			</view>
		</view>
		<nodata v-if="nodata" :text="'暂无可领' + t('优惠券')"></nodata>
		<nomore v-if="nomore"></nomore>
	</block>
	<loading v-if="loading"></loading>
	<dp-tabbar :opt="opt"></dp-tabbar>
	<popmsg ref="popmsg"></popmsg>
  <!-- 悬浮按钮 -->
  <drag-button></drag-button>
</view>
</template>

<script>
var app = getApp();

export default {
  data() {
    return {
		opt:{},
		loading:false,
		isload: false,
		menuindex:-1,
		pre_url:app.globalData.pre_url,

		nomore:false,
		nodata:false,
		datalist: [],
		pagenum: 1,
		title: '文章列表',
		needAuth: 0,
		authCanClose: false,

		event_rul: app.globalData.event_url,
    };
  },

  onLoad: function (opt) {
		this.opt = app.getopts(opt);
		this.getdata();
  },
	onPullDownRefresh: function () {
    this.pagenum = 1;
    this.datalist = [];
		this.getdata();
	},
  onReachBottom: function () {
    if (!this.nodata && !this.nomore) {
      this.pagenum = this.pagenum + 1;
      this.getdata();
    }
  },
  methods: {
    getdata: function () {
      var that = this;
      var pagenum = that.pagenum;
      var st = that.st;
      var bid = that.opt && (that.opt.bid || that.opt.bid === '0') ? that.opt.bid : '';
			that.loading = true;
			that.nomore = false;
			that.nodata = false;
      app.post('ApiCoupon/couponlist', {st: st,pagenum: pagenum,bid: bid}, function (res) {
				that.loading = false;
        var data = res.data;
        if (pagenum == 1) {
			that.datalist = data;
			console.log("打印优惠券列表datalist",that.datalist)
          if (data.length == 0) {
			  that.nodata = true;
          }
        }else{
          if (data.length == 0) {
            that.nomore = true;
          } else {
            var datalist = that.datalist;
            var newdata = datalist.concat(data);
            that.datalist = newdata;
          }
        }
				that.loaded();
      });
    },
    getcoupon: function (e) {
			var that = this;
			var datalist = that.datalist;
			var id = e.currentTarget.dataset.id;
			var score = parseInt(e.currentTarget.dataset.score);
			var price = e.currentTarget.dataset.price;

			if (price > 0) {
				app.post('ApiCoupon/buycoupon', {id: id}, function (res) {
					if(res.status == 0) {
							app.error(res.msg);
					} else {
						app.goto('/pages/pay/pay?id=' + res.payorderid);
					}
				})
				return;
			}
			var key = e.currentTarget.dataset.key;
			if (score > 0) {
				app.confirm('确定要消耗' + score + '' + that.t('积分') + '兑换吗?', function () {
					app.showLoading('兑换中');
					app.post('ApiCoupon/getcoupon', {id: id}, function (data) {
						app.showLoading(false);
						if (data.status == 0) {
							app.error(data.msg);
						} else {
							app.success(data.msg);
							datalist[key]['haveget'] = data.haveget;
							that.datalist = datalist;
						}
					});
				});
			} else {
				app.showLoading('领取中');
				app.post('ApiCoupon/getcoupon', {id: id}, function (data) {
					app.showLoading(false);
					if (data.status == 0) {
						app.error(data.msg);
					} else {
						app.success(data.msg);
						datalist[key]['haveget'] = data.haveget;
						that.datalist = datalist;
					}
				});
			}
		}
  }
};
</script>

<style lang="scss">
.container {
  background: #f5f5f5;
  min-height: 100vh;
}
.coupon-list {
  width: 100%;
  padding: 6rpx;
}
.coupon-item {
  display: flex;
  position: relative;
  z-index: 1;
  height: 234rpx;
  color: #0a5d3b;
  &:not(:first-child) {
    margin-top: -12rpx;
  }
  &__bg {
    position: absolute;
    z-index: -1;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
  }
  &__hd {
    flex-shrink: 0;
    flex-direction: column;
    color: #ffffff;
    width: 210rpx;
    padding-left: 15rpx;
    &-price {
      font-size: 36rpx;
      line-height: 50rpx;
      font-weight: bold;
      .txt {
        font-size: 72rpx;
        line-height: 100rpx;
      }
      .unit {
        padding-left: 10rpx;
      }
    }
    &-tips {
      font-size: 24rpx;
      line-height: 34rpx;
    }
    &-txt {
      font-size: 46rpx;
      font-weight: bold;
      line-height: 58rpx;
    }
  }
  &__bd {
    flex: 1;
    padding: 0 24rpx;
    min-height: 125rpx;
    flex-direction: column;
    &-title {
      font-size: 28rpx;
      line-height: 40rpx;
      color: #333333;
    }
    &-tag {
      border-radius: 4rpx;
      margin-right: 6rpx;
      border: 1rpx solid #0a5d3b;
      font-size: 22rpx;
      font-weight: bold;
      height: 32rpx;
      line-height: 30rpx;
      padding: 0 6rpx;
    }
    &-txt {
      font-size: 22rpx;
      color: #999999;
      line-height: 32rpx;
      margin-top: 16rpx;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      /* autoprefixer: ignore next */
      -webkit-box-orient: vertical;
      text-align: justify;
      white-space: pre-wrap;
      word-break: break-all;
    }
    &-time {
      margin-top: 10rpx;
      font-size: 20rpx;
      line-height: 28rpx;
    }
  }
  &__ft {
    padding: 0 50rpx 0 30rpx;
    &-txt {
      width: 20rpx;
      font-size: 24rpx;
      font-weight: bold;
      line-height: 30rpx;
    }
  }
}
</style>