<template>
	<view class="promotion">
		<custom-navbar title="推广服务" :leftImg="'/static/images/whiteBack.png'" :showUser="true"
			backgroundColor="linear-gradient( 134deg, #F52540 0%, #E8101E 100%)" titleColor="#fff"
			borderBottom="none"></custom-navbar>
		<view class="promotion-search">
			<view class="promotion-add">
				<image src="/static/images/recruit/add.png" class="promotion-add-img1" @click="goSelectServe"></image>
				<view class="promotion-add-text">添加推广服务</view>
				<image src="/static/images/recruit/guide.png" class="promotion-add-img2" @click="goToGuide"></image>
			</view>
			<uv-search placeholder="服务名称" v-model="listQuery.title" height="70rpx" :showAction="false" class="uv-search"
				@search="search">
				<template v-slot:prefix>
					<image src="/static/images/recruit/search_icon.png" class="search_icon"></image>
				</template>
			</uv-search>
		</view>
		<view class="promotion-content">
			<view class="promotion-tabs">
				<view class="promotion-tabs-tab" :class="{active:index==tabsIndex}" v-for="(item,index) in tabsList"
					:key="index" @tap="changtabs(index)">
					{{item.name}}
				</view>
			</view>
			<CommonList ref="groupRef" :apiUrl="apiObj.list[identityIndex]"
				:listScrollHeight="`calc(100vh - ${statusBarHeight * 2 + 440}rpx)`">
				<template #listData="{ list }">
					<view class="promotion-list">

						<view class="promotion-item" v-for="(item,index) in list" :key="index">
							<image src="/static/images/recruit/Effect.png" class="promotion-item-Effect"
								@click="goEffect(item)"></image>
							<view class="promotion-item-heard">
								<image :src="item.photo[0]" mode="aspectFill" class="promotion-item-heard-photo">
								</image>
								<view class="promotion-item-heard-right">
									<view class="promotion-item-heard-right-text1 text-overflow-1">{{item.title}}</view>
									<view class="promotion-item-heard-right-text2">
										佣金比例:{{percentage(item.commission_rate)}}</view>
									<view class="promotion-item-heard-right-text3">预估金额:{{item.estimate_commission}}元
									</view>
									<view class="promotion-item-heard-right-text4"><text
											style="font-size: 24rpx;font-weight: 500;">￥</text>{{item.server_price}}</view>
								</view>
								<view class="promotion-item-tip" v-if="item.server_price==0">温馨提示：最终佣金少于1分钱，将不再推广渠道展示
								</view>

							</view>
							<view class="promotion-item-bottom">
								<view class="promotion-item-bottom-btn" v-if="item.state==2" @click="copyServr(item)">
									复制活动</view>
								<view class="promotion-item-bottom-btn" v-if="item.state==1" @click="editServr(item)">编辑
								</view>
								<view class="promotion-item-bottom-btn" @click="openModal(item)" v-if="item.state==1">失效
								</view>
							</view>
						</view>
					</view>
				</template>

				<!-- 空数据状态 -->
				<template #empty>
					<noData></noData>
				</template>
			</CommonList>
		</view>


		<uv-modal ref="modal" title="提示" width="620rpx">
			<view class="slot-content">
				若推广服务设置失效，仅针对新订单生效；历史已付款订单仍按原分佣比例执行，分佣不受影响。
			</view>
			<template v-slot:confirmButton>
				<view class="modal-buttons">
					<view type="default" @click="closeModal" class="closeModal btnModal">取消</view>
					<view type="default" @click="sureModal" class="sureModal btnModal">我已了解</view>
				</view>

			</template>
		</uv-modal>
	</view>
</template>

