<template>
	<view>
		<view class="image-container" @click="selectShow = true">
			<block v-if="$u.test.isEmpty(data.urls)">
				<view class="cuIcon-roundaddfill theme-color" style="font-size: 80upx;padding: 100upx 0 30upx 0;"></view>
				<view class="text-black text-bold text-sm">添加图片或视频</view>
				<view class="text-sm text-gray margin-top-xs">建议上传横排照片或者视频</view>
			</block>
			<block v-else>
				<block v-if="data.urlsType == 0">
					<image :src="data.urls" style="height: 400upx;" mode="aspectFit" @click.stop="preViewImg(data.urls)"></image>
				</block>
				<block v-else>
					<video :src="data.urls" style="height: 400upx;"></video>
				</block>
			</block>
		</view>
		<view class="image-container" @click="chooseAvatar" v-if="data.urlsType == 1">
			<block v-if="$u.test.isEmpty(data.cover)">
				<view class="cuIcon-roundaddfill theme-color" style="font-size: 80upx;padding: 100upx 0 30upx 0;"></view>
				<view class="text-black text-bold text-sm">请上传作品封面</view>
			</block>
			<block v-else>
				<image :src="data.cover" style="height: 400upx;" mode="aspectFit" @click.stop="preViewImg(data.cover)"></image>
			</block>
		</view>
		
		<u-action-sheet :list="list" @click="upload" v-model="selectShow"></u-action-sheet>
		<view class="basic-container">
			<view class="padding-sm">
				<text class="cuIcon-titles theme-color" style="font-size: 36upx;"></text>
				<text class="text-black text-bold text-lg">基本信息</text>
			</view>
			<view class="padding-sm flex justify-around align-center">
				<block v-for="(item, index) in classification" :key="index">
					<view class="tags" :class="current == index ? 'theme-bg-color text-white' : ''" @click="select(index,item)">{{item}}</view>
				</block>
			</view>
			<view class="padding-sm">
				<text class="text-black text-bold text-lg padding-left-sm">{{detail.personTitle}}</text>
				<view style="padding: 30upx 20upx;">
					<u-input v-model="data.personName" :placeholder="'请输入' + detail.personTitle" />
				</view>
				<view style="padding: 0 20upx 40upx 20upx;">
					<u-line color="#e3e3e3" />
				</view>
				<text class="text-black text-bold text-lg padding-left-sm">{{detail.productionTitle}}</text>
				<view style="padding: 30upx 20upx;">
					<u-input v-model="data.title" :placeholder="'请输入' + detail.productionTitle" />
				</view>
				<view style="padding: 0 20upx 40upx 20upx;">
					<u-line color="#e3e3e3" />
				</view>
				<text class="text-black text-bold text-lg padding-left-sm">{{detail.introductionTitle}}</text>
				<view style="padding: 30upx 20upx 0 20upx;">
					<u-input v-model="data.introduce" type="textarea" :clearable="false" height="140" :autoHeight="false" maxlength="60" :placeholder="'请输入' + detail.introductionTitle" />
				</view>
				<view class="flex justify-end padding-right-sm padding-bottom-sm text-gray">{{data.introduce.length}} / 60</view>
				<view style="padding: 0 20upx 40upx 20upx;">
					<u-line color="#e3e3e3" />
				</view>
				<view style="margin: 30upx 0 40upx 0;">
					<u-button class="custom-style" shape="circle" @click="navWithParam">确定提交</u-button>
				</view>
			</view>
		</view>
		<view class="homeBtn" @click="home">
			<image src="/static/home.png" style="width: 40upx;height: 40upx;" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				detail:{},
				selectShow: false,
				list: [{
					text: '上传图片'
				}, {
					text: '上传视频'
				}],
				current: 0,
				classification: [],
				data: {
					userId: '',
					urlsType: 0,
					urls: '',
					cover: '',
					title: '',
					personName: '',
					personType: '',
					introduce: '',
					activityId: '',
				}
			}
		},
		onLoad(options) {
			this.getActivityDetail(options.activityId);
			this.data.activityId = options.activityId;
			this.data.userId = uni.getStorageSync("userId");
		},
		created() {
			uni.$on('uAvatarCropper', path => {
				// 可以在此上传到服务端
				uni.uploadFile({
					url: `https://music.nanyue6688.com/ldt/api/putObject`, //仅为示例，非真实的接口地址
					filePath: path,
					name: 'file',
					success: (uploadFileRes) => {
						this.data.urls = JSON.parse(uploadFileRes.data).data;
						this.data.cover = JSON.parse(uploadFileRes.data).data;
					}
				});
			})
		},
		methods: {
			async upload(index) {
				console.log(index);
				let res;
				this.data.urlsType = index
				if (index == 0) {
					//上传图片
					// res = await this.$mpi.uploadFile()
					this.chooseAvatar();
				} else {
					//上传视频
					res = await this.$mpi.uploadVideo()
				}
				let obj = JSON.parse(res);
				this.data.urls = obj.data
			},
			async uploadCover() {
				let res = await this.$mpi.uploadFile()
				this.data.cover = JSON.parse(res).data;
			},
			home() {
				uni.navigateTo({
					url: "/pages/activityList/home/home?activityId=" + this.data.activityId,
				})
			},
			getActivityDetail(id) {
				this.$u.api.activity.detail({id: id}).then(res => {
					this.detail = res
					this.classification = res.classification.split(',');
					this.data.personType = this.classification[0];
				})
			},
			preViewImg(url){
				uni.previewImage({
					urls: [url]
				});
			},
			select(index, item) {
				this.current = index;
				this.data.personType = item;
			},
			chooseAvatar() {
				// 此为uView的跳转方法，详见"文档-JS"部分，也可以用uni的uni.navigateTo
				this.$u.route({
					// 关于此路径，请见下方"注意事项"
					url: '/uview-ui/components/u-avatar-cropper/u-avatar-cropper',
					// 内部已设置以下默认参数值，可不传这些参数
					params: {
						// 输出图片宽度，高等于宽，单位px
						destWidth: 300,
						// 裁剪框宽度，高等于宽，单位px
						rectWidth: 300,
						// 输出的图片类型，如果'png'类型发现裁剪的图片太大，改成"jpg"即可
						fileType: 'jpg',
					}
				})
			},
			navWithParam() {
				console.log(this.data);
				if (this.$u.test.isEmpty(this.data.userId)) {
					uni.showToast({
						icon: "none",
						title: "请登录后再操作"
					})
					return;
				}
				if (this.$u.test.isEmpty(this.data.urls)) {
					uni.showToast({
						icon: "none",
						title: "请上传作品"
					})
					return;
				}
				if (this.data.urlsType == 1 && this.$u.test.isEmpty(this.data.cover)) {
					uni.showToast({
						icon: "none",
						title: "请上传作品封面"
					})
					return;
				}
				if (this.$u.test.isEmpty(this.data.personName)) {
					uni.showToast({
						icon: "none",
						title: "请填写个人名称"
					})
					return;
				}
				if (this.$u.test.isEmpty(this.data.title)) {
					uni.showToast({
						icon: "none",
						title: "请填写" + this.detail.productionTitle
					})
					return;
				}
				if (this.$u.test.isEmpty(this.data.introduce)) {
					uni.showToast({
						icon: "none",
						title: "请填写个人介绍"
					})
					return;
				} else {
					this.$u.api.activity.signUp(this.data).then(res => {
						uni.showToast({
							icon: "none",
							title: "提交成功",
							duration: 3000,
							success: () => {
								uni.redirectTo({
									url: '/pages/activityList/mine/introduction?id=' + res.id
								})
							}
						})
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.image-container {
		text-align: center;
		background-color: #FFFFFF;
		height: 400upx;
		margin: 30upx;
		border-radius: 30upx;
	}
	.basic-container {
		background-color: #FFFFFF;
		margin: 20upx;
		border-radius: 30upx;
	}
	.tags {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 30%;
		height: 70upx;
		border: #4f4f4f 1px solid;
		border-radius: 28upx;
	}
	.custom-style {
		background-color: #5a3ee8;
		color: #FFFFFF;
		width: 400rpx;
	}
</style>
