<template>
    <view class="container">
        <scroll-view class="coupon-list" :scroll-y="true" :scroll-top="scrollTop">
            <view class="item" @tap="getCoupon" :data-index="item.id" v-for="(item, index) in couponList" :key="index">
                <view class="tag">{{ item.tag }}</view>

                <view class="content">
                    <view class="left">
                        <view class="discount">{{ item.discount }}元</view>
                        <view class="min">满{{ item.min }}元使用</view>
                    </view>
                    <view class="right">
                        <view class="name">{{ item.name }}</view>
                        <view class="time" v-if="item.days != 0">有效期：{{ item.days }}天</view>
                        <view class="time" v-else>有效期：{{ item.startTime }} - {{ item.endTime }}</view>
                    </view>
                </view>

                <view class="condition">
                    <text class="txt">{{ item.desc }}</text>
                    <image :src="item.pic" class="icon"></image>
                </view>
            </view>

            <view class="page" v-if="showPage">
                <view :class="'prev ' + (page <= 1 ? 'disabled' : '')" @tap="prevPage">上一页</view>
                <view :class="'next ' + (count / limit < page ? 'disabled' : '')" @tap="nextPage">下一页</view>
            </view>
        </scroll-view>
    </view>
</template>

<script>
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();
export default {
    data() {
        return {
            couponList: [],
            page: 1,
            limit: 10,
            count: 0,
            scrollTop: 0,
            showPage: false
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        this.getCouponList();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        getCouponList: function () {
            let that = this;
            that.setData({
                scrollTop: 0,
                showPage: false,
                couponList: []
            });
            // 页面渲染完成
          uni.showLoading({
              title: '加载中'
          });
            util.request(api.CouponList, {
                page: that.page,
                limit: that.limit
            }).then(function (res) {
                if (res.errno === 0) {
                    that.setData({
                        scrollTop: 0,
                        couponList: res.data.list,
                        showPage: true,
                        count: res.data.total
                    });
                }
				 uni.hideLoading();
            });
        },

        getCoupon(e) {
            if (!app.globalData.hasLogin) {
                uni.navigateTo({
                    url: '/pages/auth/login/login'
                });
            }
            let couponId = e.currentTarget.dataset.index;
            util.request(
                api.CouponReceive,
                {
                    couponId: couponId
                },
                'POST'
            ).then((res) => {
                if (res.errno === 0) {
                    uni.showToast({
                        title: '领取成功'
                    });
                } else {
                    util.showErrorToast(res.errmsg);
                }
            });
        },

        nextPage: function (event) {
            var that = this;
            if (this.page > that.count / that.limit) {
                return true;
            }
            that.setData({
                page: that.page + 1
            });
            this.getCouponList();
        },

        prevPage: function (event) {
            if (this.page <= 1) {
                return false;
            }
            var that = this;
            that.setData({
                page: that.page - 1
            });
            this.getCouponList();
        }
    }
};
</script>
<style>
page {
    background: #f4f4f4;
    min-height: 100%;
}

.container {
    background: #f4f4f4;
    min-height: 100%;
    padding-top: 30rpx;
}

.coupon-list {
    width: 750rpx;
    height: 100%;
    overflow: hidden;
}

.item {
    position: relative;
    height: 290rpx;
    width: 700rpx;
    background: linear-gradient(to right, #cfa568, #e3bf79);
    margin-bottom: 30rpx;
    margin-left: 30rpx;
    margin-right: 30rpx;
    padding-top: 52rpx;
}

.tag {
    height: 32rpx;
    background: #a48143;
    padding-left: 16rpx;
    padding-right: 16rpx;
    position: absolute;
    left: 20rpx;
    color: #fff;
    top: 20rpx;
    font-size: 20rpx;
    text-align: center;
    line-height: 32rpx;
}

.content {
    margin-top: 24rpx;
    margin-left: 40rpx;
    display: flex;
    margin-right: 40rpx;
    flex-direction: row;
}

.content .left {
    flex: 1;
}

.discount {
    font-size: 50rpx;
    color: #b4282d;
}

.min {
    color: #fff;
}

.content .right {
    width: 400rpx;
}

.name {
    font-size: 44rpx;
    color: #fff;
    margin-bottom: 14rpx;
}

.time {
    font-size: 24rpx;
    color: #fff;
    line-height: 30rpx;
}

.condition {
    position: absolute;
    width: 100%;
    bottom: 0;
    left: 0;
    height: 78rpx;
    background: rgba(0, 0, 0, 0.08);
    padding: 24rpx 40rpx;
    display: flex;
    flex-direction: row;
}

.condition .txt {
    display: block;
    height: 30rpx;
    flex: 1;
    overflow: hidden;
    font-size: 24rpx;
    line-height: 30rpx;
    color: #fff;
}

.condition .icon {
    margin-left: 30rpx;
    width: 24rpx;
    height: 24rpx;
}

.page {
    width: 750rpx;
    height: 108rpx;
    background: #fff;
    margin-bottom: 20rpx;
}

.page view {
    height: 108rpx;
    width: 50%;
    float: left;
    font-size: 29rpx;
    color: #333;
    text-align: center;
    line-height: 108rpx;
}

.page .prev {
    border-right: 1px solid #d9d9d9;
}

.page .disabled {
    color: #ccc;
}
</style>
