export default {
    // NOTE：有机会统一注册、用户中心的表单逻辑
    data() {
        return {
            fullRules: {
                username: [{
                    required: true,
                    message: '姓名必填',
                    trigger: 'change'
                }],
                sex: [{
                    required: true,
                    message: '性别必填',
                    trigger: 'blur'
                }],
                schoolId: [{
                    required: true,
                    message: '学校必填',
                    trigger: 'change'
                }],
                subjectid: [{
                    required: true,
                    message: '科目必填',
                    access: ['isFrontTeacher'],
                    trigger: 'change',
                    validator: (r, v, cb) => cb(v > 0 ? undefined : new Error(r.message))
                }],
                year: [{
                    required: true,
                    message: '学年必填',
                    trigger: 'change'
                }],
                // sno: [{
                //     required: true,
                //     message: '学号必填',
                //     access: ['isFrontStudent'],
                //     trigger: 'blur'
                // }],
                clazzId: [{
                    required: true,
                    message: '班级必填',
                    access: ['isFrontStudent'],
                    trigger: 'change',
                    validator: (r, v, cb) => cb(v?.length > 0 ? undefined : new Error(r.message))
                }],
                gradeClass: [{
                    required: true,
                    message: '年级组必填',
                    access: ['isFrontTeacher'],
                    trigger: 'change',
                    validator: (r, v, cb) => cb(v?.length > 0 ? undefined : new Error(r.message))
                }],
                isHeadteacher: [{
                    required: true,
                    message: '班主任必填',
                    access: ['isFrontTeacher'],
                    trigger: 'change',
                    validator: (r, v, cb) => cb()
                }],
                headteacherClassId: [{
                    required: true,
                    message: '带班必填',
                    access: ['isFrontTeacher'],
                    trigger: 'change',
                    validator: (r, v, cb) => cb(v > 0 ? undefined : new Error(r.message))
                }],
                phoneNumber: [{
                    required: true,
                    message: '手机号必填',
                    access: [],
                    trigger: 'change'
                }, {
                    pattern: /^\d{11}$/,
                    message: '手机号格式不正确',
                    access: [],
                    trigger: 'change'
                }],
                code: [{
                    required: true,
                    message: '验证码必填',
                    access: [],
                    trigger: 'change'
                }]
            },
            rawClassTree: null,
        }
    },
    computed: {
        classTree() {
            if (!this.rawClassTree?.length) return [];
            // 转化为 element-ui 标准格式
            return this.rawClassTree.map((grade) => ({
                label: grade.name,
                value: grade.grade,
                children: grade.classList.map((clazz) => ({
                    label: clazz.className,
                    value: clazz.classId,
                })),
            }));
        },
    },
    methods: {
        extractUserRules(form) {
            const resultRules = {}
            Object.keys(form).forEach(prop => {
                const rules = this.fullRules[prop]
                if (!rules?.length) return
                const filterRules = rules.filter(rule => {
                    if (rule.access?.length > 0) {
                        return rule.access.some(getter => this.$store.getters[getter])
                    } else {
                        return true
                    }
                })
                if (filterRules.length) resultRules[prop] = filterRules
            })
            console.log('extractUserRules', resultRules)
            return resultRules
        },
        formDataToPostData(form) {
            const data = this.deepClone(form)
            // subjectIds
            if (data.subjectid) data.subjectIds = [data.subjectid]
            // grade class
            if (form.clazzId?.length == 2) {
                const [gradeId, classId] = form.clazzId
                data.gradeClass = [{
                    grade: gradeId,
                    classList: [{ classId }]
                }]
            } else if (form.gradeClass.length > 0) {
                data.gradeClass = []
                form.gradeClass.forEach(pair => {
                    if (pair.length != 2) return
                    const [gradeId, classId] = pair
                    let exist = data.gradeClass.find(g => g.grade == gradeId)
                    if (!exist) {
                        exist = {
                            grade: gradeId,
                            classList: []
                        }
                        data.gradeClass.push(exist)
                    }
                    exist.classList.push({ classId })
                })
            }
            return data
        }
    }
}
