<template>
	<view :style="componentStyle">
		<!-- 固定布局模式 -->
		<view v-if="value.showStyle == 'fixed'" :class="['goods-list', 'row1-of' + value.rowCount]"
			style="padding:20rpx;">
			<view v-for="(item, index) in value.list" :key="index" class="goods-item" @tap="showVideo(item)">
				<view class="goods-img-wrap">
					<image class="goods-img" style="border-radius:10rpx 10rpx 0 0;" :src="$util.img(item.imageUrl)"
						mode="widthFix" @error="imgError(index)"></image>
					<view style="position:absolute;top:10rpx;right:10rpx;">
						<image style="width:30rpx;" :src="$util.img('addon/personnel/shop/view/enterprise/play.png')"
							mode="widthFix"></image>
					</view>
				</view>
				<view class="info-wrap">
					<view class="goods-name" :style="{
						'font-size': (value.font.size * 2 + 'rpx') + ';',
						'font-weight': value.font.weight + ';',
						'color': value.font.color + ';'
					}">{{ item.title }}</view>
				</view>
			</view>
		</view>

		<!-- 其他布局模式 -->
		<scroll-view v-else :class="['video-nav', value.showStyle == 'fixed' ? 'fixed-layout' : value.showStyle]"
			:scroll-x="value.showStyle == 'singleSlide'">
			<view class="uni-scroll-view-content">
				<view v-for="(item, index) in value.list" :key="index" :class="['video-nav-item', value.mode]"
					:style="{ width: (100 / value.rowCount + '%') + ';' }" @tap="showVideo(item)">
					<view class="video-img">
						<image v-if="item.iconType == 'img'" :style="{
							'max-width': '200rpx;',
							'max-height': '200rpx;',
							'border-radius': '8rpx;'
						}" :src="$util.img(item.imageUrl) || $util.img('public/uniapp/default_img/goods.png')" mode="widthFix"
							:show-menu-by-longpress="true"></image>
						<view style="position:absolute;top:10rpx;right:10rpx;">
							<image style="width:30rpx;"
								:src="$util.img('addon/personnel/shop/view/enterprise/play.png')" mode="widthFix">
							</image>
						</view>
					</view>
					<view class="video-text" :style="{
						'margin-left': '16rpx;',
						'font-size': (value.font.size * 2 + 'rpx') + ';',
						'font-weight': value.font.weight + ';',
						'color': value.font.color + ';'
					}">{{ item.title }}</view>
				</view>
			</view>
		</scroll-view>

		<!-- 视频播放弹窗 -->
		<uni-popup ref="videoPopup" type="center" style="background:transparent;width:100%;height:100%;">
			<view class="video-container" style="position:fixed;top:30%;width:100%;left:0;">
				<video class="adaptive-video" :autoPauseIfNavigate="true" :autoPauseIfOpenNative="true"
					:autoplay="false" :enableAutoRotation="true" id="myVideo" :src="video_url" :controls="true"></video>
			</view>
		</uni-popup>
	</view>
</template>

<script>

import DiyMinx from './minx.js'
export default {
	name: 'diy-video-list',
	props: {
		value: {
			type: Object,
			default: () => ({})
		}
	},
	mixins: [DiyMinx],
	data() {
		return {
			pageWidth: '',
			indicatorDots: false,
			swiperCurrent: 0,
			video_url: ''
		}
	},
	created() {
		// 组件创建时的逻辑
	},
	watch: {
		componentRefresh(newValue) {
			// 监听组件刷新
		}
	},
	computed: {
		componentStyle() {
			let style = '';
			style += 'background-color:' + this.value.componentBgColor + ';';

			if (this.value.componentAngle == 'round') {
				style += 'border-top-left-radius:' + (2 * this.value.topAroundRadius) + 'rpx;';
				style += 'border-top-right-radius:' + (2 * this.value.topAroundRadius) + 'rpx;';
				style += 'border-bottom-left-radius:' + (2 * this.value.bottomAroundRadius) + 'rpx;';
				style += 'border-bottom-right-radius:' + (2 * this.value.bottomAroundRadius) + 'rpx;';
			}

			style += 'box-shadow:' + (this.value.ornament.type == 'shadow' ? '0 0 10rpx ' + this.value.ornament.color : '') + ';';
			style += 'border:' + (this.value.ornament.type == 'stroke' ? '2rpx solid ' + this.value.ornament.color : '') + ';';

			console.log(this.value);

			return style;
		},

		swiperHeight() {
			let height = 0;

			if (this.value.mode == 'graphic') {
				height = (49 + this.value.imageSize) * this.value.pageCount;
			} else if (this.value.mode == 'img') {
				height = (22 + this.value.imageSize) * this.value.pageCount;
			} else if (this.value.mode == 'text') {
				height = 43 * this.value.pageCount;
			}

			return 'height:' + (2 * height) + 'rpx';
		},

		isIndicatorDots() {
			return this.value.carousel.type != 'hide' &&
				1 != Math.ceil(this.value.list.length / (this.value.pageCount * this.value.rowCount));
		}
	},
	methods: {
		// 显示视频播放弹窗
		showVideo(item) {
			this.video_url = item.videoUrl;
			this.$refs.videoPopup.open();
		},

		// 图片加载错误处理
		imgError(index) {
			// 图片加载失败的处理逻辑
			console.log('图片加载失败:', index);
		}
	}
}
</script>

