<template>
	<view class="publish">
		<statusBar :item="navBarData"></statusBar>

		<view class="" style="background-color: #ffffff;">


			<view class="pubTitle">
				<input class="title" style="" placeholder="活动标题" v-model="activitytitle" />
			</view>

			<view class="pubMid">
				<view class="mid">
					<view class="txt">
						<textarea class="txtClass" value="" placeholder="请输入内容" v-model="activityintro" />
					</view>
					<!-- <uni-file-picker :v-model="imageValue" fileMediatype="image" file-extname="png,jpg,jpeg"
						@select="select" @success="success" limit="1"> -->
					<!-- 上传图片 -->

					<view class="pic" @click="uploadPic">
						<view class="picCont">
							<image :src="pic" mode="aspectFill"></image>
							<view
								style="font-size: 24rpx;line-height: 40rpx;font-weight: 400;color: rgba(0, 0, 0, 0.6);">
								添加图片
							</view>
							<view
								style="font-size: 20rpx;line-height: 36rpx;font-weight: 400;color: rgba(0, 0, 0, 0.4);">
								（0/1）
							</view>
						</view>
						<view class="pic_show" v-if="imageValue!==''">
							<image :src="imageValue" mode="aspectFill"></image>
						</view>
					</view>
					<!-- <view class="pic_show">
						<image :src="imageValue" mode="aspectFill"></image>
					</view> -->

					<!-- </uni-file-picker> -->

					<!-- <beUpload @getFile="getFile" :navName="'上传文件'" :accept="accept" :progress="progress"></beUpload> -->


				</view>
			</view>


			<view class="userOptionsBox">
				<view class="optionsItemBox">
					<picker mode="selector" :range="classifyList" @change="handelclassify">
						<view class="optionItem">
							<view class="option_left">
								<text class="option_text">
									活动分类
								</text>
							</view>

							<view class="option_right">
								<view class="input_details">
									<view class="details">
										<view class="uni-input text">{{classify}}</view>
									</view>
									<view class="arrow">
										<image src="https://teacherapi.cocorobo.cn/teaching-file/static//mine/arrow-right.png"
											mode="aspectFill"></image>
									</view>
								</view>
							</view>
						</view>
					</picker>

				</view>
			</view>


			<view class="userOptionsBox">
				<view class="optionsItemBox">
					<picker mode="selector" :range="recruitmentList" @change="handelRecruitment">
						<view class="optionItem">
							<view class="option_left">
								<text class="option_text">
									招募人数
								</text>
							</view>

							<view class="option_right">
								<view class="input_details">
									<view class="details">
										<view class="uni-input text">{{recruitment}}</view>
									</view>
									<view class="arrow">
										<image src="https://teacherapi.cocorobo.cn/teaching-file/static//mine/arrow-right.png"
											mode="aspectFill"></image>
									</view>
								</view>
							</view>
						</view>
					</picker>

				</view>
			</view>
			<view class="userOptionsBox">
				<view class="optionsItemBox">
					<picker mode="selector" :range="activityformList" @change="handelActivityform">
						<view class="optionItem">
							<view class="option_left">
								<text class="option_text">
									活动形式
								</text>
							</view>

							<view class="option_right">
								<view class="input_details">
									<view class="details">
										<view class="uni-input text">{{activityform}}</view>
									</view>
									<view class="arrow">
										<image src="https://teacherapi.cocorobo.cn/teaching-file/static//mine/arrow-right.png"
											mode="aspectFill"></image>
									</view>
								</view>
							</view>
						</view>
					</picker>
				</view>
			</view>
			<view class="userOptionsBox">
				<view class="optionsItemBox">
					<picker mode="selector" :range="addressList" @change="handeladdress">
						<view class="optionItem">
							<view class="option_left">
								<text class="option_text">
									活动地址
								</text>
							</view>

							<view class="option_right">
								<view class="input_details">
									<view class="details">
										<view class="uni-input text">{{address}}</view>
									</view>
									<view class="arrow">
										<image src="https://teacherapi.cocorobo.cn/teaching-file/static//mine/arrow-right.png"
											mode="aspectFill"></image>
									</view>
								</view>
							</view>
						</view>
					</picker>

				</view>
			</view>
			<view class="userOptionsBox">
				<view class="optionsItemBox">
					<!-- <picker mode="date" :value="activityDate" :start="startDate" :end="endDate"
						@change="activityDateChange">
						<view class="optionItem">
							<view class="option_left">
								<text class="option_text">
									活动时间
								</text>
							</view>

							<view class="option_right">
								<view class="input_details">
									<view class="details">
										<view class="uni-input text">{{ activityDate }}</view>
									</view>
									<view class="arrow">
										<image src="https://teacherapi.cocorobo.cn/teaching-file/static//mine/arrow-right.png" mode="aspectFill"></image>
									</view>
								</view>
							</view>
						</view>
					</picker> -->
					<view class="optionItem" @click="show = true">
						<view class="option_left">
							<text class="option_text">
								活动时间
							</text>
						</view>
					
						<view class="option_right">
							<view class="input_details">
								<view class="details">
									<!-- <view class="uni-input text">{{ activityDate }}</view> -->
									<u-calendar :show="show" :mode="mode" @confirm="confirm" :closeOnClickOverlay="true" @close="show=false"></u-calendar>
									<view class="text">
										{{ activityDate }}
									</view>
								</view>
								<view class="arrow">
									<image src="https://teacherapi.cocorobo.cn/teaching-file/static//mine/arrow-right.png" mode="aspectFill"></image>
								</view>
							</view>
						</view>
					</view>
					
				</view>
			</view>
			<view class="userOptionsBox">
				<view class="optionsItemBox">
					<picker mode="selector" :range="fundsList" @change="handelFunds">
						<view class="optionItem">
							<view class="option_left">
								<text class="option_text">
									活动费用
								</text>
							</view>

							<view class="option_right">
								<view class="input_details">
									<view class="details">
										<view class="uni-input text">{{funds}}</view>
									</view>
									<view class="arrow">
										<image src="https://teacherapi.cocorobo.cn/teaching-file/static//mine/arrow-right.png"
											mode="aspectFill"></image>
									</view>
								</view>
							</view>
						</view>
					</picker>

				</view>
			</view>
			<view class="userOptionsBox">
				<view class="optionsItemBox">
					<picker mode="date" :value="deadlineDate" :start="startDate" :end="endDate"
						@change="deadlineDateChange">
						<view class="optionItem">
							<view class="option_left">
								<text class="option_text">
									报名截止
								</text>
							</view>

							<view class="option_right">
								<view class="input_details">
									<view class="details">
										<view class="uni-input text">{{ deadlineDate }}</view>
									</view>
									<view class="arrow">
										<image src="https://teacherapi.cocorobo.cn/teaching-file/static//mine/arrow-right.png"
											mode="aspectFill"></image>
									</view>
								</view>
							</view>
						</view>
					</picker>
				</view>
			</view>


		</view>

		<view class="" style="height: 10vh;width: 750rpx;position: relative;">
			<view class="publish_now">
				<button class="btn" @click="submitForm">{{ btntext }}</button>
			</view>
		</view>

		<!-- 弹窗 -->
		<view>
			<view class="mask" v-show="showPopup" @click="closePopup"></view>
			<view class="popup-container" v-show="showPopup">
				<view class="popup">
					<view class="header">
						<text>确认{{ poptext }}</text>
					</view>
					<view class="footer">
						<button class="cancel-btn" @click="closePopup">取消</button>
						<button class="confirm-btn" @click="conf">确认</button>
					</view>
				</view>
			</view>
		</view>

		<view>
			<view class="mask" v-show="showPopupConfirm" @click="closePopup"></view>
			<view class="popup-container" style="height: 175px;" v-show="showPopupConfirm">
				<view class="popup">
					<view class="header" style="margin-top: 15px;">
						<image src="https://teacherapi.cocorobo.cn/teaching-file/static//gou1.png" mode="aspectFill"></image>
						<text style="margin-bottom: 24px;">{{ poptext }}成功</text>
					</view>
					<view class="footer" style="padding-top: 0px;">
						<button class="confirm-btn_2" @click="closePopup">确认</button>
					</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				navBarData: {
					title: '发布', //导航栏标题
					btn: 1 //是否显示返回按钮 0不显示  1 显示
				},
				//时间选择器
				show: false,
				mode: 'range',
				//控制弹窗
				showPopup: false,
				showPopupConfirm: false,
				isFirstShow: true,
				//按钮文本
				btntext: '立即发布',
				// 弹窗文本
				poptext: '发布',
				//acId
				acId: '',
				// 标题
				activitytitle: '',
				// 内容
				activityintro: '',
				// 图片
				pic: 'https://teacherapi.cocorobo.cn/teaching-file/static//publish/photo.png',
				imageValue: '',

				// 分类
				classifyList: ['直播活动', '常规教研活动', '专题教研活动'],
				classify: '请选择',

				// 人数
				recruitmentList: Array.from({
					length: 100
				}, (_, index) => index + 1), //100人
				recruitment: '请选择',

				// 活动形式
				activityformList: ['户外活动', 'xxxx', 'xxxx'],
				activityform: '请选择',

				// 活动地址
				addressList: ['深圳xxxx大学', '深圳xxxx大学', '深圳xxxx大学'],
				address: '请选择',
				// 活动费用
				fundsList: Array.from({
					length: 30
				}, (_, index) => (index + 1) * 100).map(item => item),
				funds: '请选择',


				// activityDate: currentDate,
				activityDate: '请选择',
				deadlineDate: currentDate,
				activeList: []
			};
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		methods: {
			confirm(e) {
				console.log(e);
				console.log(e[0],e[e.length-1]);
				this.activityDate = e[0] + '~' + e[e.length-1]
				this.show = false
			},
			editPublish() {
				const routes = getCurrentPages()
				console.log(routes);
				if (routes.length == 3) {
					const acId = routes[2].options.acId
					const type = routes[2].options.ty
					if (acId) {
						this.navBarData.title = '修改发布'
						this.btntext = '修改发布'
						this.poptext = '修改'
						console.log(acId);
						console.log(type);
						let data = {
							acId: acId,
							ty: type
						}
						this.$request('/selectActivityID', "POST", data).then(res => {
							console.log(res[0]);
							// this.actItemList = res[0]
							const item = res[0][0]
							this.acId = item.acId
							this.activitytitle = item.acName
							this.activityintro = item.brief
							this.imageValue = item.pic
							this.classify = item.type
							this.recruitment = item.pers
							this.activityform = item.acshape
							this.address = item.address
							this.funds = item.cost
							this.activityDate = item.begin_at
							this.deadlineDate = item.endTime

							if (this.classify == 0) {
								this.classify = "直播活动"
							} else if (this.classify == 1) {
								this.classify = "常规教研活动"
							} else {
								this.classify = "专题教研活动"
							}
						})
					}
				}
			},
			submitForm() {
				//判断表单内容是否完整
				const conditions = [
				// 添加提示信息
				  { value: this.activitytitle.trim(), message: '请填写活动标题' },
				  { value: this.activityintro.trim(), message: '请填写活动内容' },
				  { value: this.classify, message: '请填写活动分类' },
				  { value: this.recruitment, message: '请填写招募人数' },
				  { value: this.address, message: '请填写活动地址' },
				  { value: this.activityDate, message: '请填写活动时间' },
				];
				for (const condition of conditions) {
				  if (!condition.value || condition.value === '请选择') {
				    uni.showToast({
				      title: condition.message,
				      icon: 'none'
				    });
				    return;
				  }
				}

				//判断没有填写的表单内容是否填写，如果没有替换
				if (
					this.imageValue == '' ||
					this.activityform.trim() === '请选择' ||
					this.funds === '请选择'
				) {
					this.imageValue = 'https://teacherapi.cocorobo.cn/teaching-file/static//zanwu_img.png'
					this.activityform = '暂无'
					this.funds = '暂无'
				}
				this.showPopup = true; //显示弹窗

			},
			closePopup() {
				// 取消操作
				this.showPopup = false;
				this.showPopupConfirm = false;
			},
			conf() {
				// 确认操作
				if (this.classify == "直播活动") {
					this.classify = 0
				} else if (this.classify == "常规教研活动") {
					this.classify = 1
				} else {
					this.classify = 2
				}
				// 将文本框内容和图片链接存储在一个对象中
				const formData = {
					// id: this.$store.state.user.openid,
					id: this.btntext === '修改发布' ? this.acId : this.$store.state.user.openid,
					activitytitle: this.activitytitle,
					activityintro: this.activityintro,
					img: this.imageValue,
					recruitment: this.recruitment,
					activityform: this.activityform,
					address: this.address,
					classify: this.classify,
					activityDate: this.activityDate,
					deadlineDate: this.deadlineDate,
					funds: this.funds
					// price: "188",
					// count: "6",
				};

				this.showPopup = false;
				this.showPopupConfirm = true
				// return console.log(formData);
				console.log(this.btntext);
				if (this.btntext === '修改发布') {
					// formData.acId = this.acId
					// console.log(formData);
					this.$request('/updateMyPublish', 'POST', formData).then(res => {
						console.log(res);
						setTimeout(() => {
							uni.navigateBack();
						}, 500)
					})
				} else {
					this.$request('/insertActive', 'POST', formData).then(res => {
						console.log(res.data);
						if (res.code == 200) {
							setTimeout(() => {
								uni.switchTab({
									url: "/pages/activityPage/activityPage"
								})
							}, 500)
						}
					})
				}

			},
			handelclassify(e) {
				this.classify = this.classifyList[e.detail.value]
				console.log(this.classify);
			},
			handelRecruitment(e) {
				this.recruitment = this.recruitmentList[e.detail.value]
				console.log(this.imageValue);
			},
			handelActivityform(e) {
				this.activityform = this.activityformList[e.detail.value]
			},
			handeladdress(e) {
				this.address = this.addressList[e.detail.value]
			},
			handelFunds(e) {
				this.funds = this.fundsList[e.detail.value]
			},
			// activityDateChange: function(e) {
			// 	this.activityDate = e.detail.value
			// },
			deadlineDateChange: function(e) {
				this.deadlineDate = e.detail.value
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			// 上传图片
			uploadPic() {
				uni.chooseImage({
					count: 1, // 图片数量
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择或者拍照
					success: (res) => {
						const tempFilePaths = res.tempFilePaths[0];
						console.log(this);
						let that = this
						const uploadTask = uni.uploadFile({
							url: 'http://139.159.246.165:7006/img', // post请求地址
							// url: 'https://cxcy.ssti.net.cn/api/UploadFirmFile', // post请求地址
							// filePath: tempFilePaths,
							filePath: tempFilePaths,
							name: 'file', // 待确认
							header: {
								// 不要写这个不然报错
								// 'Content-Type': 'multipart/form-data',
							},
							success: function(res) {
								// console.log(res.data);
								that.imageValue = res.data
								// let url = JSON.parse(res.data)
								// console.log(url.data);
							},
							fail: function(uploadFileFail) {
								console.log('Error:', uploadFileFail.data);
							},
							complete: () => {
								// 接口调用结束的回调函数（调用成功、失败都会执行）
								// console.log('Complete:');
							}
						});
					}
				});

			},
		},
		onShow() {
			if (this.isFirstShow) {
				this.editPublish()
				this.isFirstShow = false
			}
		}
	}
