<template>
	<view class="add">
		<view class="add-hd" @click="getConfigData()">如需尽快解决问题，可直接联系在线客服<image src="@/static/jiantou1.png"></image></view>
		<view class="add-content">
			<!-- <view class="content">
				<view class="title"><text class="red">*</text>反馈类型</view>
				<view class="tab">
					<view class="li" :class="tabIndex == 1?'li-on':''" @click="tabItem(1)">界面</view>
					<view class="li" :class="tabIndex == 2?'li-on':''" @click="tabItem(2)">服务问题</view>
				</view>
				<view class="info">
					<picker mode="selector">
						<view class="picker picker-on">请选择</view>
					</picker>
				</view>
			</view> -->
			<view class="content">
				<view class="title"><text class="red">*</text>反馈内容</view>
				<view class="box">
					<textarea v-model="content" maxlength="200" placeholder="如有必要，请详细描述(200字以内)~"></textarea>
					<view class="tips">{{content.length}}/200字</view>
				</view>
			</view>
			<view class="content">
				<view class="title"><text class="red">*</text>图片上传<text class="hs">(上传聊天截图或与问题描述相关的图片)</text></view>
				<view class="list">
					<view class="item" v-for="(a,i) in imgurl" :key="i">
						<image class="pic" :src="a"></image>
						<image class="del" @click="toDel(i)" src="@/static/fabu-cha.png"></image>
					</view>
					<view class="upload" @click="chooseAvatar()">
						<image class="pic" src="@/static/fankui-tianjia.png"></image>
						<view class="txt">上传图片</view>
					</view>
				</view>
			</view>
			<view class="content">
				<view class="title"><text class="red">*</text>联系方式</view>
				<view class="list">
					<view class="dd">
						<input v-model="name" type="text" placeholder="请填写您的姓名"/>
					</view>
					<view class="dd">
						<input v-model="phone" type="text" placeholder="请填写您的电话"/>
					</view>
				</view>
			</view>
		</view>
		<view class="add-fd">
			<view class="btn" @click="toReg()">提交反馈</view>
			<view class="history" @click="toPage()">反馈记录 ></view>
		</view>
		<u-popup v-model="show" mode="center" border-radius="20" :mask-close-able="true">
			<view class="feed">
				<image src="@/static/feed-01.png"></image>
				<view class="title">反馈提交成功</view>
				<view class="btn" @click="show = false">我知道了</view>
			</view>
		</u-popup>
		<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 {
				show: false,
				show1: false,
				imgurl: [],
				content: "",
				tabIndex: 1,
				name: "",
				phone: ""
			};
		},
		methods: {
			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
							console.log(111)
						  }else{
							console.log(22)
							that.show1=false
							// 此为uView的跳转方法，详见"文档-JS"部分，也可以用uni的uni.navigateTo
							// that.uploadImg();
						  }
						},
						error => {
						  console.error('Error checking permission:', error.message);
						}
					  );
					 that.requestAndroidPermission('android.permission.WRITE_EXTERNAL_STORAGE')
					return ;
				}
				that.uploadImg();
			},
			async requestAndroidPermission(permisionID) {
				const that = this;
				var result = await permision.requestAndroidPermission(permisionID);
				if(result != 1){
					that.recorderAuthModal();
				}else{
					that.show1=false
					// 此为uView的跳转方法，详见"文档-JS"部分，也可以用uni的uni.navigateTo
					that.uploadImg();
				}
			},
			toPage() {
				uni.navigateTo({
					url: "/pages/users/feedback/list"
				})
			},
			//字典查询
			async getConfigData() {
				const that = this;
				let res1 = await that.$api.getConfigData({dictType: 'CONFIG_CUSTOMER_SERVICE_USERID'});
				if(res1.data.content){
					that.toChat(res1.data.content)
				}
			},
			//去聊天
			async toChat(sellerId) {
				const { code, data, msg } = await this.$api.findZegoToken({
					userId: uni.getStorageSync("userInfo").id,
					sellerId
				})
				if(code == 200){
					uni.setStorageSync("chatToken",data.token);
					uni.navigateTo({
						url: "/pages/users/chat/index?id="+sellerId+"&roomId="+data.roomId
					})
				}else{
					uni.showToast({
						icon: "none",
						position: "bottom",
						title: msg
					})
				}
			},
			//上传图片
			uploadImg() {
				const that = this
				uni.chooseImage({
					count: 9-that.imgurl.length,
					sizeType: ["original"], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ["album", "camera"],
					success: (res1) => {
						console.log(res1)
						for(var i = 0; i<res1.tempFilePaths.length;i++){
							uni.uploadFile({
								name: 'file',
								url: that.$baseUrl+'/prod-api/common/upload',
								filePath: res1.tempFilePaths[i],
								success: (res) => {
									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: "上传失败"
										})
									}
								},
								fail: (err) => {
									console.log(err)
								}
							});	
						}
					},
					fail(err){
						console.log("fail",err)
					}
				});
			},
			async toReg() {
				const that = this;
				if(!that.content){
					uni.showToast({
						icon: "none",
						position: "bottom",
						title: "反馈内容不能为空！"
					})
					return ;
				}
				if(that.imgurl.length == 0){
					uni.showToast({
						icon: "none",
						position: "bottom",
						title: "请上传图片"
					})
					return ;
				}
				if(!that.name){
					uni.showToast({
						icon: "none",
						position: "bottom",
						title: "姓名不能为空！"
					})
					return ;
				}
				if(!that.phone){
					uni.showToast({
						icon: "none",
						position: "bottom",
						title: "电话不能为空！"
					})
					return ;
				}
				const { code, data, msg } = await that.$api.feedback({
					userId: uni.getStorageSync("userInfo").id,
					content: that.content,
					imgurl: that.imgurl.join(","),
					name: that.name,
					phone: that.phone
				})
				if(code == 200){
					that.imgurl = [];
					that.content = "";
					that.name = "";
					that.phone = "";
					that.show = true;
				}else{
					uni.showToast({
						icon: "none",
						position: "bottom",
						title: msg
					})
				}
			},
			tabItem(index) {
				this.tabIndex = index;
			},
			toDel(i) {
				this.imgurl.splice(i,1)
			},
		}
	}
