<template>
	<view class="feedback-container">
		<!-- 顶部选项卡 -->
		<view class="tab-nav">
			<view class="tab-item" :class="{ active: currentTab === 'publish' }" @click="switchTab('publish')">
				发布
			</view>
			<view class="tab-item" :class="{ active: currentTab === 'my' }" @click="switchTab('my')">
				我的
			</view>
		</view>

		<!-- 发布反馈表单 -->
		<view v-if="currentTab === 'publish'" class="content">
			<view class="category-selector" @click="openPopup">
				<text class="category-text">{{ getCategoryText(feedback.type) || '请选择问题分类' }}</text>
				<uni-icons @tap="selectUserPopup" color="#a8aeb4" type="right" size="20"></uni-icons>
			</view>
			<!-- 文本输入区域 -->
			<textarea class="feedback-input" placeholder="如果在使用中遇到问题,请在这里向我们反馈..." v-model="feedback.content"
				maxlength="500" @input="handleInput"></textarea>
			<view class="word-count">{{feedback.content.length}}/500</view>

			<!-- 图片上传区域 -->
			<view class="upload-section">
				<view class="form-label mar-b-20">上传凭证（可选）</view>
				<uni-file-picker @delete="delFile" v-model="imageValue" fileMediatype="image" mode="grid"
					@select="select" />
			</view>

			<!-- 表单区域 -->
			<view class="form-section">
				<view class="form-item">
					<view class="label">
						电话号码（选填）
					</view>
					<input type="text" placeholder="请填写电话号码" v-model="feedback.phone" />
				</view>
				<view class="form-item">
					<view class="label">邮箱（选填）</view>
					<input type="text" placeholder="请填写邮箱" v-model="feedback.email" />
				</view>
			</view>
		</view>

		<!-- 我的反馈列表 -->
		<view v-else class="my-feedback">
			<view class="user-info">
				<image class="avatar" :src="feedback.avatar"></image>
				<text class="username">{{feedback.account}}</text>
			</view>
			<view class="feedback-stats">
				<view class="stat-item" @click="switchSubTab('feedback')"
					:class="{ active: currentSubTab === 'feedback' }">
					<text class="stat-value">{{feedback.totalFeedbackCount}}</text>
					<text class="stat-label">反馈</text>
				</view>
				<view class="stat-item" @click="switchSubTab('reply')" :class="{ active: currentSubTab === 'reply' }">
					<text class="stat-value">{{feedback.repliedFeedbackCount}}</text>
					<text class="stat-label">回复</text>
				</view>
			</view>

			<!-- 反馈列表 -->
			<view v-if="currentSubTab === 'feedback'" class="feedback-list">
				<view>
					<view v-if="list && list.length > 0" class="pad-lr-20 info">
						<view v-for="(item, index) in list" :key="index" class="feedback-item">
							<view class="feedback-content">
								<view class="feedback-type">
									<text class="type-tag"
										:class="{'type-error': item.type === 1, 'type-suggest': item.type === 2}">
										{{ item.type === 1 ? '功能异常' : '产品建议' }}
									</text>
								</view>
								<view class="main-content">{{ item.content }}</view>
								<!-- 图片展示区域 -->
								<view class="images-container" v-if="item.images">
									<image v-for="(img, imgIndex) in item.images.split(',')" :key="imgIndex" :src="img"
										mode="aspectFill" class="feedback-image"
										@click="previewImage(img, item.images.split(','))"></image>
								</view>
								<!-- 联系信息 -->
								<view class="contact-info" v-if="item.phone || item.email">
									<text v-if="item.phone" class="contact-item">电话：{{ item.phone }}</text>
									<text v-if="item.email" class="contact-item">邮箱：{{ item.email }}</text>
								</view>
								<!-- 回复内容 -->
								<view class="reply-content" v-if="item.replyContent">
									<view class="reply-title">官方回复：</view>
									<view class="reply-text">{{ item.replyContent }}</view>
								</view>
							</view>
						</view>
						<!-- 加载提示 -->
						<view v-if="isLoading" class="loading-tip">加载中...</view>
						<!-- 没有更多数据提示 -->
						<view v-if="!isLoading && noMoreData" class="no-more-tip">——没有更多数据了——</view>
					</view>
					<view v-else class="empty-state">
						<view class="flex-items-plus">
							<image src="../../static/images/empty.png" class="empty"></image>
						</view>
						<view class="font28 font-gray flex-items-plus">
							暂无反馈数据
						</view>
					</view>
				</view>
			</view>

			<!-- 回复列表 -->
			<view v-else class="reply-list">
				<view>
					<view v-if="list && list.length > 0" class="pad-lr-20 info">
						<view v-for="(item, index) in list" :key="index" class="feedback-item">
							<view class="feedback-content">
								<view class="feedback-type">
									<text class="type-tag"
										:class="{'type-error': item.type === 1, 'type-suggest': item.type === 2}">
										{{ item.type === 1 ? '功能异常' : '产品建议' }}
									</text>
								</view>
								<view class="main-content">{{ item.content }}</view>
								<!-- 图片展示区域 -->
								<view class="images-container" v-if="item.images">
									<image v-for="(img, imgIndex) in item.images.split(',')" :key="imgIndex" :src="img"
										mode="aspectFill" class="feedback-image"
										@click="previewImage(img, item.images.split(','))"></image>
								</view>
								<!-- 联系信息 -->
								<view class="contact-info" v-if="item.phone || item.email">
									<text v-if="item.phone" class="contact-item">电话：{{ item.phone }}</text>
									<text v-if="item.email" class="contact-item">邮箱：{{ item.email }}</text>
								</view>
								<!-- 回复内容 -->
								<view class="reply-content" v-if="item.replyContent">
									<view class="reply-title">官方回复：</view>
									<view class="reply-text">{{ item.replyContent }}</view>
								</view>
							</view>
						</view>
						<!-- 加载提示 -->
						<view v-if="isLoading" class="loading-tip">加载中...</view>
						<!-- 没有更多数据提示 -->
						<view v-if="!isLoading && noMoreData" class="no-more-tip">——没有更多数据了——</view>
					</view>
					<view v-else class="empty-state">
						<view class="flex-items-plus">
							<image src="../../static/images/empty.png" class="empty"></image>
						</view>
						<view class="font28 font-gray flex-items-plus">
							暂无回复数据
						</view>
					</view>
				</view>
			</view>


		</view>

		<!-- 提交按钮 -->
		<button class="fixed-button" @click="save" v-if="currentTab === 'publish'">提交反馈</button>
		<!-- 分类选择弹窗 -->
		<uni-popup ref="popup" type="bottom">
			<view class="popup-content">
				<view class="popup-header">
					<text>问题分类</text>
					<text class="close-btn" @click="closePopup">✕</text>
				</view>
				<view class="category-list">
					<view class="category-item" v-for="(item, index) in categories" :key="index"
						@click="selectCategory(item)" :class="{'active': getCategoryText(feedback.type) === item}">
						{{ item }}
					</view>
				</view>
			</view>
		</uni-popup>

		<CustomToast ref="customToast" />
	</view>
