<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="../../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="../../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="../../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="../../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="../../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="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="../../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="../../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">立即发布</button>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				navBarData: {
					title: '发布', //导航栏标题
					btn: 1 //是否显示返回按钮 0不显示  1 显示
				},
				// 标题
				activitytitle: '',
				// 内容
				activityintro: '',
				// 图片
				pic: '../../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,
				deadlineDate: currentDate,
				activeList: []
			};
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		methods: {


			submitForm() {
				if (
					this.activitytitle.trim() === '' ||
					this.activityintro.trim() === '' ||
					this.imageValue == '' ||
					this.classify == '请选择' ||
					this.recruitment.value === '请选择' ||
					this.activityform.trim() === '请选择' ||
					this.address.trim() === '请选择' ||
					this.funds === '请选择'
				) {
					// 弹出弹框或其他处理逻辑
					uni.showToast({
						title: '请填写完整的表单内容',
						icon: 'none'
					});
					return;
				}
				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,
					activitytitle: this.activitytitle,
					activityintro: this.activityintro,
					img: this.imageValue,
					classify:this.classify,
					recruitment: this.recruitment,
					activityform: this.activityform,
					address: this.address,
					funds: this.funds,
					activityDate: this.activityDate,
					deadlineDate: this.deadlineDate,
					// price: "188",
					// count: "6",
				};
				// return console.log(formData);


				this.$request('/insertActive', 'POST', formData).then(res => {
					console.log(res.data);
					if (res.code == 200) {
						uni.switchTab({
							url: "/pages/activityPage/activityPage"
						})
					}

				})

			},
			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:');
							}
						});
					}
				});
			
			},
		},
	}
</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;
			}
		}

	}
</style>