</script>

<style lang="scss" scoped>
	.publish {
		.liu {
			//留白
			width: 750rpx;
			height: 68rpx;
			position: absolute;
			bottom: 0;
			background-color: #ffffff;
		}

		.pubTitle {
			width: 750rpx;
			height: 96rpx;
			padding: 24rpx 30rpx 0 30rpx;

			.title {
				font-size: 32rpx;
				line-height: 48rpx;
				font-weight: 500;
			}
		}

		.pubMid {
			width: 750rpx;
			// height: 504rpx;
			padding: 24rpx 30rpx 0 30rpx;

			// background-color: #8BBEFF;
			.mid {
				width: 690rpx;
				height: 440rpx;

				// background-color: #f2f2f2;
				.txt {
					width: 690rpx;
					height: 192rpx;

					.txtClass {
						width: 100%;
						height: 100%;
						font-size: 28rpx;
						font-weight: 400;
						line-height: 48rpx;
					}
				}

				.pic {
					width: 224rpx;
					height: 224rpx;
					background-color: rgba(242, 242, 242, 1);
					display: flex;
					position: relative;
					justify-content: center;
					align-items: center;

					.pic_show {
						position: absolute;
						top: 0;
						left: 0;
						width: 100%;
						height: 100%;
						z-index: 10;
						background-color: #8BBEFF;

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

					.picCont {
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;

						image {
							width: 56rpx;
							height: 66rpx;
						}
					}
				}
			}
		}


		// background-color: #fff;
		height: 100vh;


		.textCont {
			padding: 16px;
		}

		.addPhoto {
			background-color: #f2f2f2;
			width: 224rpx;
			height: 224rpx;
			display: flex;
			color: #616161;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			margin-left: 30rpx;

			image {
				width: 50px;
				height: 50px;
				// margin-left: 48px;
			}

			.photo {
				color: #000000A3;
				// padding-left: 44px;
				// font-size: 14px;
			}

			.num {
				color: #919191;
				// padding-left: 56px;
				// font-size: 14px;
			}

		}

		.userOptionsBox {

			width: 750rpx;
			height: 96rpx;
			display: flex;
			justify-content: center;
			align-items: center;

			.optionsItemBox {
				width: 690rpx;

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

					.option_left {
						display: flex;
						align-items: center;
						font-size: 28rpx;
						line-height: 48rpx;
						font-weight: 400;
					}

					.option_right {
						height: 40rpx;

						.input_details {
							display: flex;
							align-items: center;

							.details {
								display: flex;
								flex-direction: row;

								.text {
									font-size: 28rpx;
									color: rgb(153, 153, 153);
								}
							}

							.arrow {
								display: flex;

								image {
									width: 32rpx;
									height: 32rpx;
								}
							}
						}

					}
				}
			}
		}

		.publish_now {
			position: absolute;
			// bottom: 100rpx;
			display: flex;
			width: 750rpx;
			justify-content: center;
			padding-bottom: 50rpx;

			.btn {
				width: 432rpx;
				height: 88rpx;
				line-height: 88rpx;
				// background-color: #8BBEFF;
				background: rgba(0, 86, 168, 1);

				color: #fff;
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-top: 20px;
			}
		}

		.mask {
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			background-color: rgba(0, 0, 0, 0.3);
			z-index: 999;
			// display: none;
		}

		.popup-container {
			position: fixed;
			top: 50%;
			left: 50%;
			transform: translate(-50%, -50%);
			width: 520rpx;
			height: 260rpx;
			z-index: 1000;
			// display: none;

			.popup {
				width: 100%;
				height: 100%;
				display: flex;
				flex-direction: column;
				background-color: #fff;
				border-radius: 16rpx;

				.header {
					flex: 1;
					display: flex;
					justify-content: center;
					align-items: center;
					flex-direction: column;
					margin-top: 24px;
					font-size: 34rpx;
					font-weight: 600;

					image {
						width: 150rpx;
						height: 150rpx;
						margin-top: 15px;
						margin-bottom: 15px;
					}
				}

				.footer {
					flex: 1;
					display: flex;
					justify-content: space-around;
					align-items: end;
					padding-top: 17px;

					.cancel-btn {
						width: 259rpx;
						height: 100rpx;
						background-color: #F7F7F7;
						color: #000;
						font-size: 34rpx;
						text-align: center;
						border-radius: 0;
						border-bottom-left-radius: 16rpx;
						line-height: 50px;
					}

					.confirm-btn {
						width: 259rpx;
						height: 100rpx;
						background-color: #0081FE;
						color: #fff;
						font-size: 34rpx;
						text-align: center;
						border-radius: 0;
						border-bottom-right-radius: 16rpx;
						line-height: 50px;
					}

					.confirm-btn_2 {
						width: 518rpx;
						height: 100rpx;
						background-color: #F7F7F7;
						color: #000;
						font-size: 34rpx;
						text-align: center;
						border-radius: 0;
						border-bottom-left-radius: 16rpx;
						border-bottom-right-radius: 16rpx;
						line-height: 50px;
					}
				}
			}
		}
	}
</style>