<template>
	<view :style="componentStyle">
		<!-- 固定布局模式 -->
		<view v-if="value.showStyle == 'fixed'" :class="['channel-list', 'row1-of' + value.rowCount]"
			style="padding:20rpx;">
			<view v-for="(item, index) in value.list" :key="index" class="channel-item" @tap="playVideo(item)">
				<view class="channel-img-wrap">
					<image class="channel-img" style="border-radius:10rpx;" :src="$util.img(item.coverUrl)"
						mode="aspectFill" @error="imgError(index)"></image>
					<view class="channel-play-btn">
						<image class="play-icon" style="width:30rpx;" :src="$util.img('addon/personnel/shop/view/enterprise/play.png')"
							mode="widthFix"></image>
					</view>
					<view class="channel-stats">
						<text>{{ item.viewCount }}次观看</text>
					</view>
				</view>
				<view class="channel-info-wrap">
					<view class="channel-name" :style="{
						'font-size': (value.font.size * 2 + 'rpx') + ';',
						'font-weight': value.font.weight + ';',
						'color': value.font.color + ';'
					}">{{ item.channelName }}</view>
					<view class="video-title" :style="{
						'font-size': (value.font.size * 1.8 + 'rpx') + ';',
						'color': '#666;'
					}">{{ item.videoTitle }}</view>
				</view>
			</view>
		</view>

		<!-- 其他布局模式 -->
		<scroll-view v-else :class="['channel-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="['channel-nav-item', value.mode]"
					:style="{ width: (100 / value.rowCount + '%') + ';' }" @tap="playVideo(item)">
					<view class="channel-img">
						<image :style="{
							'max-width': '100%;',
							'border-radius': '8rpx;'
						}" :src="$util.img(item.coverUrl)" mode="aspectFill"
							:show-menu-by-longpress="true"></image>
						<view class="channel-play-btn">
							<image class="play-icon" style="width:30rpx;" :src="$util.img('addon/personnel/shop/view/enterprise/play.png')"
								mode="widthFix"></image>
						</view>
						<view class="channel-stats">
							<text>{{ item.viewCount }}次观看</text>
						</view>
					</view>
					<view class="channel-text" :style="{
						'margin-left': '16rpx;',
						'font-size': (value.font.size * 2 + 'rpx') + ';',
						'font-weight': value.font.weight + ';',
						'color': value.font.color + ';'
					}">{{ item.videoTitle }}</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>

