<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="chooseAvatar()">
						<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>
		<u-popup v-model="show1" mode="top" :mask-close-able='false' :zIndex="5">
			<view class="permissions_box">
				当您使用APP时，为了提供图片上传的功能，我们需要申请相机/存储权限。
			</view>
		</u-popup>
	</view>
</template>

<script>
	import permision from "@/js_sdk/wa-permission/permission.js"
	export default {
		data() {
			return {
				show1: false,
				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, msg } = 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)
			},
			recorderAuthModal: function () {
				const that = this;
				uni.showModal({
					title: '温馨提示',
					content: '为了提供图片上传的功能，我们需要申请相机/存储权限，请您确认授权，否则无法使用该功能~',
					cancelText: '关闭',
					confirmText: '去授权',
					success(res) {
						that.show1=false
						if (res.confirm) {
							permision.gotoAppPermissionSetting()
						}
					}
				})
			},
			chooseAvatar() {
				const that = this;
				// APP检查权限
				if(uni.getSystemInfoSync().platform == 'android') {
					plus.android.checkPermission(
						'android.permission.WRITE_EXTERNAL_STORAGE',
						granted => {
						  if (granted.checkResult==-1){
							that.show1=true
						  }else{
							that.show1=false
							// 此为show1的跳转方法，详见"文档-JS"部分，也可以用uni的uni.navigateTo
							that.toNext();
						  }
						},
						error => {
						  console.error('Error checking permission:', error.message);
						}
					  );
					 that.requestAndroidPermission('android.permission.WRITE_EXTERNAL_STORAGE')
					return ;
				}
				that.toNext();
			},
			async requestAndroidPermission(permisionID) {
				const that = this;
				var result = await permision.requestAndroidPermission(permisionID);
				console.log("requestAndroidPermission",result)
				if(result != 1){
					that.recorderAuthModal();
				}else{
					that.show1=false
					// 此为uView的跳转方法，详见"文档-JS"部分，也可以用uni的uni.navigateTo
					that.toNext();
				}
			},
			//上传图片
			uploadImg() {
				const that = this
				uni.chooseImage({
					count: 9-that.imgurl.length,
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ["album", "camera"],
					success: (res) => {
						console.log(res)
						for(var i = 0; i<res.tempFilePaths.length;i++){
							// 可以在此上传到服务端
							uni.uploadFile({
								url: that.$baseUrl+'/prod-api/common/upload',
								filePath: res.tempFilePaths[i],
								name: 'file',
								complete: (res1) => {
									console.log(res1)
									if(res1.statusCode == 200){
										const {code, fileName, url, msg} = JSON.parse(res1.data);
										if(code == 200){
											uni.showToast({
												title: "上传成功"
											})
											that.imgurl.push(url)
											that.$forceUpdate();
										}else{
											uni.showToast({
												icon: "error",
												title: "上传失败"
											})
										}
									}else{
										uni.showToast({
											icon: "error",
											title: "上传失败"
										})
									}
									
								}
							});
							
						}
					},
					fail(err){
						console.log("fail",err)
					}
				});
			},
			toNext(){
				this.uploadImg();
				// 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>
