<template>
	<view class="page">
		<u-form :model="form" ref="uForm" label-position="left" :label-width="150">
			<view class="add-list" style="padding-bottom: 0;">
				<u-form-item label="封面图片" borderBottom labelPosition="top">
					<u-upload :action="$upload"  :auto-upload="true"  ref="uUpload" :max-count="1" ></u-upload>
				</u-form-item>
			</view>
			<view class="add-list" style="padding-bottom: 0;">
				<u-form-item label="活动名称" borderBottom>
					<u-input v-model="form.activityName"  />
				</u-form-item>
				<u-form-item label="开始时间" borderBottom>
					<u-input v-model="form.startTime" type="select" @click="startShow = true" placeholder="请选择"/>
					<u-picker mode="time" v-model="startShow" :params="params" @confirm="startTimeChange"></u-picker>
				</u-form-item>
				<u-form-item label="结束时间" borderBottom>
					<u-input v-model="form.endTime" type="select" @click="endShow = true" placeholder="请选择"/>
					<u-picker mode="time" v-model="endShow" :params="params" @confirm="endTimeChange"></u-picker>
				</u-form-item>
				<u-form-item label="活动类型" borderBottom>
					<u-input v-model="form.activityType" type="select" @click="typeShow = true" placeholder="请选择"/>
					<u-select v-model="typeShow" :list="typeList" @confirm="typeChange"></u-select>
				</u-form-item>
				<u-form-item label="可参与人数" borderBottom>
					<u-number-box v-model="form.maxParticipants" :input-width="'100%'" :min="0"></u-number-box>
				</u-form-item>
				<u-form-item label="举办区域" borderBottom>
					<u-input v-model="form.region" />
				</u-form-item>
				<u-form-item label="活动内容"  type = 'textarea'  borderBottom auto-height="100">
					<u-input v-model="form.content" />
				</u-form-item>
			</view>
			
		</u-form>
		<u-toast ref="uToast" />
		<!-- 保存按钮 -->
		<view style="height: 120rpx;"></view>
		<view class="page-footer">
			<u-button type="success"  @click="submit">提交</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				fileList: [],
				form: {
					activityName: '',
					startTime:'',
					endTime:'',
					activityType:'',
					maxParticipants:'',
					region:'',
					content:'',
					auditStatus:'1',
					createUser:'',
					createDate:'',
					coverImage:'',
					num: 0,
				},
				active: '', // 文件上传地址
				startShow: false,
				endShow: false,
				params: {
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: true
				},
				typeShow: false,
				typeList: [{ value: '1', label: '类型1' }, { value: '2', label: '类型2' }],
				readOnly: false,
				formats: {}
			};
		},
		methods:{
			startTimeChange(val) {
				this.form.startTime = this.timeChange(val);
				this.form.endTime = '';
			},
			endTimeChange(val) {
				const time = this.timeChange(val);
				if(new Date(time).getTime() > new Date(this.form.startTime).getTime()) {
					this.form.endTime = time
				} else {
					this.$refs.uToast.show({
						title: '结束时间不能小于开始时间',
						type: 'error',
					})
				}
			},
			timeChange(val) {
				const { year, month, day, hour, minute, second } = val;
				return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
			},
			typeChange(val) {
				this.form.activityType = val[0].label;
			},
			submit() {
				
							let files = []
							// 通过filter，筛选出上传进度为100的文件(因为某些上传失败的文件，进度值不为100，这个是可选的操作)
							files = this.$refs.uUpload.lists.filter(val => {
								return val.progress == 100;
							})
							// 如果您不需要进行太多的处理，直接如下即可
							// files = this.$refs.uUpload.lists;
							files.map(item=>{
								let fileList ={
									name:item.file.name,
									url:item.response.data.url
								}
								this.fileList.push(fileList)
								
							})
							if(this.fileList.length!=0){
								this.form.coverImage  = this.fileList[0].url
							}
							 this.form.createUser =  uni.getStorageSync('shopId')
							this.form.createDate = this.currentTime()
							
							this.$http.sendRequest('/cereActivityApplication/add', 'POST',this.form,1).then(res => {
							     uni.navigateTo({
							     	url: '/pages/mycreated/mycreated'
							     })
							}).catch(err => {
								console.log(err)
								//请求失败
							})
							
						},
						// 获取时间
						currentTime() {
						    let date = new Date();
						    let year = date.getFullYear();
						    let month = date.getMonth() + 1; // 月份从0~11，所以加一
						    let day = date.getDate();
						    let hours = date.getHours();
						    let minutes = date.getMinutes();
						    let seconds = date.getSeconds();
						
						    // 为月、日、时、分、秒添加前导零（如果需要）
						    month = month < 10 ? '0' + month : month;
						    day = day < 10 ? '0' + day : day;
						    hours = hours < 10 ? '0' + hours : hours;
						    minutes = minutes < 10 ? '0' + minutes : minutes;
						    seconds = seconds < 10 ? '0' + seconds : seconds;
						
						    // 拼接日期和时间字符串
						    let strDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
						    return strDate;
						},
		}
	}
</script>

<style scoped lang="scss">
	@import 'activityAdd.scss';
</style>
