<template>
    <el-dialog title="创建学习班级" :visible.sync="visible" width="800px" @close="closeDialog">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="班级名称" prop="className">
                        <el-input v-model="form.className" placeholder="请输入班级名称" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="授课老师" prop="teacherId">
                        <userSelect v-model="form.teacherId" placeholder="请选择授课老师" @change="onTeacherChange" />
                    </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="form.startTime" type="datetime" placeholder="选择开始时间"
                            value-format="yyyy-MM-dd HH:mm:ss" style="width: 100%">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="结束时间" prop="endTime">
                        <el-date-picker v-model="form.endTime" type="datetime" placeholder="选择结束时间"
                            value-format="yyyy-MM-dd HH:mm:ss" style="width: 100%">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-form-item label="备注">
                <el-input v-model="form.remark" type="textarea" :rows="3" placeholder="请输入备注信息" />
            </el-form-item>

            <!-- 学员信息 -->
            <el-form-item label="学员信息">
                <div class="student-section">
                    <div class="student-header">
                        <span>学员列表</span>
                        <el-button type="primary" size="small" icon="el-icon-plus" @click="addStudent">添加学员</el-button>
                    </div>
                    <el-table :data="form.students" border style="width: 100%; margin-top: 10px;">
                        <el-table-column prop="employeeId" label="选择学员" width="200">
                            <template slot-scope="scope">
                                <userSelect v-model="scope.row.employeeId" placeholder="请选择学员"
                                    @change="(ids, users) => onStudentChange(scope.$index, ids, users)" size="small" />
                            </template>
                        </el-table-column>
                        <el-table-column prop="employeeName" label="学员姓名" width="120">
                            <template slot-scope="scope">
                                <el-input v-model="scope.row.employeeName" placeholder="学员姓名" size="small" readonly />
                            </template>
                        </el-table-column>
                        <el-table-column prop="employeePhone" label="手机号" width="140">
                            <template slot-scope="scope">
                                <el-input v-model="scope.row.employeePhone" placeholder="请输入手机号" size="small" />
                            </template>
                        </el-table-column>
                        <el-table-column prop="admissionTime" label="入学时间" width="140">
                            <template slot-scope="scope">
                                <el-date-picker v-model="scope.row.admissionTime" type="date" placeholder="选择时间"
                                    value-format="yyyy-MM-dd" size="small" style="width: 100%">
                                </el-date-picker>
                            </template>
                        </el-table-column>
                        <el-table-column prop="hrBelong" label="HR归属" width="120">
                            <template slot-scope="scope">
                                <el-input v-model="scope.row.hrBelong" placeholder="请输入HR归属" size="small" />
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" width="80">
                            <template slot-scope="scope">
                                <el-button type="text" size="small" @click="removeStudent(scope.$index)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="submitForm" :loading="submitLoading">确定</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { createClassWithStudents } from '@/api/extend/lqStudyClass'

export default {
    name: 'CreateClassForm',
    data() {
        return {
            visible: false,
            submitLoading: false,
            form: {
                className: '',
                teacherId: '',
                teacherName: '',
                startTime: '',
                endTime: '',
                remark: '',
                students: []
            },
            rules: {
                className: [
                    { required: true, message: '请输入班级名称', trigger: 'blur' }
                ],
                teacherId: [
                    { required: true, message: '请选择授课老师', trigger: 'change' }
                ],
                startTime: [
                    { required: true, message: '请选择开始时间', trigger: 'change' }
                ],
                endTime: [
                    { required: true, message: '请选择结束时间', trigger: 'change' }
                ]
            }
        }
    },
    methods: {
        init() {
            this.visible = true
            this.resetForm()
        },

        resetForm() {
            this.form = {
                className: '',
                teacherId: '',
                teacherName: '',
                startTime: '',
                endTime: '',
                remark: '',
                students: []
            }
            this.$nextTick(() => {
                if (this.$refs.form) {
                    this.$refs.form.clearValidate()
                }
            })
        },

        // 老师选择变化
        onTeacherChange(ids, users) {
            if (users && users.length > 0) {
                this.form.teacherName = users[0].fullName
            } else {
                this.form.teacherName = ''
            }
        },

        // 学员选择变化
        onStudentChange(index, ids, users) {
            if (users && users.length > 0) {
                const user = users[0]
                this.form.students[index].employeeName = user.fullName
                // 手机号需要用户手动填写，因为用户选择组件没有提供手机号信息
            } else {
                this.form.students[index].employeeName = ''
            }
        },

        // 添加学员
        addStudent() {
            this.form.students.push({
                employeeName: '',
                employeePhone: '',
                employeeId: '',
                admissionTime: '',
                hrBelong: ''
            })
        },

        // 提交表单
        submitForm() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    if (this.form.students.length === 0) {
                        this.$message.warning('请至少添加一名学员')
                        return
                    }

                    this.submitLoading = true
                    createClassWithStudents(this.form).then(response => {
                        if (response.code === 200) {
                            this.$message.success(response.message || '创建成功')
                            this.closeDialog()
                            this.$emit('refreshDataList')
                        } else {
                            this.$message.error(response.msg || '创建失败')
                        }
                        this.submitLoading = false
                    }).catch(() => {
                        this.submitLoading = false
                    })
                }
            })
        },

        // 关闭弹窗
        closeDialog() {
            this.visible = false
            this.resetForm()
        }
    }
}
</script>

<style scoped>
.student-section {
    width: 100%;
}

.student-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: 600;
    color: #303133;
}
</style>
