<template>
	<view class="ProblemFeedback">
		<!-- 科室选择 -->
		<view class="report">
			<view class="feedback_name">请选择科室</view>
			<view class="feedback_name copy" style="padding: 0;">
				<view class="uni-textarea" style="width:100%">
					<uni-section style="width:100%;padding: 0;" title="">
						<uni-data-picker placeholder="请选择科室" :localdata="group_list" @change="onchange">
						</uni-data-picker>
					</uni-section>
				</view>
			</view>
		</view>
		<!-- 是否匿名 -->
		<view class="report">
			<view class="feedback_name">是否匿名</view>
			<view class="feedback_name copy" style="padding: 0;">
				<view class="uni-textarea">
					<uni-data-checkbox v-model="feedback.open_status" @change="radioChange"
						:localdata="label"></uni-data-checkbox>
				</view>
			</view>
		</view>
		<!-- 举报类型 -->
		<view class="report">
			<view class="feedback_name copy">
				<text>详情描述</text>
				<text>{{ feedback.politics_content.length }}/200</text>
			</view>
			<view class="uni-textarea">
				<textarea maxlength="200" style="color: #666;" v-model="feedback.politics_content"
					class="activeTextCopy" placeholder="提供更多信息有助于投诉建议被快速处理~" />
			</view>
		</view>
		<!-- 图片上传 -->
		<view class="Upload_module">
			<view class="feedback_name evidence copy">
				<text>图片证据</text>
			</view>
			<view class="layout">
				<!-- 图片展示 -->
				<view v-if="feedback.politics_images != ''" class="picture_list">
					<view>
						<view @tap="handlePrevewImage(feedback.politics_images)" class="cu-avatar view"
							:style="{ 'backgroundImage': 'url(' + feedback.politics_images + ')' }"></view>
						<uni-icons @tap="bandleRemoveImg()" class="cuIcon-close delete_img" color="#ffffff"
							type="closeempty" size="15"></uni-icons>
					</view>
				</view>
				<view class="uploadImg" @click="uploadImg">
					<view class="image">
						<uni-icons type="plusempty" size="30"></uni-icons>
					</view>
				</view>
			</view>
		</view>


		<view class="btn_bottom">
			<button @click="submitDo" class="cu-btn bnt1">提交反馈</button>
		</view>

		<!-- 添加底部TabBar -->
		<view class="custom-tabbar">
			<view class="tabbar-item" :class="{ active: currentTab === 'complaint' }"
				@click="switchTab('/pages/index/complaint')">
				<view class="tabbar-icon">
					<uni-icons type="chatboxes" size="24"
						:color="currentTab === 'complaint' ? '#4d5eff' : '#999'"></uni-icons>
				</view>
				<view class="tabbar-text" :style="{ color: currentTab === 'complaint' ? '#4d5eff' : '#999' }">投诉建议
				</view>
			</view>
			<view class="tabbar-item" :class="{ active: currentTab === 'my' }" @click="switchTab('/pages/index/user')">
				<view class="tabbar-icon">
					<uni-icons type="person" size="24" :color="currentTab === 'my' ? '#4d5eff' : '#999'"></uni-icons>
				</view>
				<view class="tabbar-text" :style="{ color: currentTab === 'my' ? '#4d5eff' : '#999' }">我的</view>
			</view>
		</view>
	</view>
</template>