<script>
	import CommonList from "@/components/common/CommonList.vue";
	import request from "@/utils/request";
	export default {
		components: {
			CommonList,
		},
		data() {
			return {
				selectItem: {},
				apiObj: {
					list: {
						2: "/sj/serversPromotion/list",
						1: "/syr/serversPromotion/list"
					},
					switch: {
						2: "/sj/serversPromotion/switch",
						1: "/syr/serversPromotion/switch"
					}
				},
				identityIndex: 2,
				tabsList: [{
						name: "推广中",
					},
					{
						name: "已失效",
					},

				],
				tabsIndex: 0, //根据身份确定index值
				keyword: '',
				listQuery: {
					state: 1,
					title: "",
				},
			}
		},
		computed: {

		},
		async onLoad() {
			const systemInfo = uni.getSystemInfoSync();
			this.statusBarHeight = systemInfo.statusBarHeight;
			this.$nextTick(() => {
				this.search();
			})
		},
		methods: {
			goEffect(item) {
				uni.navigateTo({
					url: `/pages/promotion/effect?id=${item.id}` // 路径根据实际情况修改
				});
			},
			editServr(item) {
				let selectedItems = [item]
				// 3. 序列化数组（数组不能直接作为URL参数，需转为字符串）
				const params = encodeURIComponent(JSON.stringify(selectedItems));

				// 4. 跳转页面（使用uni.navigateTo，路径替换为你的下一页路径）
				uni.navigateTo({
					url: `/pages/promotion/setUpServe?type=2&selectedItems=${params}` // 路径根据实际情况修改
				});
			},
			copyServr(item) {
				let selectedItems = [item]
				// 3. 序列化数组（数组不能直接作为URL参数，需转为字符串）
				const params = encodeURIComponent(JSON.stringify(selectedItems));

				// 4. 跳转页面（使用uni.navigateTo，路径替换为你的下一页路径）
				uni.navigateTo({
					url: `/pages/promotion/setUpServe?selectedItems=${params}` // 路径根据实际情况修改
				});
			},
			//tab切换
			changtabs(index) {
				this.listQuery.state = index + 1
				this.tabsIndex = index
				this.$nextTick(() => {
					this.search();
				})
			},
			//打开弹窗
			openModal(item) {
				this.selectItem = item
				this.$refs.modal.open();
			},
			//关闭弹窗
			closeModal() {
				this.$refs.modal.close();
			},
			//弹窗确认
			sureModal() {
				this.$refs.modal.close();
				let obj = {
					id: this.selectItem.id,
					state: 2
				}
				request.post(this.apiObj.switch[this.identityIndex], obj).then((res) => {
					if (res.code == 200) {
						uni.showToast({
							title: `提交成功`,
							icon: "none",
						});
						this.search();
					} else {
						uni.showToast({
							title: `提交失败`,
							icon: "none",
						});
					}
				}).catch(err => {
					console.error(err)
				});
			},
			percentage(decimalNum) {
				const keepDecimal = 1; // 保留 1 位小数
				const percentValue = (decimalNum * 100).toFixed(keepDecimal);
				return `${percentValue}%`;
			},
			//列表跟新
			search() {
				// 触发列表刷新
				this.$refs.groupRef.manualRefresh(this.listQuery);
			},
			// 去新手引导
			goToGuide() {
				uni.navigateTo({
					url: "/pages/agreement/agreement?type=13&title=推广服务说明",
				});
			},
			goSelectServe() {
				uni.navigateTo({
					url: "/pages/promotion/selectServe",
				});
			},
		}
	}
</script>