</script>

<style scoped lang="scss">
.add{
	overflow: hidden;
	padding-bottom: 200rpx;
	&-hd{
		padding: 0 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		line-height: 80rpx;
		background: #FBEBEB;
		font-weight: 400;
		font-size: 26rpx;
		color: #E53623;
		image{
			width: 45rpx;
			height: 45rpx;
		}
	}
	.feed{
		width: 670rpx;
		height: 688rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		text-align: center;
		padding-top: 70rpx;
		box-sizing: border-box;
		image{
			width: 408rpx;
			height: 315rpx;
		}
		.title{
			font-weight: 500;
			font-size: 36rpx;
			color: #222222;
			margin-top: 50rpx;
		}
		.btn{
			width: 400rpx;
			line-height: 90rpx;
			background: linear-gradient(0deg, #000000, #3D3B38);
			box-shadow: 0px 4rpx 18rpx 0px rgba(42,41,39,0.34);
			border-radius: 90rpx;
			font-weight: 500;
			font-size: 30rpx;
			color: #FFFFFF;
			margin: 0 auto;
			margin-top: 50rpx;
		}
	}
	&-content{
		padding: 30rpx;
		overflow: hidden;
		background: #FFFFFF;
		border-radius: 20rpx;
		width: 700rpx;
		margin: 0 auto;
		margin-top: 20rpx;
		box-sizing: border-box;
		.content{
			box-sizing: border-box;
			overflow: hidden;
			.box{
				width: 100%;
				background: #F6F6F6;
				border-radius: 10rpx;
				padding: 30rpx;
				box-sizing: border-box;
				margin: 30rpx auto;
				textarea{
					width: 100%;
					height: 300rpx;
					font-weight: 400;
					font-size: 28rpx;
					color: #212121;
				}
				.tips{
					font-weight: 400;
					font-size: 28rpx;
					color: #999999;
					margin-top: 20rpx;
					text-align: right;
				}
			}
			.picker{
				height: 90rpx;
				line-height: 90rpx;
				background: #F6F6F6;
				border-radius: 10rpx;
				padding: 0 30rpx;
				box-sizing: border-box;
				font-size: 28rpx;
				color: #333333;
				margin: 30rpx auto;
			}
			.tab{
				display: flex;
				align-items: center;
				justify-content: flex-start;
				margin-top: 20rpx;
				.li{
					padding: 0 40rpx;
					font-weight: 500;
					font-size: 30rpx;
					color: #333333;
					line-height: 61rpx;
					background: #FFFFFF;
					border-radius: 61rpx;
					border: 1px solid #CCCCCC;
					text-align: center;
					margin-right: 20rpx;
					&-on{
						background: linear-gradient(0deg, #000000, #3D3B38);
						border: 1px solid #2261CD;
						color: #FFFFFF;
					}
				}
			}
		}
		.title{
			.red{
				color: #E53623;
			}
			.hs{
				font-size: 24rpx;
				color: #999999;
			}
			font-weight: 400;
			font-size: 30rpx;
			color: #212121;
		}
		.list{
			width: 110%;
			margin-bottom: 30rpx;
			overflow: hidden;
			.dd{
				width: 100%;
				padding: 0 20rpx;
				box-sizing: border-box;
				background: #F6F6F6;
				border-radius: 10rpx;
				margin-top: 20rpx;
				input{
					width: 100%;
					height: 90rpx;
					line-height: 90rpx;
					font-weight: 400;
					font-size: 28rpx;
					color: #212121;
				}
			}
			.upload{
				width: 150rpx;
				height: 150rpx;
				background: #FFFFFF;
				border-radius: 10rpx;
				border: 1px solid #DDDDDD;
				float: left;
				font-weight: 400;
				text-align: center;
				font-size: 24rpx;
				color: #999999;
				margin-top: 30rpx;
				image{
					width: 50rpx;
					height: 50rpx;
					display: block;
					margin: 0 auto;
					margin-top: 25rpx;
					margin-bottom: 10rpx;
				}
			}
			.item{
				width: 150rpx;
				height: 150rpx;
				border-radius: 10rpx;
				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{
		width: 100%;
		padding: 20rpx 25rpx;
		box-sizing: border-box;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 22;
		background-color: #FFFFFF;
		.btn{
			width: 100%;
			line-height: 90rpx;
			background: linear-gradient(0deg, #000000, #3D3B38);
			box-shadow: 0px 4rpx 18rpx 0px rgba(42,41,39,0.34);
			border-radius: 90rpx;
			text-align: center;
			font-weight: 400;
			font-size: 30rpx;
			color: #FFFFFF;
			letter-spacing: 2rpx;
		}
		.history{
			text-align: center;
			font-weight: 400;
			font-size: 26rpx;
			color: #666666;
			margin-top: 20rpx;
		}
	}
}
</style>