<style lang="scss" scoped>
.goods-name {
	white-space: nowrap;
	text-overflow: ellipsis;
	overflow: hidden;
}

.goods-list {
	&.row1-of3 {
		display: flex;
		flex-wrap: wrap;

		.goods-item {
			position: relative;
			display: flex;
			flex-direction: column;
			margin-top: 20rpx;
			width: calc(33.3333333% - 14rpx);
			box-sizing: border-box;

			&:nth-child(3n + 3) {
				width: calc(33.33% - 15rpx);
			}

			&:nth-of-type(1),
			&:nth-of-type(2),
			&:nth-of-type(3) {
				margin-top: 0;
			}

			&:nth-child(3n) {
				width: calc(33.3333333% - 15rpx);
			}

			&:nth-child(3n-1) {
				margin-left: 20rpx;
				margin-right: 20rpx;
			}

			&.shadow {
				width: calc(33.3333333% - 18rpx);

				&:nth-of-type(1),
				&:nth-of-type(2),
				&:nth-of-type(3) {
					margin-top: 8rpx;
				}

				&:nth-child(1n) {
					margin-left: 8rpx;
				}

				&:nth-child(3n-1) {
					margin-left: 20rpx;
					margin-right: 20rpx;
				}

				&:nth-child(3n) {
					margin-right: 0;
					margin-left: 0;
				}
			}

			.goods-img-wrap {
				position: relative;
				overflow: hidden;
				height: 220rpx;
			}

			.goods-img {
				width: 100%;
			}

			.info-wrap {
				display: flex;
				flex-direction: column;
				flex: 1;

				.pro-info {
					margin-top: auto;
					display: flex;
					flex-direction: column;
					justify-content: space-between;

					.discount-price {
						display: flex;
						justify-content: space-between;
						align-items: center;

						.price-wrap {
							white-space: nowrap;

							.unit {
								font-size: 24rpx !important;
							}

							.price {
								font-size: 32rpx;

								text {
									font-weight: 700;
								}
							}
						}
					}

					.delete-price {
						text-decoration: line-through;
						flex: 1;
						line-height: 28rpx;
						color: #909399;
						font-size: 20rpx;
					}
				}
			}
		}

		&.style-1 {
			.pro-info {
				.price-wrap {
					line-height: 1;
				}

				.discount-price {
					justify-content: unset !important;
					align-items: baseline !important;
					flex-wrap: wrap;
				}

				.delete-price {
					margin-left: 10rpx;
				}
			}
		}

		&.style-2 {
			.pro-info {
				position: relative;
				flex-direction: row !important;
				align-items: center;

				.price-wrap {
					line-height: 1;
				}

				.discount-price {
					align-items: flex-end !important;
					flex-wrap: wrap;
					justify-content: unset !important;
				}

				.delete-price {
					margin: 20rpx 0;
					flex-basis: 100% !important;
				}

				.buy-btn {
					min-width: 112rpx;
					height: 52rpx;
					padding: 0 20rpx;
					line-height: 52rpx;
					text-align: center;
					box-sizing: border-box;
				}
			}
		}

		.sell-out {
			text {
				font-size: 150rpx;
			}
		}
	}

	&.row1-of2 {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;

		.goods-item {
			position: relative;
			margin-top: 20rpx;
			width: calc(50% - 10rpx);
			display: flex;
			flex-direction: column;
			box-sizing: border-box;

			&:nth-child(2n) {
				margin-right: 0 !important;
			}

			&:nth-of-type(1),
			&:nth-of-type(2) {
				margin-top: 0;
			}

			&.shadow {
				width: calc(50% - 18rpx);

				&:nth-child(2n-1) {
					margin-left: 8rpx;
				}

				&:nth-child(2n) {
					margin-right: 8rpx !important;
				}

				&:nth-of-type(1),
				&:nth-of-type(2) {
					margin-top: 8rpx;
				}
			}

			.goods-img-wrap {
				position: relative;
				overflow: hidden;
				height: 340rpx;
			}

			.goods-img {
				width: 100%;
			}

			.info-wrap {
				display: flex;
				flex-direction: column;
				flex: 1;

				.sale {
					flex-basis: 100%;
				}

				.pro-info {
					margin-top: auto;
					display: flex;
					flex-direction: row;
					justify-content: space-between;

					.discount-price {
						.price-wrap {
							white-space: nowrap;

							.unit {
								font-weight: 700;
								font-size: 24rpx !important;
							}

							.price {
								font-weight: 700;
								font-size: 32rpx !important;
							}
						}
					}

					.delete-price {
						text-decoration: line-through;
						flex: 1;
						line-height: 28rpx;
						color: #909399;
						font-size: 20rpx;
					}
				}
			}
		}

		&.style-1 {
			.pro-info {
				.discount-price {
					display: flex;
					flex-wrap: wrap;
					align-items: baseline;

					.price-wrap {
						display: inline-block;

						text {
							font-weight: 700;
						}

						line-height: 1;
					}
				}

				.delete-price {
					margin-top: 6rpx;
					flex-basis: 100% !important;
				}
			}
		}

		&.style-2 {
			.pro-info {
				position: relative;
				align-items: center;

				.price-wrap {
					line-height: 1;
				}

				.discount-price {
					display: flex;
					flex-wrap: wrap;
					align-items: baseline;
				}

				.delete-price {
					margin-top: 4rpx;
					flex-basis: 100% !important;
				}

				.sale {
					line-height: 1;
					margin-top: 10rpx;
				}

				.buy-btn {
					min-width: 140rpx;
					height: 52rpx;
					padding: 0 20rpx;
					line-height: 52rpx;
					text-align: center;
					box-sizing: border-box;
				}
			}
		}

		&.style-3 {
			.pro-info {
				.member-price {
					margin-right: auto;
					align-self: flex-end;
					margin-bottom: 4rpx;
				}

				.sale {
					line-height: 1;
					align-self: center;
					margin-top: 8rpx;
				}

				.discount-price {
					display: flex;
					flex-wrap: wrap;
					flex: 1;
					align-content: center;

					.price-wrap {
						display: flex;
						align-items: baseline;
						line-height: 1;
						align-self: center;
					}
				}
			}

			.swiper {
				padding: 20rpx 0;
			}
		}

		.sell-out {
			text {
				font-size: 250rpx;
			}
		}
	}
}

