<template>
    <div class="ml-[20px] min-h-[70vh] px-[20px] py-[30px] w-[1000px] bg-[#fff] rounded-[var(--rounded-big)]">
        <div class="full">
            <div>
                <el-tabs v-model="activeName" class="demo-tabs" @tab-change="handleClick">
                    <el-tab-pane :label="t('unused')" name="1"></el-tab-pane>
                    <el-tab-pane :label="t('hasUsed')" name="2"></el-tab-pane>
                    <el-tab-pane :label="t('expired')" name="3"></el-tab-pane>
                </el-tabs>
                <div class="min-h-[300px]" v-loading="couponTableData.loading">
                    <div class="flex flex-wrap" v-if="couponTableData.data.length && !couponTableData.loading" >
                        <template v-for="(item,index) in couponTableData.data" :key="index">
                            <div class="w-[300px] h-[150px] mb-[15px] box-shadow flex" :class="{'mr-[15px]': (index + 1) % 3}">
                                <div class="pl-[14px] flex-1">
                                    <div class="w-[62px] h-[24px] leading-[24px] text-[14px] bg-[rgba(233,51,35,.1)] text-center text-[var(--el-color-primary)]">{{item.type_name}}</div>
                                    <div class="mt-[14px] flex items-center">
                                        <div class="text-[16px] text-[var(--el-color-primary)] mr-[14px]">
                                            <span class="price-font">￥</span>
                                            <span class="text-[32px] font-600 price-font">{{item.coupon_price}}</span>
                                        </div>
                                        <div>
                                            <div class="text-[#282828]  max-w-[107px] truncate mb-[6px]">{{item.title}}</div>
                                            <div class="text-[#969696] text-[14px]">
                                                <span v-if="item.min_condition_money === '0.00'">无门槛使用</span>
                                                <span v-else>满{{ item.coupon_min_price }}元可用</span>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="mt-[10px] text-[14px] text-[#282828] leading-[22px] truncate max-w-[220px]">适用店铺:{{item.site_name}}</div>
                                    <div class="mt-[10px] text-[12px] text-[#969696] ">
                                        <div v-if="item.valid_type == 1">领取之日起<span>{{ item.length || '' }}</span>天内有效</div>
                                        <div v-else> 有效期至<span>{{ item.expire_time ? item.expire_time.slice(0, 10) : '' }}</span></div>
                                    </div>
                                </div>
                                <div v-if="activeName == 1" class="w-[52px] px-[18px] text-[16px] text-[#fff] cursor-pointer flex items-center leading-[24px] bg-[var(--el-color-primary)]" @click="toUse(item)">
                                    去使用
                                </div>
                                <div v-if="activeName == 2" class="w-[52px] px-[18px] text-[16px] text-[#fff] cursor-pointer flex items-center leading-[24px] bg-[#999]">
                                    已使用
                                </div>
                                <div v-if="activeName == 3" class="w-[52px] px-[18px] text-[16px] text-[#fff] cursor-pointer flex items-center leading-[24px] bg-[#999]">
                                    已过期
                                </div>
                            </div>
                        </template>
                    </div>
                    <div class="flex items-center justify-center min-h-[300px] w-full" v-if="!couponTableData.data.length && !couponTableData.loading" >
                        <el-empty :description="t('emptyTips')" :image-size="200" :image="img('static/resource/images/system/empty.png')"/>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { getMyCouponList } from '@/addon/mall/api/coupon'
import { useRouter } from 'vue-router'

const router = useRouter()
const activeName = ref('1')
// 获取会员列表
const couponTableData = reactive({
    page: 1,
    limit: 10,
    total: 0,
    loading: false,
    data: []
})

const loadCouponList = (page = 1) =>{
    couponTableData.page = page
    couponTableData.loading = true
    getMyCouponList({
        page: couponTableData.page,
        limit: couponTableData.limit,
        status: activeName.value
    }).then(res =>{
        couponTableData.loading = false
        couponTableData.total = res.data.total
        couponTableData.data = res.data.data
    })
}
loadCouponList()
// 去使用
const toUse = (data) =>{
    router.push({ path: '/goods/list', query: { coupon_id: data.coupon_id,site_id: data.site_id }})
}
// 切换状态
const handleClick = (event) =>{
    activeName.value = event
    loadCouponList()
}
</script>

<style lang="scss" scoped>
.box-shadow{
    box-shadow: 0 3px 20px rgba(0,0,0,.08);
}
</style>