<template>
	<view class="page">
		<u-navbar title="意见与反馈" :border-bottom="false" :background="bgc" title-color='#000' title-size='36'
			height='45'></u-navbar>
		<view class="cardbox">
			<view class="tip">
				反馈类型<view class="ipnt">*</view>
			</view>
			<view class="checkbox">
				<view class="check_li" v-for="(item, index) in list" :key="index" @click="toggleCheckbox(index,item)"
					:class="{'act1': selectedIndex === index}">
					<view class="check_li_text">{{ item.dictLabel }}</view>
					<image src="https://lxnapi.ccttiot.com/bike/img/static/u2jYgqIRlFcHjt20uA90" mode=""
						v-if="selectedIndex === index"></image>
				</view>
			</view>
		</view>
		<view class="cardbox">
			<view class="tip">
				问题描述<view class="ipnt">*</view>
			</view>
			<view class="input-container">
				<view class="placeholder" v-if="!textValue">请详细描述您的问题或建议</view>
				<textarea class="custom-textarea" v-model="textValue" style="border: none;"
					@input="updateWordCount"></textarea>
				<text class="word-count">{{ currentCount }}/500</text>
			</view>
		</view>
		<view class="cardbox">
			<view class="tip">
				上传图片
			</view>
			<view class="icon">
				<view class="imgbox" v-for="(item, index) in imglist" :key="index">
					<image :src="item" mode="aspectFill"></image>
					<view class="delete-btn" @click.stop="deleteImage(index)">
						
						<image src="https://lxnapi.ccttiot.com/smartmeter/img/static/ui63BrTf8rlk4VAGx8Tf" mode="aspectFit"></image>
					</view>
				</view>
				<view class="imgbox add-btn" @click="btn" v-if="imglist.length < 9">
					<image src="https://api.ccttiot.com/smartmeter/img/static/uY8CPw9YE6JxPzcHUaqf" mode=""></image>
				</view>
			</view>
		</view>
		<view class="cardbox">
			<view class="tip">
				联系方式<view class="ipnt">*</view>
			</view>
			<view class="input-box">
				<input type="text"  placeholder="请留下手机号/邮箱/微信号，以便我们回复您" v-model="contactInfo"/>
			</view>
		</view>
		<view class="btn" @click="sub()">
			提交
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			bgc: {
				backgroundColor: " ",
			},
			currentCount: 0,
			textValue: '',
			imglist: [],
			token: '',
			sn: '',
			list: [],
			selectedIndex: null,
			upurl: '', // 上传地址前缀
			userImgs: '' ,// 临时存储上传后的图片地址
			type:null,
			contactInfo:''
		}
	},
	onShow() {
		this.getQiniuToken()
	},
	onLoad(e) {
		if (e.sn) {
			this.sn = e.sn
		}
		this.getlist()
	},

	methods: {
		toggleCheckbox(index,item) {
			this.selectedIndex = index;
			this.type=item.dictSort
		},
		
		// 删除图片
		deleteImage(index) {
			uni.showModal({
				title: '提示',
				content: '确定要删除这张图片吗？',
				success: (res) => {
					if (res.confirm) {
						this.imglist.splice(index, 1)
					}
				}
			})
		},

		sub() {
			if (this.selectedIndex === null) {
				uni.showToast({
					title: '请选择反馈类型',
					icon: 'none',
					duration: 2000
				});
				return;
			}
			if (this.contactInfo =='') {
				uni.showToast({
					title: '请输入联系方式',
					icon: 'none',
					duration: 2000
				});
				return;
			}
			if (!this.textValue.trim()) {
				uni.showToast({
					title: '请填写问题描述',
					icon: 'none',
					duration: 2000
				});
				return;
			}

			let data = {
				
				issueDescription: this.textValue,
				uploadedImage: this.imglist.join(','), // 将图片数组转换为逗号分隔的字符串
				type: this.type,
				contactInfo:this.contactInfo
			}
			
			this.$u.post("/appVerify/feedback", data).then((res) => {
				if (res.code == 200) {
					uni.showToast({
						title: '提交成功',
						icon: 'success',
						duration: 1000
					});
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						});
					}, 1100)
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 2000
					});
				}
			});
		},

		getQiniuToken() {
			this.$u.get("/common/qiniu/uploadInfo").then((res) => {
				if (res.code == 200) {
					this.token = res.token
					this.upurl = res.domain
				}
			});
		},

		getlist() {
			this.$u.get("/appVerify/getDictData?dictType=as_feedback_type").then((res) => {
				if (res.code == 200) {
					this.list = res.data
				} else {
					uni.showToast({
						title: '获取反馈类型失败',
						icon: 'none',
						duration: 2000
					});
				}
			});
		},

		btn() {
			if (this.imglist.length >= 9) {
				uni.showToast({
					title: '最多上传9张图片',
					icon: 'none'
				})
				return
			}

			let _this = this
			let math = 'static/' + _this.$u.guid(20)
			uni.chooseImage({
				count: 9 - _this.imglist.length,
				type: 'all',
				success(res) {
					const tempFilePaths = res.tempFiles
					wx.uploadFile({
						url: 'https://up-z2.qiniup.com',
						name: 'file',
						filePath: tempFilePaths[0].path,
						formData: {
							token: _this.token,
							key: 'smartmeter/img/' + math
						},
						success: function(res) {
							let str = JSON.parse(res.data)
							_this.userImgs = _this.upurl + '/' + str.key
							_this.imglist.push(_this.userImgs)
						},
						fail: function() {
							uni.showToast({
								title: '上传失败',
								icon: 'none'
							})
						}
					});
				}
			})
		},

		updateWordCount() {
			this.currentCount = this.textValue.trim().replace(/\s+/g, '').length;
			if (this.currentCount > 500) {
				this.textValue = this.textValue.slice(0, 500);
				this.currentCount = 500;
				uni.showToast({
					title: '字数已达到500字上限',
					icon: 'none'
				});
			}
		},
	}
}
</script>

