<template>
    <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
                 label-width="80px">
<!--            <el-form-item v-if="!dataForm.id ? true : false" label="身份码" prop="userId">-->
<!--                <el-input v-model="dataForm.userId" placeholder="登录帐号"></el-input>-->
<!--            </el-form-item>-->
            <el-form-item label="用户名" >
                <el-input v-model="dataForm.userName" placeholder="用户名"></el-input>
            </el-form-item>
            <el-form-item label="工号" >
                <el-input v-model="dataForm.workNumber" placeholder="工号"></el-input>
            </el-form-item>
            <el-form-item label="职务" >
                <el-input v-model="dataForm.duty" placeholder="职务"></el-input>
            </el-form-item>
            <el-form-item label="部门" >
                <el-input v-model="dataForm.department" placeholder="部门"></el-input>
            </el-form-item>
            <el-form-item label="岗位" >
                <el-input v-model="dataForm.job" placeholder="岗位"></el-input>
            </el-form-item>
<!--            <el-form-item v-if="!dataForm.id ? true : false" label="身份">-->
<!--                <el-select v-model="dataForm.identity" placeholder="身份">-->
<!--                    <el-option v-for="(name,key) in identityes" :key="key+1" :value="key+1" :label="name"></el-option>-->
<!--                </el-select>-->
<!--            </el-form-item>-->
            <el-form-item label="所属组">
                <el-select v-model="dataForm.groupId" placeholder="所属组">
                    <el-option v-for="item in groupIdes" :key="item.id" :value="item.id" :label="item.name">{{item.name}}</el-option>
                </el-select>
            </el-form-item>
<!--            <el-form-item label="状态" size="mini" prop="status">-->
<!--                <el-radio-group v-model="dataForm.status">-->
<!--                    <el-radio :label="1">禁用</el-radio>-->
<!--                    <el-radio :label="0">正常</el-radio>-->
<!--                </el-radio-group>-->
<!--            </el-form-item>-->
        </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 {is8Number} from '@/utils/validate'
import myRequest from "@/views/modules/h5/myRequest";

export default {
    data() {
        var validateuserId = (rule, value, callback) => {
            if (!is8Number(value)) {
                callback(new Error('请输入8位身份码(数字)'))
            } else {
                callback()
            }
        }
        var validatePassword = (rule, value, callback) => {
            if (!this.dataForm.id && !/\S/.test(value)) {
                callback(new Error('密码不能为空'))
            } else {
                callback()
            }
        }

        return {
            visible: false,
            roleList: [],
            dataForm: {
                id: 0,
                userName: '',
                groupId: '',
                userId: '',
                workNumber:'',
                // major_id,
                duty:'',
                department:'',
                job:''
            },
            identityes: ['管理员', '研究生', '本校老师', '外校老师'],
            groupIdes: [],
            dataRule: {
                userName: [
                    {required: true, message: '用户名不能为空', trigger: 'blur'}
                ],
                password: [
                    {validator: validatePassword, trigger: 'blur'}
                ],
                userId: [
                    {required: true, message: '身份码不能为空', trigger: 'blur'},
                    {validator: validateuserId, trigger: 'blur'}
                ]
            }
        }
    },
    methods: {
        init(id) {
            myRequest.getGroupList2(this).then(res => {
                this.groupIdes = res.data.list
            })
            if (id==undefined){
                this.dataForm.userId = ''
                this.dataForm.userName = ''
                this.dataForm.groupId = ''
                this.dataForm.workNumber = ''
                this.dataForm.duty = ''
                this.dataForm.department = ''
                this.dataForm.job = ''
            }
            this.dataForm.id = id || 0
            this.$http({
                url: this.$http.adornUrl('/sys/role/select'),
                method: 'get',
                params: this.$http.adornParams()
            }).then(({data}) => {
                this.roleList = data && data.code === 200 ? data.list : []
            }).then(() => {
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                })
            }).then(() => {
                if (this.dataForm.id) {
                    console.log('this.dataForm.id', this.dataForm.id)
                    this.$http({
                        url: this.$http.adornUrl(`/admin/info/${this.dataForm.id}`),
                        method: 'get',
                        params: this.$http.adornParams()
                    }).then(({data}) => {
                        console.log(data)
                        if (data && data.code === 200) {
                            this.dataForm.userName = data.user.userName
                            this.dataForm.groupId = data.user.groupId
                            this.dataForm.userId = data.user.userId
                            this.dataForm.workNumber = data.user.workNumber
                            this.dataForm.duty = data.user.duty
                            this.dataForm.department = data.user.department
                            this.dataForm.job = data.user.job
                        }
                    })
                }
            })
        },
        // 表单提交
        dataFormSubmit() {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    this.$http({
                        url: this.$http.adornUrl(`/admin/${!this.dataForm.id ? 'save' : 'update'}`),
                        method: 'post',
                        data: this.$http.adornData({
                            'id':this.dataForm.id,
                            'userName': this.dataForm.userName,
                            'groupId': this.dataForm.groupId,
                            'userId': this.dataForm.userId,
                            'workNumber': this.dataForm.workNumber,
                            'duty': this.dataForm.duty,
                            'department': this.dataForm.department,
                            'job': this.dataForm.job,

                        })
                    }).then(({data}) => {
                        console.log(data)
                        if (data && data.code === 200) {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.visible = false
                                    this.$emit('refreshDataList')
                                }
                            })
                        } else {
                            this.$message.error(data.msg)
                        }
                    })
                }
            })
        }
    }
}
</script>
