<template>
	<view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title='意见反馈' :titleStyle="{'color':'transparent'}" bgColor='transparent'></cus-header>
		<image class="topbg" src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/05/29/ebaaeb8a-ca70-4caa-b55a-87ba3bd5de23.png"></image>
		<div class="box" style="margin-top: 189rpx;">
			<div class="title adfac">选择问题类型<span>*</span></div>
			<div class="list adfacjb">
				<div class="pre adffcacjc" :class="{'active':tidx===1}" @tap="changeType(1)">
					<text>功能异常</text>
					<image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/05/29/8e24c230-a530-48ae-a2ed-d41262b9a051.png" v-if="tidx===1"></image>
				</div>
				<div class="pre adffcacjc" :class="{'active':tidx===2}" @tap="changeType(2)">
					<text>新功能建议</text>
					<image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/05/29/8e24c230-a530-48ae-a2ed-d41262b9a051.png" v-if="tidx===2"></image>
				</div>
				<div class="pre adffcacjc" :class="{'active':tidx===3}" @tap="changeType(3)">
					<text>其他</text>
					<image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/05/29/8e24c230-a530-48ae-a2ed-d41262b9a051.png" v-if="tidx===3"></image>
				</div>
			</div>
		</div>
		<div class="box">
			<div class="title adfac">问题描述<span>*</span></div>
			<div class="desc">
				<u-textarea height="160rpx" v-model="dto.suggestion" placeholder="尽量详细描述您遇到的问题、具体位置及体现形式等" border="none"></u-textarea>
			</div>
		</div>
		<div class="box">
			<div class="title adfac">图片补充<span class="tip">上传问题截图可以让问题快速解决哦!</span></div>
			<div class="upload">
				<u-upload width="148rpx" height="148rpx"
					:fileList="fileList"
				    @afterRead="afterRead"
				    @delete="deletePic"
				    :maxCount="1"
				></u-upload>
			</div>
		</div>
		<div class="box lxfs adfacjb">
			<div class="title">联系方式</div>
			<div class="contact">
				<u-input v-model="dto.phone" placeholder="手机号/QQ号/微信号,方便与您联系" border="none"></u-input>
			</div>
		</div>
		<div class="zt_btn" @tap="submit">提交</div>
	</view>
</template>

<script>
	const baseApi = require('@/http/baseApi.js')
	export default {
		data(){
			return {
				tidx:1,
				dto:{
					type:1,
					suggestion:'',
					pics:'',
					phone:''
				},
				fileList:[],
			}
		},
		methods:{
			changeType(type){
				this.tidx = type;
				this.dto.type = type;
			},
			// 删除图片
			deletePic(event) {
				this.fileList.splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
				let lists = [].concat(event.file);
				let fileListLen = this.fileList.length;
				lists.map((item) => {
				  this.fileList.push({
					...item,
					status: "uploading",
					message: "上传中",
				  });
				});
				for (let i = 0; i < lists.length; i++) {
				  const result = await this.uploadFilePromise(lists[i].url);
				  let item = this.fileList[fileListLen];
				  this.fileList.splice(
					fileListLen,
					1,
					Object.assign(item, {
					  status: "success",
					  message: "",
					  url: result,
					})
				  );
				  fileListLen++;
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
				  uni.uploadFile({
					url: baseApi.BaseApi + '/sys/oss/uploadFile',
					filePath: url,
					name: "file",
					header:{
						token: uni.getStorageSync('token')	
					},
					success: (res) => {
						let data = JSON.parse(res.data);
						if(data){
							if(data.code!==0) return
							setTimeout(() => {
								resolve(data.data);
							}, 1000);
						}
					}
				  });
				});
			},
			submit(){
				if(!this.dto.suggestion) return this.$showToast('请输入问题描述');
				if(this.fileList.length) this.dto.pics = this.fileList[0].url;
				this.$api.post('/sys/suggest/save',this.dto).then(res=>{
					if(res.data.code!==0) return this.$showToast(res.data.msg)
					this.$showToast('提交成功~');
					setTimeout(()=>{
						uni.reLaunch({
							url:'/pages/my'
						})
					},1500)
				})
			}
		}
	}
</script>

<style scoped lang="less">
	.page{
		background: #F7F6F9;
		padding: 0 30rpx 30rpx;
		box-sizing: border-box;
		
		.topbg{
			width: 100%;
			position: fixed;
			left: 0;
			top: 0;
		}
		
		.box{
			width: 100%;
			background: #FFFFFF;
			border-radius: 24rpx;
			margin-top: 20rpx;
			padding: 40rpx 24rpx;
			box-sizing: border-box;
			position: relative;
			
			.title{
				font-family: PingFang-SC, PingFang-SC;
				font-weight: bold;
				font-size: 32rpx;
				color: #111111;
				line-height: 32rpx;
				span{
					color: #F31616;
					&.tip{
						font-weight: 400;
						font-size: 24rpx;
						color: #A6A6A6;
						margin-left: 10rpx;
					}
				}
			}
			
			&.lxfs{
				.title{
					width: 154rpx;
				}
				.contact{
					width: calc(100% - 154rpx);
				}
			}
		}
		
		.list{
			margin-top: 48rpx;
			.pre{
				width: calc(100% / 3 - 14rpx);
				height: 80rpx;
				background: #FFFFFF;
				border-radius: 16rpx;
				border: 1rpx solid #C7C7C7;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #333333;
				line-height: 80rpx;
				text-align: center;
				position: relative;
				image{
					width: 48rpx;
					height: 48rpx;
					position: absolute;
					right: 0;
					bottom: 0;
				}
				&.active{
					background: #D9F159;
					border: 1rpx solid #D9F159;
					font-weight: bold;
					color: #111111;
				}
			}
		}
		
		.desc{
			margin-top: 40rpx;
		}
		
		.upload{
			margin-top: 54rpx;
			width: 148rpx;
			height: 148rpx;
			background: url('https://transcend.ringzle.com/xiaozhi-app/profile/2025/05/29/45ebe996-192d-47ec-a749-33ddb3869a23.png') no-repeat;
			background-size: 100% 100%;
		}
		
		.zt_btn{
			margin-top: 30rpx;
		}
	}
</style>