<style lang="less">
	.promotion {
		.promotion-search {
			background: linear-gradient(134deg, #f52540 0%, #e8101e 100%);
			box-sizing: border-box;
			padding: 31rpx 20rpx 60rpx 20rpx;

			.promotion-add {
				display: flex;
				flex-wrap: nowrap;
				align-items: center;

				.promotion-add-text {
					font-weight: 400;
					font-size: 30rpx;
					color: #FFFFFF;
					text-align: left;
					font-style: normal;
					margin: 0 10rpx;
					white-space: nowrap;
				}

				.promotion-add-img1 {
					width: 36rpx;
					height: 36rpx;
				}

				.promotion-add-img2 {
					width: 26rpx;
					height: 26rpx;
				}
			}

			.uv-search {
				margin-top: 20rpx !important;

				.uni-input-placeholder {
					color: #cccccc !important;
					font-size: 28rpx;
				}

				.search_icon {
					width: 35rpx;
					height: 34rpx;
				}
			}
		}

		.promotion-content {
			transform: translateY(-20rpx);

			.promotion-tabs {
				padding: 20rpx 110rpx;
				background: #F5F5F5;
				border-radius: 30rpx 30rpx 0rpx 0rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;


				.promotion-tabs-tab {
					font-weight: 500;
					font-size: 30rpx;
					color: #333333;
					line-height: 42rpx;
					text-align: left;
					font-style: normal;
					padding-bottom: 11rpx;
					position: relative;

					&.active {
						color: #E8101E;

						&::after {
							content: '';
							// 绝对定位在元素底部
							position: absolute;
							width: 43rpx;
							left: 50%;
							transform: translateX(-50%);
							bottom: 0rpx; // 距离文字底部的距离（可调整）
							height: 6rpx; // 横条高度
							background: linear-gradient(306deg, #FC437C 0%, #F25988 100%); // 横条颜色（与文字颜色一致）
							border-radius: 3rpx; // 横条圆角（可选，更柔和）
						}

					}
				}


			}

			.promotion-list {
				.promotion-item {
					padding: 20rpx;
					background: #FFFFFF;
					border-radius: 20rpx;
					margin-bottom: 20rpx;
					position: relative;

					.promotion-item-tip {
						width: 100%;
						font-weight: 400;
						font-size: 22rpx;
						color: #E8101E;
						line-height: 30rpx;
						text-align: left;
						font-style: normal;
					}

					.promotion-item-Effect {
						position: absolute;
						right: 0;
						top: 0;
						width: 165rpx;
						height: 49rpx;
					}

					.promotion-item-heard {
						display: flex;
						flex-wrap: wrap;
						gap: 20rpx;
						padding-bottom: 20rpx;
						border-bottom: 1rpx solid #f5f5f5;

						.promotion-item-heard-photo {
							width: 186rpx;
							height: 186rpx;
							border-radius: 16rpx;
						}

						.promotion-item-heard-right {
							.promotion-item-heard-right-text1 {
								font-weight: 500;
								font-size: 30rpx;
								color: #333333;
								line-height: 42rpx;
								text-align: left;
								font-style: normal;
								margin-bottom: 10rpx;
								margin-top: -6rpx;

								width: 300rpx;
							}

							.promotion-item-heard-right-text2 {
								margin-bottom: 11rpx;
								padding: 2rpx 8rpx;
								border-radius: 6rpx;
								width: fit-content;
								border: 1rpx solid rgba(232, 16, 30, 0.2);
								font-weight: 400;
								font-size: 24rpx;
								color: #E8101E;
								line-height: 33rpx;
								text-align: left;
								font-style: normal;
							}

							.promotion-item-heard-right-text3 {
								padding: 3rpx 8rpx;
								border-radius: 6rpx;
								width: fit-content;
								border: 1rpx solid rgba(182, 115, 43, 0.2);
								font-weight: 400;
								font-size: 24rpx;
								color: #B6732B;
								line-height: 33rpx;
								text-align: left;
								font-style: normal;
								margin-bottom: 17rpx;
							}

							.promotion-item-heard-right-text4 {
								font-family: DINPro, DINPro;
								font-weight: 500;
								font-size: 36rpx;
								color: #E8101E;
								line-height: 46rpx;
								text-align: left;
								font-style: normal;
								margin-top: -6rpx;
							}
						}
					}

					.promotion-item-bottom {
						display: flex;
						justify-content: flex-end;
						gap: 18rpx;
						margin-top: 30rpx;

						.promotion-item-bottom-btn {
							width: 133rpx;
							height: 60rpx;
							border-radius: 32rpx;
							border: 2rpx solid #979797;
							font-weight: 400;
							font-size: 24rpx;
							color: #333333;
							text-align: left;
							font-style: normal;
							display: flex;
							align-items: center;
							justify-content: center;
						}
					}
				}
			}

			.common-list {
				background-color: #fafafa;
				padding: 0;

				::v-deep .list-container {
					padding: 0 20rpx;
				}
			}
		}

	}


	.modal-buttons {

		padding: 0 54rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 60rpx;
		margin-top: 0rpx;

		.btnModal {
			width: 246rpx;
			height: 80rpx;
			border-radius: 40rpx;
			font-weight: 500;
			font-size: 28rpx;
			text-align: left;
			font-style: normal;
			display: flex;
			align-items: center;
			justify-content: center;
			box-sizing: border-box;
		}

		.closeModal {
			border: 2rpx solid #E8101E;
			color: #E8101E;
		}

		.sureModal {
			background: #E8101E;
			border-radius: 40rpx;
			color: #FFFFFF;
		}
	}



	::v-deep .uv-modal__title {
		padding-top: 50rpx;
		font-weight: 500;
		font-size: 32rpx;
		color: #333333 !important;
		line-height: 45rpx;
		text-align: center;
		font-style: normal;
	}

	::v-deep .uv-modal__content {
		padding: 30rpx 32rpx 30rpx 30rpx;
		font-weight: 400;
		font-size: 30rpx;
		color: #333333;
		line-height: 40rpx;
		text-align: left;
		font-style: normal;
	}
</style>