</template>

<script>
	import CustomToast from '../../components/CustomToast.vue';
	import {
		UPLOAD_URL
	} from '../../common/config.js';
	import {
		feedbackSave,
		feedbackList,
		getFeedback
	} from '../../config/api.js';
	export default {
		components: {
			CustomToast,
		},
		data() {
			return {
				list: [],
				isLoading: false, // 是否正在加载
				noMoreData: false, // 是否没有更多数据
				imageValue: [],
				currentTab: 'publish',
				currentSubTab: 'feedback',
				token: "",
				params: {
					current: 1,
					type: "",
					size: 10,
				},
				feedback: {
					content: "",
					type: "",
					images: "",
					phone: "",
					email: "",
					imgList: [],
				},
				categories: ['功能异常或闪退', '产品功能体验与建议']
			}
		},
		onReachBottom() {
			if (!this.isLoading && !this.noMoreData) {
				this.getList();
			}
		},
		onLoad() {
			this.token = uni.getStorageSync('access_token');
			this.getDetail()
		},
		methods: {
			getDetail() {
				getFeedback().then((res) => {
					this.feedback = res.data
				})
			},
			getList() {
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				this.isLoading = true;
				feedbackList(this.params).then((res) => {
					if (res.data.records.length === 0) {
						this.noMoreData = true;
					} else {
						this.list = this.list.concat(res.data.records);
						this.params.current++;
					}
					uni.hideLoading()
					this.isLoading = false;
				})
			},
			switchTab(tab) {
				this.currentTab = tab;
				if (tab === 'my') {
					// 切换到"我的"时重置列表相关数据
					this.list = [];
					this.params.current = 1;
					this.noMoreData = false;
					this.getList();
				}
			},
			switchSubTab(tab) {
				this.currentSubTab = tab;
				// 重置列表相关数据
				this.list = [];
				this.params.current = 1;
				this.noMoreData = false;
				// 设置type并获取列表
				this.params.type = tab === 'reply' ? '1' : '';
				this.getList();
			},
			handleInput(e) {
				if (this.feedback.content.length >= 500) {
					this.feedback.content = this.feedback.content.slice(0, 500);
				}
			},
			openPopup() {
				this.$refs.popup.open();
			},
			closePopup() {
				this.$refs.popup.close();
			},
			selectCategory(category) {
				this.feedback.type = category === '功能异常或闪退' ? '1' : '2';
				this.closePopup();
			},
			getCategoryText(type) {
				return type === '1' ? '功能异常或闪退' : type === '2' ? '产品功能体验与建议' : '';
			},
			// 获取选择的文件
			async select(e) {
				let list = [].concat(e.tempFiles);
				for (let i = 0; i < list.length; i++) {
					const result = await this.uploadFilePromise(list[i].url);
					this.feedback.imgList.push(result.data.link);
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: UPLOAD_URL,
						filePath: url,
						header: {
							"Blade-Auth": this.token
						},
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							resolve(JSON.parse(res.data));
						}
					});
				});
			},
			delFile(e) {
				this.feedback.imgList.splice(e.index, 1); // 删除对应 index 的附件
			},

			// 提交反馈
			save() {
				// 校验问题分类
				if (!this.feedback.type) {
					uni.showToast({
						title: "请选择问题分类",
						icon: "none"
					})
					return;
				}

				// 校验反馈内容
				if (!this.feedback.content.trim()) {
					uni.showToast({
						title: "请填写反馈内容",
						icon: "none"
					})
					return;
				}
				this.feedback.images = this.feedback.imgList.join(',');
				feedbackSave(this.feedback).then((res) => {
					if (res.code == 200) {
						this.$refs.customToast.showToast('提交成功');
						this.feedback = {
							imgList: [],
							content: "",
							type: "",
							images: "",
							phone: "",
							email: "",
						}
						this.imageValue = []
					}
				})
			},
			// 预览图片
			previewImage(current, urls) {
				uni.previewImage({
					current,
					urls
				});
			},
		}
	}