<script>
import {
	getUserGroupList, PoliticsAdd
} from '@/util/api.js';
import store from '@/store/index.js'
export default {
	data() {
		return {
			currentTab: 'complaint',
			// 反馈类型列表
			feedback: {
				group_id: "",
				open_status: 2,
				politics_content: "",
				politics_images: '',
				token: '',
			},
			group_list: [],
			label: [{
				text: '否',
				value: 2
			}, {
				text: '是',
				value: 1
			}],
			chooseImags: [],
		}
	},
	onLoad() {
		var user = uni.getStorageSync('userInfo');
		if (!user) {
			uni.reLaunch({
				url: "/pages/index/index"
			})
		}
		this.getGroupList()
	},
	methods: {
		getGroupList() {
			getUserGroupList()
				.then(res => {
					console.log(res);
					var list = this.transformGroupData(res.data);
					this.group_list = list
				})
				.catch(error => {
					uni.showToast({
						title: error,
						icon: 'none',
						duration: 2000
					});
				})
		},
		transformGroupData(data) {
			if (!data || !Array.isArray(data)) return [];

			return data.map(item => {
				const newItem = {
					value: item.id,
					text: item.name
				};
				// 递归处理children
				if (item.children && Array.isArray(item.children)) {
					newItem.children = this.transformGroupData(item.children);
				}
				return newItem;
			});
		},
		// 选择科室
		onchange(e) {
			console.log(e.detail.value[1].value);
			this.feedback.group_id = e.detail.value[1].value;
		},
		//是否匿名
		radioChange(e) {
			console.log("e:", e);
			this.feedback.open_status = e.detail.value
		},

		// 上传图片
		uploadImg() {
			var that = this;
			uni.chooseImage({
				//同时选中的图片的数量
				count: 1,
				//图片的格式  原图  压缩
				sizeType: ['original', 'compressed'],
				//图片的来源  相册  照相机
				sourceType: ['album', 'camera'],
				success: (res) => {
					console.log(res)
					var url = res.tempFilePaths[0];
					uni.uploadFile({
						url: store.state.apiUrl + 'api/common/upload', //仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						success: (uploadFileRes) => {
							var data = JSON.parse(uploadFileRes.data);
							console.log(data);
							that.feedback.politics_images = data.data.fullurl;
						}
					});
				},
			});
		},
		//反馈,图片放大预览
		handlePrevewImage(e) {
			//先构造要预览的图标数组
			const urls = this.feedback.politics_images;
			//接收传递过来的图片url
			const current = e;
			wx.previewImage({
				current: current, // 当前显示图片的http链接
				urls: [urls] // 需要预览的图片http链接列表
			})
		},

		//删除data里面图片的方法
		bandleRemoveImg(index) {
			this.feedback.politics_images = '';
		},
		submitDo() {
			console.log(this.feedback);
			if (this.feedback.group_id == '') {
				uni.showToast({
					title: '科室不能为空！',
					icon: 'none',
					duration: 2000
				});
				return;
			}
			if (this.feedback.politics_content == '') {
				uni.showToast({
					title: '详情描述不能为空！',
					icon: 'none',
					duration: 2000
				});
				return;
			}
			var user = uni.getStorageSync('userInfo');
			this.feedback.token = user.token;
			PoliticsAdd(this.feedback)
				.then(res => {
					if (res.code == 1) {
						uni.showToast({
							title: '提交成功！',
							icon: 'none',
							duration: 2000
						});
						setTimeout(() => {
							uni.redirectTo({
								url: "/pages/index/list"
							})
						}, 1000)
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
					console.log(res);
				})
				.catch(error => {
					uni.showToast({
						title: error,
						icon: 'none',
						duration: 2000
					});
				})
		},
		switchTab(url) {
			uni.redirectTo({
				url: url
			})
		},
	}
}
</script>


<style lang="scss" scoped>
.uni-textarea-placeholder {
	color: #ccc;
}

/deep/.uni-section-header {
	padding: 0 !important;
}

.report {
	padding: 0 32rpx;
}

/* ... 其他现有样式 ... */

/* 添加TabBar样式 */
.custom-tabbar {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	height: 100rpx;
	background-color: #ffffff;
	display: flex;
	box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
	z-index: 999;
}

.tabbar-item {
	flex: 1;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	padding: 10rpx 0;
}

.tabbar-icon {
	height: 50rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.tabbar-text {
	font-size: 24rpx;
	margin-top: 6rpx;
}

.tabbar-item.active .tabbar-text {
	color: #4d5eff;
}

/* 为底部TabBar预留空间 */
.btn_bottom {
	margin-bottom: 120rpx;
}

.evidence {
	margin-bottom: 20rpx;
}

.ProblemFeedback {
	background: #fafbfb;
	height: 100%;
	padding-bottom: 50rpx;

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

		view {
			width: 158rpx;
			margin-right: 14rpx;
			height: 158rpx;
			position: relative;

			image,
			.view {
				width: 158rpx;
				height: 158rpx;
				border-radius: 12rpx;
			}

			.delete_img {
				background-color: red;
				color: #fff;
				position: absolute;
				right: 10rpx;
				z-index: 10;
				top: 10rpx;
				text-align: center;
				line-height: 36rpx;
				width: 32rpx;
				height: 32rpx;
				border-radius: 4rpx;
			}
		}
	}

	.btn_bottom {
		height: 160rpx;
		padding: 12rpx 32rpx;
		background-color: #fafbfb;
		margin-top: 100rpx;

		.bnt1 {
			width: 100%;
			color: #fff;
			background: #4d5eff;
			height: 88rpx;
			line-height: 88rpx;

		}
	}

	.Upload_module {
		margin: 0 34rpx;
		margin-top: 40rpx;

		.uploadImg {
			width: 160rpx;
			height: 160rpx;
			border: 2rpx solid #dfdfdf;
			border-radius: 12rpx;
			background: #fff;
			display: flex;
			align-items: center;
			justify-content: center;


			.image {
				image {
					width: 70rpx;
					height: 70rpx;
				}

			}
		}

		.Tips {
			font-size: 24rpx;
			margin-top: 20rpx;
			color: #999999;
		}

		.layout {
			display: flex;
			align-items: center;
		}
	}

	.feedback_name {
		font-size: 30rpx;
		color: #333;
		padding: 36rpx 0 0 0;
	}

	.copy {
		display: flex;
		justify-content: space-between;
		margin-top: 20rpx;

		&>text:nth-child(2) {
			color: #b0b0b0;
			font-size: 26rpx;
		}
	}

	.uni-textarea {
		display: flex;
		margin-top: 20rpx;
		justify-content: center;

		.uni-textarea-compute {
			height: 100%;
		}

		.activeText {
			border: 3rpx solid #4d5eff;
		}

		.activeTextCopy {
			border: 3rpx solid #eee;
		}

		textarea {
			padding: 24rpx;
			font-size: 28rpx;
			width: 710rpx !important;
			height: 200rpx !important;
			background: #fff;
			border-radius: 10rpx;
		}
	}

}
</style>