.video-container {
	width: 100%;
	height: 300px;
	position: relative;
}

.adaptive-video {
	width: 100%;
	height: 100%;
	object-fit: contain;
	position: absolute;
	top: 0;
	left: 0;
}

.video-nav {
	padding: 16rpx;
	box-sizing: border-box;

	&.fixed-layout {
		.uni-scroll-view-content {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
		}
	}

	&.singleSlide {
		.uni-scroll-view-content {
			display: flex;
		}

		.video-nav-item {
			flex-shrink: 0;
		}
	}

	&.pageSlide {
		position: relative;

		.uni-swiper-dots-horizontal {
			bottom: 0rpx;
		}

		&.straightLine {
			.uni-swiper-dot {
				width: 30rpx;
				border-radius: 0;
				height: 8rpx;
			}
		}

		&.circle {
			.uni-swiper-dot {
				width: 14rpx;
				height: 14rpx;
			}
		}
	}

	.video-nav-wrap {
		display: flex;
		flex-wrap: wrap;
		width: 100%;
		height: 100%;
	}

	.video-nav-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 14rpx 0;
		box-sizing: border-box;

		.video-text {
			padding-top: 12rpx;
			line-height: 1.5;
			white-space: nowrap;
			text-overflow: ellipsis;
			overflow: hidden;
			width: 100%;
			text-align: center;

			&.alone {
				padding-top: 0;
			}
		}

		&.text {
			.video-text {
				padding-top: 0;
			}
		}

		.video-img {
			position: relative;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 90rpx;

			.tag {
				position: absolute;
				top: -10rpx;
				right: -24rpx;
				color: #fff;
				border-radius: 24rpx;
				border-bottom-left-radius: 0;
				-webkit-transform: scale(0.8);
				transform: scale(0.8);
				padding: 8rpx 16rpx;
				line-height: 1;
				font-size: 24rpx;
			}

			.icon {
				font-size: 50rpx;
				color: #606266;
			}
		}
	}
}

.swiper-dot-box {
	width: 100%;
	display: flex;
	align-items: center;
	justify-content: center;
	margin-top: -20rpx;
	padding-bottom: 8rpx;

	.swiper-dot {
		background-color: rgba(0, 0, 0, 0.3);
		margin: 8rpx;

		&.active {
			background-color: #000;
		}
	}

	&.straightLine {
		.swiper-dot {
			width: 30rpx;
			border-radius: 0;
			height: 8rpx;
		}
	}

	&.circle {
		.swiper-dot {
			width: 15rpx;
			border-radius: 50%;
			height: 15rpx;
		}
	}
}
</style>