<template>
	<view class="add">
		<view class="add-content">
			<view class="content">
				<textarea v-model="content" maxlength="-1" placeholder="请输入您想发布的内容哦~"></textarea>
			</view>
			<view class="content">
				<view class="title">上传图片</view>
				<view class="list">
					<view class="item" v-for="(a,index) in imgurl" :key="index">
						<image class="pic" :src="a"></image>
						<image class="del" @click="toDel(index)" src="@/static/fabu-cha.png"></image>
					</view>
					<view class="upload" @click="toNext()">
						<image class="pic" src="@/static/fabu-tianjia.png"></image>
						<view class="txt">上传</view>
					</view>
				</view>
			</view>
		</view>
		<view class="add-fd">
			<view class="btn" @click="toReg()">发布</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				content: "",
				imgurl: []
			};
		},
		onReady() {
			// 监听从裁剪页发布的事件，获得裁剪结果
			uni.$on('uAvatarCropper', path => {
				const that = this;
				// 可以在此上传到服务端
				uni.uploadFile({
					url: that.$baseUrl+'/prod-api/common/upload',
					filePath: path,
					name: 'file',
					complete: (res) => {
						if(res.statusCode == 200){
							const {code, fileName, url, msg} = JSON.parse(res.data);
							if(code == 200){
								uni.showToast({
									title: "上传成功"
								})
								that.imgurl.push(url)
							}else{
								uni.showToast({
									icon: "error",
									title: "上传失败"
								})
							}
						}else{
							uni.showToast({
								icon: "error",
								title: "上传失败"
							})
						}
						
					}
				});
			})
		},
		methods: {
			async toReg() {
				const { code, data } = await this.$api.publishArticle({
					userId: uni.getStorageSync("userInfo").id,
					content: this.content,
					imgurl: this.imgurl.join(","),
				})
				if(code == 200){
					uni.showToast({
						title:"发布成功"
					})
					uni.$emit("toAddDongtai")
					setTimeout(()=>{
						uni.navigateBack({
							delta: 1
						})
					},2000)
				}else{
					uni.showToast({
						icon: "none",
						position: "bottom",
						title: msg
					})
				}
			},
			toDel(index) {
				this.imgurl.splice(index,1)
			},
			toNext(){
				this.$u.route({
					// 关于此路径，请见下方"注意事项"
					url: '/uni_modules/vk-uview-ui/components/u-avatar-cropper/u-avatar-cropper',
					// 内部已设置以下默认参数值，可不传这些参数
					params: {
						// 输出图片宽度，高等于宽，单位px
						destWidth: 900,
						// 裁剪框宽度，高等于宽，单位px
						rectWidth: 300,
						// 输出的图片类型，如果'png'类型发现裁剪的图片太大，改成"jpg"即可
						fileType: 'jpg',
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
.add{
	overflow: hidden;
	padding-bottom: 110rpx;
	&-content{
		padding: 0 25rpx;
		overflow: hidden;
		.content{
			background: #FFFFFF;
			border-radius: 20rpx;
			padding: 30rpx;
			box-sizing: border-box;
			margin-top: 20rpx;
			overflow: hidden;
			textarea{
				width: 100%;
				height: 320rpx;
				font-weight: 400;
				font-size: 30rpx;
				color: #212121;
			}
		}
		.title{
			font-size: 30rpx;
			color: #333333;
		}
		.list{
			width: 110%;
			.upload{
				width: 206rpx;
				height: 206rpx;
				background: #F8F8F8;
				border-radius: 5rpx;
				float: left;
				font-weight: 400;
				text-align: center;
				font-size: 24rpx;
				color: #333333;
				margin-top: 30rpx;
				image{
					width: 50rpx;
					height: 50rpx;
					display: block;
					margin: 0 auto;
					margin-top: 55rpx;
					margin-bottom: 10rpx;
				}
			}
			.item{
				width: 206rpx;
				height: 206rpx;
				border-radius: 5rpx;
				float: left;
				margin-top: 30rpx;
				overflow: hidden;
				position: relative;
				margin-right: 12rpx;
				.pic{
					width: 100%;
					height: 100%;
				}
				.del{
					width: 35rpx;
					height: 35rpx;
					position: absolute;
					right: 0;
					top: 0;
					z-index: 2;
				}
			}
		}
	}
	&-fd{
		background-color: #F3F4F5;
		width: 100%;
		padding: 30rpx 25rpx;
		box-sizing: border-box;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 22;
		.btn{
			width: 100%;
			line-height: 98rpx;
			background: linear-gradient(0deg, #000000, #3D3B38);
			box-shadow: 0px 4rpx 18rpx 0px rgba(42,41,39,0.34);
			border-radius: 98rpx;
			text-align: center;
			font-weight: 400;
			font-size: 30rpx;
			color: #FFFFFF;
			letter-spacing: 5rpx;
		}
	}
}
</style>
