<template>
	<view class="feedback">
		<view class="hd" @click="getlianxi()">如需尽快解决问题，可直接联系在线客服<image src="https://zhengxin.njrenzhou.cn/static/jiantou2.png"></image>
		</view>
		<view class="bd">
			<view class="title"><text>*</text>反馈类型</view>
			<view class="tab">
				<view class="item" v-for="(a,i) in articleList" :key="i" :class="tabIndex == i?'item-on':''" @click="tabItem(i)">{{a.title}}</view>
			</view>
			<picker mode="selector" :range="childlist" :value="chilIndex" range-key="title" @change="bindPickerChange">
				<view class="picker">
					{{childlist[chilIndex].title}}<image src="https://zhengxin.njrenzhou.cn/static/jiantouxia.png"></image>
				</view>
			</picker>
			<view class="title"><text>*</text>反馈内容</view>
			<view class="textarea">
				<textarea class="box" maxlength="200" v-model="content" placeholder="请输入文字"></textarea>
				<view class="num">{{content.length}}/200字</view>
			</view>
			<view class="title"><text>*</text>图片上传<view class="sub">(上传聊天截图或与问题描述相关的图片)</view></view>
			<view class="pic">
				<view class="li" v-for="(a,i) in imgs" :key="i">
					<image class="img" mode="heightFix" :src="baseUrl+a"></image>
					<image class="del" mode="heightFix" @click="deletePic(i)" src="https://zhengxin.njrenzhou.cn/static/icon-del.png"></image>
				</view>
				<view class="upload" @click="uploadImg">
					<image src="https://zhengxin.njrenzhou.cn/static/paizhao.png"></image>
					<view class="txt">上传图片</view>
				</view>
			</view>
			<view class="title"><text>*</text>联系方式</view>
			<view class="content">
				<view class="item">
					<input type="text" v-model="email" placeholder="请填写您的姓名" />
				</view>
				<view class="item">
					<input type="phone" v-model="phone" placeholder="请填写您的电话" />
				</view>
			</view>
		</view>
		<view class="fd">
			<view class="btn" @click="toSubmit">提交反馈</view>
			<view class="jilu" @click="openPage()">反馈记录<image src="https://zhengxin.njrenzhou.cn/static/icon-arrow.png"></image></view>
		</view>
		<u-popup :show="show" :round="10" mode="center">
			<view class="wechat">
				<view class="title">联系客服</view>
				<view class="info">
					<view class="item" v-for="(a,i) in lianxiInfo" :key="i" v-if="i<=2">{{a.title}}：<text>{{a.number}}</text></view>
				</view>
				<view class="btn" @click="show = false">我知道了</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				lianxiInfo: [],
				show: false,
				articleList: [],
				childlist: [],
				tabIndex: 0,
				chilIndex: 0,
				content: "",
				imgs: [],
				email: "",
				phone: "",
				baseUrl: "",
				statusBarHeight: 0,
				bgColor: '#EBEDEC'
			}
		},
		mounted() {
			this.baseUrl = this.$baseUrl;
			this.systemfankuiGettype();
		},
		methods: {
			//联系客服
			async getlianxi() {
				const { code, data } = await this.$api.getlianxi({})
				if(code == 1){
					this.lianxiInfo = data
					this.show = true
				}
			},
			bindPickerChange(e){
				 this.chilIndex = e.detail.value
			},
			//获取反馈类型
			async systemfankuiGettype() {
				const { code, data } = await this.$api.systemfankuiGettype({})
				if(code == 1){
					if(data.length > 0){
						this.childlist = data[this.tabIndex].childlist
					}
					this.articleList = data
				}
			},
			async toSubmit() {
				const that = this;
				if (!this.content) {
					uni.showToast({
						title: '请输入反馈内容',
						icon: 'none',
					})
					return
				}
				if (this.imgs.length == 0) {
					uni.showToast({
						title: '请上传图片',
						icon: 'none',
					})
					return
				}
				if (!this.email) {
					uni.showToast({
						title: '请输入你的姓名',
						icon: 'none',
					})
					return
				}
				if (!this.phone) {
					uni.showToast({
						title: '请输入你的手机号',
						icon: 'none',
					})
					return
				}
				if(!/^1[3456789]\d{9}$/.test(this.phone)){
				  uni.showToast({
				  	title: '手机号格式不正确',
				  	icon: "none",
				  })
				  return ;
				}
				const { code, data, msg } = await this.$api.systemfankuiSetfankui({
					image: this.imgs.join(","),
					type: this.articleList[this.tabIndex].title,
					type_item: this.childlist[this.chilIndex].title,
					phone: this.phone,
					email: this.email,
					token: uni.getStorageSync("token"),
					content: this.content,
				});
				if(code == 1){
					uni.showToast({
						title: "提交成功"
					})
					setTimeout(()=>{
						that.content = "";
						that.imgs = [];
						that.phone = "";
						that.email = "";
						that.tabIndex = 0;
						that.chilIndex = 0;
					},2000)
				}else{
					uni.showToast({
						title: msg,
						icon: "none"
					})
				}
			},
			//上传图片
			uploadImg() {
				const that = this
				uni.chooseImage({
					count: 9-that.imgs.length,
					sizeType: ["original"], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ["album", "camera"],
					success: (res) => {
						console.log(res)
						for(var i = 0; i<res.tempFilePaths.length;i++){
							uni.uploadFile({
								name: 'file',
								url: this.$baseUrl+'/api/Common/upload',
								filePath: res.tempFilePaths[i],
								formData: {
									"token": uni.getStorageSync('token')
								},
								success: (upRes) => {
									const {code, data, msg } = JSON.parse(upRes.data);
									if(code == 1){
										uni.showToast({
											icon: "success",
											title: "上传成功"
										})
										that.imgs.push(data.url)
									}else{
										uni.showToast({
											icon: "error",
											title: "上传失败"
										})
									}
								},
								fail: (err) => {
									console.log("toUpload err",err)
									console.log(err)
								}
							});	
						}
					},
					fail(err){
						console.log("fail",err)
					}
				});
			},
			openPage(){
				uni.navigateTo({
					url: "/pages/user/feedList"
				})
			},
			tabItem(index){
				this.tabIndex = index;
				this.childlist = this.articleList[index].childlist
			},
			deletePic(index) {
				this.imgs.splice(index, 1)
			},
		},
	}
