<template>
	<div class="fix-video is-bg" :style="baseStyle">
		<div class="inner" v-if="video">
			<template v-if="mode == 'horizontal'">
				<video :poster="cover" width="360" height="206" controls>
					<source :src="video" type="video/mp4" />
				</video>
			</template>
			<template v-else>
				<video :poster="cover" width="360" height="612" controls>
					<source :src="video" type="video/mp4" />
				</video>
			</template>
		</div>
		<template v-else>
			<div class="empty">
				<span>视频播放器</span>
			</div>
		</template>
	</div>
</template>

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

const props = defineProps({
	mode: {
		type: String,
		default: "horizontal"
	},
	video: {
		type: String,
		default: ""
	},
	cover: {
		type: String,
		default: ""
	},
	type: {
		type: String,
		default: "local"
	},
	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
			};
		}
	}
});
const baseStyle = computed(() => {
	return {
		margin: `${props.styleSpacing.marginTop / 2}px ${props.styleSpacing.marginLR / 2}px ${props.styleSpacing.marginBottom / 2}px ${props.styleSpacing.marginLR / 2}px`,
		color: props.styleColor.color,
		padding: `${props.styleSpacing.padding / 2}px`,
		"--opacity": props.styleColor.opacity,
		"--background": props.styleColor.backgroundColor,
		borderRadius: `${props.styleSpacing.borderTopLR / 2}px ${props.styleSpacing.borderTopLR / 2}px ${props.styleSpacing.borderBottomLR / 2}px  ${props.styleSpacing.borderBottomLR / 2}px`
	};
});
</script>

<style lang="scss" scoped>
.fix-video {
	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;
}
video {
	margin: 0;
	padding: 0;
	display: block; /* 避免 inline 元素造成的间隙 */
	object-fit: cover;
}
</style>