<style lang="scss">
page {
	overflow-y: auto;
	background-image: url('https://lxnapi.ccttiot.com/bike/img/static/uYRs7Cv2Pbp95w3KjGO3');
	background-size: cover;
	background-position: center;
	background-repeat: no-repeat;
	min-height: 100vh;
}

.page {
	width: 750rpx;
	padding-bottom: 200rpx;

	.btn {
		position: fixed;
		left: 34rpx;
		bottom: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 680rpx;
		height: 90rpx;
		background: #4297F3;
		border-radius: 54rpx;
		font-weight: 500;
		font-size: 40rpx;
		color: #FFFFFF;
	}

	.cardbox {
		margin: 30rpx auto;
		width: 680rpx;
		padding: 20rpx 45rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 10rpx 64rpx 0rpx rgba(0, 0, 0, 0.08);
		border-radius: 20rpx;
		.input-box{
			margin-bottom: 20rpx;
			width: 612rpx;
			height: 80rpx;
			box-shadow: 0rpx 16rpx 40rpx 0rpx rgba(42, 130, 228, 0.1);
			border-radius: 20rpx;
			margin-top: 40rpx;
			overflow: hidden;
			padding-right: 38rpx;
			box-sizing: border-box;
			border: 2rpx solid #C7C7C7;
			input{
				padding-left: 20rpx;
				width: 100%;
				height: 100%;
			}
		}
		.tip {
			display: flex;
			flex-wrap: nowrap;
			font-weight: 700;
			font-size: 32rpx;
			color: #3D3D3D;
			line-height: 44rpx;

			.ipnt {
				margin-top: 10rpx;
				margin-right: 12rpx;
				color: #FF4444;
				font-size: 48rpx;
			}
		}

		.checkbox {
			display: flex;
			flex-wrap: wrap;

			.check_li {
				position: relative;
				margin-right: 10rpx;
				margin-top: 18rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 14rpx 36rpx;
				border-radius: 20rpx;
				border: 2rpx solid #C4C4C4;

				image {
					position: absolute;
					right: 0;
					bottom: 0;
					width: 40rpx;
					height: 20rpx;
				}
			}

			.check_li:nth-child(4n) {
				margin-right: 0;
			}

			.act1 {
				border: 2rpx solid #4297F3;
			}
		}

		.input-container {
			position: relative;
			width: 612rpx;
			height: 248rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 16rpx 40rpx 0rpx rgba(42, 130, 228, 0.1);
			border-radius: 20rpx;
			margin-top: 40rpx;
			overflow: hidden;
			padding-right: 38rpx;
			box-sizing: border-box;
			border: 2rpx solid #C7C7C7;
		}

		.placeholder {
			position: absolute;
			top: 18rpx;
			left: 38rpx;
			color: #999;
			pointer-events: none;
		}

		.custom-textarea {
			width: 100%;
			height: 100%;
			padding-top: 18rpx;
			padding-left: 38rpx;
			box-sizing: border-box;
		}

		.word-count {
			position: absolute;
			right: 10px;
			bottom: 10px;
			font-size: 12px;
			color: #999;
		}

		.icon {
			display: flex;
			flex-wrap: wrap;
			align-items: center;
			margin-top: 40rpx;
			
			.imgbox {
				position: relative;
				width: 33%;
				margin-bottom: 20rpx;
				
				image {
					width: 142rpx;
					height: 142rpx;
					border-radius: 10rpx;
				}
				
				.delete-btn {
					position: absolute;
					top: -16rpx;
					right: -16rpx;
					width: 32rpx;
					height: 32rpx;
					background: #DCEDFF ;
					border-radius: 50%;
					display: flex;
					align-items: center;
					justify-content: center;
					z-index: 1;
					
					image {
						width: 20rpx;
						height: 20rpx;
					}
				}
			}
			
			.add-btn {
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}
}
</style>