<template>
    <div class="page" v-if="list">
        <div v-if="!empty" class="used">
            可用兑换券
            <span>（{{ list.length }}）</span>
        </div>
        <div v-for="(item, index) in list" :key="index">
            <div class="coupon" @click="choose">
                <div class="box">
                    <img class="img" src="../assets/icon_youhuiquan_01@3x.png" alt="" />
                    <div class="coupon-top">
                        <div class="text1">{{ item.name }}</div>
                        <div class="text2" v-if="item.limited">仅限规定商品可用</div>
                    </div>
                </div>
                <div class="box1">
                    <div style="width: 100%">
                        <div class="top">
                            <span class="text3 name1"
                                >兑换券编码<span class="text4"> {{ item.id }}</span></span
                            >
                            <!-- <div class="text6" v-if="item.used">已使用</div> -->
                        </div>

                        <div class="top1">
                            <div class="text5">有效期至:{{ item.expiration }}</div>
                            <div class="btn">立即使用</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div v-if="!empty" class="used">以下兑换券已过期/已使用</div>
        <div v-for="item in list2" :key="item.id">
            <div class="coupon">
                <div class="box">
                    <img class="img" src="../assets/icon_youhuiquan_01@3x (2).png" alt="" />
                    <div class="coupon-top">
                        <div class="text1">{{ item.name }}</div>
                        <div class="text2" v-if="item.limited">仅限规定商品可用</div>
                    </div>
                </div>
                <div class="box1">
                    <div style="width: 100%">
                        <div class="top">
                            <span class="text3 name1"
                                >兑换券编码<span class="text4"> {{ item.id }}</span></span
                            >
                            <div class="text6">{{ checkTime(item) ? '已使用' : '已过期' }}</div>
                        </div>

                        <div class="top1">
                            <div class="text5">有效期至:{{ item.expiration }}</div>
                            <div class="btn2">立即使用</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <el-empty v-if="empty" description="暂无兑换券哦"></el-empty>
    </div>
</template>

<script>
import { mapState } from 'vuex';
import coupon from '../mixins/coupon';
export default {
    mixins: [coupon],
    data() {
        return {
            list: [],
            list2: [],
            empty: false,
            chooseId: 0
        };
    },
    computed: {
        ...mapState(['couponInfo', 'userInfo'])
    },
    mounted() {
        if (this.isLogin) {
            this.add();
        }
    },
    methods: {
        add() {
            this.$http
                .post(
                    '/userCoupon/all',
                    {
                        query: {
                            userId: this.userInfo.id
                            // used: false
                        }
                    },
                    {
                        body: 'json'
                    }
                )
                .then(res => {
                    this.list = res.content.filter(item => {
                        return !item.used && this.checkTime(item);
                    });
                    this.list2 = res.content.filter(item => {
                        return item.used || !this.checkTime(item);
                    });
                    if (this.list.length === 0) {
                        this.empty = true;
                    }
                });
        },
        choose(info) {
            this.$router.push('/collection');
        }
    }
};
</script>

<style lang="less" scoped>
.list {
    padding: 24px 0 50px;
}
.used {
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #939599;
    line-height: 24px;
    margin: 60px 0 20px 10px;
}
.coupon {
    border-radius: 8px;
    display: flex;
    padding: 0 10px;
    margin-bottom: 16px;
    .box {
        display: flex;
        align-items: center;
        justify-content: center;
        .img {
            width: 189px;
            height: 120px;
        }
        .coupon-top {
            padding: 16px 0;
            position: absolute;
            z-index: 2;
            .text1 {
                text-align: center;
                font-size: 24px;
                font-family: PingFangSC-Medium, PingFang SC;
                font-weight: bold;
                color: #1a1a1a;
                line-height: 36px;
            }
            .text2 {
                font-size: 12px;
                font-weight: 400;
                color: #1a1a1a;
                line-height: 22px;
                text-align: center;
            }
        }
    }
    .box1 {
        background: #1f2230;
        border-radius: 0 8px 8px 0;
        width: 100%;
        display: flex;
        justify-content: space-between;
        padding-left: 16px;
        .top {
            display: flex;
            align-items: center;
            justify-content: space-between;
            .text3 {
                font-size: 14px;
                font-weight: 400;
                color: #939599;
                line-height: 24px;
                &.name1 {
                    padding-top: 15px;
                }
            }
            .text4 {
                font-size: 14px;
                font-weight: 400;
                color: #ffffff;
                line-height: 24px;
                text-align: bottom;
            }
            .text6 {
                width: 49px;
                height: 18px;
                background: #939599;
                text-align: center;
                font-size: 11px;
                margin-top: -18px;
                color: #ffffff;
                border-radius: 0px 8px 0px 8px;
            }
        }
        .top1 {
            display: flex;
            justify-content: space-between;
            .text5 {
                font-size: 11px;
                font-weight: 400;
                color: #939599;
                line-height: 16px;
                margin-top: 45px;
            }
            .btn {
                width: 180px;
                height: 28px;
                background: linear-gradient(135deg, #fdfb60 0%, #ff8f3e 100%);
                border-radius: 14px;
                font-size: 12px;
                color: #1a1a1a;
                line-height: 28px;
                text-align: center;
                cursor: pointer;
                margin: 11px 20px 0 0;
            }
            .btn2 {
                width: 180px;
                height: 28px;
                font-size: 12px;
                color: #ffffff;
                background: #939599;
                border-radius: 14px;
                line-height: 28px;
                text-align: center;
                margin: 11px 20px 0 0;
            }
        }
    }
}
</style>
