<template>
	<view class="page">
		<navbar title="创建群组" bgColor="#5baaff" color="#fff" leftClick @leftClick="$utils.navigateBack" />
		
		<view class="form-container">
			<!-- 群组头像 -->
			<view class="avatar-section">
				<view class="avatar-title">群组头像</view>
				<view class="avatar-upload" @click="chooseAvatar">
					<image v-if="groupForm.avatar" :src="groupForm.avatar" class="avatar-preview" mode="aspectFill"></image>
					<view v-else class="avatar-placeholder">
						<uv-icon name="camera" size="60rpx" color="#ccc"></uv-icon>
						<text>点击上传头像</text>
					</view>
				</view>
			</view>
			
			<!-- 群组信息表单 -->
			<view class="form-section">
				<view class="form-item">
					<view class="form-label">群组名称</view>
					<uv-input 
						v-model="groupForm.name"
						placeholder="请输入群组名称"
						:border="false"
						:clearable="true"
					></uv-input>
				</view>
				
				<view class="form-item">
					<view class="form-label">群组描述</view>
					<uv-textarea 
						v-model="groupForm.description"
						placeholder="请输入群组描述"
						:border="false"
						:maxlength="200"
						height="120"
					></uv-textarea>
				</view>
				
				<view class="form-item">
					<view class="form-label">群组类型</view>
					<uv-picker 
						:list="groupTypes"
						v-model="groupForm.type"
						@confirm="onTypeConfirm"
					>
						<view class="picker-trigger">
							<text>{{ getTypeName(groupForm.type) }}</text>
							<uv-icon name="arrow-right" size="30rpx" color="#ccc"></uv-icon>
						</view>
					</uv-picker>
				</view>
				
				<view class="form-item">
					<view class="form-label">群组公告</view>
					<uv-textarea 
						v-model="groupForm.announcement"
						placeholder="请输入群组公告（可选）"
						:border="false"
						:maxlength="500"
						height="120"
					></uv-textarea>
				</view>
				
				<view class="form-item">
					<view class="form-label">加入方式</view>
					<uv-radio-group v-model="groupForm.joinType">
						<view class="radio-item">
							<uv-radio name="free" label="自由加入"></uv-radio>
						</view>
						<view class="radio-item">
							<uv-radio name="approve" label="需要审核"></uv-radio>
						</view>
					</uv-radio-group>
				</view>
			</view>
			
			<!-- 创建按钮 -->
			<view class="submit-section">
				<uv-button 
					type="primary" 
					text="创建群组"
					:loading="submitting"
					@click="createGroup"
				></uv-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				submitting: false,
				groupForm: {
					name: '',
					description: '',
					avatar: '',
					type: 'local',
					announcement: '',
					joinType: 'free'
				},
				groupTypes: [
					{
						label: '同城群',
						value: 'local'
					},
					{
						label: '兴趣群',
						value: 'interest'
					},
					{
						label: '工作群',
						value: 'work'
					},
					{
						label: '学习群',
						value: 'study'
					}
				]
			}
		},
		methods: {
			// 选择头像
			chooseAvatar() {
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						this.groupForm.avatar = res.tempFilePaths[0]
						// 这里可以上传图片到服务器
						this.uploadAvatar(res.tempFilePaths[0])
					}
				})
			},
			
			// 上传头像
			uploadAvatar(filePath) {
				// 这里实现图片上传逻辑
				console.log('上传头像:', filePath)
			},
			
			// 类型选择确认
			onTypeConfirm(e) {
				this.groupForm.type = e.value
			},
			
			// 获取类型名称
			getTypeName(type) {
				const typeItem = this.groupTypes.find(item => item.value === type)
				return typeItem ? typeItem.label : '请选择群组类型'
			},
			
			// 创建群组
			createGroup() {
				if (!this.groupForm.name.trim()) {
					uni.showToast({
						title: '请输入群组名称',
						icon: 'none'
					})
					return
				}
				
				if (!this.groupForm.description.trim()) {
					uni.showToast({
						title: '请输入群组描述',
						icon: 'none'
					})
					return
				}
				
				this.submitting = true
				
				// 模拟创建群组
				setTimeout(() => {
					this.submitting = false
					uni.showToast({
						title: '创建成功',
						icon: 'success'
					})
					
					// 跳转到群组详情页
					setTimeout(() => {
						this.$utils.navigateTo('/pages_order/group/groupDetail?id=999')
					}, 1500)
				}, 1000)
			}
		}
	}
</script>

<style scoped lang="scss">
.page {
	background-color: #f5f5f5;
	min-height: 100vh;
	
	.form-container {
		padding: 20rpx;
		
		.avatar-section {
			background-color: #fff;
			padding: 30rpx;
			border-radius: 20rpx;
			margin-bottom: 20rpx;
			
			.avatar-title {
				font-size: 30rpx;
				font-weight: bold;
				color: #333;
				margin-bottom: 20rpx;
			}
			
			.avatar-upload {
				display: flex;
				justify-content: center;
				
				.avatar-preview {
					width: 120rpx;
					height: 120rpx;
					border-radius: 20rpx;
				}
				
				.avatar-placeholder {
					width: 120rpx;
					height: 120rpx;
					border: 2rpx dashed #ddd;
					border-radius: 20rpx;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					
					text {
						font-size: 20rpx;
						color: #999;
						margin-top: 10rpx;
					}
				}
			}
		}
		
		.form-section {
			background-color: #fff;
			padding: 30rpx;
			border-radius: 20rpx;
			margin-bottom: 20rpx;
			
			.form-item {
				margin-bottom: 30rpx;
				
				&:last-child {
					margin-bottom: 0;
				}
				
				.form-label {
					font-size: 28rpx;
					color: #333;
					margin-bottom: 15rpx;
					font-weight: bold;
				}
				
				.picker-trigger {
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding: 20rpx 0;
					border-bottom: 1rpx solid #f0f0f0;
					
					text {
						font-size: 28rpx;
						color: #333;
					}
				}
				
				.radio-item {
					margin-bottom: 15rpx;
					
					&:last-child {
						margin-bottom: 0;
					}
				}
			}
		}
		
		.submit-section {
			padding: 40rpx 0;
			
			.uv-button {
				width: 100%;
				height: 80rpx;
				border-radius: 40rpx;
				font-size: 32rpx;
			}
		}
	}
}
</style> 