<template>
	<el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" width="800px">
		<el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="活动名称" prop="eventName">
						<el-input v-model="dataForm.eventName" placeholder="请输入活动名称" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="活动编号" prop="eventNumber">
						<el-input v-model="dataForm.eventNumber" placeholder="请输入活动编号" />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="活动负责人" prop="eventCoordinator">
						<el-input v-model="dataForm.eventCoordinator" placeholder="请输入活动负责人" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="参与人数" prop="memberCount">
						<el-input-number v-model="memberCount" :min="0" :max="1000" placeholder="参与人数" />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="开始时间" prop="startTime">
						<el-date-picker
							v-model="dataForm.startTime"
							type="datetime"
							placeholder="选择开始时间"
							value-format="yyyy-MM-dd HH:mm:ss"
							style="width: 100%"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="结束时间" prop="endTime">
						<el-date-picker
							v-model="dataForm.endTime"
							type="datetime"
							placeholder="选择结束时间"
							value-format="yyyy-MM-dd HH:mm:ss"
							style="width: 100%"
						/>
					</el-form-item>
				</el-col>
			</el-row>
			
			<!-- 成员管理 -->
			<el-divider content-position="left">活动成员</el-divider>
			<div class="member-management">
				<div class="member-header">
					<el-button type="primary" size="small" icon="el-icon-plus" @click="addMember">添加成员</el-button>
					<span class="member-count">共 {{ dataForm.members.length }} 人</span>
				</div>
				<el-table :data="dataForm.members" border style="margin-top: 10px;">
					<el-table-column prop="userId" label="用户ID" width="120">
						<template slot-scope="scope">
							<el-input v-model="scope.row.userId" placeholder="请输入用户ID" size="small" />
						</template>
					</el-table-column>
					<el-table-column prop="depId" label="部门ID" width="120">
						<template slot-scope="scope">
							<el-input v-model="scope.row.depId" placeholder="请输入部门ID" size="small" />
						</template>
					</el-table-column>
					<el-table-column prop="storeId" label="门店ID" width="120">
						<template slot-scope="scope">
							<el-input v-model="scope.row.storeId" placeholder="请输入门店ID" size="small" />
						</template>
					</el-table-column>
					<el-table-column prop="teamName" label="团队名称" min-width="180">
						<template slot-scope="scope">
							<el-input v-model="scope.row.teamName" placeholder="请输入团队名称" size="small" />
						</template>
					</el-table-column>
					<el-table-column label="操作" width="80" align="center">
						<template slot-scope="scope">
							<el-button type="text" size="small" @click="removeMember(scope.$index)">删除</el-button>
						</template>
					</el-table-column>
				</el-table>
			</div>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmit()">确定</el-button>
		</span>
	</el-dialog>
</template>

<script>
import { getLqEventInfo, createLqEvent, updateLqEvent } from '@/api/extend/lqevent'

export default {
	name: 'LqEventForm',
	data() {
		return {
			visible: false,
			dataForm: {
				id: '',
				eventName: '',
				eventNumber: '',
				eventCoordinator: '',
				startTime: '',
				endTime: '',
				members: []
			},
			memberCount: 0,
			dataRule: {
				eventName: [
					{ required: true, message: '活动名称不能为空', trigger: 'blur' }
				],
				eventNumber: [
					{ required: true, message: '活动编号不能为空', trigger: 'blur' }
				],
				eventCoordinator: [
					{ required: true, message: '活动负责人不能为空', trigger: 'blur' }
				],
				startTime: [
					{ required: true, message: '开始时间不能为空', trigger: 'change' }
				],
				endTime: [
					{ required: true, message: '结束时间不能为空', trigger: 'change' }
				]
			}
		}
	},
	methods: {
		init(id) {
			this.dataForm.id = id || ''
			this.visible = true
			this.$nextTick(() => {
				this.$refs['dataForm'].resetFields()
				if (this.dataForm.id) {
					this.getInfo()
				} else {
					this.dataForm.members = []
					this.memberCount = 0
				}
			})
		},
		// 获取信息
		getInfo() {
			getLqEventInfo(this.dataForm.id).then(response => {
				this.dataForm = response.data
                this.dataForm.members = response.data.Members || []
				this.memberCount = this.dataForm.members ? this.dataForm.members.length : 0
			})
		},
		// 添加成员
		addMember() {
			this.dataForm.members.push({
				id: '',
				eventId: this.dataForm.id || '',
				userId: '',
				depId: '',
				storeId: '',
				teamName: ''
			})
			this.memberCount = this.dataForm.members.length
		},
		// 删除成员
		removeMember(index) {
			this.dataForm.members.splice(index, 1)
			this.memberCount = this.dataForm.members.length
		},
		// 表单提交
		dataFormSubmit() {
			this.$refs['dataForm'].validate((valid) => {
				if (valid) {
					// 验证时间
					if (this.dataForm.startTime && this.dataForm.endTime) {
						if (new Date(this.dataForm.startTime) >= new Date(this.dataForm.endTime)) {
							this.$message.error('结束时间必须大于开始时间')
							return
						}
					}
					
					// 验证成员信息
					for (let i = 0; i < this.dataForm.members.length; i++) {
						const member = this.dataForm.members[i]
						if (!member.userId || !member.depId || !member.storeId || !member.teamName) {
							this.$message.error(`第${i + 1}个成员信息不完整`)
							return
						}
					}
					
					const data = { ...this.dataForm }
					if (!data.id) {
						// 新增
						createLqEvent(data).then(() => {
							this.$message({
								message: '操作成功',
								type: 'success'
							})
							this.visible = false
							this.$emit('refreshDataList')
						})
					} else {
						// 修改
						updateLqEvent(data.id, data).then(() => {
							this.$message({
								message: '操作成功',
								type: 'success'
							})
							this.visible = false
							this.$emit('refreshDataList')
						})
					}
				}
			})
		}
	}
}
</script>

<style scoped>
.member-management {
	margin-top: 20px;
}

.member-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 10px;
}

.member-count {
	color: #606266;
	font-size: 14px;
}

.dialog-footer {
	text-align: right;
}
</style>
