<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 copy">
				<text>详情描述</text>
				<text>{{ feedback.content.length }}/200</text>
			</view>
			<view class="uni-textarea">
				<textarea maxlength="200" style="color: #666;" v-model="feedback.content" class="activeTextCopy"
					placeholder="提供更多信息有助于投诉建议被快速处理~" />
			</view>
		</view>
		<!-- 图片上传 -->
		<view class="btn_bottom">
			<button @click="submitDo" class="cu-btn bnt1">提交反馈</button>
		</view>

		<!-- 登录弹窗 -->
		<view class="login-modal" v-if="showLoginModal">
			<view class="login-mask" @click="closeLoginModal"></view>
			<view class="login-content">
				<view class="login-title">请登录</view>
				<view class="login-form">
					<view class="form-item">
						<uni-easyinput class="input" trim="all" v-model="login.username" placeholder="请输入账号"
							style="width: 100%;"></uni-easyinput>
					</view>
					<view class="form-item">
						<uni-easyinput class="input" type="password" trim="all" v-model="login.password"
							placeholder="请输入密码" style="width: 100%;"></uni-easyinput>
					</view>
				</view>
				<view class="login-btn-wrapper">
					<button @click="handleLogin" class="cu-btn login-btn">登 录</button>
				</view>
				<view class="login-close" @click="closeLoginModal">
					<text class="close-icon">×</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import {
	getUserGroupList,
	Politicsh5Add,
	userLogin
} from '@/util/api.js';
import store from '@/store/index.js'
export default {
	data() {
		return {
			feedback: {
				group_id: "",
				content: "",
				user_id: 0,
				politics_type: 2,
			},
			group_list: [],
			showLoginModal: false, // 控制登录弹窗显示
			login: {
				username: '',
				password: ''
			}
		}
	},
	onLoad() {
		var user = uni.getStorageSync('userInfo');
		if (!user) {
			this.showLoginModal = true; // 如果未登录，显示登录弹窗
		}
		this.getGroupList()
	},
	methods: {
		// 关闭登录弹窗
		closeLoginModal() {
			this.showLoginModal = false;
		},

		// 处理登录
		handleLogin() {
			if (!this.login.username || !this.login.password) {
				uni.showToast({
					title: '用户名和密码不能为空',
					icon: 'none'
				});
				return;
			}
			userLogin(this.login)
				.then(res => {
					console.log(res);
					if (res.code == 0) {
						uni.showToast({
							title: '登陆失败！',
							icon: 'none',
							duration: 2000
						});
					} else {
						uni.setStorageSync('userInfo', res.data);
						uni.showToast({
							title: '登陆成功！',
							icon: 'none',
							duration: 2000
						});
						this.closeLoginModal()
					}
				})
				.catch(error => {
					uni.showToast({
						title: error,
						icon: 'none',
						duration: 2000
					});
				})
		},
		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;
		},
		submitDo() {
			// 检查是否登录
			var user = uni.getStorageSync('userInfo');
			if (!user) {
				this.showLoginModal = true;
				return;
			}

			console.log(this.feedback);
			if (this.feedback.group_id == '') {
				uni.showToast({
					title: '科室不能为空！',
					icon: 'none',
					duration: 2000
				});
				return;
			}
			if (this.feedback.content == '') {
				uni.showToast({
					title: '详情描述不能为空！',
					icon: 'none',
					duration: 2000
				});
				return;
			}
			this.feedback.user_id = user.user.id;
			Politicsh5Add(this.feedback)
				.then(res => {
					if (res.code == 1) {
						uni.showToast({
							title: '提交成功！',
							icon: 'none',
							duration: 2000
						});
						this.feedback = {
							group_id: "",
							content: "",
							user_id: 0,
							politics_type: 2,
						}
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
					console.log(res);
				})
				.catch(error => {
					uni.showToast({
						title: error,
						icon: 'none',
						duration: 2000
					});
				})
		},
	}
}
</script>


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

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

.report {
	padding: 0 32rpx;
}

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

/* 登录弹窗相关样式 */
.login-modal {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	z-index: 100;
	display: flex;
	justify-content: center;
	align-items: center;
}

.login-mask {
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background-color: rgba(0, 0, 0, 0.5);
}

.login-content {
	position: relative;
	width: 600rpx;
	background-color: #fff;
	border-radius: 20rpx;
	padding: 60rpx 40rpx;
	z-index: 10000;
}

.login-title {
	font-size: 36rpx;
	font-weight: bold;
	text-align: center;
	margin-bottom: 30rpx;
}

.login-form {
	margin-bottom: 40rpx;
}

.form-item {
	margin-bottom: 30rpx;
}

.input-field {
	width: 100%;
	height: 88rpx;
	background-color: #f5f5f5;
	border-radius: 10rpx;
	padding: 0 20rpx;
	font-size: 28rpx;
}

.login-btn-wrapper {
	margin-top: 20rpx;
}

.login-btn {
	width: 100%;
	height: 88rpx;
	line-height: 88rpx;
	text-align: center;
	background-color: #4d5eff;
	color: #fff;
	border-radius: 10rpx;
	font-size: 30rpx;
}

.login-close {
	position: absolute;
	top: 20rpx;
	right: 20rpx;
	width: 60rpx;
	height: 60rpx;
	display: flex;
	justify-content: center;
	align-items: center;
	cursor: pointer;
}

.close-icon {
	font-size: 48rpx;
	color: #999;
}

/* 添加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: 94vh;

	.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>