import DiyMinx from './minx.js'
export default {
	name: 'diy-wechat-channel-list',
	props: {
		value: {
			type: Object,
			default: () => ({})
		}
	},
	mixins: [DiyMinx],
	data() {
		return {
			pageWidth: '',
			indicatorDots: false,
			swiperCurrent: 0
		}
	},
	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 : '') + ';';

			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: {
		// 播放视频
		async playVideo(item) {
			await this.__$emitEvent({
				eventName: 'video-play', data: item, promiseCallback: (event, handler, awaitedResult) => {
					if (!awaitedResult) return;
					// 检查微信环境
					if (typeof wx === 'undefined') {
						console.error('当前环境不是微信小程序');
						return;
					}
					// 检查基础库版本
					const systemInfo = wx.getSystemInfoSync();
					const SDKVersion = systemInfo.SDKVersion;
					const versionCompare = (v1, v2) => {
						const arr1 = v1.split('.');
						const arr2 = v2.split('.');
						for (let i = 0; i < Math.max(arr1.length, arr2.length); i++) {
							const num1 = parseInt(arr1[i] || 0);
							const num2 = parseInt(arr2[i] || 0);
							if (num1 > num2) return 1;
							if (num1 < num2) return -1;
						}
						return 0;
					};
					if (versionCompare(SDKVersion, '2.19.2') < 0) {
						console.error('当前微信基础库版本过低，需要 2.19.2 或以上版本');
						return;
					}
					// 调用微信视频号播放API
					if (wx.openChannelsActivity) {
						wx.openChannelsActivity({
							feedId: item.feedId,
							finderUserName: item.finderUserName,
							success: (res) => {
								console.log('打开视频号成功', res);
							},
							fail: (err) => {
								console.error('打开视频号失败', err);
								// 错误码处理
								switch (err.errCode) {
									case 40001:
										console.error('错误：40001，检查主体要求或嵌入式打开的关联关系');
										break;
									case 40002:
										console.error('错误：40002，参数错误，检查 feedId 和 finderUserName');
										break;
									default:
										console.error('错误：' + err.errCode + '，' + (err.errMsg || '未知错误'));
										break;
								}
							}
						});
					} else {
						console.error('当前环境不支持微信视频号');
					}
				}
			})
		},

		// 图片加载错误处理
		imgError(index) {
			// 图片加载失败的处理逻辑
			console.log('图片加载失败:', index);
			// 为失败的图片设置默认图片
			const item = this.value.list[index];
			if (item) {
				// 使用默认图片替代加载失败的图片
				item.coverUrl = 'addon/personnel/shop/view/enterprise/default-video-cover.png';
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.channel-list {
	&.row1-of3 {
		display: flex;
		flex-wrap: wrap;

		.channel-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;
			}

			.channel-img-wrap {
				position: relative;
				overflow: hidden;
				height: 220rpx;
				border-radius: 10rpx;
			}

			.channel-img {
				width: 100%;
				height: 100%;
			}

			.channel-play-btn {
				position: absolute;
				top: 50%;
				left: 50%;
				transform: translate(-50%, -50%);
				width: 60rpx;
				height: 60rpx;
				background-color: rgba(0, 0, 0, 0.4);
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
			}

			.channel-stats {
				position: absolute;
				bottom: 0;
				left: 0;
				right: 0;
				background: linear-gradient(transparent, rgba(0, 0, 0, 0.6));
				padding: 10rpx;
				color: #fff;
				font-size: 20rpx;
			}

			.channel-info-wrap {
				display: flex;
				flex-direction: column;
				flex: 1;
				padding: 10rpx 0;

				.channel-name {
					margin-bottom: 4rpx;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
				}

				.video-title {
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
				}
			}
		}
	}

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

		.channel-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;
			}

			.channel-img-wrap {
				position: relative;
				overflow: hidden;
				height: 340rpx;
				border-radius: 10rpx;
			}

			.channel-img {
				width: 100%;
				height: 100%;
			}

			.channel-play-btn {
				position: absolute;
				top: 50%;
				left: 50%;
				transform: translate(-50%, -50%);
				width: 80rpx;
				height: 80rpx;
				background-color: rgba(0, 0, 0, 0.4);
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
			}

			.channel-stats {
				position: absolute;
				bottom: 0;
				left: 0;
				right: 0;
				background: linear-gradient(transparent, rgba(0, 0, 0, 0.6));
				padding: 10rpx;
				color: #fff;
				font-size: 20rpx;
			}

			.channel-info-wrap {
				display: flex;
				flex-direction: column;
				flex: 1;
				padding: 10rpx 0;

				.channel-name {
					margin-bottom: 4rpx;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
				}

				.video-title {
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
				}
			}
		}
	}
}

.channel-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;
		}

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

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

		.channel-img {
			position: relative;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 100%;
			height: 200rpx;
			border-radius: 10rpx;
			overflow: hidden;

			image {
				width: 100%;
				height: 100%;
			}

			.channel-play-btn {
				position: absolute;
				top: 50%;
				left: 50%;
				transform: translate(-50%, -50%);
				width: 60rpx;
				height: 60rpx;
				background-color: rgba(0, 0, 0, 0.4);
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
			}

			.channel-stats {
				position: absolute;
				bottom: 0;
				left: 0;
				right: 0;
				background: linear-gradient(transparent, rgba(0, 0, 0, 0.6));
				padding: 10rpx;
				color: #fff;
				font-size: 20rpx;
			}
		}

		.channel-text {
			padding-top: 12rpx;
			line-height: 1.5;
			white-space: nowrap;
			text-overflow: ellipsis;
			overflow: hidden;
			width: 100%;
			text-align: center;
		}
	}
}
</style>