<script setup lang="ts">
import { PageQuery } from '~/types/global'
import { reactive, ref } from 'vue'
import { couponCollectionApi } from '~/server/activityApi'
import { couponTypeFilter } from '~/utils/filter'
/**
 * 列表
 */
const where = reactive<PageQuery>({
  page: 1,
  limit: 10,
  category: 0,
})
const {
  data: tableData,
  refresh,
  pending: listLoading,
} = await useAsyncData(async () => couponCollectionApi(where), {
  default: () => ({
    list: [],
    total: 0,
  }),
})
// 分页数据
const pageChange = (e: number) => {
  where.page = e
  refresh()
}

const parsePrice = (price) => {
  if (price >= 100) {
    return parseFloat(price)
  } else {
    return price
  }
}

//领券优惠券
const useMerchants = useMerchant()
const handleReceiveCoupon = async (item: any) => {
  if (item.isUserReceive) return
  await useMerchants.onReceiveCoupon(item.id).then(() => {
    item.isUserReceive = !item.isUserReceive
  })
  // if (item.isUserReceive) return
  // loading.value = true
  // try {
  //   await couponReceiveApi(item.id)
  //   feedback.msgSuccess('领取成功')
  //   item.isUserReceive = !item.isUserReceive
  //   loading.value = false
  // } catch (e) {
  //   loading.value = false
  // }
}

//去使用
import { linkNavigateTo } from '~/utils/util'
const handleToUse = async (item:any) => {
  await linkNavigateTo('/activity/coupon_goods_list',{couponId: item.id, isUserReceive:item.isUserReceive})
}
</script>

<template>
  <div class="coupon_list pt-60px">
    <div class="wrapper_1200">
      <div class="fontColor333 text-50px text-center mb-50px">领券中心</div>
      <div class="listBox acea-row">
        <template v-for="item in tableData.list" :key="item.id">
          <div class="list acea-row row-around">
            <div class="left" v-if="item.productVoList.length" @click="handleToUse(item)">
              <div class="acea-row row-middle mb-20px">
                <div
                    class="couponlogo b-rd-14px font-color borderSolE9 w-45px h-20px lh-19px text-12px text-center mr-10px"
                >
                  {{ couponTypeFilter(item.category) }}
                </div>
                <div class="tips line1 text-#282828 text-16px">{{ item.name || '' }}</div>
              </div>
              <div class="info">
                <div class="info-box mr-10px" v-for="(items, index) in item.productVoList" :key="index">
                  <el-image :src="items.image" mode="" class="w-110px h-110px b-rd-12px mb-15px"></el-image>
                  <div class="money">
                    ¥ <span class="text-20px text-#333">{{ items.price }}</span>
                  </div>
                </div>
              </div>
            </div>
            <div class="right">
              <div class="title">
                <div>
                  <p class="fontsweight">
                    <text class="text-34px font-color">¥</text>
                    <text class="text-60px font-color">{{ parsePrice(item.money) }}</text>
                  </p>
                  <p class="text-16px font-color">
                    <text v-if="item.minPrice == 0">无使用门槛</text>
                    <text v-else>满{{ parsePrice(item.minPrice) }}元可用</text>
                  </p>
                </div>
                <div v-if="item.lastTotal === 0">
                  <div class="btn">已领完</div>
                  <span class="iconfont icon-yilingwan1"></span>
                </div>
                <div v-else>
                  <div class="btn cursors" v-if="!item.isUserReceive" @click="handleReceiveCoupon(item)">立即领取</div>
                  <div v-if="item.lastTotal > 0 && item.isUserReceive">
                    <div class="btn cursors" @click="handleToUse(item)">去使用</div>
                    <span class="iconfont icon-yilingqu"></span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </template>
      </div>
      <div class="acea-row row-middle mt-20px">
        <el-pagination
            background
            layout="prev, pager, next"
            :total="tableData.total"
            class="page-item"
            :page-size="where.limit"
            :current-page="where.page"
            @update:current-page="pageChange"
        />
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.coupon_list {
  background-image: url('~/assets/images/lqbj.png');
  background-size: 100% 160px;
  background-repeat: no-repeat;
}
.listBox {
  .list {
    width: 390px;
    height: 160px;
    overflow: hidden;
    background-size: cover;
    margin-bottom: 20px;
    text-align: center;
    background-image: url('@/assets/images/lq.png');
    background-repeat: no-repeat;
    margin-right: 20px;
    &:nth-child(2n) {
      margin-right: 0;
    }

    .cross {
      display: flex;

      .cross-left {
        margin-top: 6px;

        image {
          width: 200px;
          height: 200px;
          border-radius: 12px;
        }

        ._empty {
          width: 200px;
          height: 200px;
          border-radius: 12px;
          font-size: 24px;

          .iconfont {
            font-size: 48px;
            padding-top: 10px;
          }
        }
      }

      .cross-right {
        margin-left: 20px;
        margin-top: 6px;
        margin-bottom: 14px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;

        .title {
          width: 234px;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
        }

        .time {
          font-size: 20px;
          font-weight: 400;
          color: #666666;
        }
      }
    }

    .left {
      width: 390px;
      height: 230px;
      text-align: left;
      padding: 20px;

      .tips {
        width: 292px;
        font-weight: 600;
        color: #282828;
      }

      .info {
        display: flex;
        justify-content: flex-start;

        .info-box {
          .money {
            text-align: center;
            font-size: 12px;
            font-weight: 400;
            color: #333;
          }
        }
      }
    }

    .right {
      /* #ifdef APP */
      width: 188px;
      /* #endif */
      /* #ifndef APP */
      width: 200px;
      /* #endif */
      height: 100%;
      background-size: 100% 100%;
      position: relative;

      .icon-yilingqu,
      .icon-yilingwan1 {
        position: absolute;
        opacity: 0.1;
        font-size: 122px;
        top: -34px;
      }

      .title {
        width: 100%;
        height: 169px;
        margin: 30px auto 0;

        .font1 {
          font-size: 34px;
          font-weight: 600;
        }

        .font2 {
          font-size: 60px;
          font-weight: 600;
        }

        .font3 {
          font-size: 20px;
          font-weight: 400;
        }

        p {
          margin-top: 10px;
        }

        .btn {
          width: 130px;
          border-radius: 22px;
          font-size: 16px;
          font-weight: 400;
          color: #ffffff;
          line-height: 44px;
          text-align: center;
          margin: 20px auto 0;
          background: #e93323;

          &.disabled {
            pointer-events: none;
            background: #ccc;
            color: #fff;
            border-color: #ccc;
            cursor: not-allowed;
          }
        }

        .shiyong {
          border: 2px solid #e93323;
          background: none;
          color: #e93323;
          border-radius: 26px;

          &.disabled {
            pointer-events: none;
            color: #ccc;
            border-color: #ccc;
            cursor: not-allowed;
            background: none;
          }
        }
      }
    }
  }

  .list:nth-child(1) {
    .right {
      &::after {
        background-color: #e93323;
      }
    }
  }
}
</style>
