<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="班级名称">
                        <el-input v-model="className" placeholder="班级名称" readonly />
                    </el-form-item>
                </el-col>
                <!-- <el-col :span="12">
                    <el-form-item label="班级ID">
                        <el-input v-model="form.classId" placeholder="班级ID" readonly />
                    </el-form-item>
                </el-col> -->
            </el-row>

            <!-- 学员信息 -->
            <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 { addStudentsToClass } from '@/api/extend/lqStudyClass'

export default {
    name: 'AddStudentForm',
    data() {
        return {
            visible: false,
            submitLoading: false,
            className: '',
            form: {
                classId: '',
                students: []
            },
            rules: {
                classId: [
                    { required: true, message: '班级ID不能为空', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        addStudent(){
            this.form.students.push({
                employeeName: '',
                employeePhone: '',
                employeeId: '',
                admissionTime: '',
                hrBelong: ''
            })
        },
        init(classId = '', className = '') {
            this.visible = true
            this.className = className
            this.form.classId = classId
            this.form.students = []
        },

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

        // 删除学员
        removeStudent(index) {
            this.form.students.splice(index, 1)
        },

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

                    this.submitLoading = true
                    addStudentsToClass(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.className = ''
            this.form = {
                classId: '',
                students: []
            }
        }
    }
}
</script>

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

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