</script>

<style lang="scss" scoped>
.feedback {
	padding-bottom: 200rpx;
	overflow: hidden;
	.wechat{
		width: 620rpx;
		height: 510rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		text-align: center;
		.title{
			font-weight: 500;
			font-size: 36rpx;
			color: #333333;
			padding: 30rpx 0;
			border-bottom: 1px solid #EAEAEA;
		}
		.info{
			padding: 30rpx 50rpx;
			overflow: hidden;
			.item{
				text-align: left;
				line-height: 60rpx;
				font-weight: 500;
				font-size: 30rpx;
				color: #999999;
				text{
					color: #333333;
				}
			}
		}
		.btn{
			width: 300rpx;
			height: 90rpx;
			background: #FFFFFF;
			border-radius: 90rpx;
			border: 1px solid #00C6A9;
			text-align: center;
			font-weight: 500;
			font-size: 30rpx;
			color: #00C6A9;
			line-height: 90rpx;
			margin: 0 auto;
			margin-top: 20rpx;
		}
	}
	.fd{
		width: 100%;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 22;
		background-color: #FFFFFF;
		padding: 25rpx;
		box-sizing: border-box;
		.btn{
			width: 100%;
			height: 90rpx;
			line-height: 90rpx;
			background: #00C6A9;
			border-radius: 90rpx;
			text-align: center;
			font-weight: 400;
			font-size: 30rpx;
			color: #FFFFFF;
		}
		.jilu{
			display: flex;
			align-items: center;
			justify-content: center;
			font-weight: 400;
			font-size: 26rpx;
			color: #666666;
			margin-top: 10rpx;
			image{
				width: 30rpx;
				height: 30rpx;
			}
		}
	}
	.hd{
		width: 100%;
		line-height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 25rpx;
		box-sizing: border-box;
		background: #FBEBEB;
		font-weight: 400;
		font-size: 26rpx;
		color: #E53623;
		position: relative;
		overflow: hidden;
		.btns{
			width: 100%;
			height: 100%;
			position: absolute;
			left: 0;
			top: 0;
			z-index: 2;
			opacity: 0;
		}
		image{
			width: 14rpx;
			height: 24rpx;
		}
	}
	.bd{
		padding: 0 25rpx 15rpx;
		box-sizing: border-box;
		width: 100%;
		background: #FFFFFF;
		box-shadow: 0px 3rpx 10rpx 0px rgba(9,44,39,0.05);
		border-radius: 20rpx;
		margin-top: 20rpx;
		.title{
			padding: 30rpx 0;
			font-weight: 400;
			font-size: 30rpx;
			color: #333;
			display: flex;
			align-items: center;
			text{
				color: #E53623;
			}
			.sub{
				margin-left: 10rpx;
				font-size: 24rpx;
				color: #999999;
			}
		}
		.tab{
			display: flex;
			align-items: center;
			.item{
				padding: 0 40rpx;
				line-height: 61rpx;
				height: 61rpx;
				border-radius: 61rpx;
				border: 1px solid #CCCCCC;
				text-align: center;
				font-weight: 400;
				font-size: 28rpx;
				color: #333333;
				&-on{
					color: #FFFFFF;
					border: 1px solid #00C6A9;
					background: #00C6A9;
				}
				margin-right: 20rpx;
			}
		}
		.picker{
			width: 100%;
			height: 90rpx;
			background: #F6F6F6;
			border-radius: 10rpx;
			padding: 0 30rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-weight: 400;
			font-size: 28rpx;
			color: #333333;
			margin-top: 40rpx;
			box-sizing: border-box;
			image{
				width: 24rpx;
				height: 14rpx;
			}
		}
		.textarea{
			width: 100%;
			height: 360rpx;
			background: #F6F6F6;
			border-radius: 10rpx;
			padding: 25rpx;
			box-sizing: border-box;
			textarea{
				width: 100%;
				height: 250rpx;
				font-weight: 400;
				font-size: 28rpx;
				color: #333;
			}
			.num{
				text-align: right;
				font-weight: 400;
				font-size: 28rpx;
				color: #999999;
				line-height: 60rpx;
			}
		}
		.pic{
			overflow: hidden;
			padding-bottom: 30rpx;
			.li{
				width: 152rpx;
				height: 152rpx;
				border-radius: 10rpx;
				position: relative;
				overflow: hidden;
				margin-top: 20rpx;
				margin-right: 20rpx;
				float: left;
				.img{
					width: 100%;
					height: 100%;
				}
				.del{
					width: 45rpx;
					height: 45rpx;
					position: absolute;
					right: 0;
					top: 0;
					z-index: 2;
				}
			}
			.upload{
				width: 152rpx;
				height: 152rpx;
				border-radius: 10rpx;
				position: relative;
				overflow: hidden;
				border: 1px solid #DDDDDD;
				text-align: center;
				font-weight: 500;
				margin-top: 20rpx;
				font-size: 24rpx;
				color: #333333;
				float: left;
				image{
					width: 46rpx;
					height: 42rpx;
					margin-top: 35rpx;
					margin-bottom: 20rpx;
				}
			}
		}
		.content{
			.item{
				width: 100%;
				margin-bottom: 20rpx;
				overflow: hidden;
				input{
					height: 90rpx;
					line-height: 90rpx;
					background: #F6F6F6;
					border-radius: 10rpx;
					text-align: left;
					font-weight: 400;
					font-size: 28rpx;
					color: #333333;
					padding: 0 30rpx;
				}
			}
		}
	}
}
</style>
