<template>
	<div class="fix-coupon is-bg" :style="baseStyle">
		<div class="coupon-activity" v-if="item.id">
			<div class="a">
				<div class="text">
					<span class="name">{{ item?.title }}</span>
					<span class="desc">{{ item?.description }}</span>
				</div>

				<span class="tag" v-if="item">点击领券</span>
			</div>

			<div class="b">
				<template v-if="item">
					<span class="amount">{{ item?.amount || 0 }}</span>
					<span class="doc">
						{{ doc }}
					</span>
				</template>
			</div>

			<div class="c"></div>
		</div>
		<template v-else>
			<div class="empty">
				<span>优惠券</span>
			</div>
		</template>
	</div>
</template>

<script lang="ts" name="fix-coupon" setup>
import { computed, type PropType } from "vue";
import { Form } from "../../types/form";

const props = defineProps({
	item: {
		type: Object as PropType<Eps.MarketCouponInfoEntity>,
		default: () => {
			return {
				id: 0,
				title: "",
				description: "",
				amount: 0,
				type: 0,
				condition: ""
			};
		}
	},
	color: {
		type: String,
		default: "#2b2e3d"
	},
	styleSpacing: {
		type: Object as PropType<Form.Spacing>,
		default: () => {
			return {
				marginTop: 0,
				marginBottom: 0,
				marginLR: 0,
				padding: 0,
				borderTopLR: 0,
				borderBottomLR: 0
			};
		}
	},
	styleColor: {
		type: Object as PropType<Form.Color>,
		default: () => {
			return {
				color: "#000",
				backgroundColor: "#FFFFFF",
				opacity: 1
			};
		}
	}
});

/**
 * 将十六进制颜色转换为 RGB
 * @param  hex - 十六进制颜色代码 (例如: #2b2e3d)
 * @returns - RGB 颜色字符串 (例如: 43, 46, 61)
 */
function hexToRgb(hex: string) {
	// 移除前导 #
	hex = hex.replace(/^#/, "");

	// 处理 3 位和 6 位十六进制颜色
	if (hex.length === 4) {
		hex = hex
			.split("")
			.map((char: any, index: number) => (index > 0 ? char + char : char))
			.join("");
	}

	const bigint = parseInt(hex, 16);
	return [(bigint >> 16) & 255, (bigint >> 8) & 255, bigint & 255].join(", ");
}

const baseStyle = computed(() => {
	const { color, styleSpacing, styleColor } = props;

	// 计算样式值
	const marginTop = styleSpacing.marginTop / 2 + "px";
	const marginLR = styleSpacing.marginLR / 2 + "px";
	const marginBottom = styleSpacing.marginBottom / 2 + "px";
	const padding = styleSpacing.padding / 2 + "px";
	const borderRadius = `${styleSpacing.borderTopLR / 2}px ${styleSpacing.borderTopLR / 2}px ${styleSpacing.borderBottomLR / 2}px ${styleSpacing.borderBottomLR / 2}px`;

	const rgbaColor = hexToRgb(color);

	// 返回样式对象
	return {
		margin: `${marginTop} ${marginLR} ${marginBottom} ${marginLR}`,
		color: styleColor.color,
		padding,
		"--opacity": styleColor.opacity,
		"--background": styleColor.backgroundColor,
		"--color": rgbaColor,
		borderRadius
	};
});
// 使用说明
const doc = computed(() => {
	const { type, condition } = props.item;
	switch (type) {
		case 0:
			return `满${condition?.fullAmount}可用`;
	}
});
</script>

<style lang="scss" scoped>
.fix-coupon {
	box-sizing: border-box;
	overflow: hidden;
	.empty {
		height: 100px;
		display: flex;
		justify-content: center;
		align-items: center;
		span {
			font-size: 26px;
			color: var(--el-text-color-placeholder);
		}
	}
}
.is-bg {
	position: relative;
	z-index: 1; /* 确保 .is-bg 及其子元素位于 ::after 伪元素的上层 */
}
.is-bg::after {
	content: "";
	background-color: var(--background);
	opacity: var(--opacity);
	position: absolute;
	left: 0;
	top: 0;
	right: 0;
	bottom: 0;
	z-index: -1;
	pointer-events: none;
}
.coupon-activity {
	display: flex;
	position: relative;
	height: 80px;
	letter-spacing: 1px;
	margin: 0 auto;

	.a {
		background: linear-gradient(140deg, rgba(var(--color), 0.75), rgba(var(--color), 1));
		height: 70px;
		width: calc(100% - 125px);
		border-radius: 6px;
		position: absolute;
		left: 12px;
		bottom: 1px;
		z-index: 2;
		color: #fff;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 12px;
		box-sizing: border-box;

		.name {
			display: block;
			font-size: 14px;
			font-weight: 500;
		}

		.desc {
			font-size: 12px;
			color: #ccc;
		}

		.tag {
			padding: 1px 5px;
			border-radius: 2px;
			background-color: #eb10ab;
			color: #fff;
			font-size: 12px;
			margin-right: 8px;
		}
	}

	.b {
		height: 80px;
		width: 110px;
		background-color: #e2e2e2;
		box-sizing: border-box;
		position: absolute;
		right: 12px;
		bottom: 0;
		border-radius: 6px;
		z-index: 3;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		.amount {
			font-size: 30px;
			font-weight: bold;
			line-height: 1;
			color: rgba(var(--color), 1);

			&::after {
				content: "元";
				font-size: 24px;
				position: relative;
				top: -2px;
			}
		}

		.doc {
			font-size: 12px;
			color: #666;
		}
	}

	.c {
		content: "";
		display: block;
		height: 20px;
		width: 20px;
		background-color: #868686;
		border-radius: 20px;
		z-index: 1;
		position: absolute;
		right: 108px;
		top: 1px;
	}
}
</style>
