<template>
  <div class="coupon-container">
    <div class="header">
      <div class="back-btn" @click="$router.go(-1)"><</div>
      <div class="header-text">
        优惠券包
      </div>
    </div>

    <div class="coupon-content">
      <div class="discount_for_a_amount_container">
        <div class="container-title">
          满减券限量
        </div>
        <div class="coupons-content">
          <div class="coupon-item" v-for="coupon in coupons" :key="coupon.id">
            <div class="coupon-filtered-item" v-if="coupon.type === 'discount_for_a_amount'" 
            :style="{
              backgroundImage: coupon.got
                ? 'url(/imgs/shop/coupon/bg_useless1.png)' 
                : 'url(/imgs/shop/coupon/bg_useful1.png)'
            }">
              <div class="text-left">￥{{ coupon.discount }}</div>
              <div class="text-mid-w">满{{ coupon.for_a_amount }}可用</div>
              <div class="text-right" @click="getCoupon('discount_for_a_amount',coupon.id)" v-if="!coupon.got">立即领取</div>
              <div class="text-right" v-else>已领取</div>
            </div>
          </div>
        </div>
      </div>
      <div class="discount_container">
        <div class="container-title" style="margin-bottom: 10px;">
          抵扣券限量
        </div>
        <div class="coupons-content grid-container">
          <div class="coupon-item" v-for="coupon in coupons" :key="coupon.id">
            <div class="coupon-filtered-item2" v-if="coupon.type === 'deduction'"
            :style="{
              backgroundImage: coupon.got
                ? 'url(/imgs/shop/coupon/bg_useless2.png)' 
                : 'url(/imgs/shop/coupon/bg_useful2.png)'
            }">
              <div class="text-top">￥{{ coupon.price }}</div>
              <div class="text-mid-h">无门槛</div>
              <div class="text-bottom" @click="getCoupon('deduction',coupon.id)" v-if="!coupon.got">立即领取</div>
              <div class="text-bottom" v-else>已领取</div>
            </div>
          </div>
        </div>
      </div>
      <div class="deduction_container">
        <div class="container-title">
          折扣券限量
        </div>
        <div class="coupons-content">
          <div class="coupon-item" v-for="coupon in coupons" :key="coupon.id">
            <div class="coupon-filtered-item" v-if="coupon.type === 'discount'"
            :style="{
              backgroundImage: coupon.got
                ? 'url(/imgs/shop/coupon/bg_useless1.png)' 
                : 'url(/imgs/shop/coupon/bg_useful1.png)'
            }">
              <div class="text-left">{{ coupon.precent/10 }}折</div>
              <div class="text-right" @click="getCoupon('discount',coupon.id)" v-if="!coupon.got">立即领取</div>
              <div class="text-right" v-else>已领取</div>
            </div>
          </div>
        </div>
      </div>
    </div>

  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { useUserStore } from '@/stores/user'
import api from '@/utils/api'
import { ElMessage } from 'element-plus'

const userStore = useUserStore()

const coupons = ref([])

const getAllCoupons = async () => {
  const {data} = await api.get('/coupon',{
    params: {
      user_id: userStore.user.id
    }
  })
  if (data.success) {
    coupons.value = data.coupon
    console.log(123,coupons.value)
  } else {
    ElMessage.error(data.message || '优惠券领取失败')
  }
}

const getCoupon = async (coupon_type,coupon_id) => {
  try {
    const {data} = await api.get(`/coupon/${userStore.user.id}`,{
      params: {
        coupon_type: coupon_type,
        coupon_id: coupon_id
      }
    })
    if (data.success) {
      ElMessage.success('优惠券领取成功')
      getAllCoupons()
    } else {
      ElMessage.error(data.msg || '优惠券领取失败')
    }
  } catch (error) {
    ElMessage.error('优惠券领取失败')
    console.error('优惠券领取失败:', error)
  }
}

onMounted(() => {
  getAllCoupons()
})
</script>

<style scoped>
.coupon-container {
  width: 100%;
  height: 100vh;
  background-image: url(/imgs/shop/coupon/background.png);
}

.header {
  display: flex;
  align-items: center;
  justify-content: center;
  padding-left: 20px;
  position: relative;
}

.back-btn {
  font-size: 15px;
  width: 26px;
  height: 26px;
  color: #000000;
  background: transparent;
  border: none;
  position: absolute;
  left: 0;
  margin-left: 30px;
  margin-top: 5px;
}

.header-text {
  width: 80px;
  height: 28px;
  opacity: 1;
  font-family: SF Pro;
  font-weight: 650;
  font-style: Expanded Semibold;
  font-size: 20px;
  line-height: 28px;
  letter-spacing: 0%;
  color: #2F4FB5;
  text-align: center;
}

.coupon-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 20px;
  padding: 20px;
  height: 100vh;
}

.discount_for_a_amount_container,
.discount_container,
.deduction_container {
  background: white;
  width: 348px;
  padding: 10px;
  top: 32px;
  opacity: 1;
  border-radius: 8px;
  margin-bottom: 20px;
}

.container-title {
  width: 96;
  height: 20;
  top: 324px;
  left: 105px;
  opacity: 1;
  font-family: SF Pro;
  font-weight: 700;
  font-style: Bold;
  font-size: 16px;
  line-height: 20px;
  letter-spacing: 0%;
  color: #2F4FB5;
  text-align: center;
}

.coupon-filtered-item {
  width: 324px;
  height: 66px;
  top: 58px;
  left: 12px;
  opacity: 1;
  background-size: 100% 100%;
  margin-top: 10px;

  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.text-left {
  font-size: 25px;
  font-weight: bold;
  color: #305DEF;
}

.text-mid-w {
  font-size: 14px;
  color: #305DEF;
  flex: 1;
  text-align: center;
  font-family: SF Pro;
  font-weight: 590;
  font-style: Semibold;
  font-size: 16px;
  line-height: 20px;
  letter-spacing: 0%;
}

.text-right {
  font-size: 14px;
  color: #ffffff;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 100px;
  background: #305DEF;
  transition: background-color 0.3s;
  margin-right: -10px;
}

.text-right:hover {
  background-color: #305DEF;
}

.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  justify-items: center;
}

.coupon-item {
  display: contents;
}

.coupon-filtered-item2 {
  width: 83px;
  height: 94px;
  opacity: 1;
  border-radius: 4px;
  background-size: 100% 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
}

.text-top {
  font-family: SF Pro;
  font-weight: 700;
  font-style: Bold;
  font-size: 18px;
  line-height: 20px;
  letter-spacing: 0%;
  color: #305DEF;
}

.text-mid-h {
  font-family: SF Pro;
  font-weight: 400;
  font-style: Regular;
  font-size: 15px;
  line-height: 20px;
  letter-spacing: 0%;
  color: #305DEF;
  margin-top: 10px;
}

.text-bottom {
  font-size: 10px;
  color: #305DEF;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 100px;
  transition: background-color 0.3s;
  margin-top: 8px;
}
</style>
