<script setup>
import DraggableButton from "@/pages/components/goHome.vue";
/**
 * name：
 * user：sa0ChunLuyu
 * date：2024年9月11日 19:24:50
 */
import { ref } from "vue";
import { $api, $response } from "@/api";
import { onShow } from "@dcloudio/uni-app";
import { useStore } from "@/store";
const $store = useStore();

let couponList = ref([]);
let yytjInfo = ref({});
const mountedAction = () => {
  yytjInfo.value = $store.getYytjInfo();
  GetPersonIntegralSaveMoneyCouponInfo();
};

const GetPersonIntegralSaveMoneyCouponInfo = async () => {
  const response = await $api("GetPersonIntegralSaveMoneyCouponInfo", {
    person_id: $store.getUser()?.person_id,
  });
  $response(response, () => {
    couponList.value = response.data.coupon_list;
  });
};

const selectCoupon = (item) => {
  yytjInfo.value.couponId = item.id;
  yytjInfo.value.couponPrice = item.price;
  $store.setYytjInfo(yytjInfo.value);
 let buyinfo=$store.getBuyInfo()
 buyinfo.coupon_id= item.id
 $store.setBuyInfo(buyinfo);
  uni.navigateBack({ delta: 1 });
};

const noSelectCoupon = () => {
  yytjInfo.value.couponId = "";
  yytjInfo.value.couponPrice = "";
  $store.setYytjInfo(yytjInfo.value);
  let buyinfo=$store.getBuyInfo()
  buyinfo.coupon_id=null
  $store.setBuyInfo(buyinfo);
};

const config_ref = ref(null);
const configRef = (e) => {
  if (!config_ref.value) {
    config_ref.value = e;
    mountedAction();
	uni.showModal({
		title: '提示',
		content: '优惠券仅可用于抵扣自选项目金额',
		showCancel:false,
		confirmText:'确定',
		success: function (res) {
			if (res.confirm) {
				 
			} else if (res.cancel) {
				 
			}
		}
	});
  }
};

onShow(() => {
  if (!!config_ref.value) {
    mountedAction();
  }
});
</script>
<template>
  <DraggableButton />
  <view>
    <view v-if="!!$store.config">
      <view :ref="configRef"></view>
    </view>
  </view>
  <view class="px-20rpx py-10rpx box-border">
    <view
      class="px-10rpx py-30rpx box-border bg-#fff shadow-[0rpx_0rpx_10rpx_0rpx_rgba(0,0,0,0.04)] rounded-15rpx"
    >
      <view
        v-for="(item, index) in couponList"
        :key="index"
        class="mb-20rpx flex"
        @click="selectCoupon(item)"
      >
        <view class="min-w-200rpx max-w-200rpx h-170rpx yhq1 center flex-col">
          <view>
            <text class="text-#fff text-24rpx">¥</text>
            <text class="text-#fff text-50rpx">{{ item.price }}</text>
          </view>
          <text class="text-#fff text-22rpx">{{ item.desc }}</text>
        </view>
        <view
          class="grow box-border b-1 b-l-0 b-#E5E5E5 b-solid rounded-r-15rpx flex between"
        >
          <view
            class="grow px-30rpx py-25rpx box-border flex-col between h-full items-start b-0 b-r-1 b-#E5E5E5 b-solid"
          >
            <text class="text-28rpx text-#B38933">{{ item.title }}</text>
            <text class="text-20rpx text-#7C7C7C">{{
              item.date_range?.join(" —— ")
            }}</text>
          </view>
          <view
            v-if="yytjInfo.couponId != item.id"
            class="max-w-60rpx h-full min-w-60rpx text-#B38933 rounded-r-15rpx text-25rpx center mx-0"
          >
            立<br />即<br />使<br />用
          </view>
          <view
            v-else
            @click.stop="noSelectCoupon()"
            class="max-w-60rpx h-full min-w-60rpx text-#fff rounded-r-15rpx bg-#B38933 text-25rpx center mx-0"
          >
            取<br />消<br />使<br />用
          </view>
        </view>
      </view>
    </view>
  </view>
</template>
<style scoped>
.yhq1 {
  background-image: url("@/static/assets/slices/yhq1.png");
  background-size: 100% 100%;
}
</style>