</script>

<style lang="scss">
	.aite {
		height: 50rpx;
		width: 50rpx;
		margin-right: 20rpx;
		margin-left: 10rpx;
	}

	.category-selector {
		background-color: #fff;
		padding: 30rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;
		border-radius: 16rpx;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.03);
		border: 1rpx solid #f0f0f0;

		.category-text {
			font-size: 28rpx;
			color: #333;
			font-weight: 500;
		}

		.arrow-down {
			font-size: 24rpx;
			color: #999;
		}
	}

	.popup-content {
		background-color: #fff;
		border-radius: 24rpx 24rpx 0 0;
		padding-bottom: env(safe-area-inset-bottom);

		.popup-header {
			padding: 32rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 1rpx solid #f5f5f5;

			text {
				font-size: 32rpx;
				color: #333;
				font-weight: 600;
			}

			.close-btn {
				color: #909399;
				font-size: 40rpx;
				padding: 10rpx;
			}
		}

		.category-list {
			padding: 20rpx 32rpx;

			.category-item {
				padding: 32rpx 24rpx;
				font-size: 28rpx;
				color: #333;
				border-bottom: 1rpx solid #f5f5f5;
				transition: all 0.3s;

				&.active {
					color: #007AFF;
					background-color: #f0f7ff;
					border-radius: 8rpx;
				}

				&:last-child {
					border-bottom: none;
				}
			}
		}
	}

	.fixed-button {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		padding: 15rpx;
		background-color: #3C86F7;
		color: white;
		border: none;
		font-size: 32rpx;
		font-weight: 600;
	}

	.feedback-container {
		min-height: 100vh;
		background-color: #f5f5f5;
		padding-bottom: 120rpx;

		.nav-bar {
			height: 44px;
			background-color: #fff;
			display: flex;
			align-items: center;
			padding: 0 15px;
			position: relative;

			.back-icon {
				font-size: 20px;
			}

			.title {
				position: absolute;
				left: 50%;
				transform: translateX(-50%);
				font-size: 16px;
				font-weight: 500;
			}
		}

		.tabs {
			display: flex;
			background-color: #fff;
			padding: 10px 15px;

			.tab-item {
				padding: 8px 20px;
				margin-right: 15px;
				border-radius: 20px;
				font-size: 14px;

				&.active {
					background-color: #f0f0f0;
				}
			}
		}

		.content {
			padding: 30rpx;

			.feedback-input {
				width: 100%;
				height: 300rpx;
				background-color: #fff;
				border-radius: 16rpx;
				padding: 24rpx;
				font-size: 28rpx;
				margin-bottom: 8rpx;
				box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.03);
				border: 1rpx solid #f0f0f0;
			}

			.word-count {
				text-align: right;
				font-size: 24rpx;
				color: #909399;
				padding: 8rpx 16rpx;
				margin-bottom: 20rpx;
			}

			.upload-section {
				background-color: #fff;
				border-radius: 16rpx;
				padding: 24rpx;
				margin-bottom: 20rpx;
				box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.03);
				border: 1rpx solid #f0f0f0;

				.form-label {
					font-size: 28rpx;
					color: #333;
					font-weight: 500;
					margin-bottom: 24rpx;
				}
			}

			.form-section {
				background-color: #fff;
				border-radius: 16rpx;
				padding: 0 24rpx;
				box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.03);
				border: 1rpx solid #f0f0f0;

				.form-item {
					padding: 24rpx 0;
					border-bottom: 1rpx solid #f5f5f5;

					&:last-child {
						border-bottom: none;
					}

					.label {
						margin-bottom: 16rpx;
						font-size: 28rpx;
						color: #333;
						font-weight: 500;
					}

					input {
						width: 100%;
						height: 80rpx;
						background-color: #f5f7fa;
						border-radius: 8rpx;
						padding: 0 24rpx;
						font-size: 28rpx;
						color: #333;

						&::placeholder {
							color: #909399;
						}
					}
				}
			}
		}

		.tech-support {
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 20px 0;

			image {
				width: 20px;
				height: 20px;
				margin-right: 5px;
			}

			text {
				font-size: 12px;
				color: #999;
			}
		}

		.tab-bar {
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			height: 50px;
			background-color: #fff;
			display: flex;
			border-top: 1px solid #f0f0f0;

			.tab-bar-item {
				flex: 1;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				font-size: 12px;
				color: #666;

				&.active {
					color: #007AFF;
				}

				.iconfont {
					font-size: 20px;
					margin-bottom: 2px;
				}
			}
		}

		.tab-nav {
			display: flex;
			background-color: #fff;
			padding: 20rpx 40rpx;
			margin-bottom: 20rpx;
			box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
			position: sticky;
			top: 0;
			z-index: 100;

			.tab-item {
				flex: 1;
				text-align: center;
				font-size: 32rpx;
				color: #666;
				position: relative;
				padding: 20rpx 0;
				transition: all 0.3s;

				&.active {
					color: #007AFF;
					font-weight: 600;

					&::after {
						content: '';
						position: absolute;
						bottom: 0;
						left: 50%;
						transform: translateX(-50%);
						width: 48rpx;
						height: 6rpx;
						background-color: #007AFF;
						border-radius: 4rpx;
						transition: all 0.3s;
					}
				}
			}
		}

		.my-feedback {
			background-color: #fff;
			margin: 20rpx;
			border-radius: 16rpx;
			box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);

			.user-info {
				padding: 40rpx;
				text-align: center;
				border-bottom: 1rpx solid #f5f5f5;

				.avatar {
					width: 120rpx;
					height: 120rpx;
					border-radius: 50%;
					margin-bottom: 20rpx;
					box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
				}

				.username {
					display: block;
					font-size: 32rpx;
					color: #333;
					margin-bottom: 12rpx;
					font-weight: 600;
				}

				.coins {
					font-size: 24rpx;
					color: #909399;
					background-color: #f5f7fa;
					padding: 4rpx 16rpx;
					border-radius: 20rpx;
					display: inline-block;
				}
			}

			.feedback-stats {
				display: flex;
				padding: 30rpx 0;
				border-bottom: 1rpx solid #f5f5f5;
				background: linear-gradient(to bottom, #fff, #f9f9f9);

				.stat-item {
					flex: 1;
					text-align: center;
					position: relative;
					cursor: pointer;

					.stat-value {
						display: block;
						font-size: 36rpx;
						color: #333;
						margin-bottom: 12rpx;
						font-weight: 600;
					}

					.stat-label {
						font-size: 26rpx;
						color: #909399;
					}

					&.active {
						.stat-value {
							color: #007AFF;
							transform: scale(1.05);
							transition: all 0.3s;
						}

						.stat-label {
							color: #007AFF;
						}

						&::after {
							content: '';
							position: absolute;
							bottom: -30rpx;
							left: 50%;
							transform: translateX(-50%);
							width: 48rpx;
							height: 6rpx;
							background-color: #007AFF;
							border-radius: 4rpx;
						}
					}
				}
			}

			.feedback-list,
			.reply-list {
				min-height: 300rpx;
			}

			.no-feedback {
				padding: 100rpx 0;
				text-align: center;
				color: #999;
				font-size: 28rpx;
			}

			.tech-support {
				padding: 40rpx;
				text-align: center;

				image {
					width: 40rpx;
					height: 40rpx;
					vertical-align: middle;
					margin-right: 10rpx;
				}

				text {
					font-size: 24rpx;
					color: #999;
					vertical-align: middle;
				}
			}
		}
	}

	.loading-tip,
	.no-more-tip {
		text-align: center;
		font-size: 24rpx;
		color: #909399;
		padding: 30rpx 0;
		background: transparent;
	}

	.pad-lr-20 {
		background-color: #f5f5f5;
		min-height: 100vh;
		padding: 20rpx;
	}

	.feedback-item {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
		border: 1rpx solid #f0f0f0;
		transition: all 0.3s;

		&:hover {
			transform: translateY(-2rpx);
			box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
		}

		.feedback-type {
			margin-bottom: 24rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.type-tag {
				display: inline-block;
				padding: 8rpx 24rpx;
				border-radius: 8rpx;
				font-size: 24rpx;
				font-weight: 500;

				&.type-error {
					background-color: #FEF0F0;
					color: #F56C6C;
					border: 1rpx solid rgba(245, 108, 108, 0.2);
				}

				&.type-suggest {
					background-color: #F0F9EB;
					color: #67C23A;
					border: 1rpx solid rgba(103, 194, 58, 0.2);
				}
			}
		}

		.main-content {
			font-size: 28rpx;
			color: #333;
			line-height: 1.6;
			margin-bottom: 24rpx;
			padding: 0 4rpx;
		}

		.images-container {
			display: flex;
			flex-wrap: wrap;
			gap: 16rpx;
			margin-bottom: 24rpx;
			padding: 0 4rpx;

			.feedback-image {
				width: 180rpx;
				height: 180rpx;
				border-radius: 12rpx;
				box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
				transition: all 0.3s;

				&:active {
					transform: scale(0.98);
				}
			}
		}

		.contact-info {
			font-size: 24rpx;
			color: #909399;
			margin-bottom: 24rpx;
			padding: 16rpx 24rpx;
			background-color: #f5f7fa;
			border-radius: 12rpx;

			.contact-item {
				margin-right: 30rpx;
				display: inline-flex;
				align-items: center;

				&:before {
					content: '';
					display: inline-block;
					width: 6rpx;
					height: 6rpx;
					border-radius: 50%;
					background-color: #909399;
					margin-right: 8rpx;
				}
			}
		}

		.reply-content {
			background-color: #f0f7ff;
			padding: 24rpx;
			border-radius: 12rpx;
			margin-top: 16rpx;
			border-left: 6rpx solid #007AFF;

			.reply-title {
				font-size: 26rpx;
				color: #333;
				margin-bottom: 12rpx;
				font-weight: 600;
				display: flex;
				align-items: center;

				&:before {
					content: '';
					display: inline-block;
					width: 6rpx;
					height: 6rpx;
					border-radius: 50%;
					background-color: #007AFF;
					margin-right: 8rpx;
				}
			}

			.reply-text {
				font-size: 26rpx;
				color: #606266;
				line-height: 1.6;
				padding-left: 16rpx;
			}
		}
	}

	.empty-state {
		padding: 120rpx 0;
		text-align: center;
		background: #fff;
		margin: 20rpx;
		border-radius: 16rpx;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);

		.empty {
			width: 240rpx;
			height: 240rpx;
			margin-bottom: 30rpx;
			opacity: 0.8;
		}

		.font28 {
			color: #909399;
			font-size: 28rpx;
		}
	}
</style>